Search code examples
ethereumblockchaincryptocurrencyuniswap

Is there a way to get the value of a crypto in another one with uniswap


Im a beginner in the blockchain/crypto world and I was wondering if there is a way to get the value of a crypto in another one on uniswap (like in the image where we can see that on uniswap 1 ETH = 2997 1INCH). , like using the SDK or the Router ecc.. If you have any suggestion please help me.

1

I tried using the uniswap sdk and the smart contracts periphery contracts but i couldn't find nothing that could help me


Solution

  • Yes. You can get the price of a cryptocurrency as it is traded in Uniswap.

    • For Uniswp v2 like exchanges, the price can be calculated as a ratio of reverse0 / reserve1

    • The related smart contract call is pair.getReserves

    • For more complete example, you can read the source code of get_current_mid_price() function here

    • For Uniswap v3, it is there are similar helper functions in Uniswap perihelia contracts