Skip to content

Commit a94f232

Browse files
notJoononlyhyder3v4s
authored
refactor: dry swap (#421)
* remove duplicate functions * GSW-1838 refactor: Using the new constructor to protect raw data in sqrtRatio calculations * GSW-1838 refactor: need to lock pool.slot0 to prevent re-entry * refactor: Using clone data to protect original data * refactor: remove unused import * fix: sqrtRatio calculation default value issue * refactor: swap and swap math * refactor: computeSwapStep - edge case test (amount : zero and over liquidity) - EXACT IN / EXACT OUT Case * refactor: Separate pool transfer-related test code * refactor: Rename a receiver function param and liquidity math bug fix - Fix absolute value before checking if delta value is negative to fix handling error issue if negative. - Rename the param in the receiver function from pool to p. * refactor: Changed large numbers to const for readability & removed some comments * refactor: Remove unnecessary function usage * refactor: Fix error messages to make their meaning clear --------- Co-authored-by: 0xTopaz <[email protected]> Co-authored-by: 0xTopaz <[email protected]> Co-authored-by: Blake <[email protected]>
1 parent 5aadc0c commit a94f232

18 files changed

+2448
-1555
lines changed

Diff for: _deploy/p/gnoswap/pool/sqrt_price_math.gno

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(
118118

119119
res := new(u256.Uint).Add(sqrtPX96, quotient)
120120
if res.Gt(max160) {
121-
panic("sqrtPx96 + quotient overflow uint160")
121+
panic("GetNextSqrtPriceFromAmount1RoundingDown sqrtPx96 + quotient overflow uint160")
122122
}
123123
return res
124124
} else {

Diff for: _deploy/r/gnoswap/common/tick_math.gno

+14-8
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,40 @@ var binaryLogConsts = [8]*u256.Uint{
4343

4444
var (
4545
shift1By32Left = u256.MustFromDecimal("4294967296") // (1 << 32)
46+
maxTick = int32(887272)
4647
)
4748

4849
func TickMathGetSqrtRatioAtTick(tick int32) *u256.Uint { // uint160 sqrtPriceX96
4950
absTick := abs(tick)
50-
if absTick > 887272 { // MAX_TICK
51+
if absTick > maxTick {
5152
panic(addDetailToError(
5253
errOutOfRange,
53-
ufmt.Sprintf("tick_math.gno__TickMathGetSqrtRatioAtTick() || tick is out of range (larger than 887272), tick: %d", tick),
54+
ufmt.Sprintf("tick is out of range (larger than 887272), tick: %d", tick),
5455
))
5556
}
5657

57-
ratio := u256.MustFromDecimal("340282366920938463463374607431768211456") // consts.Q128
58+
var initialBit int32 = 0x1
59+
var ratio *u256.Uint
60+
if (absTick & initialBit) != 0 {
61+
ratio = tickRatioMap[initialBit]
62+
} else {
63+
ratio = u256.MustFromDecimal("340282366920938463463374607431768211456") // consts.Q128
64+
}
5865

5966
for mask, value := range tickRatioMap {
60-
if absTick&mask != 0 {
67+
if (mask != initialBit) && absTick&mask != 0 {
6168
// ratio = (ratio * value) >> 128
6269
ratio = ratio.Mul(ratio, value)
6370
ratio = ratio.Rsh(ratio, 128)
6471
}
6572
}
6673

6774
if tick > 0 {
68-
_maxUint256 := u256.MustFromDecimal("115792089237316195423570985008687907853269984665640564039457584007913129639935") // consts.MAX_UINT256
69-
_tmp := new(u256.Uint).Div(_maxUint256, ratio)
70-
ratio = _tmp.Clone()
75+
maxUint256 := u256.MustFromDecimal("115792089237316195423570985008687907853269984665640564039457584007913129639935") // consts.MAX_UINT256
76+
ratio = new(u256.Uint).Div(maxUint256, ratio)
7177
}
7278

73-
shifted := ratio.Rsh(ratio, 32).Clone() // ratio >> 32
79+
shifted := new(u256.Uint).Rsh(ratio, 32) // ratio >> 32
7480
remainder := ratio.Mod(ratio, shift1By32Left) // ratio % (1 << 32)
7581

7682
var adj *u256.Uint

Diff for: _deploy/r/gnoswap/consts/consts.gno

+1-2
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ const (
9090
UINT64_MAX uint64 = 18446744073709551615
9191

9292
MAX_UINT128 string = "340282366920938463463374607431768211455"
93-
9493
MAX_UINT160 string = "1461501637330902918203684832716283019655932542975"
95-
94+
MAX_INT256 string = "57896044618658097711785492504343953926634992332820282019728792003956564819968"
9695
MAX_UINT256 string = "115792089237316195423570985008687907853269984665640564039457584007913129639935"
9796

9897
// Tick Related

Diff for: pool/_RPC_dry.gno

-208
This file was deleted.

0 commit comments

Comments
 (0)