Swaps
An overview on what Swaps on Duality are
Swaps are equivalent to market orders in that they will buy at the best available prices until the trade is complete. This is the most common way to exchange assets in DeFi. It entails selecting an input token, output token and the amount you want to swap into the pool. Users have the option to choose their own route or to dynamically route their assets.
To prevent swaps from incurring too much loss, users set a preferred slippage tolerance. This prevents the best available price from changing so much that the trade is no longer wanted by the user. This also puts bounds on how much they can be sandwiched, since attackers can't frontrun their trade below the slippage tolerance.
Current routing designs use snapshots of the state of a DEX to make off-chain calculations on the best path to take. This has two big issues:
- 1.The route is broadcasted in the mempool, which can allow searchers and block builders to frontrun / sandwich it for profit.
- 2.If the price moves (crypto is very volatile) by the time the trade is executed the predetermined route may no longer be ideal.
By calculating the route at execution, we can eliminate both of these problems almost completely. When a trade is dynamically routed, all that's broadcasted to the mempool is the amount in, token in and token out. Since the path(s) being route through are not set prior to execution, they can't be front-run and are dynamically found in response to changes in price due to previous trades.
The trade-off is that dynamic routing uses more on-chain computation than not dynamically routing it.
Last modified 4mo ago