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

Your Answer

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

Browse other questions tagged or ask your own question.