如何使用 UNISWAP API 获取代币价格

时间:2021-01-09 16:27:26

标签: python ethereum etherscan

我正在使用 uniswap python api 来获取实时令牌价格。我正在使用内置函数的所有变体。但是,它没有给我正确的价值。

这是我的代码

address = "0x0000000000000000000000000000000000000000"
private_key =  None
uniswap_wrapper = Uniswap(address, private_key,infura_url,version=2)  
dai = "0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359"


print(uniswap_wrapper.get_eth_token_input_price(dai, 5*10**18))
print(uniswap_wrapper.get_token_eth_input_price(dai, 5*10**18))
print(uniswap_wrapper.get_eth_token_output_price(dai, 5*10**18))
print(uniswap_wrapper.get_token_eth_output_price(dai, 5*10**18))

这些分别是我的结果,

609629848330146249678
24997277527023953
25306950626771242
2676124437498249933489

我不想使用 coingecko 或 coinmarketcaps api,因为它们不会立即列出新发布的代币价格。

我尝试过 etherscan 来获取代币价格,但它没有内置功能。有人对如何解决这个问题有任何建议吗,或者您知道任何替代方案吗?

1 个答案:

答案 0 :(得分:0)

将结果传给这个函数,将它们从wei转换成

 from decimal import Decimal
    
    web3.fromWei(3841357360894980500000001, 'ether')
    Decimal('3841357.360894980500000001')