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 24 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
33 changes: 17 additions & 16 deletions _deploy/p/gnoswap/pool/sqrt_price_math.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
u256 "gno.land/p/gnoswap/uint256"
)

const Q96_RESOLUTION = uint(96)

func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(
sqrtPX96 *u256.Uint, // uint160
liquidity *u256.Uint, // uint128
Expand All @@ -15,7 +17,7 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(
return sqrtPX96
}

numerator1 := new(u256.Uint).Lsh(liquidity, 96)
numerator1 := new(u256.Uint).Lsh(liquidity, Q96_RESOLUTION)
product := new(u256.Uint).Mul(amount, sqrtPX96)

if add {
Expand All @@ -27,9 +29,9 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(
}
}

div := new(u256.Uint).Div(numerator1, sqrtPX96)
add := new(u256.Uint).Add(div, amount)
return u256.DivRoundingUp(numerator1, add)
divValue := new(u256.Uint).Div(numerator1, sqrtPX96)
addValue := new(u256.Uint).Add(divValue, amount)
return u256.DivRoundingUp(numerator1, addValue)
} else {
cond1 := new(u256.Uint).Div(product, amount).Eq(sqrtPX96)
cond2 := numerator1.Gt(product)
Expand All @@ -53,24 +55,23 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(

if add {
if amount.Lte(u256.MustFromDecimal(MAX_UINT160)) {
notJoon marked this conversation as resolved.
Show resolved Hide resolved
value1 := new(u256.Uint).Lsh(amount, 96)
quotient = new(u256.Uint).Div(value1, liquidity)
value := new(u256.Uint).Lsh(amount, Q96_RESOLUTION)
quotient = new(u256.Uint).Div(value, liquidity)
} else {
quotient = u256.MulDiv(amount, u256.MustFromDecimal(Q96), liquidity)
}

res := new(u256.Uint).Add(sqrtPX96, quotient)
max160 := u256.MustFromDecimal("1461501637330902918203684832716283019655932542975")
max160 := u256.MustFromDecimal(MAX_UINT160)

if res.Gt(max160) {
panic("sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown sqrtPx96 + quotient overflow uint160")
panic("GetNextSqrtPriceFromAmount1RoundingDown sqrtPx96 + quotient overflow uint160")
}
return res

} else {
if amount.Lte(u256.MustFromDecimal(MAX_UINT160)) {
value1 := new(u256.Uint).Lsh(amount, 96)
quotient = u256.DivRoundingUp(value1, liquidity)
value := new(u256.Uint).Lsh(amount, Q96_RESOLUTION)
quotient = u256.DivRoundingUp(value, liquidity)
} else {
quotient = u256.MulDivRoundingUp(amount, u256.MustFromDecimal(Q96), liquidity)
}
Expand Down Expand Up @@ -134,19 +135,19 @@ func sqrtPriceMathGetAmount0DeltaHelper(
sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96
}

numerator1 := new(u256.Uint).Lsh(liquidity, 96)
numerator1 := new(u256.Uint).Lsh(liquidity, Q96_RESOLUTION)
numerator2 := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96)

if !(sqrtRatioAX96.Gt(u256.Zero())) {
panic("pool_sqrt price math #3")
}
notJoon marked this conversation as resolved.
Show resolved Hide resolved

if roundUp {
value1 := u256.MulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96)
return u256.DivRoundingUp(value1, sqrtRatioAX96)
value := u256.MulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96)
return u256.DivRoundingUp(value, sqrtRatioAX96)
} else {
value1 := u256.MulDiv(numerator1, numerator2, sqrtRatioBX96)
return new(u256.Uint).Div(value1, sqrtRatioAX96)
value := u256.MulDiv(numerator1, numerator2, sqrtRatioBX96)
return new(u256.Uint).Div(value, sqrtRatioAX96)
}
}

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 { // MAX_TICK
r3v4s marked this conversation as resolved.
Show resolved Hide resolved
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