Search code examples
pythonethereumetherscan

How to get token prices with UNISWAP API


I am using uniswap python api to get live token prices. I am using all the variation of the builtin functions. However, it does not give me the right value.

HERE IS MY CODE

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

And these are my results respectively,

609629848330146249678
24997277527023953
25306950626771242
2676124437498249933489

I don't want to use coingecko or coinmarketcaps api as they do not list newly released token prices immediately.

I tried etherscan to get token prices but it does not have a built-in function for that. Does anybody any suggestions on how to fix that or do you know any alternatives?


Solution

  • I don't have the time or setup to test this right now, but I believe that what you want is something like this:

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