I'm using Python Web3 and Uniswap to create a bot prototyp. I am currently trying to calculate the estimated pair price and I think I have done everything right. I have followed the following tutorial: https://dailydefi.org/articles/price-impact-and-how-to-calculate/
I actually calculate the output amount in the same way as in the tutorial. First I calculate the constant product by multiplying the two pool reserves. Then I divide the constant product by the reserve of token a + the deposited amount to calculate the new reserve of token b. Then I subtract this value from the old pool reserve for token b to get the output amount.
Here is the code of the estimated pair price:
amount = 1 reserves = pair_contract.functions.getReserves().call() token0_reserve = w3.fromWei(reserves, 'ether') token1_reserve = w3.fromWei(reserves, 'ether') constant_product = token0_reserve * token1_reserve new_token1_reserve = constant_product / (token0_reserve + amount) token1_out = token1_reserve - new_token1_reserve
I would expect that the "real" output from the uniswap router is nearly equal to that value but that wasnt so.
That is the Code for getting the real pair output:
token0_amount = self.w3.toWei(1, 'ether') amount_out = self.uniswap_router_contract.functions.getAmountsOut(token0_amount, [self.w3.toChecksumAddress(token0_address), self.w3.toChecksumAddress(token1_address)]).call() amount_out = self.w3.fromWei(amount_out, 'ether')
Why do the prices for example WETH/SHIBASWAP differ so much between 0.00004 (estimated) and 27964.26725 (real out)?
Does my code have a bug or do I have a gap in understanding? I ask for help! Surely someone has had similar comprehension problems as I have... hopefully otherwise I feel stupid.