Skip to content

Commit

Permalink
refactor: dry swap (#421)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
4 people committed Dec 18, 2024
1 parent b31b81e commit 168c7df
Show file tree
Hide file tree
Showing 18 changed files with 2,448 additions and 1,557 deletions.
2 changes: 1 addition & 1 deletion _deploy/p/gnoswap/pool/sqrt_price_math.gno
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(

res := new(u256.Uint).Add(sqrtPX96, quotient)
if res.Gt(max160) {
panic("sqrtPx96 + quotient overflow uint160")
panic("GetNextSqrtPriceFromAmount1RoundingDown sqrtPx96 + quotient overflow uint160")
}
return res
} else {
Expand Down
22 changes: 14 additions & 8 deletions _deploy/r/gnoswap/common/tick_math.gno
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,40 @@ var binaryLogConsts = [8]*u256.Uint{

var (
shift1By32Left = u256.MustFromDecimal("4294967296") // (1 << 32)
maxTick = int32(887272)
)

func TickMathGetSqrtRatioAtTick(tick int32) *u256.Uint { // uint160 sqrtPriceX96
absTick := abs(tick)
if absTick > 887272 { // MAX_TICK
if absTick > maxTick {
panic(addDetailToError(
errOutOfRange,
ufmt.Sprintf("tick_math.gno__TickMathGetSqrtRatioAtTick() || tick is out of range (larger than 887272), tick: %d", tick),
ufmt.Sprintf("tick is out of range (larger than 887272), tick: %d", tick),
))
}

ratio := u256.MustFromDecimal("340282366920938463463374607431768211456") // consts.Q128
var initialBit int32 = 0x1
var ratio *u256.Uint
if (absTick & initialBit) != 0 {
ratio = tickRatioMap[initialBit]
} else {
ratio = u256.MustFromDecimal("340282366920938463463374607431768211456") // consts.Q128
}

for mask, value := range tickRatioMap {
if absTick&mask != 0 {
if (mask != initialBit) && absTick&mask != 0 {
// ratio = (ratio * value) >> 128
ratio = ratio.Mul(ratio, value)
ratio = ratio.Rsh(ratio, 128)
}
}

if tick > 0 {
_maxUint256 := u256.MustFromDecimal("115792089237316195423570985008687907853269984665640564039457584007913129639935") // consts.MAX_UINT256
_tmp := new(u256.Uint).Div(_maxUint256, ratio)
ratio = _tmp.Clone()
maxUint256 := u256.MustFromDecimal("115792089237316195423570985008687907853269984665640564039457584007913129639935") // consts.MAX_UINT256
ratio = new(u256.Uint).Div(maxUint256, ratio)
}

shifted := ratio.Rsh(ratio, 32).Clone() // ratio >> 32
shifted := new(u256.Uint).Rsh(ratio, 32) // ratio >> 32
remainder := ratio.Mod(ratio, shift1By32Left) // ratio % (1 << 32)

var adj *u256.Uint
Expand Down
3 changes: 1 addition & 2 deletions _deploy/r/gnoswap/consts/consts.gno
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ const (
UINT64_MAX uint64 = 18446744073709551615

MAX_UINT128 string = "340282366920938463463374607431768211455"

MAX_UINT160 string = "1461501637330902918203684832716283019655932542975"

MAX_INT256 string = "57896044618658097711785492504343953926634992332820282019728792003956564819968"
MAX_UINT256 string = "115792089237316195423570985008687907853269984665640564039457584007913129639935"

// Tick Related
Expand Down
208 changes: 0 additions & 208 deletions pool/_RPC_dry.gno

This file was deleted.

Loading

0 comments on commit 168c7df

Please sign in to comment.