[Ethereum] Why is the @uniswap/sdk DAI/USDT price impact at 100 and returning incorrect prices


Following the documentation here: https://uniswap.org/docs/v2/javascript-SDK/pricing/

I have been able to replicate the results using WETH, and I have decided to have a go at getting a similar result with DAI and USDT.

const { ChainId, Fetcher, Route, Trade, TokenAmount, TradeType, WETH } = require('@uniswap/sdk');

const init = async () => {
    const dai = await Fetcher.fetchTokenData(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F');
    const tether = await Fetcher.fetchTokenData(ChainId.MAINNET, '0xdAC17F958D2ee523a2206206994597C13D831ec7');
    const pair = await Fetcher.fetchPairData(dai, tether);
    const route = new Route([pair], tether);

    const trade = new Trade(route, new TokenAmount(tether, '1000000000000000000'), TradeType.EXACT_INPUT);


I retrieve the Token Data for both Dai and Tether, then fetch pair data which I then put into a route.

These abovementioned steps must be correct as the an in-function log of


return values that make sense.

Where things become odd is with


According to these results, my price impact is at 100, the execution price is around 0.000000278029, and the output amount is at 278028.

This doesn't sound right at all.

What is the mistake in my code that brings about this error?

Best Answer

There is a decimal mistake in the TokenAmount. It is important to check how many decimals any specific token has. While 1000000000000000000 wei is 1 ether, 1000000000000000000 specified for USDT, which has 6 decimal places, would equal 1000000000000, which is much greater than the current liquidity supply on Uniswap. 1000000 specified for USDT would equal 1 USDT and return reasonable results for

Related Topic