Skip to content

Commit 2e113b6

Browse files
committed
chore
1 parent 8f0660f commit 2e113b6

File tree

10 files changed

+119
-251
lines changed

10 files changed

+119
-251
lines changed

contract/p/gnoswap/consts/consts.gno

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@ import (
44
"std"
55
)
66

7-
// GNOSWAP SERVICE
7+
// ADDRESS
88
const (
9-
ADMIN std.Address = "g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d"
10-
DEV_OPS std.Address = "g1mjvd83nnjee3z2g7683er55me9f09688pd4mj9"
11-
TOKEN_REGISTER std.Address = "g1er355fkjksqpdtwmhf5penwa82p0rhqxkkyhk5"
12-
13-
TOKEN_REGISTER_NAMESPACE string = "gno.land/r/g1er355fkjksqpdtwmhf5penwa82p0rhqxkkyhk5"
14-
15-
BLOCK_GENERATION_INTERVAL int64 = 2 // seconds
9+
ADMIN std.Address = "g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d"
10+
DEV_OPS std.Address = "g1mjvd83nnjee3z2g7683er55me9f09688pd4mj9"
1611
)
1712

1813
// WRAP & UNWRAP
@@ -71,8 +66,6 @@ const (
7166

7267
LAUNCHPAD_PATH string = "gno.land/r/gnoswap/v1/launchpad"
7368
LAUNCHPAD_ADDR std.Address = "g122mau2lp2rc0scs8d27pkkuys4w54mdy2tuer3"
74-
75-
INIT_REGISTER_PATH string = "gno.land/r/g1er355fkjksqpdtwmhf5penwa82p0rhqxkkyhk5/v2/register_gnodev"
7669
)
7770

7871
// NUMBER
@@ -122,14 +115,17 @@ const (
122115
SECOND_IN_MILLISECOND = 1000
123116

124117
// in seconds
125-
TIMESTAMP_MINUTE = 60
126-
TIMESTAMP_HOUR = 3600
127-
TIMESTAMP_DAY = 86400
128-
TIMESTAMP_YEAR = 31536000
118+
TIMESTAMP_DAY = 86400
119+
TIMESTAMP_YEAR = 31536000
129120

130121
DAY_PER_YEAR = 365
131122
)
132123

124+
// BLOCK TIME
125+
const (
126+
BLOCK_GENERATION_INTERVAL int64 = 2 // seconds
127+
)
128+
133129
// ETCs
134130
const (
135131
// REF: https://github.com/gnolang/gno/pull/2401#discussion_r1648064219

contract/p/gnoswap/gnsmath/bit_math.gno

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,43 @@ type bitShift struct {
99
shift uint
1010
}
1111

12+
var (
13+
square_2_128 = u256.MustFromDecimal(Q128)
14+
square_2_64 = u256.MustFromDecimal(Q64)
15+
square_2_32 = u256.NewUint(0x100000000)
16+
square_2_16 = u256.NewUint(0x10000)
17+
square_2_8 = u256.NewUint(0x100)
18+
square_2_4 = u256.NewUint(0x10)
19+
square_2_2 = u256.NewUint(0x4)
20+
square_2_1 = u256.NewUint(0x2)
21+
)
22+
23+
var (
24+
max_uint128 = u256.MustFromDecimal(MAX_UINT128)
25+
max_uint64 = u256.MustFromDecimal(MAX_UINT64)
26+
max_uint32 = u256.MustFromDecimal(MAX_UINT32)
27+
max_uint16 = u256.MustFromDecimal(MAX_UINT16)
28+
max_uint8 = u256.MustFromDecimal(MAX_UINT8)
29+
num_15 = u256.MustFromDecimal(0xf)
30+
num_3 = u256.MustFromDecimal(0x3)
31+
num_1 = u256.MustFromDecimal(0x1)
32+
)
33+
34+
// BitMathMostSignificantBit returns the index of the most significant bit set to 1 in the given Uint.
1235
func BitMathMostSignificantBit(x *u256.Uint) uint8 {
1336
if x.IsZero() {
1437
panic("BitMathMostSignificantBit: x should not be zero")
1538
}
1639

1740
shifts := []bitShift{
18-
{u256.MustFromDecimal(Q128), 128}, // 2^128
19-
{u256.MustFromDecimal(Q64), 64}, // 2^64
20-
{u256.NewUint(0x100000000), 32},
21-
{u256.NewUint(0x10000), 16},
22-
{u256.NewUint(0x100), 8},
23-
{u256.NewUint(0x10), 4},
24-
{u256.NewUint(0x4), 2},
25-
{u256.NewUint(0x2), 1},
41+
{square_2_128, 128},
42+
{square_2_64, 64},
43+
{square_2_32, 32},
44+
{square_2_16, 16},
45+
{square_2_8, 8},
46+
{square_2_4, 4},
47+
{square_2_2, 2},
48+
{square_2_1, 1},
2649
}
2750

2851
r := uint8(0)
@@ -36,20 +59,21 @@ func BitMathMostSignificantBit(x *u256.Uint) uint8 {
3659
return r
3760
}
3861

62+
// BitMathLeastSignificantBit returns the index of the least significant bit set to 1 in the given Uint.
3963
func BitMathLeastSignificantBit(x *u256.Uint) uint8 {
4064
if x.IsZero() {
4165
panic("BitMathLeastSignificantBit: x should not be zero")
4266
}
4367

4468
shifts := []bitShift{
45-
{u256.MustFromDecimal(MAX_UINT128), 128},
46-
{u256.MustFromDecimal(MAX_UINT64), 64},
47-
{u256.MustFromDecimal(MAX_UINT32), 32},
48-
{u256.MustFromDecimal(MAX_UINT16), 16},
49-
{u256.MustFromDecimal(MAX_UINT8), 8},
50-
{u256.NewUint(0xf), 4},
51-
{u256.NewUint(0x3), 2},
52-
{u256.NewUint(0x1), 1},
69+
{max_uint128, 128},
70+
{max_uint64, 64},
71+
{max_uint32, 32},
72+
{max_uint16, 16},
73+
{max_uint8, 8},
74+
{num_15, 4},
75+
{num_3, 2},
76+
{num_1, 1},
5377
}
5478

5579
r := uint8(255)

contract/p/gnoswap/gnsmath/sqrt_price_math.gno

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,21 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(
5858
divValue := new(u256.Uint).Div(numerator1, sqrtPX96)
5959
addValue := new(u256.Uint).Add(divValue, amount)
6060
return u256.DivRoundingUp(numerator1, addValue)
61-
} else {
62-
cond1 := new(u256.Uint).Div(product, amount).Eq(sqrtPX96)
63-
cond2 := numerator1.Gt(product)
61+
}
6462

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+
}
6868

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")
7674
}
75+
return nextSqrtPrice
7776
}
7877

7978
// sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown calculates the next square root price
@@ -121,26 +120,25 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(
121120
panic("GetNextSqrtPriceFromAmount1RoundingDown sqrtPx96 + quotient overflow uint160")
122121
}
123122
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)
124127
} 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+
}
131130

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+
}
135134

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)
143140
}
141+
return res
144142
}
145143

146144
// sqrtPriceMathGetNextSqrtPriceFromInput calculates the next square root price
@@ -175,9 +173,9 @@ func sqrtPriceMathGetNextSqrtPriceFromInput(
175173

176174
if zeroForOne {
177175
return sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountIn, true)
178-
} else {
179-
return sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true)
180176
}
177+
178+
return sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountIn, true)
181179
}
182180

183181
// sqrtPriceMathGetNextSqrtPriceFromOutput calculates the next square root price
@@ -222,9 +220,9 @@ func sqrtPriceMathGetNextSqrtPriceFromOutput(
222220

223221
if zeroForOne {
224222
return sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amountOut, false)
225-
} else {
226-
return sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false)
227223
}
224+
225+
return sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amountOut, false)
228226
}
229227

230228
// sqrtPriceMathGetAmount0DeltaHelper calculates the absolute difference between the amounts of token0 in two
@@ -256,20 +254,20 @@ func sqrtPriceMathGetAmount0DeltaHelper(
256254
sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96
257255
}
258256

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)
261259

262260
if !(sqrtRatioAX96.Gt(u256.Zero())) {
263261
panic("sqrtRatioAX96 must be greater than zero")
264262
}
265263

266264
if roundUp {
267-
value := u256.MulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96)
265+
value := u256.MulDivRoundingUp(scaledLiquidity, ratioDiff, sqrtRatioBX96)
268266
return u256.DivRoundingUp(value, sqrtRatioAX96)
269-
} else {
270-
value := u256.MulDiv(numerator1, numerator2, sqrtRatioBX96)
271-
return new(u256.Uint).Div(value, sqrtRatioAX96)
272267
}
268+
269+
value := u256.MulDiv(scaledLiquidity, ratioDiff, sqrtRatioBX96)
270+
return new(u256.Uint).Div(value, sqrtRatioAX96)
273271
}
274272

275273
// sqrtPriceMathGetAmount1DeltaHelper calculates the absolute difference between the amounts of token1 in two
@@ -303,9 +301,9 @@ func sqrtPriceMathGetAmount1DeltaHelper(
303301
diff := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96)
304302
if roundUp {
305303
return u256.MulDivRoundingUp(liquidity, diff, u256.MustFromDecimal(Q96))
306-
} else {
307-
return u256.MulDiv(liquidity, diff, u256.MustFromDecimal(Q96))
308304
}
305+
306+
return u256.MulDiv(liquidity, diff, u256.MustFromDecimal(Q96))
309307
}
310308

311309
// SqrtPriceMathGetAmount0DeltaStr calculates the difference in the amount of token0
@@ -344,14 +342,14 @@ func SqrtPriceMathGetAmount0DeltaStr(
344342
}
345343
i := i256.FromUint256(u)
346344
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()
354345
}
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()
355353
}
356354

357355
// SqrtPriceMathGetAmount1DeltaStr calculates the difference in the amount of token1
@@ -387,12 +385,12 @@ func SqrtPriceMathGetAmount1DeltaStr(
387385
}
388386
i := i256.FromUint256(u)
389387
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()
397388
}
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()
398396
}

contract/p/gnoswap/uint256/uint256.gno

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ package uint256
44

55
import (
66
"errors"
7-
"strconv"
87
"math/bits"
8+
"strconv"
99
)
1010

1111
const (
@@ -53,8 +53,6 @@ func (z *Uint) SetOne() *Uint {
5353
return z
5454
}
5555

56-
const twoPow256Sub1 = "115792089237316195423570985008687907853269984665640564039457584007913129639935"
57-
5856
// SetFromDecimal sets z from the given string, interpreted as a decimal number.
5957
// OBS! This method is _not_ strictly identical to the (*big.Uint).SetString(..., 10) method.
6058
// Notable differences:
@@ -77,11 +75,11 @@ func (z *Uint) SetFromDecimal(s string) (err error) {
7775
}
7876
s = s[i:]
7977
}
80-
if len(s) < len(twoPow256Sub1) {
78+
if len(s) < len(MAX_UINT256) {
8179
return z.fromDecimal(s)
8280
}
83-
if len(s) == len(twoPow256Sub1) {
84-
if s > twoPow256Sub1 {
81+
if len(s) == len(MAX_UINT256) {
82+
if s > MAX_UINT256 {
8583
return ErrBig256Range
8684
}
8785
return z.fromDecimal(s)

contract/r/gnoswap/common/address_and_username.gno

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)