PancakeSwapV3 里面,对于一个PancakePool里面的sqrtPriceX96如何计算得到 newSqrtPriceX96,这个公式是怎么样的?

直接上代码

func GetAmountOutV3(amountIn, sqrtPriceX96, fee *big.Int, zeroForOne bool) (*big.Int, *big.Int) {
    var amountOutInt *big.Int
    var amountOutFloat *big.Float
    var accuracy big.Accuracy
    var newSqrtPriceX96 *big.Int

    feeInHundredthsOfBip, _ := fee.Float64() // 2500 bips in hundredths of a bip

    Q96 := new(big.Int).Exp(big.NewInt(2), big.NewInt(96), nil)
    sqrtPrice := new(big.Float).Quo(new(big.Float).SetInt(sqrtPriceX96), new(big.Float).SetInt(Q96))
    price := new(big.Float).Mul(sqrtPrice, sqrtPrice)

    fmt.Println("price:", price)

    // Convert feeInHundredthsOfBip to a multiplier
    feeMultiplier := new(big.Float).SetFloat64(1 - (feeInHundredthsOfBip / 1000000.0))

    amountInFloat := new(big.Float).SetInt(amountIn)
    amountInFloat.Mul(amountInFloat, feeMultiplier)

    // 看路径是正还是反
    if zeroForOne {
        amountOutFloat = new(big.Float).Mul(amountInFloat, price)

    } else {
        amountOutFloat = new(big.Float).Quo(amountInFloat, price)

    }

    amountOutInt, accuracy = amountOutFloat.Int(nil)
    if accuracy != big.Exact {
        fmt.Println("Accuracy error:", accuracy)
    }

    fmt.Println("Amount of you can get: ", amountOutInt)

    fmt.Println("newSqrtPriceX96:", newSqrtPriceX96)

    return amountOutInt, newSqrtPriceX96
}

现在得到的 amountOutInt 是符合预期的,但是这个newSqrtPriceX96不知道怎么计算了。求助!

请先 登录 后评论

1 个回答

赵逸 - 区块链开发
请先 登录 后评论
  • 1 关注
  • 0 收藏,1025 浏览
  • 赵逸 提出于 2024-06-26 00:17