@@ -58,22 +58,21 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(
58
58
divValue := new(u256.Uint).Div(numerator1, sqrtPX96)
59
59
addValue := new(u256.Uint).Add(divValue, amount)
60
60
return u256.DivRoundingUp(numerator1, addValue)
61
- } else {
62
- cond1 := new(u256.Uint).Div(product, amount).Eq(sqrtPX96)
63
- cond2 := numerator1.Gt(product)
61
+ }
64
62
65
- if !(cond1 && cond2) {
66
- panic("invalid pool sqrt price calculation: product/amount != sqrtPX96 or numerator1 <= product")
67
- }
63
+ isProductDivAmountEqualSqrtPX96 := new(u256.Uint).Div(product, amount).Eq(sqrtPX96)
64
+ isNumeratorGreaterThanProduct := numerator1.Gt(product)
65
+ if !(isProductDivAmountEqualSqrtPX96 && isNumeratorGreaterThanProduct) {
66
+ panic("invalid pool sqrt price calculation: product/amount != sqrtPX96 or numerator1 <= product")
67
+ }
68
68
69
- denominator := new(u256.Uint).Sub(numerator1, product)
70
- nextSqrtPrice := u256.MulDivRoundingUp(numerator1, sqrtPX96, denominator)
71
- max160 := u256.MustFromDecimal(MAX_UINT160)
72
- if nextSqrtPrice.Gt(max160) {
73
- panic("nextSqrtPrice overflows uint160")
74
- }
75
- return nextSqrtPrice
69
+ denominator := new(u256.Uint).Sub(numerator1, product)
70
+ nextSqrtPrice := u256.MulDivRoundingUp(numerator1, sqrtPX96, denominator)
71
+ max160 := u256.MustFromDecimal(MAX_UINT160)
72
+ if nextSqrtPrice.Gt(max160) {
73
+ panic("nextSqrtPrice overflows uint160")
76
74
}
75
+ return nextSqrtPrice
77
76
}
78
77
79
78
// sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown calculates the next square root price
@@ -121,26 +120,25 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(
121
120
panic("GetNextSqrtPriceFromAmount1RoundingDown sqrtPx96 + quotient overflow uint160")
122
121
}
123
122
return res
123
+ }
124
+ if amount.Lte(u256.MustFromDecimal(MAX_UINT160)) {
125
+ value := new(u256.Uint).Lsh(amount, Q96_RESOLUTION)
126
+ quotient = u256.DivRoundingUp(value, liquidity)
124
127
} else {
125
- if amount.Lte(u256.MustFromDecimal(MAX_UINT160)) {
126
- value := new(u256.Uint).Lsh(amount, Q96_RESOLUTION)
127
- quotient = u256.DivRoundingUp(value, liquidity)
128
- } else {
129
- quotient = u256.MulDivRoundingUp(amount, u256.MustFromDecimal(Q96), liquidity)
130
- }
128
+ quotient = u256.MulDivRoundingUp(amount, u256.MustFromDecimal(Q96), liquidity)
129
+ }
131
130
132
- if !(sqrtPX96.Gt(quotient)) {
133
- panic("sqrt price exceeds calculated quotient")
134
- }
131
+ if !(sqrtPX96.Gt(quotient)) {
132
+ panic("sqrt price exceeds calculated quotient")
133
+ }
135
134
136
- res := new(u256.Uint).Sub(sqrtPX96, quotient)
137
- if res.Gt(max160) {
138
- mask := new(u256.Uint).Lsh(u256.One(), Q160_RESOLUTION)
139
- mask = mask.Sub(mask, u256.One())
140
- res = res.And(res, mask)
141
- }
142
- return res
135
+ res := new(u256.Uint).Sub(sqrtPX96, quotient)
136
+ if res.Gt(max160) {
137
+ mask := new(u256.Uint).Lsh(u256.One(), Q160_RESOLUTION)
138
+ mask = mask.Sub(mask, u256.One())
139
+ res = res.And(res, mask)
143
140
}
141
+ return res
144
142
}
145
143
146
144
// sqrtPriceMathGetNextSqrtPriceFromInput calculates the next square root price
@@ -175,9 +173,9 @@ func sqrtPriceMathGetNextSqrtPriceFromInput(
175
173
176
174
if zeroForOne {
177
175
return sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true)
178
- } else {
179
- return sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true)
180
176
}
177
+
178
+ return sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true)
181
179
}
182
180
183
181
// sqrtPriceMathGetNextSqrtPriceFromOutput calculates the next square root price
@@ -222,9 +220,9 @@ func sqrtPriceMathGetNextSqrtPriceFromOutput(
222
220
223
221
if zeroForOne {
224
222
return sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountOut, false)
225
- } else {
226
- return sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false)
227
223
}
224
+
225
+ return sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false)
228
226
}
229
227
230
228
// sqrtPriceMathGetAmount0DeltaHelper calculates the absolute difference between the amounts of token0 in two
@@ -256,20 +254,20 @@ func sqrtPriceMathGetAmount0DeltaHelper(
256
254
sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96
257
255
}
258
256
259
- numerator1 := new(u256.Uint).Lsh(liquidity, Q96_RESOLUTION)
260
- numerator2 := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96)
257
+ scaledLiquidity := new(u256.Uint).Lsh(liquidity, Q96_RESOLUTION)
258
+ ratioDiff := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96)
261
259
262
260
if !(sqrtRatioAX96.Gt(u256.Zero())) {
263
261
panic("sqrtRatioAX96 must be greater than zero")
264
262
}
265
263
266
264
if roundUp {
267
- value := u256.MulDivRoundingUp(numerator1, numerator2 , sqrtRatioBX96)
265
+ value := u256.MulDivRoundingUp(scaledLiquidity, ratioDiff , sqrtRatioBX96)
268
266
return u256.DivRoundingUp(value, sqrtRatioAX96)
269
- } else {
270
- value := u256.MulDiv(numerator1, numerator2, sqrtRatioBX96)
271
- return new(u256.Uint).Div(value, sqrtRatioAX96)
272
267
}
268
+
269
+ value := u256.MulDiv(scaledLiquidity, ratioDiff, sqrtRatioBX96)
270
+ return new(u256.Uint).Div(value, sqrtRatioAX96)
273
271
}
274
272
275
273
// sqrtPriceMathGetAmount1DeltaHelper calculates the absolute difference between the amounts of token1 in two
@@ -303,9 +301,9 @@ func sqrtPriceMathGetAmount1DeltaHelper(
303
301
diff := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96)
304
302
if roundUp {
305
303
return u256.MulDivRoundingUp(liquidity, diff, u256.MustFromDecimal(Q96))
306
- } else {
307
- return u256.MulDiv(liquidity, diff, u256.MustFromDecimal(Q96))
308
304
}
305
+
306
+ return u256.MulDiv(liquidity, diff, u256.MustFromDecimal(Q96))
309
307
}
310
308
311
309
// SqrtPriceMathGetAmount0DeltaStr calculates the difference in the amount of token0
@@ -344,14 +342,14 @@ func SqrtPriceMathGetAmount0DeltaStr(
344
342
}
345
343
i := i256.FromUint256(u)
346
344
return i256.Zero().Neg(i).ToString()
347
- } else {
348
- u := sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, liquidity.Abs(), true)
349
- if u.Gt(u256.MustFromDecimal(MAX_INT256)) {
350
- // if u > (2**255 - 1), cannot cast to int256
351
- panic("SqrtPriceMathGetAmount0DeltaStr: overflow")
352
- }
353
- return i256.FromUint256(u).ToString()
354
345
}
346
+
347
+ u := sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, liquidity.Abs(), true)
348
+ if u.Gt(u256.MustFromDecimal(MAX_INT256)) {
349
+ // if u > (2**255 - 1), cannot cast to int256
350
+ panic("SqrtPriceMathGetAmount0DeltaStr: overflow")
351
+ }
352
+ return i256.FromUint256(u).ToString()
355
353
}
356
354
357
355
// SqrtPriceMathGetAmount1DeltaStr calculates the difference in the amount of token1
@@ -387,12 +385,12 @@ func SqrtPriceMathGetAmount1DeltaStr(
387
385
}
388
386
i := i256.FromUint256(u)
389
387
return i256.Zero().Neg(i).ToString()
390
- } else {
391
- u := sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, liquidity.Abs(), true)
392
- if u.Gt(u256.MustFromDecimal(MAX_INT256)) {
393
- // if u > (2**255 - 1), cannot cast to int256
394
- panic("SqrtPriceMathGetAmount1DeltaStr: overflow")
395
- }
396
- return i256.FromUint256(u).ToString()
397
388
}
389
+
390
+ u := sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioAX96, sqrtRatioBX96, liquidity.Abs(), true)
391
+ if u.Gt(u256.MustFromDecimal(MAX_INT256)) {
392
+ // if u > (2**255 - 1), cannot cast to int256
393
+ panic("SqrtPriceMathGetAmount1DeltaStr: overflow")
394
+ }
395
+ return i256.FromUint256(u).ToString()
398
396
}
0 commit comments