uniswap v2 getAmountsout价格分数值代币
我想获得代币的价格,例如 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亿caw
的589.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论