4

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);
};

init();

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

console.log(route.midPrice.toSignificant(6));
console.log(route.midPrice.invert().toSignificant(6));

return values that make sense.

Where things become odd is with

console.log(trade.priceImpact.toSignificant(6));
console.log(trade.executionPrice.toSignificant(6));
console.log(trade.outputAmount.toSignificant(6));

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?

1
  • Has this been resolved for you? I have exactly the same problem -- ETH/DAI pairs work fine, but USDX/PAX returns nonsense compared to what the uniswap web site shows? – nxstock-trader Jul 2 at 5:24
3

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

console.log(trade.priceImpact.toSignificant(6));
console.log(trade.executionPrice.toSignificant(6));
console.log(trade.outputAmount.toSignificant(6));
2
  • Anyway to calculate max to buy / too big price impact? I have a Solidity contract that use it's ether to buy X token using swapExactETHForTokens. But I don't find how I can make it to use the MAX possible eth so that uniswap let my transaction go without revert. – btc4cash Oct 17 '20 at 18:19
  • @Vasya I use the same method to quote the price of other token pairs (e.g. LINK/DAI), but it returns absolutely irrelevant results (compared to prices on Uniswap website). Do you have any idea about that? – Ehsan Khodarahmi Feb 14 at 9:09

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.