Anatomy Of A DeFi Token Swap

Many DeFi users tend to have an oversimplified understanding of how token swaps work. We take a glimpse at what is going on under the hood.

Juan Pellicer
IntoTheBlock
5 min readApr 13, 2023

--

Many DeFi users often take for granted the mechanics of token swaps in decentralized exchanges (DEXs), particularly in the most prevalent type of DEXs: Automated Market Makers (AMMs). This confusion may stem from the simplicity of most AMM interfaces, which, while contributing to their excellent usability, masks the complexity of the various factors involved in executing a trade.

Another potential cause of misunderstanding could be the preconceptions that traders carry from their experiences with centralized exchanges (CEXs). In CEXs, trading occurs through centralized limit order books (CLOBs), which operate quite differently from AMMs. Additionally, the logic for transaction inclusion and ordering logic per block found in blockchains aggravates the problem. For those traders aiming to achieve the best possible price execution, it is essential to have a solid grasp of these concepts.

Sell Amount Or Buy Amount?

As an example, let us examine the interface presented for conducting a token swap. We chose the most used DEX to date, Uniswap. We will initially just perform 2 actions, setting up the amount of the token to sell: 100 ETH, and the token we want to buy, USDT. This is what the interface would look like at that point:

Current extended view of a Uniswap token exchange

As can be seen, there are four main highlights, 2 numerical amounts and their respective token buttons. First misconception is here: the first top number is the selling amount of the token that the trader is willing to swap out. And after specifying it, Uniswap will retrieve a price quote, which is the second amount shown, which might not match with the amount that the trader will finally receive. Why is this? This is due to slippage, and if the reader thinks that this is related to the pool price impact or gas costs then definitely needs to keep reading towards the next section.

But before jumping in, this is not a misconception but a use case often overlooked, but did you know that some AMMs like Uniswap besides specifying the amount of ETH that one is willing to sell, one can specify the amount of USDT that one would be willing to receive? This would allow uniswap to fill the selling amount of the token that would be needed to receive an approximate amount of the buying amount. This will fill for the user the amount of the token that is needed to be sold in order to receive that amount of the buying token, but the final received amount still is at the mercy of the incurred slippage.

Slippage: It Is Unpredictable Which Transactions Will Trade Before Yours In The Next Block

An AMM price quote is not deterministic most of the time. By most of the time I mean that committing a swap transaction between constantly traded tokens like WETH or USDT have a high chance of ending up with other transactions executed before the committed swap transaction. This uncertainty translates into an executed price that might differ to the quoted one by uniswap. This is what is understood in AMMs as slippage. A diagram of the mentioned process can be seen next:

Lifecycle of an AMM token swap

Slippage can be either positive or negative. Positive would yield in the trader favor, and will result in the trader receiving a larger amount than the initially quoted by the AMM. This could have happened if in the block where the AMM trader transaction is included, there would be one or several other transactions that trade with the same pool and this leaves the pool for the AMM trader transaction with a better price than the one initially quoted by the AMM.

While the negative slippage (often wrongly referred to as simply slippage) would mean exactly the opposite. A worse execution than the initially quoted, which in some cases could have originated from MEV bots seeing the trader transaction in the public mempool and ‘sandwiching’ it. It is worth mentioning that often the source of these sandwich exploits come from traders setting a too high slippage tolerance. This choice is usually motivated due to some traders swapping illiquid tokens with the need of trading immediately and fearing that the transaction might fail due to previous trades affecting the pool price substantially. This tolerance value can be set up in the advanced options menu of many AMMs:

Slippage tolerance menu of Uniswap

Slippage tolerance is a percentage that defines the worst acceptable price respective to the received quote. For example, an AMM like Uniswap by default sets a (negative) slippage tolerance of 0.10%. This means that the transaction would succeed if the final received amount is within that percentage limit. The impacts of all of these factors can be seen in the next diagram below, depicting how there is an initial divergence from the ideal price due to the chosen pool dynamics:

Pricing mechanics involved in an AMM token swap

The insights for this diagram are that our final receiving price will be inversely proportional with the size of our trade, due to the pool price impact worsening the higher the trading size is. The cost of the pool LP fee is constant regardless of any user input. And finally, the slippage cost will depend on the volatility of the token that is being traded and the preference of an acceptable price by the user. This will be a tradeoff that the trader will have to decide, always taking into consideration sandwich risks if the transactions are being streamed publicly to the mempool instead of using a private relayer.

Finally, we did not speak about gas costs, since these are paid in the native currency of the blockchain so measuring these costs would certainly need to convert these costs paid on each step into a common currency, usually dollars.

--

--