uniswap v2 getAmountsout价格分数值代币

发布于 2025-02-05 02:45:31 字数 1502 浏览 2 评论 0原文

我想获得代币的价格,例如 shinja> shinja 等,等,而不是使用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"]

上面的代码与WBTC,DAI,USDC等任何主要硬币一起使用。

但是,当我尝试使用此功能时,例如caw,我将获得0作为其ETH值。这可能是由于这些令牌的价格很低:例如,我可以看到60.7亿caw589.3763 USDC

我尝试在getAmountSout函数中增加输入金额,并获得了非零值,但这是不正确的。

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"]

输出:token_eth_price = 8.474e-24

我在做错什么还是有其他方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文