3

I want to get price of tokens like CAW, SHINJA, etc on blockchain and not using uniswap subgraph api.

router_contract_obj = w3.eth.contract(
   address=Web3.toChecksumAddress(IUNISWAP_V2_ROUTER),
   abi=router_contract_abi
)

prices = await router_contract_obj.functions.getAmountsOut(
   10 ** int(token_metadata['decimals']), 
   [
      Web3.toChecksumAddress(token_metadata['address']),
      Web3.toChecksumAddress(WETH)
   ]
).call()

token_eth_price = token_eth_price[1]/10**tokens_decimals["WETH"]

above code works with any major coin like WBTC, DAI, USDC, etc..

But when I try this for tokens like CAW I get 0 as its eth value. This is probably due to very low price of these tokens: e.g. from uniswap transaction page I can see for 6.07 billion CAW is swapped for 589.3763 USDC.

I tried increasing input amount in getAmountsOut function and got the non zero value but it is not correct.

router_contract_obj = w3.eth.contract(
   address=Web3.toChecksumAddress(IUNISWAP_V2_ROUTER),
   abi=router_contract_abi
)

prices = await router_contract_obj.functions.getAmountsOut(
   1000000000 * 10 ** int(token_metadata['decimals']), 
   [
      Web3.toChecksumAddress(token_metadata['address']),
      Web3.toChecksumAddress(WETH)
   ]
).call()

token_eth_price = token_eth_price[1]/1000000000
token_eth_price = token_eth_price/10**tokens_decimals["WETH"]

output: token_eth_price = 8.474e-24

Am I doing something wrong or is there any other way for this?

AtiqGauri
  • 1,483
  • 13
  • 26

0 Answers0