Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

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

Open
wmahmood1984 opened this issue Feb 17, 2024 · 0 comments
Open

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

wmahmood1984 opened this issue Feb 17, 2024 · 0 comments

Comments

@wmahmood1984
Copy link

wmahmood1984 commented Feb 17, 2024

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')

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant