0

I would start with a sample on exact UniswapV2 pair SHIB/WETH (SushiSwap). I use local hardhat node and there's no possibility of 3-rd party effect on LP.

I have this toSwapTokensAmount = 2857840823995360316751873 amount of SHIB. The liquidity pool has the next amount of reserves: SHIB=109916954769052324620139928, WEI=887807065530129372. I call the getAmountsOut uniswap-v2 router contract function and receive outputWeiAmount = 22432246070044323 WEI as the result.

I use two approaches in my code:

  1. Price calculation approach based on the amount of the tokens to sell and received amount of Weis:
myPrice = toSwapTokensAmount / outputWeiAmount = 2857840823995360316751873 / 22432246070044323 = 127398781.873
  1. Price calculation approach based on the amount of the reserves (including the changes in reserves):
initialShibReserves = 109916954769052324620139928
initialWeiReserves = 887807065530129372
toSwapTokensAmount = 2857840823995360316751873
outputWeiAmount = routerContract.functions.getAmountsOut() # 22432246070044323
// change LP reserves according to swap operation direction: sell SHIB, receive WETH
newShibReserves = initialShibReserves + toSwapTokensAmount // 112774795593047684936891801
newWeiReserves = initialWeiReserves - outputWeiAmount // 865374819460085049
priceFromReserves = newShibReserves / newWeiReserves // 130319016.750925

The both approaches results are not equal: myPrice != priceFromReserves -> 127398781.873 != 130319016.750925.

I found the way to calculate the price based on reserves very popular in the other repositories: reserve0/reserve1 (or vice versa), so it looks like the issue is not here, which means the issue is with toSwapTokensAmount / outputWeiAmount. Worth to mention that after the actual swap I see almost the same price as priceFromReserves, but probably because the calculation depends on the amount of the reserves.

So the question is why the results are different. Why I can't calculate the price using the amount of the tokens to sell and Wei amount to receive which is common when you buy something in a shop/market?

2 Answers 2

0

Slippage causes the price difference.

  1. Pre-trade price: toSwapTokensAmount / outputWeiAmount reflects the ideal price before the trade happens.
  2. Actual price: newShibReserves / newWeiReserves reflects the actual price after the trade considering slippage. Slippage happens because your trade size affects the pool's reserves, slightly changing the final price.

Think of it like a real market. A store might show a fixed price, but large purchases can cause minor price adjustments.

1
  • Thank you for the reply. As I mentioned at the beginning of the question I use local hardhat node and there's no possibility of 3-rd party effect on LP. Means slippage in this condition could be ignored because nobody else interacts with the node.
    – dvcr
    Commented Apr 28 at 10:28
0

I got an answer from the official Uniswap discord channel from one of the members.

The price impact given by the formula shows by how much your price will differ from the price of the pool before the swap. It is not the total change in the pool price after the swap. See:

  • Price of the pool before your swap: 123807253.89183342 SHIB per 1 ETH (109916954769052324620139928/887807065530129372),
  • 127398781.873 per 1 eth - the price your swap was executed at (-2.5%),
  • 130319016.750925 per 1 eth (-5%) - price in the pool after the swap (112774795593047684936891801/865374819460085049)

So difference in the pool price before and after the swap is about 5% and your swap price is in the middle as it (swap) starts from the price before the swap and ends in the price after the swap... Your price can never be the same as the price before or after the swap.

To summarize:

  • initial reserves would give me the current price of the pool
  • the formula in the thread* would give the price for my "transaction"
  • the updated reserves with my values would give me the next pool price:
newTokenReserve = tokenReserve + swapAmount
newWethReserve = wethReserve - outputWeiAmount
price = tokenReserve / wethReserve

* the formula from thread:

the formula from thread

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.