'Discrepancy between the getAmountsOut function and the prices on the exchange
I am making an app to get live prices of tokens from the different exchanges. To do this I'm using web3.py to access to the DEX smart contract and call the getAmountsOut function. It should be the most accurate since it comes directly from the dex router, but I often see discrepancies between the numbers I get and the numbers on the actual exchange, and even with dexscreener.
Am I doing something wrong? should I try to include slippage to make it match closer to the exchange? As a note, often the price that I retrieve from the getAmountsOut function is accurate.
Here is the code of the function I am using in python.
routerContract = web3.eth.contract(address=uniswapV2Router, abi=uniswapV2ABI)
oneToken = web3.toWei(1, 'Ether')
price = routerContract.functions.getAmountsOut(oneToken, [tokenAddress, DAI]).call()
readablePrice = web3.fromWei(price[1], 'Ether')
The connection to the node is not show here, could it be a limitation of the endpoint? Thank you!
Solution 1:[1]
There's a 0.3% uniswap fee thats factored in the calculations of getAmountsOut, as such the price with something like dexscreaner will be different
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gavriel Cohen |