Skip to content
Advertisement

How to get all tick ranges with non-zero liquidity to finally calculate Total Value Locked Uniswap V3?

The aim is to calculate the uniswap v3 pool’s total value locked (TVL).

JavaScript

This does print liquidity in MIN_TICK and MAX_TICK but takes a lot of time and waste web3 calls as it is iterating on zero liquidity ticks also. Right now these are hardcoded, here I want to know what can be the value of min-max so that range does not contain any zero liquidity tick.

Advertisement

Answer

  • Getting pair token balance of contracts

web3.eth.contract(address=token_address,abi=abi).functions.balanceOf(contract_address).call()

  • and then get current price of each token / USDT by calling function slot0 in pool tokenA/USDT & tokenB/USDT

slot0 = contract.functions.slot0().call()

sqrtPriceCurrent = slot0[0] / (1 << 96)

priceCurrent = sqrtPriceCurrent ** 2

decimal_diff = USDT_decimal – TOKEN_A_decimal

token_price = 10**(-decimal_diff)/( priceCurrent) if token0_address == USDT_address else priceCurrent/(10**decimal_diff)

  • Finally, TVL = sum(token_balance * token_price)

** Remember: check price from big pool

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement