Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: dry swap #421

Merged
merged 30 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1aa9e14
dry swap
notJoon Dec 6, 2024
8f762e0
Merge branch 'main' into dry-swap
notJoon Dec 10, 2024
7205d6b
add test and mov file
notJoon Dec 10, 2024
fb3d577
remove duplicate functions
notJoon Dec 11, 2024
7631ecb
fix
notJoon Dec 11, 2024
8b83678
GSW-1838 refactor: Using the new constructor to protect raw data in s…
onlyhyde Dec 11, 2024
53bc59f
GSW-1838 refactor: need to lock pool.slot0 to prevent re-entry
onlyhyde Dec 11, 2024
7d3f7db
refactor: Using clone data to protect original data
onlyhyde Dec 11, 2024
e11c132
Merge branch 'main' into dry-swap
onlyhyde Dec 11, 2024
616681f
refactor: remove unused import
onlyhyde Dec 11, 2024
30f91b5
Merge branch 'dry-swap' of https://github.com/gnoswap-labs/gnoswap in…
onlyhyde Dec 11, 2024
5fb5341
fix: test error
onlyhyde Dec 11, 2024
9e6ffc2
fix: sqrtRatio calculation default value issue
onlyhyde Dec 11, 2024
cd3db8b
refactor: swap and swap math
onlyhyde Dec 11, 2024
5b8e6b4
fix: test code fail issue
onlyhyde Dec 11, 2024
6bd8d59
Apply suggestions from code review
notJoon Dec 12, 2024
66c1202
test
notJoon Dec 12, 2024
81c8884
refactor: computeSwapStep
onlyhyde Dec 12, 2024
5b2c48b
update docs
notJoon Dec 13, 2024
5a924a9
test: add test case
onlyhyde Dec 13, 2024
0b2b1c5
refactor: Separate pool transfer-related test code
onlyhyde Dec 15, 2024
6baeae7
refactor: Rename a receiver function param and liquidity math bug fix
onlyhyde Dec 15, 2024
295ad8d
Merge branch 'main' into dry-swap
onlyhyde Dec 15, 2024
0686a75
Merge branch 'main' into dry-swap
onlyhyde Dec 15, 2024
906ab51
refactor: Changed large numbers to const for readability & removed so…
onlyhyde Dec 15, 2024
71823f1
Merge branch 'dry-swap' of https://github.com/gnoswap-labs/gnoswap in…
onlyhyde Dec 15, 2024
d2bb70c
Merge branch 'main' into dry-swap
onlyhyde Dec 16, 2024
d71fbb4
Apply suggestions from code review
onlyhyde Dec 16, 2024
5da17f4
refactor: Remove unnecessary function usage
onlyhyde Dec 16, 2024
bcb6edf
refactor: Fix error messages to make their meaning clear
onlyhyde Dec 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 13 additions & 7 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),
onlyhyde marked this conversation as resolved.
Show resolved Hide resolved
))
}

ratio := u256.MustFromDecimal("340282366920938463463374607431768211456") // consts.Q128
var initialBit int32 = 0x1
var ratio *u256.Uint
r3v4s marked this conversation as resolved.
Show resolved Hide resolved
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 {
notJoon marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading