如何计算新创建的BTC/USDc池的初始平方根价格的sqrtPriceX96值,其中1 BTC = 20k USDC,BTC为8 Decimal,USDC为6 Decimal。
sqrtPriceX96需要createAndInitializePoolIfNecessary函数
发布于 2023-03-16 21:22:58
如果您有创建池的blockNumber,您可以执行这样的调用:
const slot0 = await pool.slot0({blockTag: blockNumber})
然后
const price = slot0.sqrtPricex96._hex
或者你可以:
const price = 1/20000
然后调用priceToSqrtPrice
export const priceToSqrtPrice = (price: number, token0Decimals: number, token1Decimals: number) => {
const decimalAdjustment = 10 ** (token0Decimals - token1Decimals);
const mathPrice = price / decimalAdjustment;
let sqrtPriceX96 = Math.floor(Math.sqrt(mathPrice) * 2 ** 96);
return BigInt(sqrtPriceX96);
};https://ethereum.stackexchange.com/questions/147496
复制相似问题