1

How hard can it be to learn the price of an erc20 token in terms of usdc in uniswapv3. a hint: its really hard. Any resource that you know that exactly explains how to get a price of any erc20 token from uniswapv3 with code?

1 Answer 1

1

The idea is to get the conversion to of the token to ETH, and then the conversion of ETH to USDC (assuming that the token's main liquidity is Token/WETH).

You can do this by checking the pair address of the Token/WETH and then getting its sqrtPriceX96 by reading slot0. You will be able to get the conversion ratio in this way.

For example: In NEIRO/WETH pair address "read contract". If you call slot0, the first index will display sqrtPriceX96. You can then get the actual token to WETH conversion through this formula:

price_ratio = ((sqrtpricex96 ** 2) / (2 ** 192))
weth_amount = price_ratio * token_amount_in_wei

The resulting weth_amount will be in WEI. The formula is explained here for reference

And so, after this, just repeat the steps for converting the wETH amount to USDT or USDC.

1
  • 1
    got you thx I ll try and get back. Commented Jul 30 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.