Advanced DEX Routing: Introducing Next Gen Router, Argon | Duncan Townsend - 0x
Ethereum Denver·Wed, Oct 9, 2024, 12:00 AM
The increasing complexities of web3 has made it harder than ever to effectively route value across web3. 2024+ requires an evolution of DEX routing technology, and more transparency around the economics & methodologies underpinning these critical services we rely on in DeFi. APPLICATIONS FOR ETHDenver 2024: #YearOfTheSpork ARE OPEN APPLY NOW https://ethdenver.com/apply
Transcript
[Music] all right hello uh I am Duncan with zerx project and I am here to tell you about Advanced Dex routing and xerox's new uh router argon and I want to give a big thanks to my colleagues who collaborated with me on this presentation Andy Bliss Q Chang Kevin Lao Robert paluba and Henry zuo without them this would not have been possible now is this presentation right for you are you D enough you should have a decent idea of the answers to these questions that are on screen right now you should know what gas is what a swap is what a DEX is and hopefully you know that ZX is a DEX aggregator and that there are many others and we're one of the leading ones now if you are a little bit more dgen perhaps you know what a private Market maker is and maybe you know what price impact is and what the difference between price impact and slippage is and if you know those you're in pretty good shape but we're going to go one step deeper here maybe you know what Arbitrage is and why swaps generate me and why even if you use an me blocker RPC and you get a refund for the me that you generate that's still bad for you that's money out of your pocket and so argon is designed to keep all of that me in your wallet generate as little me as possible and give you the best possible price why is it necessary to build argon that next Generation Dex aggregation router it comes down to liquidity fragmentation I bet all of you in the audience could name at least three dexes maybe five maybe more well univ V4 is coming with denune and it's going to make all of that so much worse and so we need a Next Generation Dex aggregation router to take optimal advantage of all of that liquidity onchain and we also want to take advantage of offchain liquidity through those private market makers through RFQ systems this presentation is going to give you an overview of techniques for routing and at the beginning for those of you who like bright colors and pretty images a compar between argon and a bunch of other industry leing Dex aggregation routers so by the end of this you should understand all of these things if you didn't download my slides already you should because I'm not going to talk through these but you're not going to understand our router implementation I'm not going to give that away for free you're probably not going to understand how either of these algorithms work but you will have the knowledge of those names of those algorithms and and I'm going to send you a bunch of links if you want to read those and learn more some warnings this information is not complete if you go and code this up you're going to get wrecked I don't want you to get wrecked so please don't go do that if you decide to build your own router this is just a starting point you're going to have to put in a lot of work if you want to make a router that's as good as ours please do this is just a starting point now how can you as a Trader know that your Dex aggregation router is giving you the best possible price now this swap for a million wrapped BTC to die on our competitor's router looks pretty good why did I choose WAP BTC to die well these are Big tokens they have big market caps they have lots of Juicy liquidity but there's very little direct liquidity between these two tokens and that makes it an interesting problem to Route our competitors's router does a pretty good job but our router is better when you see a swap like this that's how you know that the Dex aggregation router that you're using is giving you the best price is taking optimal advantage of all that onchain liquidity if you're seeing a route like the one on the left our competitor's route you're not getting the best possible price now we call this a yarn ball swap regardless of whether this is what's actually being uh displayed if this is what's happening under the hook Hood you're getting the best price you also need to have a router that makes good use of RFQ RFQ is when the uh the trader requests a quote from a private Market maker for a portion of their swap when you do that you're moving more of your swap through that offchain liquidity which means that the remainder that goes through onchain liquidity has lower price impact you're getting a better price and sometimes even your whole swap will go through offchain private liquidity you get lower gas costs and a better price our competitor here does not use RFQ in this Swap and so again at that 100 at that million dooll wrapped BTC to die swap we're seeing 75 bips of price impact it's kind of a lot for this swap I didn't have a good graphic for what it looks like on our end with RFQ liquidity but ours is better still even without that RFQ liquidity now you might be saying Duncan I recognize that competitor and I know that they have an RFQ system and that RFQ system is probably going to give them a better price than what you're showing me thank you for keeping me honest but I did look at that with their RFQ you only get back 10 bips so our route's still better even though we're not using RFQ and we also do use RFQ so it's going to be better still just RFQ is also not good enough now this is kind of an unusual Swap this is a million dollars of a to Leo and this is our existing routers swap so you're getting some significant price impact but it's not the end of the world this is still a swap that you could do if you really felt like it but our competitor who only uses RFQ uh nobody's willing to make that swap for you you do not get to swap today so you want to use a DEX aggregation router that always gives you a route that always gives you something to compare against even if it's a weird swap now and you're asking well Duncan why don't you show us the latest and greatest route for that Swap and sure there it is again you see yarn ball swap lower price impact making better use of available onchain liquidity this is what you should be seeing out of your Dex aggregation router three hops is the magic number of hops if your swap goes through three distinct swaps using three distinct sources of liquidity and involves four distinct tokens you're probably taking optimal advantage of onchain liquidity you want to see lots of swaps like that first line from our competitor swap because the sell token may not have good liquidity with the buy token as we saw in that previous previous Slide the sell token and the intermediate token may not have good liquidity with the buy token but if there's some liquidity somewhere if you go A to B to C to D you're going to take optimal advantage of onchain liquidity you need at least four tokens at least three swaps for complex Dex aggregation routing and of course here is this dydx to usdt swap our price impact is better I'm not sure I would call that good so maybe that's just better price impact but that's what it means to take better advantage of available onchain liquidity now also you want your swap to work even when you're not trading a million raise your hands if you're trading a million dollars on chain every week okay this slide is not for you this is slide is for those people who are trading four five maybe six figures on chain when you're trading quantities of that order the execution gas of your swap has a major impact on the price that you realize and if you're swapping frequently that could become a major cost so you want your router to give you the best swap even when it's small even when gas is high somebody's minting lots of nfts today gas is high you really need to make that swap you still want the best price our competitor swap here this is not a good swap it costs a lot of gas the amount out is not very good we should not be seeing 8% price impact for a $3,000 Swap this is our swap you can't really see this on the chart but not only do we know that you only want to interact with one Dex even if that means you are getting a little bit less one or2 dollars less but we also know that univ V2 the Decks that we're interacting with here is more gas efficient than other dexes and that saves you more than what you might be losing by not using a more advanced Dex okay so that's what success looks like how do we achieve that success let's take a look at three strategies for routing first with a really bad one which in case I didn't say it it's really bad don't do this but it's really simple so this is what we're going to start with first we're going to generate some disjoint routes disjoint in the sense that each potential route doesn't use any of the amm from any of the other routes and then we're just going to do this loop we're going to trade some tokens at the best amm and we're going to update for Price impact and then we're going to trade some more tokens and you do that until it's done and this this works it's not ideal but it's nice and simple you can make it aware of gas and if you're smart then your routes are going to be smart but it's a greedy algorithm the price is not going to be optimal and it's only as smart as you are you want your algorithms to be smarter than you are you want your algorithms to find things that surprise you this is not going to find you a yarn ball Swap and it's also slow if you're trying to get uh you know sub 100 millisecond uh latency on your router this is not the way for you you already saw this one this route was produced by a router that works approximately like that it's not exact it's close but moving on from the bad router let's talk about a slightly more advanced technique successive shortest path or minimum cost flow Kevin Wayne all of his materials excellent I recommend you go read them I can't explain it all here but let's go a little bit into detail on it this is pretty similar to the obviously incorrect way oh and uh by the way we're going to get into some math if you're allergic to math maybe start looking away we're going to treat this problem as a graph we're going to treat the tokens as nodes we're going to treat the amm as edges we're going to treat each unit sized chunk of liquidity as its own amm you get to decide how big that unit is it affects how fast your algorithm runs we'll see that later and then we set the cost of each Edge to the negative logarithm of the price why you need to go read the paper and then we execute this rout We Run The Bellman Ford algorithm we delete the shortest path from the graph and then we add it to the swap and do it again and we do this a lot because we have to do this until the entire swap is consumed what's Bellman Ford you ask well it's this great graph theoretic algorithm for finding the shortest path which in this case the shortest path is the lowest price impact from The Source that's the sell token to the sync that's the buy token it's a dynamic programming algorithm which means I'm not going to put it up here on screen and the runtime is Theta of V * e where V and E are these and remember that e is the number of split amm the different chunks so that's big and this is why setting that chunk size is important because this affects the runtime of your algorithm you want to know how bman Ford Works go read Wikipedia it's great why is this algorithm great well Bellman Ford's really fast there are lots of optimized techniques out there for doing it and there are other algorithms that do the same thing that uh you can use that are going to make it even faster this works with private market makers big plus we can take advantage of offchain liquidity and there's a ton of available literature it does produce an optimal route but if there's Arbitrage your algorithm just kind of crashes because Bellman Ford doesn't like it when there are Cycles in your graph and that happens when there's Arbitrage and it still can't account for gas okay so let's look at one more what about convex optimization these guys Geniuses absolute goat go read their papers this involves constant function automated market makers constant function in the sense that there's some constant like in the case of univ V2 that's k and a function for computing it in the case of univ V2 that's the balance of token Zer times the balance of token 1 and K has to only go up that's what it means for it to be a constant function automated Market maker now it turns out that CF amm are convex which means that we can apply this technique convex meaning that there's no token discount token volume discount if you're selling one eth you're going to get a better price than if you're selling 10 or if you're selling 100 and the objective get the most tokens out is a convex objective and the fees charged by CFM are also convex so this is a problem that is really easily adapted to convex optimization you throw it in the convex solver now for those of you who are really allergic to math absolutely Look Away now this next slide is very math heavy you can come back later I'll let you know when it's time to come back given these parameters the reserves of the amm the mapping from token addresses to token zero and token one the amounts that we're going to sell the amounts that we're going to receive we're going to constrain our problem like this we have this network trade Vector which is just the sum of all the token amounts that we're buying and selling and this equation down here basically says that K is not allowed to go down that's the rule and there's this fees parameter in there and our goal is to maximize u u ofs is our objective and if we're trying to get the most amount out it's like that now we're done with the math you can come back why is this good well it's applicable to many different types of uh things that require onchain liquidity you can have a swap that sells multiple different tokens you can have a swap that buys multiple different tokens if you don't care which uh stable coin you get you can have it ask you can have it get you the uh the basket of stable coins that has the most amount of those stable coins without caring which is which it's also good for rebalancing positions and this algorithm was designed for extracting Arbitrage so it's very good at that you also get a price Oracle as a side effect that's pretty cool who doesn't want to build their own price Oracle and you do get optimal amount out why is this bad well you kind of have to be really smart you need to know the inner workings of every single am that you're going to integrate you can't use it with pmm because they're not going to tell you the function that they're trading against and for those uh constant function automated market makers that have uh non-analytic or cubic or higher order uh constant functions your solver might barf there are some solvers that will do this okay tick-based amm also kind of a pain and gas fees aren't convex so we can't account for gas except there are some techniques that you can use to account for gas but those are really Advanced but lastly you say Duncan and jerison out they wrote a second paper that solves all of those problems and you're right it does it doesn't solve all of them you still can't account for gas and it is much faster but I'm out of time and I can't really explain this to you anymore but I hope you enjoyed I hope you learned about why gas aware optimal Dex plus RFQ aggregation is a hard problem I hope that you see that our router improves on the state-of-the-art our competitors's routers and then we talked about two techniques minimum cost flow also called successive shortest path for routing and convex optimization as a technique for routing including its dual decomposition variant we're here all the time you want to talk to us you want to learn more about ZX scan this QR seek us out join our bleon thank you very much for listening
Automatic transcript — names and jargon may be misspelled.