Context: We are creating a smart contract which integrates Uniswap DEX for swapping Tokens to ETH. We need to make sure one swap does not impact the token price more than 3%.

We are in need to write a function which takes in input the price_impact: uint, reserve0: uint and reserve1: uint and outputs the maximum amount of tokens which when swapped for ETH on uniswap will have the given price impact on the token price.

So, can someone please help in understanding how is the price impact calculated on the uniswap swap page, and also in writing a function which ouputs maximum number of tokens to be swapped for ETH which would have the given price impact?

Exchange: https://uniswap.org

Docs: https://uniswap.org/docs/v2

Pricing Docs: https://uniswap.org/docs/v2/javascript-SDK/pricing

Glossary: https://uniswap.org/docs/v2/protocol-overview/glossary/#docs-header

  • Do you want the solution in solidity or javascript (since your are linking to the js SDK) ? You need to fetch reserve for the pair, then solve the equation which would imo look like something like this reserveB - getAmountOut(x) = reserveB B*0.97 whereas reserveB = token reserve. getAmountOut : github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/… – Xavier59 Nov 11 '20 at 23:46

Let X = ETH

Let Y = TokenA

price impact for buying Y amount of tokens:

price impact on buying = (Yamount*0.97) / current Yamount in pool

price impact for selling Y amount of tokens:

  • Let u = Y amount of tokens to be sold
  • Let er = current ETH exchange rate in reference with TokenA

er = X - { (XY) / (Y+0.97u) }

price impact on selling = 0.97er / current Xamount in pool

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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