Uniswap / v2-sdk

🛠 An SDK for building applications on top of Uniswap V2
https://uniswap.org/docs/v2/SDK/getting-started
MIT License
428 stars 1.09k forks source link

not able to get price using other than Weth token #161

Open wmahmood1984 opened 7 months ago

wmahmood1984 commented 7 months ago

Hi,

I am using v2 sdk for one of the project and and creating trade instance with one of my token (USDT in Goerli). The instance work perfectly with WETH. however it is not working with USDT.

here is my code.

const chainId = ChainId.GOERLI const web3 = new Web3(new Web3.providers.HttpProvider("https://goerli.infura.io/v3/2d0256aba07e4704add58fd0713e24d5")) const poolAbi = poolAbi const USDT = new Token(chainId, '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984', 18, 'USDT', 'Tether')

async function createPair() { const pairAddress = Pair.getAddress(USDT, WETH9[USDT.chainId])

// Setup provider, import necessary ABI ... const pairContract = new web3.eth.Contract(poolAbi,pairAddress) const reserves = await pairContract.methods["getReserves"]().call() const {reserve0, reserve1} = reserves

const tokens = [USDT, WETH9[USDT.chainId]] const [token0, token1] = tokens[0].sortsBefore(tokens[1]) ? tokens : [tokens[1], tokens[0]]

const pair = new Pair(CurrencyAmount.fromRawAmount(token0, reserve0.toString()), CurrencyAmount.fromRawAmount(token1, reserve0.toString())) return pair }

const abc = async (sellAmount) => { try { const pair = await createPair() console.log('pair', pair)

  const route = new Route([pair], WETH9[USDT.chainId], USDT)
  console.log('route', route)

  const trade = new Trade(route, CurrencyAmount.fromRawAmount(USDT, sellAmount.toString()), TradeType.EXACT_INPUT)
  console.log('trade', trade)

  return trade.executionPrice.toSignificant(18)
} catch (error) {
  console.error('Error in abc function:', error)
  throw error
}

}

abc('100')