我正在计算UniswapV3池的token0价格( Node.js )。使用UniswapV3 SDK提供了正确的结果,但手工计算却不是。
从文档 -
sqrtPriceX96 = sqrt(price) * 2 ** 96
# divide both sides by 2 ** 96
sqrtPriceX96 / (2 ** 96) = sqrt(price)
# square both sides
(sqrtPriceX96 / (2 ** 96)) ** 2 = price
# expand the squared fraction
(sqrtPriceX96 ** 2) / ((2 ** 96) ** 2) = price
# multiply the exponents in the denominator to get the final expression
sqrtRatioX96 ** 2 / 2 ** 192 = price现在让我们以多边形上的池WMATIC-USDC为例,查询slot0返回
sqrtPriceX96 uint160 : 76424446980909989196624
tick int24 : -277045
observationIndex uint16 : 4
observationCardinality uint16 : 64
observationCardinalityNext uint16 : 64
feeProtocol uint8 : 0
unlocked bool : true让我们把这个值分配到上面的公式中-
token0Price = 76140828840970807549375^2 / 2^192 = 0.000000000000924这是错误的,它应该是0.9102578244 (由UniswapV3 SDK计算)。
知道那里出了什么问题吗?
发布于 2022-05-10 20:06:25
这似乎是一个浮点问题,如果使用它的标记小数来转换值,则公式有效,在这种情况下,WMATIC有18小数位数,USDC有6-
76140828840970807549375^2 / 2^192 * 10^18 / 10^6
= 76140828840970807549375^2 / 2^192 * 10^12
= 0.924https://ethereum.stackexchange.com/questions/127947
复制相似问题