Math behind Uniswap — swap ( part2 )

Jarekkkkk
5 min readJan 15, 2022

--

This article will be the succeeding discussion from my previous article, https://medium.com/@jarekcoding/math-behind-uniswap-swap-part1-88dcc18a9612 , which is mainly focusing on swap transactions in the Uniswap v3. We had a glimpse into how to calculate the number of tokens we can get by giving a fixed amount of tokens. However, version 3 will be more complex and involve more math for efficiency reasons.

Firstly, we make a quick reference to the Uniswap white paper that has pointed out what tricks they have done with the constant product formula.

As the function says, the new version translates the curve to the left and below to keep the price from leaving the boundary. Surprisingly, it shows the familiar pattern we had discussed in the previous article with a below easy sense

To be more specific, they have translated the original formula to make position solvent within the range [ Pa, Pb ], the price liquidity provider can set up, while it will be a huge topic to dive into, currently, it can be thought of as a new feature of Uniswap v3. This will be easier to understand when taking a look at the digram

Reference: Uniswap whitepaper-v3

Because when the LP setup the price boundary, Pa for a lower price; Pb for a higher one, the price will never leave this range. For example, if traders swap 3 ETH for dai in the DAI/ETH pool, the price of the pool (in the terns of the ETH) will be getting lower as constantly pouring ETH into the pool. While the movement is unstoppable in the v2 pool, the price will be halted in v3 owing to this new mechanism, in which the pool will be unavailable to do the transactions for DAI anymore except the reverse transactions for Eth. And the reason behind that is we translate the curve to the left and the below.

However, it still doesn’t show up as identical as our last saw. Internally, the definition of both reserves has been slightly different since we make it more explicit by distinguishing reserve from real and virtual. Don’t be scared of these new terms, they are simply just new variables to track down for smart contracts.
As mentioned before, how many amounts of reserves pool should keep will follow the value of Liquidity, therefore, calculation of liquidity will always be our first priority, especially for swap transactions when liquidity will always be constant, making us solve the amount of asset by following below equation.

Unfortunately, we are not allowed to directly calculate liquidity by multiplying x by y as we have done in v2 because both assets stands for virtual assets.

Keep in mind that reserves shows in the v2 will all point to the real reserve.

To calculate virtual assets, you can simply check the math formula in uniswap v3-periphery smart contract,

https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/LiquidityAmounts.sol

also recommending take a serious deep look at the article from Atis Elsts who really gives the detailed and brief explanation on terse parts of white paper.

Moving forward, we can solve the liquidity by the formula from both sides in your preference for the sake of constant value of liquidity under multiple swap transactions

Equation (1)

It will be more clear to introduce with some example and math calculation,

taking the example of DAI/ETH pool with transactions 0.3% transactions fee

X = 61.17 ETH

Y = 91753 DAI

dX = 10

P = 1500

[Pa,Pb] = [1000, 2250]

Calculate the immutable liquidity from equation with the intial provided information

Lx= Ly = 12910.13 ,getting 5.45xx larger as much as provided in v2 pool. It turns out we supply more efficiency and deep pool for traders in addition to charging more transaction fees for LP provider with exactly same required tokens.

Then, we can restate the equation to solve the value of current price P by substituting x, L for 71.17, 12910.13 that are new amount in the pool

Getting P = 1413.8968, therefore, we can easily solve the real reserve y with same implementation

The answer y = 77190.2189, the current amount of token y should stay in the pool. Finally, the token we are able to swap for is 14562.78109

To prove our outcome, as i had mentioned in the previous article, the function below is also applicable, but X, Y represent the virtual reserve that required some additional works.

Congratulations 🎉 , we get around the same outcome from two different ways on which either using price calculation or initial amount of both real reserves.

--

--