直接上代码
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不知道怎么计算了。求助!