Retrieve the current token price ratio for a existing pool.
Code Overview
The code below demonstrates one of the methods for retrieving the current price ratio of two tokens of an existing liquidity pool, which is needed to calculate the precise amounts required for each token when establishing a new liquidity position. The token ratio allows you to calculate the amount needed for the other token.
The pool address, token addresses, token decimal places, and the fee tier can be retrieved from SaucerSwap's REST API. Refer to Fetch all pools (V2) for details.
import*as uniswap_sdk from'@uniswap/v3-sdk';import*as uniswap_core from'@uniswap/sdk-core';import*as ethers from'ethers'; //V6//Set one of Hedera's JSON RPC Relay as the providerconstprovider=newethers.JsonRpcProvider(hederaJsonRelayUrl,'', { batchMaxCount:1,//workaround for V6});//load the ABI data containing Pool's liquidity() and slot0()constabiInterfaces=newethers.Interface(abi);//construct the pool contractconstpoolContract=newethers.Contract(poolEvmAddress,abiInterfaces.fragments, provider);//construct the tokensconsttoken0=newuniswap_core.Token(hederaChainId, token0Address, token0Decimals);consttoken1=newuniswap_core.Token(hederaChainId, token1Address, token1Decimals);//get current slot0 and liquidity data from JSON-RPCconst [slot0,liquidity] =awaitPromise.all([poolContract.slot0(),poolContract.liquidity()]);//construct the pool using the latest on-chain dataconstpool=newuniswap_sdk.Pool( token0, token1, feeTierBip,slot0.sqrtPriceX96.toString(),liquidity.toString(),Number(slot0.tick));// Get the price of token0 in terms of token1:constpriceOfToken0InToken1=pool.token0Price.toFixed(token1Decimals);// Get the price of token1 in terms of token0:constpriceOfToken1InToken0=pool.token1Price.toFixed(token0Decimals);console.log(`Current price: ${priceOfToken1InToken0} Token0 per Token1`);console.log(`Current price: ${priceOfToken0InToken1} Token1 per Token0`);