From 6ac464ec17a6f8733dd5cdc482786d2e0a2462c6 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 14 Mar 2024 10:58:44 +0900 Subject: [PATCH 01/11] GSW-925 feat: naked return --- gov/realm_variables.gno | 2 +- packages/big/int256/int256_test.gno | 1 - packages/big/uint256/uint256.gno | 2 +- packages/big/uint256/uint256_test.gno | 18 -- .../gnoswap/common/_TEST_tick_math_test.gno | 256 ------------------ pool/_RPC_dry.gno | 13 +- pool/pool.gno | 9 - pool/pool_manager.gno | 45 ++- pool/swap_math.gno | 13 +- pool/swap_parameters.gno | 4 +- pool/tick.gno | 9 +- pool/tick_bitmap.gno | 4 +- pool/withdrawal_fee.gno | 11 +- ...st_two_position_used_single_swap_test.gnoa | 2 +- position/nft_helper.gno | 4 - position/sqrt_price_math.gno | 28 -- position/utils.gno | 34 +++ router/router.gno | 2 +- router/router_dry.gno | 5 +- staker/_TEST_staker_one_external_test.gnoa | 10 +- ...EST_staker_one_increase_external_test.gno} | 10 +- ...est.gno => _TEST_staker_rpc_get_test.gnoa} | 12 +- staker/staker_parameters.gno | 8 +- staker/utils.gno | 1 - 24 files changed, 101 insertions(+), 402 deletions(-) delete mode 100644 packages/big/int256/int256_test.gno delete mode 100644 packages/big/uint256/uint256_test.gno delete mode 100644 packages/gnoswap/common/_TEST_tick_math_test.gno rename staker/{_TEST_staker_one_increase_external_test.gnoa => _TEST_staker_one_increase_external_test.gno} (94%) rename staker/{_TEST_staker_rpc_get_test.gno => _TEST_staker_rpc_get_test.gnoa} (94%) diff --git a/gov/realm_variables.gno b/gov/realm_variables.gno index b95377d1..4f8debdb 100644 --- a/gov/realm_variables.gno +++ b/gov/realm_variables.gno @@ -49,7 +49,7 @@ func vote(proposalID uint64, address string, option VoteOption) { vote, ok := p.Voters.Get(address) if !ok { // new voter, add to voters map and increase the total voting power - p.updateVotingPower(bigint(0), power) + p.updateVotingPower(0, power) } diff --git a/packages/big/int256/int256_test.gno b/packages/big/int256/int256_test.gno deleted file mode 100644 index e5b80598..00000000 --- a/packages/big/int256/int256_test.gno +++ /dev/null @@ -1 +0,0 @@ -package int256 diff --git a/packages/big/uint256/uint256.gno b/packages/big/uint256/uint256.gno index 8f454ce5..ccce6090 100644 --- a/packages/big/uint256/uint256.gno +++ b/packages/big/uint256/uint256.gno @@ -963,7 +963,7 @@ func (z *Uint) fromDecimal(bs string) error { z.SetUint64(num) } else { base := NewUint(num) - z.UnsafeAdd(z, base.Mul(base, mult)) + z.Add(z, base.Mul(base, mult)) } // Chop off another 19 characters if remaining > 19 { diff --git a/packages/big/uint256/uint256_test.gno b/packages/big/uint256/uint256_test.gno deleted file mode 100644 index 5c56abd5..00000000 --- a/packages/big/uint256/uint256_test.gno +++ /dev/null @@ -1,18 +0,0 @@ -package uint256 - -import ( - "testing" -) - -func TestFuncs(t *testing.T) { - x := NewUint(0) - y := NewUint(1) - - // println(x.Dec()) - - var m256 bigint = 115792089237316195423570985008687907853269984665640564039457584007913129639935 - - z := FromBigint(m256) - - println(z.Dec()) -} diff --git a/packages/gnoswap/common/_TEST_tick_math_test.gno b/packages/gnoswap/common/_TEST_tick_math_test.gno deleted file mode 100644 index 41602a31..00000000 --- a/packages/gnoswap/common/_TEST_tick_math_test.gno +++ /dev/null @@ -1,256 +0,0 @@ -package common - -import ( - "testing" - - "gno.land/r/demo/consts" -) - -func TestFindMSB(t *testing.T) { - tests := []struct { - name string - ratio bigint - expectedMSB bigint - expectedRatio bigint - }{ - { - name: "very low ratio", - ratio: bigint(1 << 10), - expectedMSB: 10, - expectedRatio: 1, - }, - { - name: "low ratio", - ratio: bigint(1 << 30), - expectedMSB: 30, - expectedRatio: 1, - }, - { - name: "medium ratio", - ratio: bigint(1 << 50), - expectedMSB: 50, - expectedRatio: 1, - }, - { - name: "high ratio", - ratio: 1 << 100, - expectedMSB: 100, - expectedRatio: 1, - }, - { - name: "very high ratio", - ratio: 1 << 110, - expectedMSB: 110, - expectedRatio: 1, - }, - { - name: "extreme ratio", - ratio: 1 << 200, - expectedMSB: 200, - expectedRatio: 1, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - msb, ratio := findMSB(tt.ratio) - - if msb != tt.expectedMSB { - t.Errorf("expected MSB to be %d, got %d", tt.expectedMSB, msb) - } - - if ratio != tt.expectedRatio { - t.Errorf("expected ratio to be %d, got %d", tt.expectedRatio, ratio) - } - }) - } -} - -func TestTickMathGetSqrtRatioAtTick(t *testing.T) { - testCases := []struct { - name string - tick int32 - expected bigint - isErr bool - }{ - { - name: "positive 1", - tick: 1, - expected: 79232123823359799118286999568, - isErr: false, - }, - { - name: "Test Case 1", - tick: int32(10), - expected: 79267784519130042428790663799, - isErr: false, - }, - { - name: "Test Case 2", - tick: int32(20), - expected: 79307426338960776842885539845, - isErr: false, - }, - { - name: "Test Case 3", - tick: int32(30), - expected: 79347087983666005045280518415, - isErr: false, - }, - { - name: "negative tick", - tick: -1, - expected: 79224201403219477170569942574, - isErr: false, - }, - { - name: "zero tick", - tick: 0, - expected: 79228162514264337593543950337, - isErr: false, - }, - { - name: "maximum tick", - tick: 887272, - expected: 1461446703485210103287273052203988822378723970342, - isErr: false, - }, - { - name: "minimum tick", - tick: -887272, - expected: 4295128739, - isErr: false, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - result := TickMathGetSqrtRatioAtTick(tc.tick) - if tc.isErr && result != 0 { - t.Errorf("(%s) expected error, got %d", tc.name, result) - } - - if result != tc.expected { - t.Errorf("(%s) expected %d, got %d", tc.name, tc.expected, result) - } - }) - } -} - -func TestTickMathGetTickAtSqrtRatio(t *testing.T) { - testCases := []struct { - name string - sqrtPriceX96 bigint - expectedResult int32 - isErr bool - }{ - { - name: "Test Case 1", - sqrtPriceX96: 130621891405341611593710811006, - expectedResult: int32(10000), - isErr: false, - }, - { - name: "max uint64", - sqrtPriceX96: consts.MAX_UINT64, - expectedResult: -443637, - isErr: false, - }, - { - name: "max uint128", - sqrtPriceX96: consts.MAX_UINT128, - expectedResult: 443636, - isErr: false, - }, - { - name: "min sqrt ratio", - sqrtPriceX96: consts.MIN_SQRT_RATIO, - expectedResult: -887272, - isErr: false, - }, - { - name: "q96", - sqrtPriceX96: consts.Q96, - expectedResult: -1, - isErr: false, - }, - { - name: "q128", - sqrtPriceX96: consts.Q128, - expectedResult: 443636, - isErr: false, - }, - { - name: "Test Case for typical sqrtPriceX96", - sqrtPriceX96: 79228162514264337593543950336, - expectedResult: -1, - isErr: false, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - result := TickMathGetTickAtSqrtRatio(tc.sqrtPriceX96) - - if tc.isErr && result != 0 { - t.Errorf("(%s) expected error, got %d", tc.name, result) - } - }) - } -} - -func TestCalculateLog2(t *testing.T) { - testCases := []struct { - name string - msb bigint - ratio bigint - expected bigint - }{ - { - name: "100", - msb: 100, - ratio: 1000, - expected: -516508834063867445248, - }, - { - name: "msb at higher bound with ratio 1", - msb: 256, - ratio: 1, - expected: 2361183241434822606848, - }, - { - name: "higher msb with higher ratio", - msb: 256, - ratio: 2, - expected: 2361183241434822606848, - }, - { - name: "lower bound", - msb: 128, - ratio: 1 << 127, - expected: 0, - }, - { - name: "high msb with exact square", - msb: 256, - ratio: 1 << 128, - expected: 2379629985508532158464, - }, - { - name: "double value", - msb: 129, - ratio: 1 << 128, - expected: 1 << 64, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - result := calculateLog2(tc.msb, tc.ratio) - - if result != tc.expected { - t.Errorf("(%s) expected %d, got %d", tc.name, tc.expected, result) - } - }) - } -} diff --git a/pool/_RPC_dry.gno b/pool/_RPC_dry.gno index 2e82e25c..5b908182 100644 --- a/pool/_RPC_dry.gno +++ b/pool/_RPC_dry.gno @@ -4,7 +4,6 @@ import ( "std" "gno.land/p/demo/common" - "gno.land/p/demo/ufmt" "gno.land/r/demo/consts" @@ -20,20 +19,14 @@ func DrySwap( zeroForOne bool, _amountSpecified string, // int256 _sqrtPriceLimitX96 string, // uint160 -) (string, string, bool) { // int256 x2 +) (string, string, bool) { if _amountSpecified == "0" { return "0", "0", false } - amountSpecified, err := i256.FromDecimal(_amountSpecified) - if err != nil { - panic(ufmt.Sprintf("[POOL] _RPC_dry.gno__DrySwap() || u256.FromDecimal(_amountSpecified[%s]) failed", _amountSpecified)) - } - sqrtPriceLimitX96, err := u256.FromDecimal(_sqrtPriceLimitX96) - if err != nil { - panic(ufmt.Sprintf("[POOL] _RPC_dry.gno__DrySwap() || u256.FromDecimal(_sqrtPriceLimitX96[%s]) failed", _sqrtPriceLimitX96)) - } + amountSpecified := i256.MustFromDecimal(_amountSpecified) + sqrtPriceLimitX96 := u256.MustFromDecimal(_sqrtPriceLimitX96) pool := GetPool(token0Path, token1Path, fee) slot0Start := pool.slot0 diff --git a/pool/pool.gno b/pool/pool.gno index df0d781b..1b81fd97 100644 --- a/pool/pool.gno +++ b/pool/pool.gno @@ -523,15 +523,6 @@ func CollectProtocol( amount0Requested := u256.MustFromDecimal(_amount0Requested) amount1Requested := u256.MustFromDecimal(_amount1Requested) - amount0Requested, err := u256.FromDecimal(_amount0Requested) - if err != nil { - panic(ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || u256.FromDecimal(_amount0Requested[%s]) failed", _amount0Requested)) - } - amount1Requested, err := u256.FromDecimal(_amount1Requested) - if err != nil { - panic(ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || u256.FromDecimal(_amount1Requested[%s]) failed", _amount1Requested)) - } - pool := GetPool(token0Path, token1Path, fee) amount0 := u256Min(amount0Requested, pool.protocolFees.token0) diff --git a/pool/pool_manager.gno b/pool/pool_manager.gno index fb068df6..cade64a8 100644 --- a/pool/pool_manager.gno +++ b/pool/pool_manager.gno @@ -57,15 +57,7 @@ func CreatePool( panic(ufmt.Sprintf("[POOl] pool_manager.gno__CreatePool() || expected token0Path(%s) != token1Path(%s)", token0Path, token1Path)) } - sqrtPriceX96, err := u256.FromDecimal(_sqrtPriceX96) - if err != nil { - panic("CREATE POOL SQRT STRING") - } - - sqrtPriceX96, err := u256.FromDecimal(_sqrtPriceX96) - if err != nil { - panic("CREATE POOL SQRT STRING") - } + sqrtPriceX96 := u256.MustFromDecimal(_sqrtPriceX96) if token1Path < token0Path { token0Path, token1Path = token1Path, token0Path @@ -75,8 +67,6 @@ func CreatePool( // check tickSpacing for fee tickSpacing := feeAmountTickSpacing[fee] - // requirePositive(bigint(tickSpacing), ufmt.Sprintf("[POOL] pool_manager.gno__CreatePool() || expected tickSpacing(%d) > 0", tickSpacing)) - // calculate poolPath poolPath := GetPoolPath(token0Path, token1Path, fee) @@ -188,22 +178,23 @@ func newPool( token1: u256.Zero(), } - return &Pool{ - token0Path: token0Path, - token1Path: token1Path, - balances: balances, - fee: fee, - tickSpacing: tickSpacing, - maxLiquidityPerTick: maxLiquidityPerTick, - slot0: slot0, - feeGrowthGlobal0X128: u256.Zero(), - feeGrowthGlobal1X128: u256.Zero(), - protocolFees: protocolFees, - liquidity: u256.Zero(), - ticks: Ticks{}, - tickBitmaps: TickBitmaps{}, - positions: Positions{}, - } + pool := &Pool{} + pool.token0Path = token0Path + pool.token1Path = token1Path + pool.balances = balances + pool.fee = fee + pool.tickSpacing = tickSpacing + pool.maxLiquidityPerTick = maxLiquidityPerTick + pool.slot0 = slot0 + pool.feeGrowthGlobal0X128 = u256.Zero() + pool.feeGrowthGlobal1X128 = u256.Zero() + pool.protocolFees = protocolFees + pool.liquidity = u256.Zero() + pool.ticks = Ticks{} + pool.tickBitmaps = TickBitmaps{} + pool.positions = Positions{} + + return pool } func isAdmin(target std.Address) bool { diff --git a/pool/swap_math.gno b/pool/swap_math.gno index daf07211..d7372f8a 100644 --- a/pool/swap_math.gno +++ b/pool/swap_math.gno @@ -11,20 +11,24 @@ func swapMathComputeSwapStep( liquidity *u256.Uint, // uint128 amountRemaining *i256.Int, // int256 feePips uint64, -) (sqrtRatioNextX96, amountIn, amountOut, feeAmount *u256.Uint) { +) (*u256.Uint, *u256.Uint, *u256.Uint, *u256.Uint) { // ) (sqrtRatioNextX96, amountIn, amountOut, feeAmount *u256.Uint) { isToken1Expensive := sqrtRatioCurrentX96.Gte(sqrtRatioTargetX96) // POSTIVIE == EXACT_IN => Estimated AmountOut // NEGATIVE == EXACT_OUT => Estimated AmountIn exactIn := !(amountRemaining.IsNeg()) // amountRemaining >= 0 + sqrtRatioNextX96 := u256.Zero() + amountIn := u256.Zero() + amountOut := u256.Zero() + feeAmount := u256.Zero() + if exactIn { amountRemainingLessFee := u256.Zero() - - // amountRemainingLessFee := amountRemaining * bigint(uint64(1000000-feePips)) / 1000000 _amountRemaining := amountRemaining.Abs() amountRemainingLessFee = amountRemainingLessFee.Mul(_amountRemaining, u256.NewUint(1000000-feePips)) amountRemainingLessFee = amountRemainingLessFee.Div(amountRemainingLessFee, u256.NewUint(1000000)) + if isToken1Expensive { amountIn = sqrtPriceMathGetAmount0DeltaHelper(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, true) } else { @@ -41,6 +45,7 @@ func swapMathComputeSwapStep( isToken1Expensive, ) } + } else { if isToken1Expensive { amountOut = sqrtPriceMathGetAmount1DeltaHelper(sqrtRatioTargetX96, sqrtRatioCurrentX96, liquidity, false) @@ -56,7 +61,7 @@ func swapMathComputeSwapStep( liquidity, amountRemaining.Abs(), isToken1Expensive, - ) + ) } } diff --git a/pool/swap_parameters.gno b/pool/swap_parameters.gno index 3564a0ba..b0b4515b 100644 --- a/pool/swap_parameters.gno +++ b/pool/swap_parameters.gno @@ -12,8 +12,8 @@ func SubmitProposalParameterProtocolFees( metadata string, initialDeposit uint64, - newFee0 bigint, - newFee1 bigint, + newFee0 uint32, + newFee1 uint32, ) uint64 { if !((newFee0 == 0 || (newFee0 >= 4 && newFee0 <= 10)) && (newFee1 == 0 || (newFee1 >= 4 && newFee1 <= 10))) { panic(ufmt.Sprintf("[POOL] pool.gno__SubmitProposalParameterProtocolFees() || expected (newFee0(%d) == 0 || (newFee0(%d) >= 4 && newFee0(%d) <= 10)) && (newFee1(%d) == 0 || (newFee1(%d) >= 4 && newFee1(%d) <= 10))", newFee0, newFee0, newFee0, newFee1, newFee1, newFee1)) diff --git a/pool/tick.gno b/pool/tick.gno index 64b10bcc..832e6571 100644 --- a/pool/tick.gno +++ b/pool/tick.gno @@ -12,9 +12,6 @@ func tickTickSpacingToMaxLiquidityPerTick(tickSpacing int32) *u256.Uint { maxTick := (consts.MAX_TICK / tickSpacing) * tickSpacing numTicks := uint64((maxTick-minTick)/tickSpacing) + 1 - // requireUnsigned(consts.MAX_UINT128/numTicks, ufmt.Sprintf("[POOL] tick.gno__tickTickSpacingToMaxLiquidityPerTick() || consts.MAX_UINT128(%d) / numTicks(%d) >= 0(%d)", consts.MAX_UINT128, numTicks, consts.MAX_UINT128/numTicks)) - // return consts.MAX_UINT128 / numTicks - _maxUint128 := u256.MustFromDecimal(consts.MAX_UINT128) return new(u256.Uint).Div(_maxUint128, u256.NewUint(numTicks)) } @@ -25,7 +22,7 @@ func (pool *Pool) tickGetFeeGrowthInside( tickCurrent int32, feeGrowthGlobal0X128 *u256.Uint, // uint256 feeGrowthGlobal1X128 *u256.Uint, // uint256 -) (feeGrowthInside0X128, feeGrowthInside1X128 *u256.Uint) { // uint256 +) (*u256.Uint, *u256.Uint) { // uint256 lower := pool.ticks[tickLower] upper := pool.ticks[tickUpper] @@ -47,8 +44,8 @@ func (pool *Pool) tickGetFeeGrowthInside( feeGrowthAbove1X128 = new(u256.Uint).Sub(feeGrowthGlobal1X128, upper.feeGrowthOutside1X128) } - feeGrowthInside0X128 = new(u256.Uint).Sub(new(u256.Uint).Sub(feeGrowthGlobal0X128, feeGrowthBelow0X128), feeGrowthAbove0X128) - feeGrowthInside1X128 = new(u256.Uint).Sub(new(u256.Uint).Sub(feeGrowthGlobal1X128, feeGrowthBelow1X128), feeGrowthAbove1X128) + feeGrowthInside0X128 := new(u256.Uint).Sub(new(u256.Uint).Sub(feeGrowthGlobal0X128, feeGrowthBelow0X128), feeGrowthAbove0X128) + feeGrowthInside1X128 := new(u256.Uint).Sub(new(u256.Uint).Sub(feeGrowthGlobal1X128, feeGrowthBelow1X128), feeGrowthAbove1X128) return feeGrowthInside0X128, feeGrowthInside1X128 } diff --git a/pool/tick_bitmap.gno b/pool/tick_bitmap.gno index 6aa1d875..8ff8b306 100644 --- a/pool/tick_bitmap.gno +++ b/pool/tick_bitmap.gno @@ -70,7 +70,7 @@ func (pool *Pool) tickBitmapNextInitializedTickWithInOneWord( shift1LeftByBitPos := new(u256.Uint).Lsh(u256.NewUint(1), uint(bitPos)) _mask := shift1LeftByBitPos.Clone() _mask = new(u256.Uint).Sub(_mask, u256.NewUint(1)) - mask := bigintBitwiseNotForUint256BitmapIndexing(_mask) + mask := bitwiseNotForUint256BitmapIndexing(_mask) pool.tickBitmaps[wordPos] = pool.tickBitmaps[wordPos].NilToZero() masked := new(u256.Uint).And(pool.tickBitmaps[wordPos], mask) @@ -86,7 +86,7 @@ func (pool *Pool) tickBitmapNextInitializedTickWithInOneWord( } } -func bigintBitwiseNotForUint256BitmapIndexing(x *u256.Uint) *u256.Uint { +func bitwiseNotForUint256BitmapIndexing(x *u256.Uint) *u256.Uint { // REF: https://stackoverflow.com/a/77071037 // Create a mask with all bits set to 1 diff --git a/pool/withdrawal_fee.gno b/pool/withdrawal_fee.gno index ac7d31c9..912e0fd2 100644 --- a/pool/withdrawal_fee.gno +++ b/pool/withdrawal_fee.gno @@ -26,19 +26,12 @@ func HandleWithdrawalFee( panic(ufmt.Sprintf("[POOL] withdrawal_fee.gno__HandleWithdrawFee() || expected std.PrevRealm().PkgPath()(%s), got %s", consts.POSITION_PATH, std.PrevRealm().PkgPath())) } - amount0, err := u256.FromDecimal(_amount0) - if err != nil { - ufmt.Sprintf("[POOL] withdrawal_fee.gno__HandleWithdrawFee() ||u256.FromDecimal(%s) failed", _amount0) - } - amount1, err := u256.FromDecimal(_amount1) - if err != nil { - ufmt.Sprintf("[POOL] withdrawal_fee.gno__HandleWithdrawFee() ||u256.FromDecimal(%s) failed", _amount1) - } + amount0 := u256.MustFromDecimal(_amount0) + amount1 := u256.MustFromDecimal(_amount1) if withdrawalFee == 0 { return _amount0, _amount1 } - feeAmount0 := new(u256.Uint).Mul(amount0, u256.NewUint(withdrawalFee)) feeAmount0 = new(u256.Uint).Div(feeAmount0, u256.NewUint(100)) diff --git a/position/_TEST_position_test_two_position_used_single_swap_test.gnoa b/position/_TEST_position_test_two_position_used_single_swap_test.gnoa index 16b3d1f5..254e5088 100644 --- a/position/_TEST_position_test_two_position_used_single_swap_test.gnoa +++ b/position/_TEST_position_test_two_position_used_single_swap_test.gnoa @@ -115,7 +115,7 @@ func TestCollectFeeAfterSwapPos1(t *testing.T) { tokenId, fee0, fee1, poolPath := CollectFee(1) shouldEQ(t, tokenId, uint64(1)) - // shouldEQ(t, fee0, bigint(617)) + // shouldEQ(t, fee0, uint64(617)) shouldEQ(t, fee0, "126") shouldEQ(t, fee1, "0") shouldEQ(t, poolPath, "gno.land/r/demo/bar:gno.land/r/demo/foo:500") diff --git a/position/nft_helper.gno b/position/nft_helper.gno index 5406e70b..3e29624a 100644 --- a/position/nft_helper.gno +++ b/position/nft_helper.gno @@ -9,10 +9,6 @@ import ( "gno.land/r/demo/gnft" ) -func nftBalanceOf(addr std.Address) bigint { - return bigint(gnft.BalanceOf(a2u(addr))) -} - func exists(tokenId uint64) bool { // non exist tokenId will panic // use defer to catch the panic diff --git a/position/sqrt_price_math.gno b/position/sqrt_price_math.gno index 3de01094..8ccf6415 100644 --- a/position/sqrt_price_math.gno +++ b/position/sqrt_price_math.gno @@ -80,34 +80,6 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown( quotient = new(u256.Uint).Sub(quotient, u256.One()) return new(u256.Uint).Sub(sqrtPX96, quotient) } - - // quotient mutst be positive when amount and liquidity are positive - if add { - if amount.Lte(u256.UnsafeFromDecimal(consts.MAX_UINT160)) { - quotient = new(u256.Uint).Lsh(amount, 96) - quotient = new(u256.Uint).Div(quotient, liquidity) - } else { - quotient = u256.MulDiv(amount, u256.UnsafeFromDecimal(consts.Q96), liquidity) - } - - quotient = new(u256.Uint).Sub(quotient, u256.One()) - return new(u256.Uint).Add(sqrtPX96, quotient) - } else { - if amount.Lte(u256.UnsafeFromDecimal(consts.MAX_UINT160)) { - value1 := new(u256.Uint).Lsh(amount, 96) - quotient = u256.DivRoundingUp(value1, liquidity) - } else { - quotient = u256.MulDiv(amount, u256.UnsafeFromDecimal(consts.Q96), liquidity) - } - - if !(sqrtPX96.Gt(quotient)) { - panic("position_sqrt price math #2") - } - - // roundDown - quotient = new(u256.Uint).Sub(quotient, u256.One()) - return new(u256.Uint).Sub(sqrtPX96, quotient) - } } func sqrtPriceMathGetNextSqrtPriceFromInput( diff --git a/position/utils.gno b/position/utils.gno index e69de29b..3c0ccc81 100644 --- a/position/utils.gno +++ b/position/utils.gno @@ -0,0 +1,34 @@ +package position + +import ( + "std" + "strconv" + "strings" + "time" + + "gno.land/p/demo/ufmt" + + "gno.land/r/demo/users" +) + +func checkDeadline(deadline int64) { + if time.Now().Unix() > deadline { + panic(ufmt.Sprintf("[POSITION] utils.gno__checkDeadline() || transaction too old__time.Now().Unix()(%d) <= deadline(%d)", time.Now().Unix(), deadline)) + } +} + +func a2u(addr std.Address) users.AddressOrName { + return users.AddressOrName(addr) +} + +func poolKeyDivide(poolKey string) (string, string, uint32) { + res := strings.Split(poolKey, ":") + if len(res) != 3 { + panic(ufmt.Sprintf("[POSITION] utils.gno__poolKeyDivide() || invalid poolKey(%s)", poolKey)) + } + + pToken0, pToken1, pFeeStr := res[0], res[1], res[2] + + pFee, _ := strconv.Atoi(pFeeStr) + return pToken0, pToken1, uint32(pFee) +} diff --git a/router/router.gno b/router/router.gno index ba48426a..ca7a6a8f 100644 --- a/router/router.gno +++ b/router/router.gno @@ -32,7 +32,7 @@ func SwapRoute( panic("[ROUTER] router.gno__SwapRoute() || amountSpecified == 0") } - tokenAmountLimit, err := u256.FromDecimal(_tokenAmountLimit) + tokenAmountLimit := u256.MustFromDecimal(_tokenAmountLimit) if err != nil { panic(ufmt.Sprintf("[ROUTER] router.gno__SwapRoute() || u256.FromDecimal(_tokenAmountLimit(%s) failed", _tokenAmountLimit)) } diff --git a/router/router_dry.gno b/router/router_dry.gno index a1d9e5bf..580582fe 100644 --- a/router/router_dry.gno +++ b/router/router_dry.gno @@ -7,7 +7,7 @@ import ( "gno.land/p/demo/ufmt" i256 "gno.land/p/big/int256" -u256 "gno.land/p/big/uint256" + u256 "gno.land/p/big/uint256" ) func DrySwapRoute( @@ -86,10 +86,7 @@ func DrySwapRoute( } // PROTOCOL FEE - // resultAmountOut = handleProtocolFee(outputToken, resultAmountOut, true) // DrySwap should return raw resultAmountOut( ≈ with protocol fee ) - // Interface needs to calculate 0.15 protocol fee - if swapType == "EXACT_IN" { if !(i256.FromUint256(resultAmountIn).Eq(amountSpecified)) { return "-1" // if pool doesn't have enough output token amount to swap against input token amount diff --git a/staker/_TEST_staker_one_external_test.gnoa b/staker/_TEST_staker_one_external_test.gnoa index 4fb93015..b9c0a302 100644 --- a/staker/_TEST_staker_one_external_test.gnoa +++ b/staker/_TEST_staker_one_external_test.gnoa @@ -65,6 +65,7 @@ func TestPositionMint01(t *testing.T) { "1", // amount0Min "1", // amount1Min max_timeout, // deadline + test1.String(), ) std.TestSkipHeights(1) @@ -96,6 +97,7 @@ func TestPositionMint02(t *testing.T) { "1", // amount0Min "1", // amount1Min max_timeout, // deadline + test1.String(), ) std.TestSkipHeights(1) @@ -167,8 +169,8 @@ func TestUnstakeToken01(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(1)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 99500006126) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 499990000000315) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 99500018377) // internal + shouldEQ(t, obl.BalanceOf(a2u(test1)), 499990000000945) // external } func TestUnstakeToken02(t *testing.T) { @@ -179,8 +181,8 @@ func TestUnstakeToken02(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(2)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 99500046964) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 499990000002415) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 99500140890) // internal + shouldEQ(t, obl.BalanceOf(a2u(test1)), 499990000007247) // external } func TestEndExternalIncentive(t *testing.T) { diff --git a/staker/_TEST_staker_one_increase_external_test.gnoa b/staker/_TEST_staker_one_increase_external_test.gno similarity index 94% rename from staker/_TEST_staker_one_increase_external_test.gnoa rename to staker/_TEST_staker_one_increase_external_test.gno index 0b9059e7..686e6d16 100644 --- a/staker/_TEST_staker_one_increase_external_test.gnoa +++ b/staker/_TEST_staker_one_increase_external_test.gno @@ -65,6 +65,7 @@ func TestPositionMint01(t *testing.T) { "1", // amount0Min "1", // amount1Min max_timeout, // deadline + test1.String(), ) std.TestSkipHeights(1) @@ -96,6 +97,7 @@ func TestPositionMint02(t *testing.T) { "1", // amount0Min "1", // amount1Min max_timeout, // deadline + test1.String(), ) std.TestSkipHeights(1) @@ -172,8 +174,8 @@ func TestUnstakeToken01(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(1)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 99500006126) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 499980000000630) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 99500018377) // internal + shouldEQ(t, obl.BalanceOf(a2u(test1)), 499980000001890) // external } func TestUnstakeToken02(t *testing.T) { @@ -184,8 +186,8 @@ func TestUnstakeToken02(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(2)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 99500046964) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 499980000004831) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 99500140890) // internal + shouldEQ(t, obl.BalanceOf(a2u(test1)), 499980000014494) // external } func TestEndExternalIncentive(t *testing.T) { diff --git a/staker/_TEST_staker_rpc_get_test.gno b/staker/_TEST_staker_rpc_get_test.gnoa similarity index 94% rename from staker/_TEST_staker_rpc_get_test.gno rename to staker/_TEST_staker_rpc_get_test.gnoa index 433b32b3..cd1e4d36 100644 --- a/staker/_TEST_staker_rpc_get_test.gno +++ b/staker/_TEST_staker_rpc_get_test.gnoa @@ -64,6 +64,7 @@ func TestPositionMint01(t *testing.T) { "1", // amount0Min "1", // amount1Min max_timeout, + test1.String(), ) std.TestSkipHeights(1) @@ -95,6 +96,7 @@ func TestPositionMint02(t *testing.T) { "1", // amount0Min "1", // amount1Min max_timeout, + test1.String(), ) std.TestSkipHeights(1) @@ -175,7 +177,7 @@ func TestCollectReward(t *testing.T) { std.TestSkipHeights(1) gnsNew := gns.BalanceOf(a2u(test1)) - shouldEQ(t, gnsNew, 99500006126) + shouldEQ(t, gnsNew, 99500018377) } func TestUnstakeToken01(t *testing.T) { @@ -186,8 +188,8 @@ func TestUnstakeToken01(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(1)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 99500006126) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 499980000000630) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 99500024503) // internal + shouldEQ(t, obl.BalanceOf(a2u(test1)), 499980000002521) // external } func TestUnstakeToken02(t *testing.T) { @@ -198,9 +200,9 @@ func TestUnstakeToken02(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(2)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 99500046964) + shouldEQ(t, gns.BalanceOf(a2u(test1)), 99500187853) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 499980000004831) // external + shouldEQ(t, obl.BalanceOf(a2u(test1)), 499980000019327) // external } func TestEndExternalIncentive(t *testing.T) { diff --git a/staker/staker_parameters.gno b/staker/staker_parameters.gno index 5052977c..3ef065fc 100644 --- a/staker/staker_parameters.gno +++ b/staker/staker_parameters.gno @@ -9,20 +9,20 @@ func Init() { setInflationRate(defaultInflationRate()) } -func defaultInflationRate() bigint { +func defaultInflationRate() uint64 { return 0 // TODO } -func getInflationRate() bigint { +func getInflationRate() uint64 { param, ok := gov.GetParameter("inflation_rate") if !ok { panic(ufmt.Sprintf("[STAKER] staker_parameters.gno__getInflationRate() || inflation(%s) rate not set")) } - return param.(bigint) + return param.(uint64) } -func setInflationRate(newRate bigint) { +func setInflationRate(newRate uint64) { gov.SetGovParameter("inflation_rate", newRate) } diff --git a/staker/utils.gno b/staker/utils.gno index ac322788..44785248 100644 --- a/staker/utils.gno +++ b/staker/utils.gno @@ -12,7 +12,6 @@ import ( ) func poolKeyDivide(poolKey string) string { - res := strings.Split(poolKey, ":") if len(res) != 3 { panic(ufmt.Sprintf("[STAKER] utils.gno__poolKeyDivide() || invalid poolKey(%s)", poolKey)) From 42c086b53e819d3149c01a5c574c266e1ff8d956 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 14 Mar 2024 19:08:03 +0900 Subject: [PATCH 02/11] GSW-922 docs: godoc style metadata GSW-925 chore: naked return --- pool/_GET_no_receiver.gno | 7 - pool/_GET_no_receiver_string.gno | 8 +- pool/_GET_receiver.gno | 1 + pool/_RPC_api.gno | 10 +- pool/_RPC_dry.gno | 1 + ... => _TEST_pool_dryswap_and_swap_test.gnoa} | 0 ..._TEST_rpc_test.gnoa => _TEST_rpc_test.gno} | 0 pool/bit_math.gno | 4 +- pool/pool.gno | 35 ++++- pool/pool_manager.gno | 51 +++++++- pool/token_register.gno | 16 +++ pool/type.gno | 121 +++++++++--------- pool/withdrawal_fee.gno | 15 ++- position/_RPC_dry.gno | 1 + ..._test.gnoa => _TEST_position_api_test.gno} | 0 ...tion_test.gno => _TEST_position_test.gnoa} | 0 position/position.gno | 2 +- position/type.gno | 82 ++++++------ ... => _TEST_routes_find_swap_path_test.gnoa} | 0 router/comptue_routes.gno | 4 + router/router.gno | 12 ++ router/router_dry.gno | 12 ++ router/type.gno | 28 ++-- ...a => _TEST_staker_collect_reward_test.gno} | 0 ...ST_staker_one_increase_external_test.gnoa} | 0 staker/manage_pool_tiers.gno | 22 +++- staker/mint_stake.gno | 5 +- staker/staker.gno | 29 ++++- 28 files changed, 316 insertions(+), 150 deletions(-) rename pool/{_TEST_pool_dryswap_and_swap_test.gno => _TEST_pool_dryswap_and_swap_test.gnoa} (100%) rename pool/{_TEST_rpc_test.gnoa => _TEST_rpc_test.gno} (100%) rename position/{_TEST_position_api_test.gnoa => _TEST_position_api_test.gno} (100%) rename position/{_TEST_position_test.gno => _TEST_position_test.gnoa} (100%) rename router/{_TEST_routes_find_swap_path_test.gno => _TEST_routes_find_swap_path_test.gnoa} (100%) rename staker/{_TEST_staker_collect_reward_test.gnoa => _TEST_staker_collect_reward_test.gno} (100%) rename staker/{_TEST_staker_one_increase_external_test.gno => _TEST_staker_one_increase_external_test.gnoa} (100%) diff --git a/pool/_GET_no_receiver.gno b/pool/_GET_no_receiver.gno index 3e8fbbeb..1db6cd37 100644 --- a/pool/_GET_no_receiver.gno +++ b/pool/_GET_no_receiver.gno @@ -7,7 +7,6 @@ import ( u256 "gno.land/p/big/uint256" ) -// Slot0 func PoolGetSlot0SqrtPriceX96(poolPath string) *u256.Uint { pool, exist := pools[poolPath] if !exist { @@ -35,7 +34,6 @@ func PoolGetSlot0FeeProtocol(poolPath string) uint8 { return pool.slot0.feeProtocol } -// Balances func PoolGetToken0Balance(poolPath string) *u256.Uint { pool, exist := pools[poolPath] if !exist { @@ -54,7 +52,6 @@ func PoolGetToken1Balance(poolPath string) *u256.Uint { return pool.balances.token1 } -// ProtocolFees func PoolGetToken0ProtocolFee(poolPath string) *u256.Uint { pool, exist := pools[poolPath] if !exist { @@ -73,7 +70,6 @@ func PoolGetToken1ProtocolFee(poolPath string) *u256.Uint { return pool.protocolFees.token1 } -// PositionInfo func PoolGetPositionLiquidity(poolPath, key string) *u256.Uint { pool, exist := pools[poolPath] if !exist { @@ -144,7 +140,6 @@ func PoolGetPositionTokensOwed1(poolPath, key string) *u256.Uint { return position.tokensOwed1 } -// TickInfo func PoolGetTickLiquidityGross(poolPath string, x int32) *u256.Uint { pool, exist := pools[poolPath] if !exist { @@ -257,7 +252,6 @@ func PoolGetTickInitialized(poolPath string, x int32) bool { return tick.initialized } -// TickBitmaps func PoolGetTickBitmap(poolPath string, x int16) *u256.Uint { pool, exist := pools[poolPath] if !exist { @@ -272,7 +266,6 @@ func PoolGetTickBitmap(poolPath string, x int16) *u256.Uint { return tickBitmap } -// Pool func PoolGetToken0Path(poolPath string) string { pool, exist := pools[poolPath] if !exist { diff --git a/pool/_GET_no_receiver_string.gno b/pool/_GET_no_receiver_string.gno index 5cdd79ba..06286d79 100644 --- a/pool/_GET_no_receiver_string.gno +++ b/pool/_GET_no_receiver_string.gno @@ -4,7 +4,6 @@ import ( "gno.land/p/demo/ufmt" ) -// Balances func PoolGetToken0BalanceStr(poolPath string) string { pool, exist := pools[poolPath] if !exist { @@ -23,7 +22,6 @@ func PoolGetToken1BalanceStr(poolPath string) string { return pool.balances.token1.ToString() } -// ProtocolFees func PoolGetToken0ProtocolFeeStr(poolPath string) string { pool, exist := pools[poolPath] if !exist { @@ -42,11 +40,10 @@ func PoolGetToken1ProtocolFeeStr(poolPath string) string { return pool.protocolFees.token1.ToString() } -// PositionInfo func PoolGetPositionLiquidityStr(poolPath, key string) string { pool, exist := pools[poolPath] if !exist { - panic(ufmt.Sprintf("[POOL]_GET_no_receiver_string.gno__PoolGetPositionLiquidityStr() || pool(%s) does not exist", poolPath)) + panic(ufmt.Sprintf("[POOL]_GET_no_receiver_string.gno__PoolGetToken0ProtocolFeeStr() || pool(%s) does not exist", poolPath)) } position, exist := pool.positions[key] @@ -113,7 +110,6 @@ func PoolGetPositionTokensOwed1Str(poolPath, key string) string { return position.tokensOwed1.ToString() } -// TickInfo func PoolGetTickLiquidityGrossStr(poolPath string, x int32) string { pool, exist := pools[poolPath] if !exist { @@ -184,7 +180,6 @@ func PoolGetTickSecondsPerLiquidityOutsideX128Str(poolPath string, x int32) stri return tick.secondsPerLiquidityOutsideX128.ToString() } -// TickBitmaps func PoolGetTickBitmapStr(poolPath string, x int16) string { pool, exist := pools[poolPath] if !exist { @@ -199,7 +194,6 @@ func PoolGetTickBitmapStr(poolPath string, x int16) string { return tickBitmap.ToString() } -// Pool func PoolGetFeeGrowthGlobal0X128Str(poolPath string) string { pool, exist := pools[poolPath] if !exist { diff --git a/pool/_GET_receiver.gno b/pool/_GET_receiver.gno index 61eb7e05..ea9b760d 100644 --- a/pool/_GET_receiver.gno +++ b/pool/_GET_receiver.gno @@ -7,6 +7,7 @@ import ( u256 "gno.land/p/big/uint256" ) +// PoolGetPoolList returns a list of all pool paths func PoolGetPoolList() []string { poolPaths := []string{} for poolPath, _ := range pools { diff --git a/pool/_RPC_api.gno b/pool/_RPC_api.gno index 54dd1ce1..3cfa770f 100644 --- a/pool/_RPC_api.gno +++ b/pool/_RPC_api.gno @@ -20,16 +20,12 @@ type RpcPool struct { BalancesToken0 string `json:"balanceToken0"` BalancesToken1 string `json:"balanceToken1"` - // fee is the fee tier of the pool Fee uint32 `json:"fee"` - // tickSpacing is the spacing between ticks TickSpacing int32 `json:"tickSpacing"` - // maxLiquidityPerTick is the maximum amount of liquidity that can be added per tick MaxLiquidityPerTick string `json:"maxLiquidityPerTick"` - // slot0 is the current tick and price of the pool Slot0SqrtPriceX96 string `json:"sqrtPriceX96"` Slot0Tick int32 `json:"tick"` Slot0FeeProtocol uint8 `json:"feeProtocol"` @@ -41,20 +37,18 @@ type RpcPool struct { ProtocolFeesToken0 string `json:"protocolFeeToken0"` ProtocolFeesToken1 string `json:"protocolFeeToken1"` - // liquidity is the total amount of liquidity in the pool Liquidity string `json:"liquidity"` - // ticks is a mapping from tick index to tick Ticks RpcTicks `json:"ticks"` - // tickBitmaps is a mapping from tick index to tick bitmap TickBitmaps RpcTickBitmaps `json:"tickBitmaps"` Positions []RpcPosition `json:"positions"` } type RpcTickBitmaps map[int16]string // tick(wordPos) => bitmap(tickWord ^ mask) -type RpcTicks map[int32]RpcTickInfo // tick => RpcTickInfo + +type RpcTicks map[int32]RpcTickInfo // tick => RpcTickInfo type RpcTickInfo struct { LiquidityGross string `json:"liquidityGross"` diff --git a/pool/_RPC_dry.gno b/pool/_RPC_dry.gno index 5b908182..eab2652b 100644 --- a/pool/_RPC_dry.gno +++ b/pool/_RPC_dry.gno @@ -11,6 +11,7 @@ import ( u256 "gno.land/p/big/uint256" ) +// DrySwap simulates a swap and returns the amount0, amount1 that would be received and a boolean indicating if the swap is possible func DrySwap( token0Path string, token1Path string, diff --git a/pool/_TEST_pool_dryswap_and_swap_test.gno b/pool/_TEST_pool_dryswap_and_swap_test.gnoa similarity index 100% rename from pool/_TEST_pool_dryswap_and_swap_test.gno rename to pool/_TEST_pool_dryswap_and_swap_test.gnoa diff --git a/pool/_TEST_rpc_test.gnoa b/pool/_TEST_rpc_test.gno similarity index 100% rename from pool/_TEST_rpc_test.gnoa rename to pool/_TEST_rpc_test.gno diff --git a/pool/bit_math.gno b/pool/bit_math.gno index 95e8f112..50e7e683 100644 --- a/pool/bit_math.gno +++ b/pool/bit_math.gno @@ -7,7 +7,7 @@ import ( ) type bitShift struct { - bitPattern *u256.Uint // mask or limit + bitPattern *u256.Uint shift uint } @@ -51,8 +51,6 @@ func bitMathLeastSignificantBit(x *u256.Uint) uint8 { if new(u256.Uint).And(x, s.bitPattern).Gt(u256.Zero()) { r -= uint8(s.shift) } else { - // x >>= s.shift - // x = x >> s.shift x = new(u256.Uint).Rsh(x, s.shift) } } diff --git a/pool/pool.gno b/pool/pool.gno index 1b81fd97..b74e72ea 100644 --- a/pool/pool.gno +++ b/pool/pool.gno @@ -14,6 +14,11 @@ import ( u256 "gno.land/p/big/uint256" ) +// Mint creates a new position and mints liquidity tokens then return amount0, amount1 in string +// +// Panics if any of the following conditions are met: +// - The caller is not the position contract +// - Caller does not have enough token0 or token1 func Mint( token0Path string, token1Path string, @@ -80,6 +85,11 @@ func Mint( return amount0.ToString(), amount1.ToString() } +// Burn removes liquidity from the caller and account tokens owed for the liquidity to the position +// If liquidity of 0 is burned, it recalculates fees owed to a position +// +// Panics if any of the following conditions are met: +// - The caller is not the position contract func Burn( token0Path string, token1Path string, @@ -119,6 +129,11 @@ func Burn( return amount0.ToString(), amount1.ToString() } +// Collect collects tokens owed to a position +// +// Panics if any of the following conditions are met: +// - The caller is not the position contract +// - The position does not exist func Collect( token0Path string, token1Path string, @@ -167,6 +182,13 @@ func Collect( return amount0.ToString(), amount1.ToString() } +// Swap swaps token0 for token1, or token1 for token0 +// +// Panics if any of the following conditions are met: +// - The caller is not the router contract +// - Target pool is being used by another transaction +// - The amounSpecified is 0 +// - The SqrtPriceLimit is not within the range func Swap( token0Path string, token1Path string, @@ -482,7 +504,11 @@ func Swap( return amount0.ToString(), amount1.ToString() } -// ADMIN +// SetFeeProtocls sets the denominator of the protocol fee +// +// Panics if any of the following conditions are met: +// - The caller is not an admin +// - The feeProtocol0 or feeProtocol1 is not in the range of 0 or 4-10 func SetFeeProtocol( feeProtocol0 uint8, feeProtocol1 uint8, @@ -507,7 +533,12 @@ func SetFeeProtocol( gv.SetGovParameter("protocoL_fees", feeProtocol0+(feeProtocol1<<4)) } -// ADMIN +// CollectProtocol collects protocol fees from the pool +// +// Panics if any of the following conditions are met: +// - The caller is not an admin +// - The recipient is the zero address +// - The amount0Requested or amount1Requested is greater than the protocol fees func CollectProtocol( token0Path string, token1Path string, diff --git a/pool/pool_manager.gno b/pool/pool_manager.gno index cade64a8..1b5261d2 100644 --- a/pool/pool_manager.gno +++ b/pool/pool_manager.gno @@ -15,13 +15,19 @@ import ( ) var ( - admins []std.Address - initialized bool = false + admins []std.Address // list of admin addresses + initialized bool = false // whether the pool contract has been initialized - feeAmountTickSpacing map[uint32]int32 = make(map[uint32]int32) // map[feeAmount]tick_spacing - pools map[string]*Pool = make(map[string]*Pool) // map[poolPath]*Pool + feeAmountTickSpacing map[uint32]int32 = make(map[uint32]int32) // maps fee to tickSpacing || map[feeAmount]tick_spacing + pools map[string]*Pool = make(map[string]*Pool) // maps poolPath to pool || map[poolPath]*Pool ) +// InitManual initializes a pool contract itself +// +// Panics if any of the following conditions are mett: +// - The caller is not the user +// - The caller is not an admin +// - Pool contract has already been initialized func InitManual() { // only user can call std.AssertOriginCall() @@ -43,11 +49,20 @@ func InitManual() { initialized = true } +// CreatePool creates a new concentrated liquidity pool with the given token0Path, token1Path, fee, and sqrtPriceX96 +// Each pool is identified by the poolPath, which is a combination of token0Path, token1Path, and fee +// It validates the contract initialization, pool existence, and token0Path and token1Path +// If validation passes, it calls newPool() +// +// Panics if any of the following conditions are met: +// - The pool contract has not been initialized +// - The pool already exists +// - Both token0Path and token1Path are the same func CreatePool( token0Path string, token1Path string, fee uint32, - _sqrtPriceX96 string, // uint + _sqrtPriceX96 string, // uint256 ) *Pool { if !(initialized) { panic(ufmt.Sprintf("[POOl] pool_manager.gno__gno__CreatePool() || expected initialized(%t) == true", initialized)) @@ -77,26 +92,32 @@ func CreatePool( } if !exist { - gns.TransferFrom(a2u(std.GetOrigCaller()), a2u(consts.GNOSWAP_ADMIN), consts.POOL_CREATION_FEE) - pool = newPool(token0Path, token1Path, fee, tickSpacing, sqrtPriceX96) pools[poolPath] = pool + + gns.TransferFrom(a2u(std.GetOrigCaller()), a2u(consts.GNOSWAP_ADMIN), consts.POOL_CREATION_FEE) } return pool } +// DoesPoolExist reports whether the pool exists with the given token0Path, token1Path, and fee func DoesPoolExist(token0Path, token1Path string, fee uint32) bool { poolPath := GetPoolPath(token0Path, token1Path, fee) _, exist := pools[poolPath] return exist } +// DoesPoolPathExist reports whether the pool exists with the given poolPath func DoesPoolPathExist(poolPath string) bool { _, exist := pools[poolPath] return exist } +// GetPool returns the pool with the given token0Path, token1Path, and fee +// +// Panics if any of the following conditions are met: +// - The pool does not exist func GetPool(token0Path, token1Path string, fee uint32) *Pool { poolPath := GetPoolPath(token0Path, token1Path, fee) pool, exist := pools[poolPath] @@ -107,6 +128,10 @@ func GetPool(token0Path, token1Path string, fee uint32) *Pool { return pool } +// GetPoolFromPoolPath returns the pool with the given poolPath +// +// Panics if any of the following conditions are met: +// - The pool does not exist func GetPoolFromPoolPath(poolPath string) *Pool { pool, exist := pools[poolPath] if !exist { @@ -116,6 +141,9 @@ func GetPoolFromPoolPath(poolPath string) *Pool { return pool } +// GetPoolPath generates a poolPath from the given token0Path, token1Path, and fee +// If token0Path comes first in alphabetical order, it returns token0Path:token1Path:fee +// Otherwise, it returns token1Path:token0Path:fee func GetPoolPath(token0Path, token1Path string, fee uint32) string { if token0Path < token1Path { return token0Path + ":" + token1Path + ":" + strconv.Itoa(int(fee)) @@ -124,6 +152,10 @@ func GetPoolPath(token0Path, token1Path string, fee uint32) string { } } +// AddAdmin adds an given address to the list of admins +// +// Panics if any of the following conditions are met: +// - The caller is not an admin func AddAdmin(addr std.Address) { caller := std.PrevRealm().Addr() if isAdmin(caller) { @@ -133,6 +165,11 @@ func AddAdmin(addr std.Address) { } } +// RemoveAdmin removes given address from the list of admins +// +// Panics if any of the following conditions are met: +// - The caller is not an admin +// - The address is the last admin func RemoveAdmin(addr std.Address) { caller := std.PrevRealm().Addr() diff --git a/pool/token_register.gno b/pool/token_register.gno index 7d06c22d..1b116823 100644 --- a/pool/token_register.gno +++ b/pool/token_register.gno @@ -9,6 +9,8 @@ import ( "gno.land/r/demo/consts" ) +// GRC20Interface is the interface for GRC20 tokens +// It is used to interact with the GRC20 tokens without importing but by registering each tokens function type GRC20Interface interface { Transfer() func(to users.AddressOrName, amount uint64) TransferFrom() func(from, to users.AddressOrName, amount uint64) @@ -21,6 +23,7 @@ var ( locked = false // mutex ) +// GetRegisteredTokens returns a list of all registered tokens func GetRegisteredTokens() []string { tokens := make([]string, 0, len(registered)) for k := range registered { @@ -29,6 +32,10 @@ func GetRegisteredTokens() []string { return tokens } +// GetRegisterToken returns the GRC20Interface for the given token +// +// Panics if any of the following conditions are met: +// - token not found func GetRegisterToken(pkgPath string) GRC20Interface { pkgPath = handleNative(pkgPath) @@ -40,6 +47,11 @@ func GetRegisterToken(pkgPath string) GRC20Interface { return registered[pkgPath] } +// RegisterGRC20Interface registers a GRC20 token interface +// +// Panics if any of the following conditions are met: +// - caller is not the admin +// - token already registered func RegisterGRC20Interface(pkgPath string, igrc20 GRC20Interface) { caller := std.GetOrigCaller() if caller != consts.GNOSWAP_ADMIN { @@ -56,6 +68,10 @@ func RegisterGRC20Interface(pkgPath string, igrc20 GRC20Interface) { registered[pkgPath] = igrc20 } +// UnregisterGRC20Interface unregisters a GRC20 token interface +// +// Panics if any of the following conditions are met: +// - caller is not the admin func UnregisterGRC20Interface(pkgPath string) { // only admin can unregister caller := std.GetOrigCaller() diff --git a/pool/type.gno b/pool/type.gno index 5f665775..272dea77 100644 --- a/pool/type.gno +++ b/pool/type.gno @@ -8,119 +8,124 @@ import ( ) type Slot0 struct { - sqrtPriceX96 *u256.Uint // uint160 - tick int32 - feeProtocol uint8 - unlocked bool + sqrtPriceX96 *u256.Uint // current price of the pool as a sqrt(token1/token0) Q96 value + tick int32 // current tick of the pool, i.e according to the last tick transition that was run + feeProtocol uint8 // protocol fee for both tokens of the pool + unlocked bool // whether the pool is currently locked to reentrancy } type Balances struct { + // current balance of the pool in token0/token1 token0 *u256.Uint token1 *u256.Uint } type ProtocolFees struct { - token0 *u256.Uint // uint128 - token1 *u256.Uint // uint128 + // current protocol fees of the pool in token0/token1 + token0 *u256.Uint + token1 *u256.Uint } type ModifyPositionParams struct { - owner std.Address - tickLower int32 - tickUpper int32 - liquidityDelta *i256.Int // int128 + owner std.Address // address that owns the position + + // the tick range of the position, bounds are included + tickLower int32 + tickUpper int32 + + liquidityDelta *i256.Int // any change in liquidity } type SwapCache struct { - feeProtocol uint8 - liquidityStart *u256.Uint // uint128 + feeProtocol uint8 // protocol fee for the input token + liquidityStart *u256.Uint // liquidity at the beginning of the swap } type SwapState struct { - amountSpecifiedRemaining *i256.Int // int256 - amountCalculated *i256.Int // int256 - sqrtPriceX96 *u256.Uint // uint160 - tick int32 - feeGrowthGlobalX128 *u256.Uint // uint256 - protocolFee *u256.Uint // uint128 - liquidity *u256.Uint // uint128 + amountSpecifiedRemaining *i256.Int // amount remaining to be swapped in/out of the input/output token + amountCalculated *i256.Int // amount already swapped out/in of the output/input token + sqrtPriceX96 *u256.Uint // current sqrt(price) + tick int32 // tick associated with the current sqrt(price) + feeGrowthGlobalX128 *u256.Uint // global fee growth of the input token + protocolFee *u256.Uint // amount of input token paid as protocol fee + liquidity *u256.Uint // current liquidity in range } type StepComputations struct { - sqrtPriceStartX96 *u256.Uint // uint160 - tickNext int32 - initialized bool - sqrtPriceNextX96 *u256.Uint // uint160 - amountIn *u256.Uint // uint256 - amountOut *u256.Uint // uint256 - feeAmount *u256.Uint // uint256 + sqrtPriceStartX96 *u256.Uint // price at the beginning of the step + tickNext int32 // next tick to swap to from the current tick in the swap direction + initialized bool // whether tickNext is initialized + sqrtPriceNextX96 *u256.Uint // sqrt(price) for the next tick (token1/token0) Q96 + amountIn *u256.Uint // how much being swapped in this step + amountOut *u256.Uint // how much is being swapped out in this step + feeAmount *u256.Uint // how much fee is being paid in this step } type PositionInfo struct { - liquidity *u256.Uint // uint128 + liquidity *u256.Uint // amount of liquidity owned by this position - feeGrowthInside0LastX128 *u256.Uint // uint256 - feeGrowthInside1LastX128 *u256.Uint // uint256 + // fee growth per unit of liquidity as of the last update to liquidity or fees owed + feeGrowthInside0LastX128 *u256.Uint + feeGrowthInside1LastX128 *u256.Uint - tokensOwed0 *u256.Uint // uint128 - tokensOwed1 *u256.Uint // uint128 + // fees owed to the position owner in token0/token1 + tokensOwed0 *u256.Uint + tokensOwed1 *u256.Uint } type TickInfo struct { - liquidityGross *u256.Uint // uint128 - liquidityNet *i256.Int // int128 + liquidityGross *u256.Uint // total position liquidity that references this tick + liquidityNet *i256.Int // amount of net liquidity added (subtracted) when tick is crossed from left to right (right to left) + + // fee growth per unit of liquidity on the _other_ side of this tick (relative to the current tick) + // only has relative meaning, not absolute — the value depends on when the tick is initialized + feeGrowthOutside0X128 *u256.Uint + feeGrowthOutside1X128 *u256.Uint - feeGrowthOutside0X128 *u256.Uint // uint256 - feeGrowthOutside1X128 *u256.Uint // uint256 + tickCumulativeOutside int64 // cumulative tick value on the other side of the tick - tickCumulativeOutside int64 // int56 + // the seconds per unit of liquidity on the _other_ side of this tick (relative to the current tick) + // only has relative meaning, not absolute — the value depends on when the tick is initialized + secondsPerLiquidityOutsideX128 *u256.Uint - secondsPerLiquidityOutsideX128 *u256.Uint // uint160 - secondsOutside uint32 // uint32 + // the seconds spent on the other side of the tick (relative to the current tick) + // only has relative meaning, not absolute — the value depends on when the tick is initialized + secondsOutside uint32 - initialized bool + initialized bool // whether the tick is initialized } type Ticks map[int32]TickInfo // tick => TickInfo type TickBitmaps map[int16]*u256.Uint // tick(wordPos) => bitmap(tickWord ^ mask) type Positions map[string]PositionInfo // positionKey => PositionInfo -// type Pool describes a single Pool/s state +// type Pool describes a single Pool's state // A pool is identificed with a unique key (token0, token1, fee), where token0 < token1 type Pool struct { + // token0/token1 path of the pool token0Path string token1Path string - balances Balances + balances Balances // balances of the pool - // fee is the fee tier of the pool - fee uint32 + fee uint32 // fee tier of the pool - // tickSpacing is the spacing between ticks - tickSpacing int32 + tickSpacing int32 // spacing between ticks - // maxLiquidityPerTick is the maximum amount of liquidity that can be added per tick - maxLiquidityPerTick *u256.Uint // uint128 + maxLiquidityPerTick *u256.Uint // the maximum amount of liquidity that can be added per tick - // slot0 is the current tick and price of the pool slot0 Slot0 feeGrowthGlobal0X128 *u256.Uint // uint256 feeGrowthGlobal1X128 *u256.Uint // uint256 - // protocolFees is the amount of fees collected by the protocol - // collected by CollectFeeProtocol() protocolFees ProtocolFees - // liquidity is the total amount of liquidity in the pool - liquidity *u256.Uint // uint128 + liquidity *u256.Uint // total amount of liquidity in the pool - // ticks is a mapping from tick index to tick - ticks Ticks + ticks Ticks // maps tick index to tick - // tickBitmaps is a mapping from tick index to tick bitmap - tickBitmaps TickBitmaps + tickBitmaps TickBitmaps // maps tick index to tick bitmap - // positions maps the key (caller, lower tick, upper tick) to a unique position - positions Positions + positions Positions // maps the key (caller, lower tick, upper tick) to a unique position } diff --git a/pool/withdrawal_fee.gno b/pool/withdrawal_fee.gno index 912e0fd2..cf7ea884 100644 --- a/pool/withdrawal_fee.gno +++ b/pool/withdrawal_fee.gno @@ -10,10 +10,16 @@ import ( u256 "gno.land/p/big/uint256" ) +// withdrawalFee is the fee that is charged when a user withdraws their collected fees var ( withdrawalFee = uint64(1) ) +// HandleWithdrawalFee withdraws the fee from the user and returns the amount after the fee +// +// Panics if any of the following conditions are met: +// - caller is not the position contract +// - caller does not have enough balance to give the fee func HandleWithdrawalFee( tokenId uint64, token0Path string, @@ -55,6 +61,11 @@ func HandleWithdrawalFee( return afterAmount0.ToString(), afterAmount1.ToString() } +// SetWithdrawalFee sets the withdrawal fee +// +// Panics if any of the following conditions are met: +// - caller is not the admin + func SetWithdrawalFee(fee uint64) { // MUST BE ORIGIN CALL std.AssertOriginCall() @@ -64,8 +75,8 @@ func SetWithdrawalFee(fee uint64) { panic(ufmt.Sprintf("[POOL] withdrawal_fee.gno__SetFeeProtocol() || caller(%s) must be admin", std.GetOrigCaller())) } - if fee < 0 { - panic(ufmt.Sprintf("[POOL] withdrawal_fee.gno__SetFeeProtocol() || fee(%d) must be >= 0", fee)) + if fee > 100 { + panic(ufmt.Sprintf("[POOL] withdrawal_fee.gno__SetFeeProtocol() || fee(%d) must be in range 0 ~ 100", fee)) } withdrawalFee = fee diff --git a/position/_RPC_dry.gno b/position/_RPC_dry.gno index 4d7107b0..36c7b1af 100644 --- a/position/_RPC_dry.gno +++ b/position/_RPC_dry.gno @@ -7,6 +7,7 @@ import ( u256 "gno.land/p/big/uint256" ) +// DryMint simulates a mint and returns the amount0, amount1 that would be minted func DryMint( tickCurrent int32, tickLower int32, diff --git a/position/_TEST_position_api_test.gnoa b/position/_TEST_position_api_test.gno similarity index 100% rename from position/_TEST_position_api_test.gnoa rename to position/_TEST_position_api_test.gno diff --git a/position/_TEST_position_test.gno b/position/_TEST_position_test.gnoa similarity index 100% rename from position/_TEST_position_test.gno rename to position/_TEST_position_test.gnoa diff --git a/position/position.gno b/position/position.gno index a4a61cff..5bc65198 100644 --- a/position/position.gno +++ b/position/position.gno @@ -15,7 +15,7 @@ import ( var ( positions map[uint64]Position = make(map[uint64]Position) // tokenId -> Position - nextId uint64 = 1 + nextId uint64 = 1 // lp token id ) func Mint( diff --git a/position/type.gno b/position/type.gno index 72c93499..d41ab890 100644 --- a/position/type.gno +++ b/position/type.gno @@ -7,68 +7,72 @@ import ( ) type Position struct { - nonce *u256.Uint // uint96 + nonce *u256.Uint // nonce for permits - operator std.Address + operator std.Address // address that is approved for spending this token - poolKey string + poolKey string // poolPath of the pool which this has lp token + // the tick range of the position, bounds are included tickLower int32 tickUpper int32 - liquidity *u256.Uint // uint128 + liquidity *u256.Uint // liquidity of the position - feeGrowthInside0LastX128 *u256.Uint // uint256 - feeGrowthInside1LastX128 *u256.Uint // uint256 + // fee growth of the aggregate position as of the last action on the individual position + feeGrowthInside0LastX128 *u256.Uint + feeGrowthInside1LastX128 *u256.Uint - tokensOwed0 *u256.Uint // uint128 - tokensOwed1 *u256.Uint // uint128 + // how many uncollected tokens are owed to the position, as of the last computation + tokensOwed0 *u256.Uint + tokensOwed1 *u256.Uint - burned bool + burned bool // whether the position has been burned(≈ actuall we don't burn lp token even its empty, just update flag) } +// isClear reports whether the position is empty func (p Position) isClear() bool { return p.liquidity.IsZero() && p.tokensOwed0.IsZero() && p.tokensOwed1.IsZero() } type MintParams struct { - token0 string - token1 string - fee uint32 - tickLower int32 - tickUpper int32 - amount0Desired *u256.Uint // uint256 - amount1Desired *u256.Uint // uint256 - amount0Min *u256.Uint // uint256 - amount1Min *u256.Uint // uint256 - deadline int64 - mintTo std.Address + token0 string // token0 path for a specific pool + token1 string // token1 path for a specific pool + fee uint32 // fee for a specific pool + tickLower int32 // lower end of the tick range for the position + tickUpper int32 // upper end of the tick range for the position + amount0Desired *u256.Uint // desired amount of token0 to be minted + amount1Desired *u256.Uint // desired amount of token1 to be minted + amount0Min *u256.Uint // minimum amount of token0 to be minted + amount1Min *u256.Uint // minimum amount of token1 to be minted + deadline int64 // time by which the transaction must be included to effect the change + mintTo std.Address // address to mint lpToken } type AddLiquidityParams struct { - poolKey string - tickLower int32 - tickUpper int32 - amount0Desired *u256.Uint // uint256 - amount1Desired *u256.Uint // uint256 - amount0Min *u256.Uint // uint256 - amount1Min *u256.Uint // uint256 + poolKey string // poolPath of the pool which has the position + tickLower int32 // lower end of the tick range for the position + tickUpper int32 // upper end of the tick range for the position + amount0Desired *u256.Uint // desired amount of token0 to be minted + amount1Desired *u256.Uint // desired amount of token1 to be minted + amount0Min *u256.Uint // minimum amount of token0 to be minted + amount1Min *u256.Uint // minimum amount of token1 to be minted } type IncreaseLiquidityParams struct { - tokenId uint64 - amount0Desired *u256.Uint // uint256 - amount1Desired *u256.Uint // uint256 - amount0Min *u256.Uint // uint256 - amount1Min *u256.Uint // uint256 - deadline int64 + tokenId uint64 // tokenId of the position to increase liquidity + amount0Desired *u256.Uint // desired amount of token0 to be minted + amount1Desired *u256.Uint // desired amount of token1 to be minted + amount0Min *u256.Uint // minimum amount of token0 to be minted + amount1Min *u256.Uint // minimum amount of token1 to be minted + deadline int64 // time by which the transaction must be included to effect the change } type DecreaseLiquidityParams struct { - tokenId uint64 - liquidityRatio uint64 - amount0Min *u256.Uint // uint256 - amount1Min *u256.Uint // uint256 - deadline int64 - unwrapResult bool + tokenId uint64 // tokenId of the position to decrease liquidity + liquidityRatio uint64 // percent of liquidity to decrease, i.e '25' ≈ remove 25% of liquidity + amount0Min *u256.Uint // minimum amount of token0 to be minted + amount1Min *u256.Uint // minimum amount of token1 to be minted + deadline int64 // time by which the transaction must be included to effect the change + unwrapResult bool // whether to unwrap the token if it's wrapped native token } diff --git a/router/_TEST_routes_find_swap_path_test.gno b/router/_TEST_routes_find_swap_path_test.gnoa similarity index 100% rename from router/_TEST_routes_find_swap_path_test.gno rename to router/_TEST_routes_find_swap_path_test.gnoa diff --git a/router/comptue_routes.gno b/router/comptue_routes.gno index 41eca047..94788bec 100644 --- a/router/comptue_routes.gno +++ b/router/comptue_routes.gno @@ -8,6 +8,8 @@ import ( u256 "gno.land/p/big/uint256" ) +// PoolWithMeta is a struct that contains poolPath, token0Path, token1Path, fee, tokenPair, and liquidity +// It's used to store the pool information and sort the pools by liquidity type PoolWithMeta struct { poolPath string token0Path string @@ -22,6 +24,8 @@ func (p ByLiquidity) Len() int { return len(p) } func (p ByLiquidity) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p ByLiquidity) Less(i, j int) bool { return p[i].liquidity.Gt(p[j].liquidity) } +// BuildRoute is a struct that contains route, tokenIn, and tokenOut +// It's used to store the route information type BuildRoute struct { route []PoolWithMeta tokenIn string diff --git a/router/router.gno b/router/router.gno index ca7a6a8f..350672c2 100644 --- a/router/router.gno +++ b/router/router.gno @@ -15,6 +15,18 @@ import ( "gno.land/r/demo/wugnot" ) +// SwapRoute swaps the input token to the output token and returns the result amount +// If swapType is EXACT_IN, it returns the amount of output token ≈ amount of user to receive +// If swapType is EXACT_OUT, it returns the amount of input token ≈ amount of user to pay +// +// Panics if any of the following conditions are met: +// - amountSpecified is zero or is not numeric +// - swapType is not EXACT_IN or EXACT_OUT +// - length of route and quotes are not the same +// - length of routes is not 1 ~ 7 +// - sum of quotes is not 100 +// - number of hops is not 1 ~ 3 +// - too many token spend or too few token received func SwapRoute( inputToken string, outputToken string, diff --git a/router/router_dry.gno b/router/router_dry.gno index 580582fe..b235053a 100644 --- a/router/router_dry.gno +++ b/router/router_dry.gno @@ -10,6 +10,18 @@ import ( u256 "gno.land/p/big/uint256" ) +// DrySwapRoute simulates the swap route and returns the result amount +// If swapType is EXACT_IN, it returns the amount of output token ≈ amount of user to receive +// If swapType is EXACT_OUT, it returns the amount of input token ≈ amount of user to pay +// If swap it not possible, it returns -1 +// +// Panics if any of the following conditions are met: +// - amountSpecified is zero or is not numeric +// - swapType is not EXACT_IN or EXACT_OUT +// - length of route and quotes are not the same +// - length of routes is not 1 ~ 7 +// - sum of quotes is not 100 +// - number of hops is not 1 ~ 3 func DrySwapRoute( inputToken string, outputToken string, diff --git a/router/type.gno b/router/type.gno index d84dff0a..44b68776 100644 --- a/router/type.gno +++ b/router/type.gno @@ -16,26 +16,32 @@ const ( // SINGLE SWAP type SingleSwapParams struct { - tokenIn string - tokenOut string - fee uint32 + tokenIn string // token to spend + tokenOut string // token to receive + fee uint32 // fee of the pool used to swap + + // if positive, it's the amount of tokenIn to spend + // if negative, it's the wanted amount of tokenOut to receive amountSpecified *i256.Int } // MUTLI SWAP type SwapParams struct { - tokenIn string - tokenOut string - fee uint32 - recipient std.Address + tokenIn string // token to spend + tokenOut string // token to receive + fee uint32 // fee of the pool used to swap + recipient std.Address // address to receive the token + + // if positive, it's the amount of tokenIn to spend + // if negative, it's the wanted amount of tokenOut to receive amountSpecified *i256.Int } // SWAP DATA type SwapCallbackData struct { - tokenIn string - tokenOut string - fee uint32 + tokenIn string // token to spend + tokenOut string // token to receive + fee uint32 // fee of the pool used to swap - payer std.Address + payer std.Address // address to spend the token } diff --git a/staker/_TEST_staker_collect_reward_test.gnoa b/staker/_TEST_staker_collect_reward_test.gno similarity index 100% rename from staker/_TEST_staker_collect_reward_test.gnoa rename to staker/_TEST_staker_collect_reward_test.gno diff --git a/staker/_TEST_staker_one_increase_external_test.gno b/staker/_TEST_staker_one_increase_external_test.gnoa similarity index 100% rename from staker/_TEST_staker_one_increase_external_test.gno rename to staker/_TEST_staker_one_increase_external_test.gnoa diff --git a/staker/manage_pool_tiers.gno b/staker/manage_pool_tiers.gno index b1f50e81..28dbd51a 100644 --- a/staker/manage_pool_tiers.gno +++ b/staker/manage_pool_tiers.gno @@ -10,10 +10,18 @@ import ( "gno.land/r/demo/consts" ) +// GetPoolWithTiers returns list of pools with their tiers func GetPoolWithTiers() map[string]uint64 { return poolTiers } +// SetPoolTier sets the tier of the pool +// +// Panics if any of the following conditions are met: +// - caller is not admin +// - pool does not exist +// - pool exists in poolTiers +// - tier is not valid func SetPoolTier(pool string, tier uint64) { // check if caller is admin isAdmin() @@ -34,6 +42,13 @@ func SetPoolTier(pool string, tier uint64) { poolTiers[pool] = tier } +// ChangePoolTier changes the tier of the pool +// +// Panics if any of the following conditions are met: +// - caller is not admin +// - pool does not exist in poolTiers +// - tier is not valid +// - pool is default pool func ChangePoolTier(pool string, tier uint64) { // check if caller is admin isAdmin() @@ -46,7 +61,7 @@ func ChangePoolTier(pool string, tier uint64) { // check if tier is valid validTier(tier) - // CAN'T CHANGE TIER OF THIS GNS:GNOT 0.03% + // CAN'T CHANGE TIER OF THIS GNS:GNOT 0.3% if pool == MUST_EXISTS_IN_TIER_1 { panic(ufmt.Sprintf("[STAKER] manage_pool_tiers.gno__ChangePoolTier() || Cannot change tier of this pool(%s)")) } @@ -54,6 +69,11 @@ func ChangePoolTier(pool string, tier uint64) { poolTiers[pool] = tier } +// RemovePoolTier removes the pool from poolTiers +// +// Panics if any of the following conditions are met: +// - caller is not admin +// - pool is default pool func RemovePoolTier(pool string) { // check if caller is admin isAdmin() diff --git a/staker/mint_stake.gno b/staker/mint_stake.gno index 0fa7a400..4f37e914 100644 --- a/staker/mint_stake.gno +++ b/staker/mint_stake.gno @@ -6,8 +6,9 @@ import ( "gno.land/r/demo/consts" ) +// MintAndStake mints and stakes the LP token +// If minting is successful, it mints LP token to staker not user func MintAndStake( - // position.Mint() params token0 string, token1 string, fee uint32, @@ -18,8 +19,6 @@ func MintAndStake( amount0Min string, // *u256.Uint amount1Min string, // *u256.Uint deadline int64, - // staker.StakeToken() params - // lpTokenId uint64, // can be used from position.Mint() response ) (uint64, string, string, string, string) { // tokenId, liquidity, amount0, amount1, poolPath ( *u256.Uint x3) tokenId, liquidity, amount0, amount1 := pn.Mint( token0, diff --git a/staker/staker.gno b/staker/staker.gno index ff926257..f1faa9a9 100644 --- a/staker/staker.gno +++ b/staker/staker.gno @@ -37,9 +37,15 @@ const ( func init() { // init pool tiers // tier 1 - poolTiers[MUST_EXISTS_IN_TIER_1] = 1 // ONLY GNOT:GNS 0.03% + poolTiers[MUST_EXISTS_IN_TIER_1] = 1 // ONLY GNOT:GNS 0.3% } +// CreateExternalIncentive creates an external incentive for the targetPoolPath +// +// Panics if any of the following conditions are met: +// - startTimestamp is not in the future +// - externalDuration is not 90, 180, or 360 days +// - user doesn't have enough balance to provide as reward func CreateExternalIncentive( targetPoolPath string, rewardToken string, // token path should be registered @@ -96,6 +102,12 @@ func CreateExternalIncentive( poolIncentives[targetPoolPath] = append(poolIncentives[targetPoolPath], incentiveId) } +// StakeToken stakes the lp token to the staker contract +// +// Panics if any of the following conditions are met: +// - tokenId already staked +// - tokenId has no liquidity +// - tokenId is not owned by the caller or staker func StakeToken(tokenId uint64) (string, string, string) { // poolPath, token0Amount, token1Amount // check whether tokenId already staked or not _, exist := deposits[tokenId] @@ -138,6 +150,11 @@ func StakeToken(tokenId uint64) (string, string, string) { // poolPath, token0Am return poolKey, token0Amount.ToString(), token1Amount.ToString() } +// CollectReward collects staked reward from tokenId +// +// Panics if any of the following conditions are met: +// - token is not staked +// - caller is not owner of lp token func CollectReward(tokenId uint64) { deposit, exist := deposits[tokenId] if !exist { @@ -182,6 +199,10 @@ func CollectReward(tokenId uint64) { } } +// UnstakeToken unstakes the lp token from the staker and collects all reward from tokenId +// +// Panics if any of the following conditions are met: +// - token is not staked func UnstakeToken(tokenId uint64) (string, string, string) { // poolPath, token0Amount, token1Amount // Claim All Rewards CollectReward(tokenId) @@ -201,6 +222,12 @@ func UnstakeToken(tokenId uint64) (string, string, string) { // poolPath, token0 return poolPath, token0Amount.ToString(), token1Amount.ToString() } +// EndExternalIncentive ends the external incentive +// Only refundee or admin can end incentive +// +// Panics if any of the following conditions are met: +// - incentive expiration time is not over +// - reward token amount is not enough to refund func EndExternalIncentive(refundee std.Address, targetPoolPath, rewardToken string) { incentiveId := incentiveIdCompute(refundee, targetPoolPath, rewardToken) From 0aa61366cb3cbb803f460f0dfc93ff26e2bb4885 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 14 Mar 2024 19:49:00 +0900 Subject: [PATCH 03/11] GSW-924 feat: use primitive types for param and return value of public functions --- ...a => _TEST_pool_dryswap_and_swap_test.gno} | 4 +- pool/_TEST_pool_multi_token_test.gnoa | 20 ++-- pool/_TEST_pool_native_swap_test.gnoa | 10 +- pool/_TEST_pool_single_lp_test.gnoa | 112 ++++++++++-------- ..._TEST_rpc_test.gno => _TEST_rpc_test.gnoa} | 0 pool/pool.gno | 21 +++- pool/pool_manager.gno | 8 +- ..._test.gno => _TEST_position_api_test.gnoa} | 4 +- ..._TEST_position_increase_decrease_test.gnoa | 8 +- ...st_two_position_used_single_swap_test.gnoa | 4 +- position/liquidity_management.gno | 14 +-- position/position.gno | 4 +- ...a => _TEST_routes_find_swap_path_test.gno} | 0 router/swap_inner.gno | 4 +- ... => _TEST_staker_collect_reward_test.gnoa} | 2 +- ..._TEST_staker_one_external_native_test.gnoa | 2 +- staker/_TEST_staker_one_external_test.gnoa | 2 +- ...EST_staker_one_increase_external_test.gnoa | 4 +- ...est.gnoa => _TEST_staker_rpc_get_test.gno} | 2 +- staker/staker.gno | 4 +- 20 files changed, 128 insertions(+), 101 deletions(-) rename pool/{_TEST_pool_dryswap_and_swap_test.gnoa => _TEST_pool_dryswap_and_swap_test.gno} (93%) rename pool/{_TEST_rpc_test.gno => _TEST_rpc_test.gnoa} (100%) rename position/{_TEST_position_api_test.gno => _TEST_position_api_test.gnoa} (97%) rename router/{_TEST_routes_find_swap_path_test.gnoa => _TEST_routes_find_swap_path_test.gno} (100%) rename staker/{_TEST_staker_collect_reward_test.gno => _TEST_staker_collect_reward_test.gnoa} (96%) rename staker/{_TEST_staker_rpc_get_test.gnoa => _TEST_staker_rpc_get_test.gno} (97%) diff --git a/pool/_TEST_pool_dryswap_and_swap_test.gnoa b/pool/_TEST_pool_dryswap_and_swap_test.gno similarity index 93% rename from pool/_TEST_pool_dryswap_and_swap_test.gnoa rename to pool/_TEST_pool_dryswap_and_swap_test.gno index 7aaaf5ed..3ee7856e 100644 --- a/pool/_TEST_pool_dryswap_and_swap_test.gnoa +++ b/pool/_TEST_pool_dryswap_and_swap_test.gno @@ -48,7 +48,7 @@ func TestDrySwap_ZeroForOneTrue_AmountSpecified_Positive_16000(t *testing.T) { std.TestSetPrevRealm(consts.POSITION_PATH) std.TestSetOrigCaller(test1) - Mint(fooPath, barPath, fee500, consts.POSITION_ADDR, -tickUpper, -tickLower, "10") + Mint(fooPath, barPath, fee500, consts.POSITION_ADDR.String(), -tickUpper, -tickLower, "10") _, _, ok = DrySwap(fooPath, barPath, fee500, "_", true, "16000", consts.MIN_PRICE) shouldEQ(t, ok, false) @@ -56,7 +56,7 @@ func TestDrySwap_ZeroForOneTrue_AmountSpecified_Positive_16000(t *testing.T) { std.TestSetPrevRealm(consts.POSITION_PATH) std.TestSetOrigCaller(test1) - Mint(fooPath, barPath, fee500, consts.POSITION_ADDR, -tickUpper, -tickLower, liquidityExpect) // -11000 ~ -9000 + Mint(fooPath, barPath, fee500, consts.POSITION_ADDR.String(), -tickUpper, -tickLower, liquidityExpect) // -11000 ~ -9000 // zeroForOne true // amountSpecified 16000 diff --git a/pool/_TEST_pool_multi_token_test.gnoa b/pool/_TEST_pool_multi_token_test.gnoa index 7670578e..9c2e271d 100644 --- a/pool/_TEST_pool_multi_token_test.gnoa +++ b/pool/_TEST_pool_multi_token_test.gnoa @@ -59,7 +59,7 @@ func TestMintFooBarLiquidity(t *testing.T) { fooPath, barPath, fee500, - consts.POSITION_ADDR, + consts.POSITION_ADDR.String(), -test_tickUpper, -test_tickLower, test_liquidityExpectStr, @@ -80,7 +80,7 @@ func TestMintBarBazLiquidity(t *testing.T) { barPath, bazPath, fee500, - consts.POSITION_ADDR, + consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr, @@ -106,11 +106,11 @@ func TestSwapFooBarFooToBar(t *testing.T) { barPath, fooPath, fee500, - test1, + test1.String(), true, "16000", consts.MIN_PRICE, - std.GetOrigCaller(), + std.GetOrigCaller().String(), ) shouldEQ(t, poolIn, "16000") shouldEQ(t, poolOut, "-5882") @@ -144,11 +144,11 @@ func TestSwapBarBazBarToBaz(t *testing.T) { barPath, bazPath, fee500, - test1, + test1.String(), true, "16000", consts.MIN_PRICE, - std.GetOrigCaller(), + std.GetOrigCaller().String(), ) shouldEQ(t, poolIn, "16000") shouldEQ(t, poolOut, "-43457") @@ -181,7 +181,7 @@ func TestCollectFooBarFees(t *testing.T) { fooPath, barPath, fee500, - test1, + test1.String(), -test_tickUpper, -test_tickLower, "100000", @@ -219,7 +219,7 @@ func TestCollectBarBazFees(t *testing.T) { barPath, bazPath, fee500, - test1, + test1.String(), test_tickLower, test_tickUpper, "100000", @@ -304,7 +304,7 @@ func TestCollectFooBarLiquidity(t *testing.T) { fooPath, barPath, fee500, - test1, + test1.String(), -test_tickUpper, -test_tickLower, "100000", @@ -339,7 +339,7 @@ func TestCollectBarBazLiquidity(t *testing.T) { barPath, bazPath, fee500, - test1, + test1.String(), test_tickLower, test_tickUpper, "100000", diff --git a/pool/_TEST_pool_native_swap_test.gnoa b/pool/_TEST_pool_native_swap_test.gnoa index 62a7e40c..d3895e71 100644 --- a/pool/_TEST_pool_native_swap_test.gnoa +++ b/pool/_TEST_pool_native_swap_test.gnoa @@ -56,7 +56,7 @@ func TestMint(t *testing.T) { fooPath, consts.WRAPPED_WUGNOT, fee500, - consts.POSITION_ADDR, + consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, "10000000", @@ -83,11 +83,11 @@ func TestSwapBuyNative(t *testing.T) { fooPath, // token0 consts.WRAPPED_WUGNOT, // token1 fee500, - test1, + test1.String(), true, "10000", consts.MIN_PRICE, - std.GetOrigCaller(), + std.GetOrigCaller().String(), ) shouldEQ(t, amount0, "10000") shouldEQ(t, amount1, "-27123") @@ -115,11 +115,11 @@ func TestSwapSellNative(t *testing.T) { fooPath, // token0 consts.WRAPPED_WUGNOT, // token1 fee500, - test1, + test1.String(), false, "10000", consts.MAX_PRICE, - std.GetOrigCaller(), + std.GetOrigCaller().String(), ) shouldEQ(t, amount0, "-3687") // pool sent shouldEQ(t, amount1, "10000") // pool recv diff --git a/pool/_TEST_pool_single_lp_test.gnoa b/pool/_TEST_pool_single_lp_test.gnoa index 8cf09626..79cc55bf 100644 --- a/pool/_TEST_pool_single_lp_test.gnoa +++ b/pool/_TEST_pool_single_lp_test.gnoa @@ -48,7 +48,7 @@ func TestMint(t *testing.T) { fooPath, barPath, fee500, - consts.POSITION_ADDR, + consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr, @@ -60,16 +60,16 @@ func TestMint(t *testing.T) { test_liquidity := pool.PoolGetLiquidity() shouldEQ(t, test_liquidity.Dec(), test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - m81, m82 := Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - m101, m102 := Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) + m81, m82 := Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + m101, m102 := Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) shouldNEQ(t, m81, "0") shouldNEQ(t, m82, "0") @@ -80,27 +80,27 @@ func TestMint(t *testing.T) { shouldEQ(t, true, test_liquidity.Eq(new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(10)))) // tickLower > currentTick == don't add to current liquidity - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower2, test_tickUpper2, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower2, test_tickUpper2, test_liquidityExpectStr) // tickUpper < current tick == don't add to current liquidity - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, -test_tickUpper2, -test_tickLower2, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), -test_tickUpper2, -test_tickLower2, test_liquidityExpectStr) // tickUpper < tickLower == don't add to current liquidity - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, -test_tickUpper, -test_tickLower, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), -test_tickUpper, -test_tickLower, test_liquidityExpectStr) // test_liquidity = pool.PoolGetLiquidity() shouldEQ(t, true, test_liquidity.Eq(new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(10)))) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) test_liquidity = pool.PoolGetLiquidity() shouldEQ(t, true, test_liquidity.Eq(new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(20)))) @@ -144,44 +144,44 @@ func TestCollect(t *testing.T) { std.TestSetOrigCaller(test1) // withdraw all token before test `Collect` - Collect(barPath, fooPath, fee500, test1, test_tickLower, test_tickUpper, "50000000", "50000000") + Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") // pool should have zero liquidity pool := GetPool(barPath, fooPath, fee500) test_liquidity := pool.PoolGetLiquidity() shouldEQ(t, test_liquidity.ToString(), "0") - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) - c11, c12 := Collect(barPath, fooPath, fee500, test1, test_tickLower, test_tickUpper, "50000000", "50000000") + c11, c12 := Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) - c21, c22 := Collect(barPath, fooPath, fee500, test1, test_tickLower, test_tickUpper, "50000000", "50000000") + c21, c22 := Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") shouldEQ(t, c11, c21) // 443 shouldEQ(t, c12, c22) // 1206 - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) - c31, c32 := Collect(barPath, fooPath, fee500, test1, test_tickLower, test_tickUpper, "100", "100") + c31, c32 := Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "100", "100") shouldEQ(t, c31, "100") shouldEQ(t, c32, "100") - c41, c42 := Collect(barPath, fooPath, fee500, test1, test_tickLower, test_tickUpper, "50000000", "50000000") + c41, c42 := Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") shouldEQ(t, c41, "343") // 343 = c21 - 100 shouldEQ(t, c42, "1106") // 1106 = c22 - 100 // Mint > No Burn => nothing to collect - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) // Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, test_liquidityExpectStr*15) - c51, c52 := Collect(barPath, fooPath, fee500, test1, test_tickLower, test_tickUpper, "50000000", "50000000") + c51, c52 := Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") shouldEQ(t, c51, "0") shouldEQ(t, c52, "0") // Burn Now => something to collect Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) - c61, c62 := Collect(barPath, fooPath, fee500, test1, test_tickLower, test_tickUpper, "50000000", "50000000") + c61, c62 := Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") shouldNEQ(t, c61, "0") shouldNEQ(t, c62, "0") } @@ -194,7 +194,7 @@ func TestSwap(t *testing.T) { std.TestSetPrevRealm(consts.POSITION_PATH) std.TestSetOrigCaller(test1) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(20000)).ToString()) + Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(20000)).ToString()) // Swap several times std.TestSetPrevRealm(consts.ROUTER_PATH) @@ -208,14 +208,14 @@ func TestSwap(t *testing.T) { userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) amount0, amount1 := Swap( - fooPath, // token0Path - barPath, // token1Path - fee500, // fee - test1, // recipient - true, // zeroForOne - "10000", // _amountSpecified - test_price, // _sqrtPriceLimitX96 - std.GetOrigCaller(), // payer + fooPath, // token0Path + barPath, // token1Path + fee500, // fee + test1.String(), // recipient + true, // zeroForOne + "10000", // _amountSpecified + test_price, // _sqrtPriceLimitX96 + std.GetOrigCaller().String(), // payer ) shouldNEQ(t, amount0, "0") shouldNEQ(t, amount1, "0") @@ -241,7 +241,7 @@ func TestSwap(t *testing.T) { userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - amount0, amount1 := Swap(barPath, fooPath, fee500, test1, true, "5000", test_price, std.GetOrigCaller()) // give enough amount to take fees away + amount0, amount1 := Swap(barPath, fooPath, fee500, test1.String(), true, "5000", test_price, std.GetOrigCaller().String()) // give enough amount to take fees away intAmount0, _ := strconv.Atoi(amount0) intAmount1, _ := strconv.Atoi(amount1) @@ -264,7 +264,7 @@ func TestSwap(t *testing.T) { userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - amount0, amount1 := Swap(barPath, fooPath, fee500, test1, true, "1000", test_price, std.GetOrigCaller()) // give enough amount to take fees away + amount0, amount1 := Swap(barPath, fooPath, fee500, test1.String(), true, "1000", test_price, std.GetOrigCaller().String()) // give enough amount to take fees away intAmount0, _ := strconv.Atoi(amount0) intAmount1, _ := strconv.Atoi(amount1) @@ -288,7 +288,7 @@ func TestSwap(t *testing.T) { userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - amount0, amount1 := Swap(barPath, fooPath, fee500, test1, false, "16000", consts.MAX_PRICE, std.GetOrigCaller()) // give enough amount to take fees away + amount0, amount1 := Swap(barPath, fooPath, fee500, test1.String(), false, "16000", consts.MAX_PRICE, std.GetOrigCaller().String()) // give enough amount to take fees away intAmount0, _ := strconv.Atoi(amount0) intAmount1, _ := strconv.Atoi(amount1) @@ -334,13 +334,13 @@ func TestCollectProtocol(t *testing.T) { std.TestSetPrevRealm(consts.ROUTER_PATH) std.TestSetOrigCaller(test1) - Swap(barPath, fooPath, fee500, test1, true, "200000", consts.MIN_PRICE, std.GetOrigCaller()) // swap token0 -> token1 => fee only in token0 + Swap(barPath, fooPath, fee500, test1.String(), true, "200000", consts.MIN_PRICE, std.GetOrigCaller().String()) // swap token0 -> token1 => fee only in token0 test1OldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) test1OldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) std.TestSetPrevAddr(gsa) - amount0, amount1 := CollectProtocol(barPath, fooPath, fee500, test1, "100000", "100000") + amount0, amount1 := CollectProtocol(barPath, fooPath, fee500, test1.String(), "100000", "100000") test1NewToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) test1NewToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) @@ -356,6 +356,22 @@ func TestCollectProtocol(t *testing.T) { } } +func TestAddAdmin(t *testing.T) { + std.TestSetPrevAddr(gsa) + shouldEQ(t, len(admins), 1) + + AddAdmin(test1.String()) + shouldEQ(t, len(admins), 2) +} + +func TestRemoveAdmin(t *testing.T) { + std.TestSetPrevAddr(gsa) + shouldEQ(t, len(admins), 2) + + RemoveAdmin(test1.String()) + shouldEQ(t, len(admins), 1) +} + /* GETTER_API TEST */ func TestApiGetPools(t *testing.T) { gpls := ApiGetPools() diff --git a/pool/_TEST_rpc_test.gno b/pool/_TEST_rpc_test.gnoa similarity index 100% rename from pool/_TEST_rpc_test.gno rename to pool/_TEST_rpc_test.gnoa diff --git a/pool/pool.gno b/pool/pool.gno index b74e72ea..74822efd 100644 --- a/pool/pool.gno +++ b/pool/pool.gno @@ -23,7 +23,7 @@ func Mint( token0Path string, token1Path string, fee uint32, - recipient std.Address, + recipient string, tickLower int32, tickUpper int32, _liquidityAmount string, // uint128 @@ -38,7 +38,7 @@ func Mint( pool := GetPool(token0Path, token1Path, fee) _, amount0, amount1 := pool.modifyPosition( // int256 x2 ModifyPositionParams{ - recipient, // owner + std.Address(recipient), // owner tickLower, // tickLower tickUpper, // tickUpper i256.FromUint256(liquidityAmount), // liquidityDelta @@ -138,7 +138,7 @@ func Collect( token0Path string, token1Path string, fee uint32, - recipient std.Address, + _recipient string, tickLower int32, tickUpper int32, _amount0Requested string, // uint128 @@ -151,6 +151,9 @@ func Collect( amount0Requested := u256.MustFromDecimal(_amount0Requested) amount1Requested := u256.MustFromDecimal(_amount1Requested) + + recipient := std.Address(_recipient) + pool := GetPool(token0Path, token1Path, fee) positionKey := positionGetKey(std.PrevRealm().Addr(), tickLower, tickUpper) @@ -193,11 +196,11 @@ func Swap( token0Path string, token1Path string, fee uint32, - recipient std.Address, + _recipient string, zeroForOne bool, _amountSpecified string, // int256 _sqrtPriceLimitX96 string, // uint160 - payer std.Address, // router + _payer string, // router ) (string, string) { // int256 x2 // only router contract can call this function if std.PrevRealm().PkgPath() != consts.ROUTER_PATH { @@ -211,6 +214,9 @@ func Swap( amountSpecified := i256.MustFromDecimal(_amountSpecified) sqrtPriceLimitX96 := u256.MustFromDecimal(_sqrtPriceLimitX96) + recipient := std.Address(_recipient) + payer := std.Address(_payer) + pool := GetPool(token0Path, token1Path, fee) slot0Start := pool.slot0 @@ -543,7 +549,7 @@ func CollectProtocol( token0Path string, token1Path string, fee uint32, - recipient std.Address, + _recipient string, _amount0Requested string, // uint128 _amount1Requested string, // uint128 ) (string, string) { // uint128 x2 @@ -551,6 +557,9 @@ func CollectProtocol( if !(isAdmin(std.PrevRealm().Addr())) { panic(ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || caller(%s) must be admin", std.PrevRealm().Addr())) } + + recipient := std.Address(_recipient) + amount0Requested := u256.MustFromDecimal(_amount0Requested) amount1Requested := u256.MustFromDecimal(_amount1Requested) diff --git a/pool/pool_manager.gno b/pool/pool_manager.gno index 1b5261d2..975ebdcb 100644 --- a/pool/pool_manager.gno +++ b/pool/pool_manager.gno @@ -156,10 +156,10 @@ func GetPoolPath(token0Path, token1Path string, fee uint32) string { // // Panics if any of the following conditions are met: // - The caller is not an admin -func AddAdmin(addr std.Address) { +func AddAdmin(addr string) { caller := std.PrevRealm().Addr() if isAdmin(caller) { - admins = append(admins, addr) + admins = append(admins, std.Address(addr)) } else { panic(ufmt.Sprintf("[POOL] pool_manager.gno__AddAdmin() || caller(%s) is not admin", caller.String())) } @@ -170,7 +170,7 @@ func AddAdmin(addr std.Address) { // Panics if any of the following conditions are met: // - The caller is not an admin // - The address is the last admin -func RemoveAdmin(addr std.Address) { +func RemoveAdmin(addr string) { caller := std.PrevRealm().Addr() if isAdmin(caller) { @@ -178,7 +178,7 @@ func RemoveAdmin(addr std.Address) { panic("[POOL] pool_manager.gno__RemoveAdmin() || cannot remove last admin") } for i, v := range admins { - if v == addr { + if v == std.Address(addr) { admins = append(admins[:i], admins[i+1:]...) return } diff --git a/position/_TEST_position_api_test.gno b/position/_TEST_position_api_test.gnoa similarity index 97% rename from position/_TEST_position_api_test.gno rename to position/_TEST_position_api_test.gnoa index e90becb7..2ab79145 100644 --- a/position/_TEST_position_api_test.gno +++ b/position/_TEST_position_api_test.gnoa @@ -49,11 +49,11 @@ func TestSwap(t *testing.T) { fooPath, barPath, fee500, - test1, + test1.String(), true, "123456", consts.MIN_PRICE, - test1, + test1.String(), ) } diff --git a/position/_TEST_position_increase_decrease_test.gnoa b/position/_TEST_position_increase_decrease_test.gnoa index a9b7b2f8..f3d9098e 100644 --- a/position/_TEST_position_increase_decrease_test.gnoa +++ b/position/_TEST_position_increase_decrease_test.gnoa @@ -93,11 +93,11 @@ func TestSwap(t *testing.T) { barPath, fooPath, fee500, - test1, + test1.String(), true, "1234567", consts.MIN_PRICE, - test1, + test1.String(), ) } @@ -136,11 +136,11 @@ func TestSwap2(t *testing.T) { barPath, fooPath, fee500, - test1, + test1.String(), true, "1000000", consts.MIN_PRICE, - test1, + test1.String(), ) // UNCLAIMED_FEE diff --git a/position/_TEST_position_test_two_position_used_single_swap_test.gnoa b/position/_TEST_position_test_two_position_used_single_swap_test.gnoa index 254e5088..1b2c57d8 100644 --- a/position/_TEST_position_test_two_position_used_single_swap_test.gnoa +++ b/position/_TEST_position_test_two_position_used_single_swap_test.gnoa @@ -99,11 +99,11 @@ func TestSwap(t *testing.T) { barPath, fooPath, fee500, - test1, + test1.String(), true, "1234567", consts.MIN_PRICE, - test1, + test1.String(), ) } diff --git a/position/liquidity_management.gno b/position/liquidity_management.gno index d8b9b8f2..967d941e 100644 --- a/position/liquidity_management.gno +++ b/position/liquidity_management.gno @@ -27,13 +27,13 @@ func addLiquidity(params AddLiquidityParams) (*u256.Uint, *u256.Uint, *u256.Uint pToken0, pToken1, pFee := poolKeyDivide(params.poolKey) amount0, amount1 := pl.Mint( // string(uint256), string(uint256) - pToken0, // token0Path string - pToken1, // token1Path string - pFee, // fee uint16 - consts.POSITION_ADDR, // recipient std.Address - params.tickLower, // tickLower int32 - params.tickUpper, // tickUpper int32 - liquidity.ToString(), // _liquidityAmount string(uint128) + pToken0, // token0Path string + pToken1, // token1Path string + pFee, // fee uint16 + consts.POSITION_ADDR.String(), // recipient std.Address + params.tickLower, // tickLower int32 + params.tickUpper, // tickUpper int32 + liquidity.ToString(), // _liquidityAmount string(uint128) ) amount0Uint := u256.MustFromDecimal(amount0) diff --git a/position/position.gno b/position/position.gno index 5bc65198..4e71d980 100644 --- a/position/position.gno +++ b/position/position.gno @@ -369,7 +369,7 @@ func decreaseLiquidity(params DecreaseLiquidityParams) (uint64, *u256.Uint, *u25 pToken0, pToken1, pFee, - std.GetOrigCaller(), + std.GetOrigCaller().String(), position.tickLower, position.tickUpper, _burnedAmount0, @@ -458,7 +458,7 @@ func CollectFee(tokenId uint64) (uint64, string, string, string) { // tokenId, t token0, token1, fee, - std.GetOrigCaller(), + std.GetOrigCaller().String(), position.tickLower, position.tickUpper, consts.MAX_UINT64, // string, r3v4_xxx: current grc20 handles amount by `uint64` diff --git a/router/_TEST_routes_find_swap_path_test.gnoa b/router/_TEST_routes_find_swap_path_test.gno similarity index 100% rename from router/_TEST_routes_find_swap_path_test.gnoa rename to router/_TEST_routes_find_swap_path_test.gno diff --git a/router/swap_inner.gno b/router/swap_inner.gno index ebea9798..5282e434 100644 --- a/router/swap_inner.gno +++ b/router/swap_inner.gno @@ -39,12 +39,12 @@ func _swap( data.tokenOut, data.fee, - recipient, + recipient.String(), zeroForOne, amountSpecified.ToString(), sqrtPriceLimitX96.ToString(), - data.payer, + data.payer.String(), ) amount0, err := i256.FromDecimal(amount0Str) if err != nil { diff --git a/staker/_TEST_staker_collect_reward_test.gno b/staker/_TEST_staker_collect_reward_test.gnoa similarity index 96% rename from staker/_TEST_staker_collect_reward_test.gno rename to staker/_TEST_staker_collect_reward_test.gnoa index a5be7b5f..b28fd275 100644 --- a/staker/_TEST_staker_collect_reward_test.gno +++ b/staker/_TEST_staker_collect_reward_test.gnoa @@ -197,7 +197,7 @@ func TestUnstakeToken02(t *testing.T) { func TestEndExternalIncentive(t *testing.T) { std.TestSetPrevAddr(test1) std.TestSkipHeights(9999999) - EndExternalIncentive(std.GetOrigCaller(), "gno.land/r/demo/bar:gno.land/r/demo/qux:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() + EndExternalIncentive(std.GetOrigCaller().String(), "gno.land/r/demo/bar:gno.land/r/demo/qux:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() std.TestSkipHeights(1) shouldEQ(t, len(incentives), 0) diff --git a/staker/_TEST_staker_one_external_native_test.gnoa b/staker/_TEST_staker_one_external_native_test.gnoa index 44d9dda5..7a5afb9a 100644 --- a/staker/_TEST_staker_one_external_native_test.gnoa +++ b/staker/_TEST_staker_one_external_native_test.gnoa @@ -185,7 +185,7 @@ func TestUnstakeToken02(t *testing.T) { func TestEndExternalIncentive(t *testing.T) { std.TestSetOrigCaller(test1) std.TestSkipHeights(9999999) - EndExternalIncentive(std.GetOrigCaller(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", consts.WRAPPED_WUGNOT) // use same parameter as CreateExternalIncentive() + EndExternalIncentive(std.GetOrigCaller().String(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", consts.WRAPPED_WUGNOT) // use same parameter as CreateExternalIncentive() std.TestSkipHeights(1) shouldEQ(t, len(incentives), 0) diff --git a/staker/_TEST_staker_one_external_test.gnoa b/staker/_TEST_staker_one_external_test.gnoa index b9c0a302..be09d938 100644 --- a/staker/_TEST_staker_one_external_test.gnoa +++ b/staker/_TEST_staker_one_external_test.gnoa @@ -188,7 +188,7 @@ func TestUnstakeToken02(t *testing.T) { func TestEndExternalIncentive(t *testing.T) { std.TestSetOrigCaller(test1) std.TestSkipHeights(9999999) - EndExternalIncentive(std.GetOrigCaller(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() + EndExternalIncentive(std.GetOrigCaller().String(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() std.TestSkipHeights(1) shouldEQ(t, len(incentives), 0) diff --git a/staker/_TEST_staker_one_increase_external_test.gnoa b/staker/_TEST_staker_one_increase_external_test.gnoa index 686e6d16..4ec96c9c 100644 --- a/staker/_TEST_staker_one_increase_external_test.gnoa +++ b/staker/_TEST_staker_one_increase_external_test.gnoa @@ -197,12 +197,12 @@ func TestEndExternalIncentive(t *testing.T) { // incentive time isn't over yet shouldPanicWithMsg(t, func() { - EndExternalIncentive(test1, "gno.land/r/demo/bar:gno.land/r/demo/foo:500", "gno.land/r/demo/obl") + EndExternalIncentive(test1.String(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", "gno.land/r/demo/obl") }, "[STAKER] staker.gno__EndExternalIncentive() || cannot end incentive before endTimestamp(1242343950), current(1234567990)") std.TestSkipHeights(9999999) // not yet, one more should be good to end incentive - EndExternalIncentive(test1, "gno.land/r/demo/bar:gno.land/r/demo/foo:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() + EndExternalIncentive(test1.String(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() std.TestSkipHeights(1) shouldEQ(t, len(incentives), 0) diff --git a/staker/_TEST_staker_rpc_get_test.gnoa b/staker/_TEST_staker_rpc_get_test.gno similarity index 97% rename from staker/_TEST_staker_rpc_get_test.gnoa rename to staker/_TEST_staker_rpc_get_test.gno index cd1e4d36..fb70ccea 100644 --- a/staker/_TEST_staker_rpc_get_test.gnoa +++ b/staker/_TEST_staker_rpc_get_test.gno @@ -208,7 +208,7 @@ func TestUnstakeToken02(t *testing.T) { func TestEndExternalIncentive(t *testing.T) { std.TestSetOrigCaller(test1) std.TestSkipHeights(9999999) - EndExternalIncentive(std.GetOrigCaller(), "gno.land/r/demo/bar:gno.land/r/demo/qux:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() + EndExternalIncentive(std.GetOrigCaller().String(), "gno.land/r/demo/bar:gno.land/r/demo/qux:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() std.TestSkipHeights(1) shouldEQ(t, len(incentives), 0) diff --git a/staker/staker.gno b/staker/staker.gno index f1faa9a9..baa08d9c 100644 --- a/staker/staker.gno +++ b/staker/staker.gno @@ -228,7 +228,9 @@ func UnstakeToken(tokenId uint64) (string, string, string) { // poolPath, token0 // Panics if any of the following conditions are met: // - incentive expiration time is not over // - reward token amount is not enough to refund -func EndExternalIncentive(refundee std.Address, targetPoolPath, rewardToken string) { +func EndExternalIncentive(_refundee, targetPoolPath, rewardToken string) { + refundee := std.Address(_refundee) + incentiveId := incentiveIdCompute(refundee, targetPoolPath, rewardToken) incentive, exist := incentives[incentiveId] From 65dc364f49eabf4ffec7991841f3f2f95144b9e7 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 15 Mar 2024 11:33:07 +0900 Subject: [PATCH 04/11] chore --- packages/big/int256/int256.gno | 25 ++++++++++++-------- packages/big/uint256/uint256.gno | 40 ++++++++++++++++++-------------- pool/pool.gno | 2 +- pool/withdrawal_fee.gno | 1 - position/position.gno | 24 ++++++++++++++++++- 5 files changed, 62 insertions(+), 30 deletions(-) diff --git a/packages/big/int256/int256.gno b/packages/big/int256/int256.gno index a1028b70..79554914 100644 --- a/packages/big/int256/int256.gno +++ b/packages/big/int256/int256.gno @@ -1,4 +1,4 @@ -// github.com/mempooler/int256 +// ported from github.com/mempooler/int256 package int256 @@ -13,10 +13,12 @@ type Int struct { neg bool } +// Zero returns a new Int set to 0. func Zero() *Int { return NewInt(0) } +// One returns a new Int set to 1. func One() *Int { return NewInt(1) } @@ -536,17 +538,11 @@ func AddDeltaOverflow(z, x *uint256.Uint, y *Int) bool { return overflow } -// NIL TO ZERO -func (z *Int) NilToZero() *Int { - if z == nil { - return NewInt(0) - } - return z -} - +// OBS, differs from original holiman uint256 +// ToString returns the decimal representation of z. func (z *Int) ToString() string { if z == nil { - panic("int256: nil pointer") + panic("int256: nil pointer to ToString()") } t := z.abs.Dec() @@ -555,3 +551,12 @@ func (z *Int) ToString() string { } return t } + +// OBS, differs from original holiman uint256 +// NilToZero sets z to 0 and return it if it's nil, otherwise it returns z +func (z *Int) NilToZero() *Int { + if z == nil { + return NewInt(0) + } + return z +} diff --git a/packages/big/uint256/uint256.gno b/packages/big/uint256/uint256.gno index ccce6090..e2c50bf8 100644 --- a/packages/big/uint256/uint256.gno +++ b/packages/big/uint256/uint256.gno @@ -1,4 +1,4 @@ -// Ported from https://github.com/holiman/uint256 +// ported from https://github.com/holiman/uint256 package uint256 import ( @@ -750,6 +750,14 @@ func (z *Uint) And(x, y *Uint) *Uint { return z } +func (z *Uint) AndNot(x, y *Uint) *Uint { + z.arr[0] = x.arr[0] &^ y.arr[0] + z.arr[1] = x.arr[1] &^ y.arr[1] + z.arr[2] = x.arr[2] &^ y.arr[2] + z.arr[3] = x.arr[3] &^ y.arr[3] + return z +} + // Xor sets z = x ^ y and returns z. func (z *Uint) Xor(x, y *Uint) *Uint { z.arr[0] = x.arr[0] ^ y.arr[0] @@ -1186,14 +1194,6 @@ func (z *Uint) Dec() string { return string(out[pos-len(buf):]) } -func (z *Uint) ToString() string { - if z == nil { - panic("U256 ToString() nil") - } - - return z.Dec() -} - // Mod sets z to the modulus x%y for y != 0 and returns z. // If y == 0, z is set to 0 (OBS: differs from the big.Uint) func (z *Uint) Mod(x, y *Uint) *Uint { @@ -1236,10 +1236,24 @@ func (z *Uint) Clone() *Uint { return &x } +// OBS, differs from original holiman uint256 +// ToString returns the decimal representation of z. +func (z *Uint) ToString() string { + if z == nil { + panic("uin256: nil pointer to ToString()") + } + + return z.Dec() +} + +// OBS, differs from original holiman uint256 +// IsNil reports whether z is nil func (z *Uint) IsNil() bool { return z == nil } +// OBS, differs from original holiman uint256 +// NilToZero sets z to 0 and return it if it's nil, otherwise it returns z func (z *Uint) NilToZero() *Uint { if z == nil { z = NewUint(0) @@ -1247,11 +1261,3 @@ func (z *Uint) NilToZero() *Uint { return z } - -func (z *Uint) AndNot(x, y *Uint) *Uint { - z.arr[0] = x.arr[0] &^ y.arr[0] - z.arr[1] = x.arr[1] &^ y.arr[1] - z.arr[2] = x.arr[2] &^ y.arr[2] - z.arr[3] = x.arr[3] &^ y.arr[3] - return z -} diff --git a/pool/pool.gno b/pool/pool.gno index 74822efd..21ad5de2 100644 --- a/pool/pool.gno +++ b/pool/pool.gno @@ -510,7 +510,7 @@ func Swap( return amount0.ToString(), amount1.ToString() } -// SetFeeProtocls sets the denominator of the protocol fee +// SetFeeProtocol sets the denominator of the protocol fee // // Panics if any of the following conditions are met: // - The caller is not an admin diff --git a/pool/withdrawal_fee.gno b/pool/withdrawal_fee.gno index cf7ea884..3db7fa42 100644 --- a/pool/withdrawal_fee.gno +++ b/pool/withdrawal_fee.gno @@ -65,7 +65,6 @@ func HandleWithdrawalFee( // // Panics if any of the following conditions are met: // - caller is not the admin - func SetWithdrawalFee(fee uint64) { // MUST BE ORIGIN CALL std.AssertOriginCall() diff --git a/position/position.gno b/position/position.gno index 4e71d980..6acac105 100644 --- a/position/position.gno +++ b/position/position.gno @@ -18,6 +18,10 @@ var ( nextId uint64 = 1 // lp token id ) +// Mint creates a new position and mints liquidity tokens then return minted info +// +// Panics if any of the following conditions are met: +// - caller is not user or staker func Mint( token0 string, token1 string, @@ -166,6 +170,12 @@ func mint(params MintParams) (uint64, *u256.Uint, *u256.Uint, *u256.Uint) { return tokenId, liquidity, amount0, amount1 } +// IncreaseLiquidity increases liquidity of the existing position +// +// Panics if any of the following conditions are met: +// - position doesn't exist +// - caller is not owner of the position +// - deadline is passed func IncreaseLiquidity( tokenId uint64, _amount0Desired string, // uint256 @@ -248,6 +258,13 @@ func increaseLiquidity(params IncreaseLiquidityParams) (uint64, *u256.Uint, *u25 return params.tokenId, liquidity, amount0, amount1 } +// DecreaseLiquidity decreases liquidity of the existing position +// +// Panics if any of the following conditions are met: +// - position doesn't exist +// - caller is not owner of the position +// - liquidityRatio is not in range 1 ~ 100 +// - deadline is passed func DecreaseLiquidity( tokenId uint64, liquidityRatio uint64, @@ -257,7 +274,7 @@ func DecreaseLiquidity( unwrapResult bool, ) (uint64, string, string, string, string, string, string) { // tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath - isNormalRange := liquidityRatio > 0 && liquidityRatio <= 100 + isNormalRange := liquidityRatio >= 1 && liquidityRatio <= 100 if !isNormalRange { panic(ufmt.Sprintf("[POSITION] position.gno__decreaseLiquidity() || liquidityRatio(%d) should be in range 1 ~ 100", liquidityRatio)) } @@ -410,6 +427,11 @@ func decreaseLiquidity(params DecreaseLiquidityParams) (uint64, *u256.Uint, *u25 return params.tokenId, liquidityToRemove, fee0, fee1, amount0, amount1, position.poolKey } +// CollectFee collects swap fee from the position +// +// Panics if any of the following conditions are met: +// - position doesn't exist +// - caller is not owner or approved func CollectFee(tokenId uint64) (uint64, string, string, string) { // tokenId, tokensOwed0, tokensOwed1, poolPath // verify tokenId if !exists(tokenId) { From dcc86d48929f2acf8d78e190cf7515ecd41a8783 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 15 Mar 2024 13:48:03 +0900 Subject: [PATCH 05/11] chore: move file & folders --- {packages => lib_package}/big/int256/gno.mod | 0 .../big/int256/int256.gno | 0 .../big/uint256/bits.gno | 0 .../big/uint256/bits_errors.gno | 0 .../big/uint256/bits_table.gno | 0 .../big/uint256/consts.gno | 0 {packages => lib_package}/big/uint256/gno.mod | 0 {packages => lib_package}/big/uint256/mod.gno | 0 .../big/uint256/strconv.gno | 0 .../big/uint256/uint256.gno | 0 .../uint256/uint256_overflow_calculation.gno | 0 {json => lib_package/json}/LICENSE | 0 {json => lib_package/json}/README.md | 0 {json => lib_package/json}/buffer.gno | 0 {json => lib_package/json}/buffer_test.gno | 0 {json => lib_package/json}/decode.gno | 0 {json => lib_package/json}/decode_test.gno | 0 .../json}/eisel_lemire/eisel_lemire.gno | 0 .../json}/eisel_lemire/gno.mod | 0 {json => lib_package/json}/encode.gno | 0 {json => lib_package/json}/encode_test.gno | 0 {json => lib_package/json}/escape.gno | 0 {json => lib_package/json}/escape_test.gno | 0 {json => lib_package/json}/gno.mod | 0 {json => lib_package/json}/indent.gno | 0 {json => lib_package/json}/indent_test.gno | 0 {json => lib_package/json}/internal.gno | 0 {json => lib_package/json}/node.gno | 0 {json => lib_package/json}/node_test.gno | 0 {json => lib_package/json}/parser.gno | 0 {json => lib_package/json}/parser_test.gno | 0 {json => lib_package/json}/path.gno | 0 {json => lib_package/json}/path_test.gno | 0 {json => lib_package/json}/ryu/License | 0 {json => lib_package/json}/ryu/floatconv.gno | 0 .../json}/ryu/floatconv_test.gno | 0 {json => lib_package/json}/ryu/gno.mod | 0 {json => lib_package/json}/ryu/ryu64.gno | 0 {json => lib_package/json}/ryu/table.gno | 0 {json => lib_package/json}/token.gno | 0 .../common/allow_non_gnoswap_contracts.gno | 44 +++++++++++++++++++ .../gnoswap/common/liquidity_amounts.gno | 2 +- .../gnoswap/common/tick_math.gno | 0 .../gnoswap/consts}/consts.gno | 0 {faucet => x/faucet}/faucet.gno | 0 {faucet => x/faucet}/faucet_test.gno | 0 46 files changed, 45 insertions(+), 1 deletion(-) rename {packages => lib_package}/big/int256/gno.mod (100%) rename {packages => lib_package}/big/int256/int256.gno (100%) rename {packages => lib_package}/big/uint256/bits.gno (100%) rename {packages => lib_package}/big/uint256/bits_errors.gno (100%) rename {packages => lib_package}/big/uint256/bits_table.gno (100%) rename {packages => lib_package}/big/uint256/consts.gno (100%) rename {packages => lib_package}/big/uint256/gno.mod (100%) rename {packages => lib_package}/big/uint256/mod.gno (100%) rename {packages => lib_package}/big/uint256/strconv.gno (100%) rename {packages => lib_package}/big/uint256/uint256.gno (100%) rename {packages => lib_package}/big/uint256/uint256_overflow_calculation.gno (100%) rename {json => lib_package/json}/LICENSE (100%) rename {json => lib_package/json}/README.md (100%) rename {json => lib_package/json}/buffer.gno (100%) rename {json => lib_package/json}/buffer_test.gno (100%) rename {json => lib_package/json}/decode.gno (100%) rename {json => lib_package/json}/decode_test.gno (100%) rename {json => lib_package/json}/eisel_lemire/eisel_lemire.gno (100%) rename {json => lib_package/json}/eisel_lemire/gno.mod (100%) rename {json => lib_package/json}/encode.gno (100%) rename {json => lib_package/json}/encode_test.gno (100%) rename {json => lib_package/json}/escape.gno (100%) rename {json => lib_package/json}/escape_test.gno (100%) rename {json => lib_package/json}/gno.mod (100%) rename {json => lib_package/json}/indent.gno (100%) rename {json => lib_package/json}/indent_test.gno (100%) rename {json => lib_package/json}/internal.gno (100%) rename {json => lib_package/json}/node.gno (100%) rename {json => lib_package/json}/node_test.gno (100%) rename {json => lib_package/json}/parser.gno (100%) rename {json => lib_package/json}/parser_test.gno (100%) rename {json => lib_package/json}/path.gno (100%) rename {json => lib_package/json}/path_test.gno (100%) rename {json => lib_package/json}/ryu/License (100%) rename {json => lib_package/json}/ryu/floatconv.gno (100%) rename {json => lib_package/json}/ryu/floatconv_test.gno (100%) rename {json => lib_package/json}/ryu/gno.mod (100%) rename {json => lib_package/json}/ryu/ryu64.gno (100%) rename {json => lib_package/json}/ryu/table.gno (100%) rename {json => lib_package/json}/token.gno (100%) create mode 100644 lib_realm/gnoswap/common/allow_non_gnoswap_contracts.gno rename {packages => lib_realm}/gnoswap/common/liquidity_amounts.gno (99%) rename {packages => lib_realm}/gnoswap/common/tick_math.gno (100%) rename {consts => lib_realm/gnoswap/consts}/consts.gno (100%) rename {faucet => x/faucet}/faucet.gno (100%) rename {faucet => x/faucet}/faucet_test.gno (100%) diff --git a/packages/big/int256/gno.mod b/lib_package/big/int256/gno.mod similarity index 100% rename from packages/big/int256/gno.mod rename to lib_package/big/int256/gno.mod diff --git a/packages/big/int256/int256.gno b/lib_package/big/int256/int256.gno similarity index 100% rename from packages/big/int256/int256.gno rename to lib_package/big/int256/int256.gno diff --git a/packages/big/uint256/bits.gno b/lib_package/big/uint256/bits.gno similarity index 100% rename from packages/big/uint256/bits.gno rename to lib_package/big/uint256/bits.gno diff --git a/packages/big/uint256/bits_errors.gno b/lib_package/big/uint256/bits_errors.gno similarity index 100% rename from packages/big/uint256/bits_errors.gno rename to lib_package/big/uint256/bits_errors.gno diff --git a/packages/big/uint256/bits_table.gno b/lib_package/big/uint256/bits_table.gno similarity index 100% rename from packages/big/uint256/bits_table.gno rename to lib_package/big/uint256/bits_table.gno diff --git a/packages/big/uint256/consts.gno b/lib_package/big/uint256/consts.gno similarity index 100% rename from packages/big/uint256/consts.gno rename to lib_package/big/uint256/consts.gno diff --git a/packages/big/uint256/gno.mod b/lib_package/big/uint256/gno.mod similarity index 100% rename from packages/big/uint256/gno.mod rename to lib_package/big/uint256/gno.mod diff --git a/packages/big/uint256/mod.gno b/lib_package/big/uint256/mod.gno similarity index 100% rename from packages/big/uint256/mod.gno rename to lib_package/big/uint256/mod.gno diff --git a/packages/big/uint256/strconv.gno b/lib_package/big/uint256/strconv.gno similarity index 100% rename from packages/big/uint256/strconv.gno rename to lib_package/big/uint256/strconv.gno diff --git a/packages/big/uint256/uint256.gno b/lib_package/big/uint256/uint256.gno similarity index 100% rename from packages/big/uint256/uint256.gno rename to lib_package/big/uint256/uint256.gno diff --git a/packages/big/uint256/uint256_overflow_calculation.gno b/lib_package/big/uint256/uint256_overflow_calculation.gno similarity index 100% rename from packages/big/uint256/uint256_overflow_calculation.gno rename to lib_package/big/uint256/uint256_overflow_calculation.gno diff --git a/json/LICENSE b/lib_package/json/LICENSE similarity index 100% rename from json/LICENSE rename to lib_package/json/LICENSE diff --git a/json/README.md b/lib_package/json/README.md similarity index 100% rename from json/README.md rename to lib_package/json/README.md diff --git a/json/buffer.gno b/lib_package/json/buffer.gno similarity index 100% rename from json/buffer.gno rename to lib_package/json/buffer.gno diff --git a/json/buffer_test.gno b/lib_package/json/buffer_test.gno similarity index 100% rename from json/buffer_test.gno rename to lib_package/json/buffer_test.gno diff --git a/json/decode.gno b/lib_package/json/decode.gno similarity index 100% rename from json/decode.gno rename to lib_package/json/decode.gno diff --git a/json/decode_test.gno b/lib_package/json/decode_test.gno similarity index 100% rename from json/decode_test.gno rename to lib_package/json/decode_test.gno diff --git a/json/eisel_lemire/eisel_lemire.gno b/lib_package/json/eisel_lemire/eisel_lemire.gno similarity index 100% rename from json/eisel_lemire/eisel_lemire.gno rename to lib_package/json/eisel_lemire/eisel_lemire.gno diff --git a/json/eisel_lemire/gno.mod b/lib_package/json/eisel_lemire/gno.mod similarity index 100% rename from json/eisel_lemire/gno.mod rename to lib_package/json/eisel_lemire/gno.mod diff --git a/json/encode.gno b/lib_package/json/encode.gno similarity index 100% rename from json/encode.gno rename to lib_package/json/encode.gno diff --git a/json/encode_test.gno b/lib_package/json/encode_test.gno similarity index 100% rename from json/encode_test.gno rename to lib_package/json/encode_test.gno diff --git a/json/escape.gno b/lib_package/json/escape.gno similarity index 100% rename from json/escape.gno rename to lib_package/json/escape.gno diff --git a/json/escape_test.gno b/lib_package/json/escape_test.gno similarity index 100% rename from json/escape_test.gno rename to lib_package/json/escape_test.gno diff --git a/json/gno.mod b/lib_package/json/gno.mod similarity index 100% rename from json/gno.mod rename to lib_package/json/gno.mod diff --git a/json/indent.gno b/lib_package/json/indent.gno similarity index 100% rename from json/indent.gno rename to lib_package/json/indent.gno diff --git a/json/indent_test.gno b/lib_package/json/indent_test.gno similarity index 100% rename from json/indent_test.gno rename to lib_package/json/indent_test.gno diff --git a/json/internal.gno b/lib_package/json/internal.gno similarity index 100% rename from json/internal.gno rename to lib_package/json/internal.gno diff --git a/json/node.gno b/lib_package/json/node.gno similarity index 100% rename from json/node.gno rename to lib_package/json/node.gno diff --git a/json/node_test.gno b/lib_package/json/node_test.gno similarity index 100% rename from json/node_test.gno rename to lib_package/json/node_test.gno diff --git a/json/parser.gno b/lib_package/json/parser.gno similarity index 100% rename from json/parser.gno rename to lib_package/json/parser.gno diff --git a/json/parser_test.gno b/lib_package/json/parser_test.gno similarity index 100% rename from json/parser_test.gno rename to lib_package/json/parser_test.gno diff --git a/json/path.gno b/lib_package/json/path.gno similarity index 100% rename from json/path.gno rename to lib_package/json/path.gno diff --git a/json/path_test.gno b/lib_package/json/path_test.gno similarity index 100% rename from json/path_test.gno rename to lib_package/json/path_test.gno diff --git a/json/ryu/License b/lib_package/json/ryu/License similarity index 100% rename from json/ryu/License rename to lib_package/json/ryu/License diff --git a/json/ryu/floatconv.gno b/lib_package/json/ryu/floatconv.gno similarity index 100% rename from json/ryu/floatconv.gno rename to lib_package/json/ryu/floatconv.gno diff --git a/json/ryu/floatconv_test.gno b/lib_package/json/ryu/floatconv_test.gno similarity index 100% rename from json/ryu/floatconv_test.gno rename to lib_package/json/ryu/floatconv_test.gno diff --git a/json/ryu/gno.mod b/lib_package/json/ryu/gno.mod similarity index 100% rename from json/ryu/gno.mod rename to lib_package/json/ryu/gno.mod diff --git a/json/ryu/ryu64.gno b/lib_package/json/ryu/ryu64.gno similarity index 100% rename from json/ryu/ryu64.gno rename to lib_package/json/ryu/ryu64.gno diff --git a/json/ryu/table.gno b/lib_package/json/ryu/table.gno similarity index 100% rename from json/ryu/table.gno rename to lib_package/json/ryu/table.gno diff --git a/json/token.gno b/lib_package/json/token.gno similarity index 100% rename from json/token.gno rename to lib_package/json/token.gno diff --git a/lib_realm/gnoswap/common/allow_non_gnoswap_contracts.gno b/lib_realm/gnoswap/common/allow_non_gnoswap_contracts.gno new file mode 100644 index 00000000..b8ae9cfe --- /dev/null +++ b/lib_realm/gnoswap/common/allow_non_gnoswap_contracts.gno @@ -0,0 +1,44 @@ +package common + +import ( + "std" + + "gno.land/r/gnoswap/consts" +) + +var ( + limitCaller bool = true +) + +func GetLimitCaller() bool { + return limitCaller +} + +func SetLimitCaller(v bool) { + MustCallFromAdmin() + limitCaller = v +} + +func MustCallFromAdmin() { + caller := std.GetOrigCaller() + if caller != consts.GNOSWAP_ADMIN { + panic("must be called by admin") + } +} + +func DisallowCallFromUser() { + isOrigin := std.IsOriginCall() + if isOrigin { + panic("must be called by realm, not user") + } +} + +func AllowCallFromOnly(allowPath string) { + if !limitCaller { + prevPath := std.PrevRealm().PkgPath() + + if prevPath != allowPath { + panic("caller is not allowed to call this function") + } + } +} diff --git a/packages/gnoswap/common/liquidity_amounts.gno b/lib_realm/gnoswap/common/liquidity_amounts.gno similarity index 99% rename from packages/gnoswap/common/liquidity_amounts.gno rename to lib_realm/gnoswap/common/liquidity_amounts.gno index 90a3a4c5..6a8eb765 100644 --- a/packages/gnoswap/common/liquidity_amounts.gno +++ b/lib_realm/gnoswap/common/liquidity_amounts.gno @@ -1,7 +1,7 @@ package common import ( - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" u256 "gno.land/p/big/uint256" ) diff --git a/packages/gnoswap/common/tick_math.gno b/lib_realm/gnoswap/common/tick_math.gno similarity index 100% rename from packages/gnoswap/common/tick_math.gno rename to lib_realm/gnoswap/common/tick_math.gno diff --git a/consts/consts.gno b/lib_realm/gnoswap/consts/consts.gno similarity index 100% rename from consts/consts.gno rename to lib_realm/gnoswap/consts/consts.gno diff --git a/faucet/faucet.gno b/x/faucet/faucet.gno similarity index 100% rename from faucet/faucet.gno rename to x/faucet/faucet.gno diff --git a/faucet/faucet_test.gno b/x/faucet/faucet_test.gno similarity index 100% rename from faucet/faucet_test.gno rename to x/faucet/faucet_test.gno From 0c97c0eac6f079250005b877ec8505664cd3d42f Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 15 Mar 2024 13:48:46 +0900 Subject: [PATCH 06/11] rename --- {lib_package => _lib_package}/big/int256/gno.mod | 0 {lib_package => _lib_package}/big/int256/int256.gno | 0 {lib_package => _lib_package}/big/uint256/bits.gno | 0 {lib_package => _lib_package}/big/uint256/bits_errors.gno | 0 {lib_package => _lib_package}/big/uint256/bits_table.gno | 0 {lib_package => _lib_package}/big/uint256/consts.gno | 0 {lib_package => _lib_package}/big/uint256/gno.mod | 0 {lib_package => _lib_package}/big/uint256/mod.gno | 0 {lib_package => _lib_package}/big/uint256/strconv.gno | 0 {lib_package => _lib_package}/big/uint256/uint256.gno | 0 .../big/uint256/uint256_overflow_calculation.gno | 0 {lib_package => _lib_package}/json/LICENSE | 0 {lib_package => _lib_package}/json/README.md | 0 {lib_package => _lib_package}/json/buffer.gno | 0 {lib_package => _lib_package}/json/buffer_test.gno | 0 {lib_package => _lib_package}/json/decode.gno | 0 {lib_package => _lib_package}/json/decode_test.gno | 0 {lib_package => _lib_package}/json/eisel_lemire/eisel_lemire.gno | 0 {lib_package => _lib_package}/json/eisel_lemire/gno.mod | 0 {lib_package => _lib_package}/json/encode.gno | 0 {lib_package => _lib_package}/json/encode_test.gno | 0 {lib_package => _lib_package}/json/escape.gno | 0 {lib_package => _lib_package}/json/escape_test.gno | 0 {lib_package => _lib_package}/json/gno.mod | 0 {lib_package => _lib_package}/json/indent.gno | 0 {lib_package => _lib_package}/json/indent_test.gno | 0 {lib_package => _lib_package}/json/internal.gno | 0 {lib_package => _lib_package}/json/node.gno | 0 {lib_package => _lib_package}/json/node_test.gno | 0 {lib_package => _lib_package}/json/parser.gno | 0 {lib_package => _lib_package}/json/parser_test.gno | 0 {lib_package => _lib_package}/json/path.gno | 0 {lib_package => _lib_package}/json/path_test.gno | 0 {lib_package => _lib_package}/json/ryu/License | 0 {lib_package => _lib_package}/json/ryu/floatconv.gno | 0 {lib_package => _lib_package}/json/ryu/floatconv_test.gno | 0 {lib_package => _lib_package}/json/ryu/gno.mod | 0 {lib_package => _lib_package}/json/ryu/ryu64.gno | 0 {lib_package => _lib_package}/json/ryu/table.gno | 0 {lib_package => _lib_package}/json/token.gno | 0 .../gnoswap/common/allow_non_gnoswap_contracts.gno | 0 {lib_realm => _lib_realm}/gnoswap/common/liquidity_amounts.gno | 0 {lib_realm => _lib_realm}/gnoswap/common/tick_math.gno | 0 {lib_realm => _lib_realm}/gnoswap/consts/consts.gno | 0 44 files changed, 0 insertions(+), 0 deletions(-) rename {lib_package => _lib_package}/big/int256/gno.mod (100%) rename {lib_package => _lib_package}/big/int256/int256.gno (100%) rename {lib_package => _lib_package}/big/uint256/bits.gno (100%) rename {lib_package => _lib_package}/big/uint256/bits_errors.gno (100%) rename {lib_package => _lib_package}/big/uint256/bits_table.gno (100%) rename {lib_package => _lib_package}/big/uint256/consts.gno (100%) rename {lib_package => _lib_package}/big/uint256/gno.mod (100%) rename {lib_package => _lib_package}/big/uint256/mod.gno (100%) rename {lib_package => _lib_package}/big/uint256/strconv.gno (100%) rename {lib_package => _lib_package}/big/uint256/uint256.gno (100%) rename {lib_package => _lib_package}/big/uint256/uint256_overflow_calculation.gno (100%) rename {lib_package => _lib_package}/json/LICENSE (100%) rename {lib_package => _lib_package}/json/README.md (100%) rename {lib_package => _lib_package}/json/buffer.gno (100%) rename {lib_package => _lib_package}/json/buffer_test.gno (100%) rename {lib_package => _lib_package}/json/decode.gno (100%) rename {lib_package => _lib_package}/json/decode_test.gno (100%) rename {lib_package => _lib_package}/json/eisel_lemire/eisel_lemire.gno (100%) rename {lib_package => _lib_package}/json/eisel_lemire/gno.mod (100%) rename {lib_package => _lib_package}/json/encode.gno (100%) rename {lib_package => _lib_package}/json/encode_test.gno (100%) rename {lib_package => _lib_package}/json/escape.gno (100%) rename {lib_package => _lib_package}/json/escape_test.gno (100%) rename {lib_package => _lib_package}/json/gno.mod (100%) rename {lib_package => _lib_package}/json/indent.gno (100%) rename {lib_package => _lib_package}/json/indent_test.gno (100%) rename {lib_package => _lib_package}/json/internal.gno (100%) rename {lib_package => _lib_package}/json/node.gno (100%) rename {lib_package => _lib_package}/json/node_test.gno (100%) rename {lib_package => _lib_package}/json/parser.gno (100%) rename {lib_package => _lib_package}/json/parser_test.gno (100%) rename {lib_package => _lib_package}/json/path.gno (100%) rename {lib_package => _lib_package}/json/path_test.gno (100%) rename {lib_package => _lib_package}/json/ryu/License (100%) rename {lib_package => _lib_package}/json/ryu/floatconv.gno (100%) rename {lib_package => _lib_package}/json/ryu/floatconv_test.gno (100%) rename {lib_package => _lib_package}/json/ryu/gno.mod (100%) rename {lib_package => _lib_package}/json/ryu/ryu64.gno (100%) rename {lib_package => _lib_package}/json/ryu/table.gno (100%) rename {lib_package => _lib_package}/json/token.gno (100%) rename {lib_realm => _lib_realm}/gnoswap/common/allow_non_gnoswap_contracts.gno (100%) rename {lib_realm => _lib_realm}/gnoswap/common/liquidity_amounts.gno (100%) rename {lib_realm => _lib_realm}/gnoswap/common/tick_math.gno (100%) rename {lib_realm => _lib_realm}/gnoswap/consts/consts.gno (100%) diff --git a/lib_package/big/int256/gno.mod b/_lib_package/big/int256/gno.mod similarity index 100% rename from lib_package/big/int256/gno.mod rename to _lib_package/big/int256/gno.mod diff --git a/lib_package/big/int256/int256.gno b/_lib_package/big/int256/int256.gno similarity index 100% rename from lib_package/big/int256/int256.gno rename to _lib_package/big/int256/int256.gno diff --git a/lib_package/big/uint256/bits.gno b/_lib_package/big/uint256/bits.gno similarity index 100% rename from lib_package/big/uint256/bits.gno rename to _lib_package/big/uint256/bits.gno diff --git a/lib_package/big/uint256/bits_errors.gno b/_lib_package/big/uint256/bits_errors.gno similarity index 100% rename from lib_package/big/uint256/bits_errors.gno rename to _lib_package/big/uint256/bits_errors.gno diff --git a/lib_package/big/uint256/bits_table.gno b/_lib_package/big/uint256/bits_table.gno similarity index 100% rename from lib_package/big/uint256/bits_table.gno rename to _lib_package/big/uint256/bits_table.gno diff --git a/lib_package/big/uint256/consts.gno b/_lib_package/big/uint256/consts.gno similarity index 100% rename from lib_package/big/uint256/consts.gno rename to _lib_package/big/uint256/consts.gno diff --git a/lib_package/big/uint256/gno.mod b/_lib_package/big/uint256/gno.mod similarity index 100% rename from lib_package/big/uint256/gno.mod rename to _lib_package/big/uint256/gno.mod diff --git a/lib_package/big/uint256/mod.gno b/_lib_package/big/uint256/mod.gno similarity index 100% rename from lib_package/big/uint256/mod.gno rename to _lib_package/big/uint256/mod.gno diff --git a/lib_package/big/uint256/strconv.gno b/_lib_package/big/uint256/strconv.gno similarity index 100% rename from lib_package/big/uint256/strconv.gno rename to _lib_package/big/uint256/strconv.gno diff --git a/lib_package/big/uint256/uint256.gno b/_lib_package/big/uint256/uint256.gno similarity index 100% rename from lib_package/big/uint256/uint256.gno rename to _lib_package/big/uint256/uint256.gno diff --git a/lib_package/big/uint256/uint256_overflow_calculation.gno b/_lib_package/big/uint256/uint256_overflow_calculation.gno similarity index 100% rename from lib_package/big/uint256/uint256_overflow_calculation.gno rename to _lib_package/big/uint256/uint256_overflow_calculation.gno diff --git a/lib_package/json/LICENSE b/_lib_package/json/LICENSE similarity index 100% rename from lib_package/json/LICENSE rename to _lib_package/json/LICENSE diff --git a/lib_package/json/README.md b/_lib_package/json/README.md similarity index 100% rename from lib_package/json/README.md rename to _lib_package/json/README.md diff --git a/lib_package/json/buffer.gno b/_lib_package/json/buffer.gno similarity index 100% rename from lib_package/json/buffer.gno rename to _lib_package/json/buffer.gno diff --git a/lib_package/json/buffer_test.gno b/_lib_package/json/buffer_test.gno similarity index 100% rename from lib_package/json/buffer_test.gno rename to _lib_package/json/buffer_test.gno diff --git a/lib_package/json/decode.gno b/_lib_package/json/decode.gno similarity index 100% rename from lib_package/json/decode.gno rename to _lib_package/json/decode.gno diff --git a/lib_package/json/decode_test.gno b/_lib_package/json/decode_test.gno similarity index 100% rename from lib_package/json/decode_test.gno rename to _lib_package/json/decode_test.gno diff --git a/lib_package/json/eisel_lemire/eisel_lemire.gno b/_lib_package/json/eisel_lemire/eisel_lemire.gno similarity index 100% rename from lib_package/json/eisel_lemire/eisel_lemire.gno rename to _lib_package/json/eisel_lemire/eisel_lemire.gno diff --git a/lib_package/json/eisel_lemire/gno.mod b/_lib_package/json/eisel_lemire/gno.mod similarity index 100% rename from lib_package/json/eisel_lemire/gno.mod rename to _lib_package/json/eisel_lemire/gno.mod diff --git a/lib_package/json/encode.gno b/_lib_package/json/encode.gno similarity index 100% rename from lib_package/json/encode.gno rename to _lib_package/json/encode.gno diff --git a/lib_package/json/encode_test.gno b/_lib_package/json/encode_test.gno similarity index 100% rename from lib_package/json/encode_test.gno rename to _lib_package/json/encode_test.gno diff --git a/lib_package/json/escape.gno b/_lib_package/json/escape.gno similarity index 100% rename from lib_package/json/escape.gno rename to _lib_package/json/escape.gno diff --git a/lib_package/json/escape_test.gno b/_lib_package/json/escape_test.gno similarity index 100% rename from lib_package/json/escape_test.gno rename to _lib_package/json/escape_test.gno diff --git a/lib_package/json/gno.mod b/_lib_package/json/gno.mod similarity index 100% rename from lib_package/json/gno.mod rename to _lib_package/json/gno.mod diff --git a/lib_package/json/indent.gno b/_lib_package/json/indent.gno similarity index 100% rename from lib_package/json/indent.gno rename to _lib_package/json/indent.gno diff --git a/lib_package/json/indent_test.gno b/_lib_package/json/indent_test.gno similarity index 100% rename from lib_package/json/indent_test.gno rename to _lib_package/json/indent_test.gno diff --git a/lib_package/json/internal.gno b/_lib_package/json/internal.gno similarity index 100% rename from lib_package/json/internal.gno rename to _lib_package/json/internal.gno diff --git a/lib_package/json/node.gno b/_lib_package/json/node.gno similarity index 100% rename from lib_package/json/node.gno rename to _lib_package/json/node.gno diff --git a/lib_package/json/node_test.gno b/_lib_package/json/node_test.gno similarity index 100% rename from lib_package/json/node_test.gno rename to _lib_package/json/node_test.gno diff --git a/lib_package/json/parser.gno b/_lib_package/json/parser.gno similarity index 100% rename from lib_package/json/parser.gno rename to _lib_package/json/parser.gno diff --git a/lib_package/json/parser_test.gno b/_lib_package/json/parser_test.gno similarity index 100% rename from lib_package/json/parser_test.gno rename to _lib_package/json/parser_test.gno diff --git a/lib_package/json/path.gno b/_lib_package/json/path.gno similarity index 100% rename from lib_package/json/path.gno rename to _lib_package/json/path.gno diff --git a/lib_package/json/path_test.gno b/_lib_package/json/path_test.gno similarity index 100% rename from lib_package/json/path_test.gno rename to _lib_package/json/path_test.gno diff --git a/lib_package/json/ryu/License b/_lib_package/json/ryu/License similarity index 100% rename from lib_package/json/ryu/License rename to _lib_package/json/ryu/License diff --git a/lib_package/json/ryu/floatconv.gno b/_lib_package/json/ryu/floatconv.gno similarity index 100% rename from lib_package/json/ryu/floatconv.gno rename to _lib_package/json/ryu/floatconv.gno diff --git a/lib_package/json/ryu/floatconv_test.gno b/_lib_package/json/ryu/floatconv_test.gno similarity index 100% rename from lib_package/json/ryu/floatconv_test.gno rename to _lib_package/json/ryu/floatconv_test.gno diff --git a/lib_package/json/ryu/gno.mod b/_lib_package/json/ryu/gno.mod similarity index 100% rename from lib_package/json/ryu/gno.mod rename to _lib_package/json/ryu/gno.mod diff --git a/lib_package/json/ryu/ryu64.gno b/_lib_package/json/ryu/ryu64.gno similarity index 100% rename from lib_package/json/ryu/ryu64.gno rename to _lib_package/json/ryu/ryu64.gno diff --git a/lib_package/json/ryu/table.gno b/_lib_package/json/ryu/table.gno similarity index 100% rename from lib_package/json/ryu/table.gno rename to _lib_package/json/ryu/table.gno diff --git a/lib_package/json/token.gno b/_lib_package/json/token.gno similarity index 100% rename from lib_package/json/token.gno rename to _lib_package/json/token.gno diff --git a/lib_realm/gnoswap/common/allow_non_gnoswap_contracts.gno b/_lib_realm/gnoswap/common/allow_non_gnoswap_contracts.gno similarity index 100% rename from lib_realm/gnoswap/common/allow_non_gnoswap_contracts.gno rename to _lib_realm/gnoswap/common/allow_non_gnoswap_contracts.gno diff --git a/lib_realm/gnoswap/common/liquidity_amounts.gno b/_lib_realm/gnoswap/common/liquidity_amounts.gno similarity index 100% rename from lib_realm/gnoswap/common/liquidity_amounts.gno rename to _lib_realm/gnoswap/common/liquidity_amounts.gno diff --git a/lib_realm/gnoswap/common/tick_math.gno b/_lib_realm/gnoswap/common/tick_math.gno similarity index 100% rename from lib_realm/gnoswap/common/tick_math.gno rename to _lib_realm/gnoswap/common/tick_math.gno diff --git a/lib_realm/gnoswap/consts/consts.gno b/_lib_realm/gnoswap/consts/consts.gno similarity index 100% rename from lib_realm/gnoswap/consts/consts.gno rename to _lib_realm/gnoswap/consts/consts.gno From 4bd995572808581fa2122cced458ad9cb220d527 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 15 Mar 2024 13:49:58 +0900 Subject: [PATCH 07/11] GSW-920: allow/disallow flag for caller check --- pool/pool.gno | 40 ++++++++++--------------------- pool/pool_manager.gno | 53 ++++++++++++----------------------------- pool/withdrawal_fee.gno | 8 +++---- position/position.gno | 18 ++++++++------ router/router.gno | 7 +++++- staker/reward_fee.gno | 13 +++------- staker/staker.gno | 8 +++++-- 7 files changed, 57 insertions(+), 90 deletions(-) diff --git a/pool/pool.gno b/pool/pool.gno index 21ad5de2..47ad8152 100644 --- a/pool/pool.gno +++ b/pool/pool.gno @@ -3,10 +3,10 @@ package pool import ( "std" - "gno.land/p/demo/common" "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/common" + "gno.land/r/gnoswap/consts" gv "gno.land/r/demo/gov" @@ -17,7 +17,7 @@ import ( // Mint creates a new position and mints liquidity tokens then return amount0, amount1 in string // // Panics if any of the following conditions are met: -// - The caller is not the position contract +// - The caller is user // - Caller does not have enough token0 or token1 func Mint( token0Path string, @@ -28,10 +28,8 @@ func Mint( tickUpper int32, _liquidityAmount string, // uint128 ) (string, string) { // uint256 x2 - // only position contract can call this function - if std.PrevRealm().PkgPath() != consts.POSITION_PATH { - panic(ufmt.Sprintf("[POOL] pool.gno__Mint() || expected std.PrevRealm().PkgPath()(%s), got %s", consts.POSITION_PATH, std.PrevRealm().PkgPath())) - } + common.DisallowCallFromUser() + common.AllowCallFromOnly(consts.POSITION_PATH) liquidityAmount := u256.MustFromDecimal(_liquidityAmount) @@ -98,10 +96,8 @@ func Burn( tickUpper int32, _liquidityAmount string, // uint128 ) (string, string) { // uint256 x2 - // only position contract can call this function - if std.PrevRealm().PkgPath() != consts.POSITION_PATH { - panic(ufmt.Sprintf("[POOL] pool.gno__Burn() || expected std.PrevRealm().PkgPath()(%s), got %s", consts.POSITION_PATH, std.PrevRealm().PkgPath())) - } + common.DisallowCallFromUser() + common.AllowCallFromOnly(consts.POSITION_PATH) liquidityAmount := u256.MustFromDecimal(_liquidityAmount) @@ -144,10 +140,8 @@ func Collect( _amount0Requested string, // uint128 _amount1Requested string, // uint128 ) (string, string) { // uint128 x2 - // only position contract can call this function - if std.PrevRealm().PkgPath() != consts.POSITION_PATH { - panic(ufmt.Sprintf("[POOL] pool.gno__Collect() || expected std.PrevRealm().PkgPath()(%s), got %s", consts.POSITION_PATH, std.PrevRealm().PkgPath())) - } + common.DisallowCallFromUser() + common.AllowCallFromOnly(consts.POSITION_PATH) amount0Requested := u256.MustFromDecimal(_amount0Requested) amount1Requested := u256.MustFromDecimal(_amount1Requested) @@ -202,10 +196,8 @@ func Swap( _sqrtPriceLimitX96 string, // uint160 _payer string, // router ) (string, string) { // int256 x2 - // only router contract can call this function - if std.PrevRealm().PkgPath() != consts.ROUTER_PATH { - panic(ufmt.Sprintf("[POOL] pool.gno__Swap() || expected std.PrevRealm().PkgPath()(%s), got %s", consts.ROUTER_PATH, std.PrevRealm().PkgPath())) - } + common.DisallowCallFromUser() + common.AllowCallFromOnly(consts.ROUTER_PATH) if _amountSpecified == "0" { panic("[POOL] pool.gno__Swap() || _amountSpecified == 0") @@ -519,10 +511,7 @@ func SetFeeProtocol( feeProtocol0 uint8, feeProtocol1 uint8, ) { - - if !(isAdmin(std.PrevRealm().Addr())) { - panic(ufmt.Sprintf("[POOL] pool.gno__SetFeeProtocol() || caller(%s) must be admin", std.PrevRealm().Addr())) - } + common.MustCallFromAdmin() fee0Cond := (feeProtocol0 == 0 || (feeProtocol0 >= 4 && feeProtocol0 <= 10)) fee1Cond := (feeProtocol1 == 0 || (feeProtocol1 >= 4 && feeProtocol1 <= 10)) @@ -553,10 +542,7 @@ func CollectProtocol( _amount0Requested string, // uint128 _amount1Requested string, // uint128 ) (string, string) { // uint128 x2 - - if !(isAdmin(std.PrevRealm().Addr())) { - panic(ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || caller(%s) must be admin", std.PrevRealm().Addr())) - } + common.MustCallFromAdmin() recipient := std.Address(_recipient) diff --git a/pool/pool_manager.gno b/pool/pool_manager.gno index 975ebdcb..73f65bac 100644 --- a/pool/pool_manager.gno +++ b/pool/pool_manager.gno @@ -4,10 +4,10 @@ import ( "std" "strconv" - "gno.land/p/demo/common" "gno.land/p/demo/ufmt" + "gno.land/r/gnoswap/common" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/gns" @@ -29,13 +29,7 @@ var ( // - The caller is not an admin // - Pool contract has already been initialized func InitManual() { - // only user can call - std.AssertOriginCall() - - // only admin can call - if std.GetOrigCaller() != consts.GNOSWAP_ADMIN { - panic(ufmt.Sprintf("[POOl] pool_manager.gno__InitManual() || std.GetOrigCaller(%s) != consts.GNOSWAP_ADMIN(%s)", std.GetOrigCaller().String(), consts.GNOSWAP_ADMIN.String())) - } + common.MustCallFromAdmin() if initialized { panic(ufmt.Sprintf("[POOl] pool_manager.gno__InitManual() || initialized(%t) != false", initialized)) @@ -157,12 +151,9 @@ func GetPoolPath(token0Path, token1Path string, fee uint32) string { // Panics if any of the following conditions are met: // - The caller is not an admin func AddAdmin(addr string) { - caller := std.PrevRealm().Addr() - if isAdmin(caller) { - admins = append(admins, std.Address(addr)) - } else { - panic(ufmt.Sprintf("[POOL] pool_manager.gno__AddAdmin() || caller(%s) is not admin", caller.String())) - } + common.MustCallFromAdmin() + admins = append(admins, std.Address(addr)) + } // RemoveAdmin removes given address from the list of admins @@ -171,20 +162,17 @@ func AddAdmin(addr string) { // - The caller is not an admin // - The address is the last admin func RemoveAdmin(addr string) { - caller := std.PrevRealm().Addr() + common.MustCallFromAdmin() - if isAdmin(caller) { - if len(admins) == 1 { - panic("[POOL] pool_manager.gno__RemoveAdmin() || cannot remove last admin") - } - for i, v := range admins { - if v == std.Address(addr) { - admins = append(admins[:i], admins[i+1:]...) - return - } + if len(admins) == 1 { + panic("[POOL] pool_manager.gno__RemoveAdmin() || cannot remove last admin") + } + + for i, v := range admins { + if v == std.Address(addr) { + admins = append(admins[:i], admins[i+1:]...) + return } - } else { - panic(ufmt.Sprintf("[POOL] pool_manager.gno__RemoveAdmin() || caller(%s) is not admin", caller.String())) } } @@ -233,14 +221,3 @@ func newPool( return pool } - -func isAdmin(target std.Address) bool { - for _, admin := range admins { - if admin == target { - return true - } - } - - // target isn't one of admins - return false -} diff --git a/pool/withdrawal_fee.gno b/pool/withdrawal_fee.gno index 3db7fa42..c12272c7 100644 --- a/pool/withdrawal_fee.gno +++ b/pool/withdrawal_fee.gno @@ -3,7 +3,8 @@ package pool import ( "std" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/common" + "gno.land/r/gnoswap/consts" "gno.land/p/demo/ufmt" @@ -69,10 +70,7 @@ func SetWithdrawalFee(fee uint64) { // MUST BE ORIGIN CALL std.AssertOriginCall() - // MUST BE ADMIN - if !(isAdmin(std.GetOrigCaller())) { - panic(ufmt.Sprintf("[POOL] withdrawal_fee.gno__SetFeeProtocol() || caller(%s) must be admin", std.GetOrigCaller())) - } + common.MustCallFromAdmin() if fee > 100 { panic(ufmt.Sprintf("[POOL] withdrawal_fee.gno__SetFeeProtocol() || fee(%d) must be in range 0 ~ 100", fee)) diff --git a/position/position.gno b/position/position.gno index 6acac105..6e307f1e 100644 --- a/position/position.gno +++ b/position/position.gno @@ -8,7 +8,9 @@ import ( "gno.land/r/demo/wugnot" u256 "gno.land/p/big/uint256" - "gno.land/r/demo/consts" + + "gno.land/r/gnoswap/common" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" ) @@ -35,11 +37,14 @@ func Mint( deadline int64, mintTo string, ) (uint64, string, string, string) { // tokenId, liquidity, amount0, amount1 ( *u256.Uint x3 ) - // only user or staker can call - isUserCalled := std.IsOriginCall() - isStakerCalled := std.PrevRealm().Addr() == consts.STAKER_ADDR - if !(isUserCalled || isStakerCalled) { - panic("POSITION] position.gno__Mint() || only user or staker can call") + + if common.GetLimitCaller() { + // only user or staker can call + isUserCalled := std.IsOriginCall() + isStakerCalled := std.PrevRealm().Addr() == consts.STAKER_ADDR + if !(isUserCalled || isStakerCalled) { + panic("POSITION] position.gno__Mint() || only user or staker can call") + } } // check if wugnot is being minted @@ -184,7 +189,6 @@ func IncreaseLiquidity( _amount1Min string, // uint256 deadline int64, ) (uint64, string, string, string) { // tokenId, liquidity, amount0, amount1 - amount0Desired := u256.MustFromDecimal(_amount0Desired) amount1Desired := u256.MustFromDecimal(_amount1Desired) amount0Min := u256.MustFromDecimal(_amount0Min) diff --git a/router/router.gno b/router/router.gno index 350672c2..3fd634dd 100644 --- a/router/router.gno +++ b/router/router.gno @@ -7,7 +7,8 @@ import ( "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/common" + "gno.land/r/gnoswap/consts" i256 "gno.land/p/big/int256" u256 "gno.land/p/big/uint256" @@ -36,6 +37,10 @@ func SwapRoute( quoteArr string, // []int _tokenAmountLimit string, // uint256 ) string { // uint256 + if common.GetLimitCaller() { + std.AssertOriginCall() + } + amountSpecified, err := i256.FromDecimal(_amountSpecified) if err != nil { panic(ufmt.Sprintf("[ROUTER] router.gno__SwapRoute() || i256.FromDecimal(_amountSpecified(%s) failed", _amountSpecified)) diff --git a/staker/reward_fee.gno b/staker/reward_fee.gno index a752fa26..77ae01fc 100644 --- a/staker/reward_fee.gno +++ b/staker/reward_fee.gno @@ -1,9 +1,8 @@ package staker import ( - "std" - - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/common" + "gno.land/r/gnoswap/consts" "gno.land/p/demo/ufmt" @@ -42,13 +41,7 @@ func handleRewardFee( } func SetRewardFee(fee uint64) { - // MUST BE ORIGIN CALL - std.AssertOriginCall() - - // MUST BE ADMIN - if consts.GNOSWAP_ADMIN != std.GetOrigCaller() { - panic(ufmt.Sprintf("[STAKER] reward_fee.gno__SetFeeProtocol() || caller(%s) must be admin", std.GetOrigCaller())) - } + common.MustCallFromAdmin() if fee < 0 { panic(ufmt.Sprintf("[STAKER] reward_fee.gno__SetFeeProtocol() || fee(%d) must be >= 0", fee)) diff --git a/staker/staker.gno b/staker/staker.gno index baa08d9c..d8cdfe1a 100644 --- a/staker/staker.gno +++ b/staker/staker.gno @@ -4,13 +4,13 @@ import ( "std" "time" - "gno.land/p/demo/common" "gno.land/p/demo/ufmt" + "gno.land/r/gnoswap/common" "gno.land/r/demo/gnft" "gno.land/r/demo/gns" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" @@ -53,6 +53,10 @@ func CreateExternalIncentive( startTimestamp int64, endTimestamp int64, ) { + if common.GetLimitCaller() { + std.AssertOriginCall() + } + rewardAmount := u256.MustFromDecimal(_rewardAmount) if startTimestamp < time.Now().Unix() { From 733fc1dc37baa97fcccac5497c2f1655dd9666ba Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 15 Mar 2024 17:58:36 +0900 Subject: [PATCH 08/11] rename: import package path --- pool/_RPC_dry.gno | 4 ++-- pool/_TEST_0_INIT_TOKEN_REGISTER_test.gno | 2 +- pool/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno | 2 +- ..._swap_test.gno => _TEST_pool_dryswap_and_swap_test.gnoa} | 2 +- pool/_TEST_pool_multi_token_test.gnoa | 2 +- pool/_TEST_pool_native_swap_test.gnoa | 2 +- pool/_TEST_pool_single_lp_test.gnoa | 2 +- pool/_TEST_rpc_test.gnoa | 2 +- pool/bit_math.gno | 2 +- pool/emergency_halt.gno | 2 +- pool/gno.mod | 4 ++-- pool/gno_helper.gno | 2 +- pool/position.gno | 2 +- pool/position_modify.gno | 2 +- pool/sqrt_price_math.gno | 2 +- pool/tick.gno | 2 +- pool/tick_bitmap.gno | 2 +- pool/token_register.gno | 2 +- pool/utils.gno | 2 +- position/_RPC_api.gno | 4 ++-- position/_RPC_dry.gno | 2 +- position/_TEST_0_INIT_TOKEN_REGISTER_test.gno | 2 +- position/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno | 2 +- position/_TEST_position_api_test.gnoa | 2 +- position/_TEST_position_increase_decrease_test.gnoa | 4 ++-- position/_TEST_position_test.gnoa | 6 +++--- ...ST_position_test_two_position_used_single_swap_test.gnoa | 2 +- position/gno_helper.gno | 2 +- position/liquidity_management.gno | 4 ++-- position/nft_helper.gno | 2 +- position/sqrt_price_math.gno | 2 +- position/wrap_unwrap.gno | 2 +- router/_RPC_api.gno | 4 ++-- router/_TEST_0_INIT_TOKEN_REGISTER_test.gno | 2 +- router/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno | 2 +- router/_TEST_router_ratio_test.gnoa | 4 ++-- ...ST_router_swap_route_1route_1hop_native_in_out_test.gnoa | 2 +- .../_TEST_router_swap_route_1route_1hop_out_range_test.gnoa | 2 +- router/_TEST_router_swap_route_1route_1hop_test.gnoa | 2 +- ...r_swap_route_1route_2hop_wrapped_native_in_out_test.gnoa | 2 +- ...r_swap_route_1route_3hop_wrapped_native_middle_test.gnoa | 2 +- router/_TEST_router_swap_route_2route_2hop_test.gnoa | 2 +- ..._path_test.gno => _TEST_routes_find_swap_path_test.gnoa} | 4 ++-- router/gno_helper.gno | 2 +- router/swap_inner.gno | 2 +- router/swap_multi.gno | 2 +- router/token_register.gno | 2 +- router/wrap_unwrap.gno | 2 +- staker/_TEST_0_INIT_TOKEN_REGISTER_test.gno | 2 +- staker/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno | 2 +- staker/_TEST_staker_collect_reward_test.gnoa | 2 +- staker/_TEST_staker_get_test.gnoa | 2 +- staker/_TEST_staker_internal_external_test.gnoa | 2 +- staker/_TEST_staker_manage_pool_tiers_test.gnoa | 2 +- staker/_TEST_staker_mint_and_stake_test.gnoa | 2 +- staker/_TEST_staker_one_external_native_test.gnoa | 2 +- staker/_TEST_staker_one_external_test.gnoa | 2 +- staker/_TEST_staker_one_increase_external_test.gnoa | 2 +- ...aker_rpc_get_test.gno => _TEST_staker_rpc_get_test.gnoa} | 2 +- staker/gno.mod | 4 ++-- staker/gno_helper.gno | 2 +- staker/manage_pool_tiers.gno | 2 +- staker/mint_stake.gno | 2 +- staker/reward_math.gno | 2 +- staker/token_register.gno | 2 +- 65 files changed, 76 insertions(+), 76 deletions(-) rename pool/{_TEST_pool_dryswap_and_swap_test.gno => _TEST_pool_dryswap_and_swap_test.gnoa} (99%) rename router/{_TEST_routes_find_swap_path_test.gno => _TEST_routes_find_swap_path_test.gnoa} (98%) rename staker/{_TEST_staker_rpc_get_test.gno => _TEST_staker_rpc_get_test.gnoa} (99%) diff --git a/pool/_RPC_dry.gno b/pool/_RPC_dry.gno index eab2652b..cefdc5db 100644 --- a/pool/_RPC_dry.gno +++ b/pool/_RPC_dry.gno @@ -3,9 +3,9 @@ package pool import ( "std" - "gno.land/p/demo/common" + "gno.land/r/gnoswap/common" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" i256 "gno.land/p/big/int256" u256 "gno.land/p/big/uint256" diff --git a/pool/_TEST_0_INIT_TOKEN_REGISTER_test.gno b/pool/_TEST_0_INIT_TOKEN_REGISTER_test.gno index 87a7b1c2..49fad124 100644 --- a/pool/_TEST_0_INIT_TOKEN_REGISTER_test.gno +++ b/pool/_TEST_0_INIT_TOKEN_REGISTER_test.gno @@ -20,7 +20,7 @@ import ( "gno.land/r/demo/gns" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/users" ) diff --git a/pool/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno b/pool/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno index 8a4f3a9e..ae9c44dc 100644 --- a/pool/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno +++ b/pool/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno @@ -4,7 +4,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) var ( diff --git a/pool/_TEST_pool_dryswap_and_swap_test.gno b/pool/_TEST_pool_dryswap_and_swap_test.gnoa similarity index 99% rename from pool/_TEST_pool_dryswap_and_swap_test.gno rename to pool/_TEST_pool_dryswap_and_swap_test.gnoa index 3ee7856e..27adb3a0 100644 --- a/pool/_TEST_pool_dryswap_and_swap_test.gno +++ b/pool/_TEST_pool_dryswap_and_swap_test.gnoa @@ -4,7 +4,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/bar" "gno.land/r/demo/foo" diff --git a/pool/_TEST_pool_multi_token_test.gnoa b/pool/_TEST_pool_multi_token_test.gnoa index 9c2e271d..4aa9c137 100644 --- a/pool/_TEST_pool_multi_token_test.gnoa +++ b/pool/_TEST_pool_multi_token_test.gnoa @@ -5,7 +5,7 @@ import ( "strconv" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/bar" "gno.land/r/demo/baz" diff --git a/pool/_TEST_pool_native_swap_test.gnoa b/pool/_TEST_pool_native_swap_test.gnoa index d3895e71..48dc662f 100644 --- a/pool/_TEST_pool_native_swap_test.gnoa +++ b/pool/_TEST_pool_native_swap_test.gnoa @@ -4,7 +4,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/gns" diff --git a/pool/_TEST_pool_single_lp_test.gnoa b/pool/_TEST_pool_single_lp_test.gnoa index 79cc55bf..d9716c2a 100644 --- a/pool/_TEST_pool_single_lp_test.gnoa +++ b/pool/_TEST_pool_single_lp_test.gnoa @@ -10,7 +10,7 @@ import ( "gno.land/r/demo/foo" "gno.land/r/demo/gns" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" u256 "gno.land/p/big/uint256" ) diff --git a/pool/_TEST_rpc_test.gnoa b/pool/_TEST_rpc_test.gnoa index 32180faf..fd4f691a 100644 --- a/pool/_TEST_rpc_test.gnoa +++ b/pool/_TEST_rpc_test.gnoa @@ -6,7 +6,7 @@ import ( "encoding/gjson" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/bar" "gno.land/r/demo/baz" diff --git a/pool/bit_math.gno b/pool/bit_math.gno index 50e7e683..ceefe07a 100644 --- a/pool/bit_math.gno +++ b/pool/bit_math.gno @@ -1,7 +1,7 @@ package pool import ( - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" u256 "gno.land/p/big/uint256" ) diff --git a/pool/emergency_halt.gno b/pool/emergency_halt.gno index bc102e84..c1a8ad52 100644 --- a/pool/emergency_halt.gno +++ b/pool/emergency_halt.gno @@ -4,7 +4,7 @@ import ( "std" "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) var ( diff --git a/pool/gno.mod b/pool/gno.mod index a45f3e80..4048fe62 100644 --- a/pool/gno.mod +++ b/pool/gno.mod @@ -1,11 +1,11 @@ module gno.land/r/demo/pool require ( - gno.land/p/demo/common v0.0.0-latest + gno.land/r/gnoswap/common v0.0.0-latest gno.land/p/demo/ufmt v0.0.0-latest gno.land/r/demo/bar v0.0.0-latest gno.land/r/demo/baz v0.0.0-latest - gno.land/r/demo/consts v0.0.0-latest + gno.land/r/gnoswap/consts v0.0.0-latest gno.land/r/demo/foo v0.0.0-latest gno.land/r/demo/fred v0.0.0-latest gno.land/r/demo/gns v0.0.0-latest diff --git a/pool/gno_helper.gno b/pool/gno_helper.gno index ffc3c637..e85ba8b4 100644 --- a/pool/gno_helper.gno +++ b/pool/gno_helper.gno @@ -3,7 +3,7 @@ package pool import ( "std" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) func GetOrigPkgAddr() std.Address { diff --git a/pool/position.gno b/pool/position.gno index 4c0b9338..c13908c8 100644 --- a/pool/position.gno +++ b/pool/position.gno @@ -5,7 +5,7 @@ import ( "std" "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" i256 "gno.land/p/big/int256" u256 "gno.land/p/big/uint256" diff --git a/pool/position_modify.gno b/pool/position_modify.gno index eebdb482..5035f1cc 100644 --- a/pool/position_modify.gno +++ b/pool/position_modify.gno @@ -1,7 +1,7 @@ package pool import ( - "gno.land/p/demo/common" + "gno.land/r/gnoswap/common" i256 "gno.land/p/big/int256" ) diff --git a/pool/sqrt_price_math.gno b/pool/sqrt_price_math.gno index d5bf3005..ed4602ca 100644 --- a/pool/sqrt_price_math.gno +++ b/pool/sqrt_price_math.gno @@ -1,7 +1,7 @@ package pool import ( - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" i256 "gno.land/p/big/int256" u256 "gno.land/p/big/uint256" diff --git a/pool/tick.gno b/pool/tick.gno index 832e6571..5c9e567f 100644 --- a/pool/tick.gno +++ b/pool/tick.gno @@ -1,7 +1,7 @@ package pool import ( - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" i256 "gno.land/p/big/int256" u256 "gno.land/p/big/uint256" diff --git a/pool/tick_bitmap.gno b/pool/tick_bitmap.gno index 8ff8b306..68a6a66c 100644 --- a/pool/tick_bitmap.gno +++ b/pool/tick_bitmap.gno @@ -2,7 +2,7 @@ package pool import ( "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" u256 "gno.land/p/big/uint256" ) diff --git a/pool/token_register.gno b/pool/token_register.gno index 1b116823..d8f5906f 100644 --- a/pool/token_register.gno +++ b/pool/token_register.gno @@ -6,7 +6,7 @@ import ( "gno.land/p/demo/ufmt" "gno.land/r/demo/users" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) // GRC20Interface is the interface for GRC20 tokens diff --git a/pool/utils.gno b/pool/utils.gno index 6de8453b..7d6e4eb1 100644 --- a/pool/utils.gno +++ b/pool/utils.gno @@ -6,7 +6,7 @@ import ( "gno.land/p/demo/ufmt" "gno.land/r/demo/users" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" u256 "gno.land/p/big/uint256" ) diff --git a/position/_RPC_api.gno b/position/_RPC_api.gno index 5a1578fe..9cd0a657 100644 --- a/position/_RPC_api.gno +++ b/position/_RPC_api.gno @@ -5,10 +5,10 @@ import ( "std" "time" - "gno.land/p/demo/common" "gno.land/p/demo/ufmt" + "gno.land/r/gnoswap/common" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" diff --git a/position/_RPC_dry.gno b/position/_RPC_dry.gno index 36c7b1af..e31ae2c1 100644 --- a/position/_RPC_dry.gno +++ b/position/_RPC_dry.gno @@ -1,7 +1,7 @@ package position import ( - "gno.land/p/demo/common" + "gno.land/r/gnoswap/common" i256 "gno.land/p/big/int256" u256 "gno.land/p/big/uint256" diff --git a/position/_TEST_0_INIT_TOKEN_REGISTER_test.gno b/position/_TEST_0_INIT_TOKEN_REGISTER_test.gno index 4c4b0fd4..d75a9627 100644 --- a/position/_TEST_0_INIT_TOKEN_REGISTER_test.gno +++ b/position/_TEST_0_INIT_TOKEN_REGISTER_test.gno @@ -17,7 +17,7 @@ import ( "gno.land/r/demo/gns" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/users" diff --git a/position/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno b/position/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno index 892de222..31ae8d80 100644 --- a/position/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno +++ b/position/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno @@ -4,7 +4,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/gnft" diff --git a/position/_TEST_position_api_test.gnoa b/position/_TEST_position_api_test.gnoa index 2ab79145..ed34bf5b 100644 --- a/position/_TEST_position_api_test.gnoa +++ b/position/_TEST_position_api_test.gnoa @@ -6,7 +6,7 @@ import ( "encoding/gjson" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/bar" "gno.land/r/demo/foo" diff --git a/position/_TEST_position_increase_decrease_test.gnoa b/position/_TEST_position_increase_decrease_test.gnoa index f3d9098e..cbba3a51 100644 --- a/position/_TEST_position_increase_decrease_test.gnoa +++ b/position/_TEST_position_increase_decrease_test.gnoa @@ -4,9 +4,9 @@ import ( "std" "testing" - "gno.land/p/demo/common" + "gno.land/r/gnoswap/common" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/bar" "gno.land/r/demo/foo" diff --git a/position/_TEST_position_test.gnoa b/position/_TEST_position_test.gnoa index 4643ad2a..91ed00d4 100644 --- a/position/_TEST_position_test.gnoa +++ b/position/_TEST_position_test.gnoa @@ -5,7 +5,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" @@ -116,11 +116,11 @@ func TestSwap(t *testing.T) { barPath, fooPath, fee500, - test1, + test1.String(), true, "1234567", consts.MIN_PRICE, - test1, + test1.String(), ) shouldEQ(t, amount0, "1234567") shouldEQ(t, amount1, "-3332779") diff --git a/position/_TEST_position_test_two_position_used_single_swap_test.gnoa b/position/_TEST_position_test_two_position_used_single_swap_test.gnoa index 1b2c57d8..207c5e10 100644 --- a/position/_TEST_position_test_two_position_used_single_swap_test.gnoa +++ b/position/_TEST_position_test_two_position_used_single_swap_test.gnoa @@ -4,7 +4,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" diff --git a/position/gno_helper.gno b/position/gno_helper.gno index 3b94ea16..75fd55b5 100644 --- a/position/gno_helper.gno +++ b/position/gno_helper.gno @@ -3,7 +3,7 @@ package position import ( "std" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) func GetOrigPkgAddr() std.Address { diff --git a/position/liquidity_management.gno b/position/liquidity_management.gno index 967d941e..71452e20 100644 --- a/position/liquidity_management.gno +++ b/position/liquidity_management.gno @@ -1,9 +1,9 @@ package position import ( - "gno.land/p/demo/common" + "gno.land/r/gnoswap/common" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" diff --git a/position/nft_helper.gno b/position/nft_helper.gno index 3e29624a..4e6719d7 100644 --- a/position/nft_helper.gno +++ b/position/nft_helper.gno @@ -4,7 +4,7 @@ import ( "std" "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/gnft" ) diff --git a/position/sqrt_price_math.gno b/position/sqrt_price_math.gno index 8ccf6415..c6420634 100644 --- a/position/sqrt_price_math.gno +++ b/position/sqrt_price_math.gno @@ -1,7 +1,7 @@ package position import ( - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" i256 "gno.land/p/big/int256" u256 "gno.land/p/big/uint256" diff --git a/position/wrap_unwrap.gno b/position/wrap_unwrap.gno index 98a8fc22..8bdd4f5e 100644 --- a/position/wrap_unwrap.gno +++ b/position/wrap_unwrap.gno @@ -6,7 +6,7 @@ import ( "gno.land/r/demo/wugnot" "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) func wrap(ugnotAmount uint64) { diff --git a/router/_RPC_api.gno b/router/_RPC_api.gno index 5e09bb93..e8a3144b 100644 --- a/router/_RPC_api.gno +++ b/router/_RPC_api.gno @@ -5,11 +5,11 @@ import ( "std" "time" - "gno.land/p/demo/common" "gno.land/p/demo/ufmt" + "gno.land/r/gnoswap/common" - "gno.land/r/demo/consts" pl "gno.land/r/demo/pool" + "gno.land/r/gnoswap/consts" u256 "gno.land/p/big/uint256" ) diff --git a/router/_TEST_0_INIT_TOKEN_REGISTER_test.gno b/router/_TEST_0_INIT_TOKEN_REGISTER_test.gno index 74590ea2..abb6c19c 100644 --- a/router/_TEST_0_INIT_TOKEN_REGISTER_test.gno +++ b/router/_TEST_0_INIT_TOKEN_REGISTER_test.gno @@ -17,7 +17,7 @@ import ( "gno.land/r/demo/gns" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/users" diff --git a/router/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno b/router/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno index 937b4832..f9d41ba8 100644 --- a/router/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno +++ b/router/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno @@ -4,7 +4,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) var ( diff --git a/router/_TEST_router_ratio_test.gnoa b/router/_TEST_router_ratio_test.gnoa index db8c1fc3..555dae24 100644 --- a/router/_TEST_router_ratio_test.gnoa +++ b/router/_TEST_router_ratio_test.gnoa @@ -8,8 +8,8 @@ import ( pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" - "gno.land/p/demo/common" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/common" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/bar" "gno.land/r/demo/foo" diff --git a/router/_TEST_router_swap_route_1route_1hop_native_in_out_test.gnoa b/router/_TEST_router_swap_route_1route_1hop_native_in_out_test.gnoa index c7b4b58e..07c9c32d 100644 --- a/router/_TEST_router_swap_route_1route_1hop_native_in_out_test.gnoa +++ b/router/_TEST_router_swap_route_1route_1hop_native_in_out_test.gnoa @@ -5,7 +5,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" diff --git a/router/_TEST_router_swap_route_1route_1hop_out_range_test.gnoa b/router/_TEST_router_swap_route_1route_1hop_out_range_test.gnoa index 94f4713d..f45df726 100644 --- a/router/_TEST_router_swap_route_1route_1hop_out_range_test.gnoa +++ b/router/_TEST_router_swap_route_1route_1hop_out_range_test.gnoa @@ -5,7 +5,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" diff --git a/router/_TEST_router_swap_route_1route_1hop_test.gnoa b/router/_TEST_router_swap_route_1route_1hop_test.gnoa index 3fc60203..83d180ba 100644 --- a/router/_TEST_router_swap_route_1route_1hop_test.gnoa +++ b/router/_TEST_router_swap_route_1route_1hop_test.gnoa @@ -5,7 +5,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" diff --git a/router/_TEST_router_swap_route_1route_2hop_wrapped_native_in_out_test.gnoa b/router/_TEST_router_swap_route_1route_2hop_wrapped_native_in_out_test.gnoa index c7b4b58e..07c9c32d 100644 --- a/router/_TEST_router_swap_route_1route_2hop_wrapped_native_in_out_test.gnoa +++ b/router/_TEST_router_swap_route_1route_2hop_wrapped_native_in_out_test.gnoa @@ -5,7 +5,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" diff --git a/router/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gnoa b/router/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gnoa index f1a2df6c..9049e129 100644 --- a/router/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gnoa +++ b/router/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gnoa @@ -5,7 +5,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" diff --git a/router/_TEST_router_swap_route_2route_2hop_test.gnoa b/router/_TEST_router_swap_route_2route_2hop_test.gnoa index f4cb0a81..c81b15b5 100644 --- a/router/_TEST_router_swap_route_2route_2hop_test.gnoa +++ b/router/_TEST_router_swap_route_2route_2hop_test.gnoa @@ -5,7 +5,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" diff --git a/router/_TEST_routes_find_swap_path_test.gno b/router/_TEST_routes_find_swap_path_test.gnoa similarity index 98% rename from router/_TEST_routes_find_swap_path_test.gno rename to router/_TEST_routes_find_swap_path_test.gnoa index 3dbc29c4..4c27db63 100644 --- a/router/_TEST_routes_find_swap_path_test.gno +++ b/router/_TEST_routes_find_swap_path_test.gnoa @@ -4,8 +4,8 @@ import ( "std" "testing" - "gno.land/p/demo/common" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/common" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" diff --git a/router/gno_helper.gno b/router/gno_helper.gno index 25d34e5c..1e045bec 100644 --- a/router/gno_helper.gno +++ b/router/gno_helper.gno @@ -3,7 +3,7 @@ package router import ( "std" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) func GetOrigPkgAddr() std.Address { diff --git a/router/swap_inner.gno b/router/swap_inner.gno index 5282e434..d8586eb4 100644 --- a/router/swap_inner.gno +++ b/router/swap_inner.gno @@ -7,7 +7,7 @@ import ( pl "gno.land/r/demo/pool" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" i256 "gno.land/p/big/int256" u256 "gno.land/p/big/uint256" diff --git a/router/swap_multi.gno b/router/swap_multi.gno index d11859b6..c5de3aff 100644 --- a/router/swap_multi.gno +++ b/router/swap_multi.gno @@ -3,7 +3,7 @@ package router import ( "std" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" i256 "gno.land/p/big/int256" u256 "gno.land/p/big/uint256" diff --git a/router/token_register.gno b/router/token_register.gno index 8f448677..e08f6243 100644 --- a/router/token_register.gno +++ b/router/token_register.gno @@ -6,7 +6,7 @@ import ( "gno.land/p/demo/ufmt" "gno.land/r/demo/users" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) type GRC20Interface interface { diff --git a/router/wrap_unwrap.gno b/router/wrap_unwrap.gno index 7f994e43..b766ffa9 100644 --- a/router/wrap_unwrap.gno +++ b/router/wrap_unwrap.gno @@ -6,7 +6,7 @@ import ( "gno.land/r/demo/wugnot" "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) func wrap(ugnotAmount uint64) { diff --git a/staker/_TEST_0_INIT_TOKEN_REGISTER_test.gno b/staker/_TEST_0_INIT_TOKEN_REGISTER_test.gno index 2b05ea06..661051e3 100644 --- a/staker/_TEST_0_INIT_TOKEN_REGISTER_test.gno +++ b/staker/_TEST_0_INIT_TOKEN_REGISTER_test.gno @@ -17,7 +17,7 @@ import ( "gno.land/r/demo/gns" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/users" diff --git a/staker/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno b/staker/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno index e3fc2639..479b8f84 100644 --- a/staker/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno +++ b/staker/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno @@ -4,7 +4,7 @@ import ( "std" "testing" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) var ( diff --git a/staker/_TEST_staker_collect_reward_test.gnoa b/staker/_TEST_staker_collect_reward_test.gnoa index b28fd275..11c356de 100644 --- a/staker/_TEST_staker_collect_reward_test.gnoa +++ b/staker/_TEST_staker_collect_reward_test.gnoa @@ -5,7 +5,7 @@ import ( "testing" "time" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" diff --git a/staker/_TEST_staker_get_test.gnoa b/staker/_TEST_staker_get_test.gnoa index e0232ea9..3b1bd670 100644 --- a/staker/_TEST_staker_get_test.gnoa +++ b/staker/_TEST_staker_get_test.gnoa @@ -5,7 +5,7 @@ import ( "testing" "time" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" diff --git a/staker/_TEST_staker_internal_external_test.gnoa b/staker/_TEST_staker_internal_external_test.gnoa index 2916ae28..6885add8 100644 --- a/staker/_TEST_staker_internal_external_test.gnoa +++ b/staker/_TEST_staker_internal_external_test.gnoa @@ -5,7 +5,7 @@ import ( "testing" "time" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" diff --git a/staker/_TEST_staker_manage_pool_tiers_test.gnoa b/staker/_TEST_staker_manage_pool_tiers_test.gnoa index 11a6b805..da1559c9 100644 --- a/staker/_TEST_staker_manage_pool_tiers_test.gnoa +++ b/staker/_TEST_staker_manage_pool_tiers_test.gnoa @@ -6,7 +6,7 @@ import ( pl "gno.land/r/demo/pool" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/r/demo/gns" ) diff --git a/staker/_TEST_staker_mint_and_stake_test.gnoa b/staker/_TEST_staker_mint_and_stake_test.gnoa index d1520fc2..5cba7edc 100644 --- a/staker/_TEST_staker_mint_and_stake_test.gnoa +++ b/staker/_TEST_staker_mint_and_stake_test.gnoa @@ -12,7 +12,7 @@ import ( "gno.land/r/demo/gns" "gno.land/r/demo/qux" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) func init() { diff --git a/staker/_TEST_staker_one_external_native_test.gnoa b/staker/_TEST_staker_one_external_native_test.gnoa index 7a5afb9a..57ecb316 100644 --- a/staker/_TEST_staker_one_external_native_test.gnoa +++ b/staker/_TEST_staker_one_external_native_test.gnoa @@ -15,7 +15,7 @@ import ( "gno.land/r/demo/gnft" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) func init() { diff --git a/staker/_TEST_staker_one_external_test.gnoa b/staker/_TEST_staker_one_external_test.gnoa index be09d938..fa9f9e1e 100644 --- a/staker/_TEST_staker_one_external_test.gnoa +++ b/staker/_TEST_staker_one_external_test.gnoa @@ -17,7 +17,7 @@ import ( "gno.land/r/demo/gnft" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) func init() { diff --git a/staker/_TEST_staker_one_increase_external_test.gnoa b/staker/_TEST_staker_one_increase_external_test.gnoa index 4ec96c9c..a64700ac 100644 --- a/staker/_TEST_staker_one_increase_external_test.gnoa +++ b/staker/_TEST_staker_one_increase_external_test.gnoa @@ -17,7 +17,7 @@ import ( "gno.land/r/demo/gnft" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) func init() { diff --git a/staker/_TEST_staker_rpc_get_test.gno b/staker/_TEST_staker_rpc_get_test.gnoa similarity index 99% rename from staker/_TEST_staker_rpc_get_test.gno rename to staker/_TEST_staker_rpc_get_test.gnoa index fb70ccea..458f6e90 100644 --- a/staker/_TEST_staker_rpc_get_test.gno +++ b/staker/_TEST_staker_rpc_get_test.gnoa @@ -17,7 +17,7 @@ import ( "gno.land/r/demo/gnft" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) func init() { diff --git a/staker/gno.mod b/staker/gno.mod index de893116..e457518a 100644 --- a/staker/gno.mod +++ b/staker/gno.mod @@ -1,12 +1,12 @@ module gno.land/r/demo/staker require ( - gno.land/p/demo/common v0.0.0-latest + gno.land/r/gnoswap/common v0.0.0-latest gno.land/p/demo/grc/grc721 v0.0.0-latest gno.land/p/demo/ufmt v0.0.0-latest gno.land/r/demo/bar v0.0.0-latest gno.land/r/demo/baz v0.0.0-latest - gno.land/r/demo/consts v0.0.0-latest + gno.land/r/gnoswap/consts v0.0.0-latest gno.land/r/demo/foo v0.0.0-latest gno.land/r/demo/fred v0.0.0-latest gno.land/r/demo/gnft v0.0.0-latest diff --git a/staker/gno_helper.gno b/staker/gno_helper.gno index 9e3aa049..ff807a00 100644 --- a/staker/gno_helper.gno +++ b/staker/gno_helper.gno @@ -3,7 +3,7 @@ package staker import ( "std" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) func GetOrigPkgAddr() std.Address { diff --git a/staker/manage_pool_tiers.gno b/staker/manage_pool_tiers.gno index 28dbd51a..b9e22ab6 100644 --- a/staker/manage_pool_tiers.gno +++ b/staker/manage_pool_tiers.gno @@ -7,7 +7,7 @@ import ( "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) // GetPoolWithTiers returns list of pools with their tiers diff --git a/staker/mint_stake.gno b/staker/mint_stake.gno index 4f37e914..f1885602 100644 --- a/staker/mint_stake.gno +++ b/staker/mint_stake.gno @@ -3,7 +3,7 @@ package staker import ( pn "gno.land/r/demo/position" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) // MintAndStake mints and stakes the LP token diff --git a/staker/reward_math.gno b/staker/reward_math.gno index 2f8ea43e..fa8effc0 100644 --- a/staker/reward_math.gno +++ b/staker/reward_math.gno @@ -4,7 +4,7 @@ import ( "std" "time" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" "gno.land/p/demo/ufmt" diff --git a/staker/token_register.gno b/staker/token_register.gno index b461f1d2..4c7eb595 100644 --- a/staker/token_register.gno +++ b/staker/token_register.gno @@ -6,7 +6,7 @@ import ( "gno.land/p/demo/ufmt" "gno.land/r/demo/users" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) type GRC20Interface interface { From 4fc85b8f8c566edb030cf2a8d70fb28aee515ff9 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 15 Mar 2024 18:19:59 +0900 Subject: [PATCH 09/11] chore: change lib path --- _setup/gns/gns.gno | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_setup/gns/gns.gno b/_setup/gns/gns.gno index c085bdf1..577b73a6 100644 --- a/_setup/gns/gns.gno +++ b/_setup/gns/gns.gno @@ -8,7 +8,7 @@ import ( "gno.land/p/demo/ufmt" "gno.land/r/demo/users" - "gno.land/r/demo/consts" + "gno.land/r/gnoswap/consts" ) const MAXIMUM_SUPPLY = uint64(1_000_000_000_000_000) From fc51ef6c535ba9644b2b73b873b82d20e5a7bbbd Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 15 Mar 2024 18:20:07 +0900 Subject: [PATCH 10/11] fix: type for primitive --- pool/_TEST_rpc_test.gnoa | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pool/_TEST_rpc_test.gnoa b/pool/_TEST_rpc_test.gnoa index fd4f691a..3bf92c59 100644 --- a/pool/_TEST_rpc_test.gnoa +++ b/pool/_TEST_rpc_test.gnoa @@ -62,7 +62,7 @@ func TestMintFooBarLiquidity(t *testing.T) { barPath, fooPath, fee500, - consts.POSITION_ADDR, + consts.POSITION_ADDR.String(), -11000, // -11000 -9000, // -9000 test_liquidityExpectStr, @@ -85,7 +85,7 @@ func TestMintBarBazLiquidity(t *testing.T) { barPath, bazPath, fee500, - consts.POSITION_ADDR, + consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr, From 7ef5241f1d962daf3125008f9206a442d37b2adf Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 15 Mar 2024 18:20:38 +0900 Subject: [PATCH 11/11] chore: extract common calc --- pool/sqrt_price_math.gno | 38 +++++++++++++++++------------------- position/sqrt_price_math.gno | 38 +++++++++++++++++------------------- 2 files changed, 36 insertions(+), 40 deletions(-) diff --git a/pool/sqrt_price_math.gno b/pool/sqrt_price_math.gno index ed4602ca..7d02baa9 100644 --- a/pool/sqrt_price_math.gno +++ b/pool/sqrt_price_math.gno @@ -18,9 +18,9 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp( } numerator1 := new(u256.Uint).Lsh(liquidity, 96) + product := new(u256.Uint).Mul(amount, sqrtPX96) if add { - product := new(u256.Uint).Mul(amount, sqrtPX96) if new(u256.Uint).Div(product, amount).Eq(sqrtPX96) { denominator := new(u256.Uint).Add(numerator1, product) @@ -33,8 +33,6 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp( _value2 := new(u256.Uint).Add(_value1, amount) return u256.DivRoundingUp(numerator1, _value2) } else { - product := new(u256.Uint).Mul(amount, sqrtPX96) - cond1 := new(u256.Uint).Div(product, amount).Eq(sqrtPX96) cond2 := numerator1.Gt(product) @@ -54,22 +52,23 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown( add bool, ) *u256.Uint { // uint160 quotient := u256.Zero() + + if !(amount.Lte(u256.MustFromDecimal(consts.MAX_UINT160))) { + quotient = u256.MulDiv(amount, u256.MustFromDecimal(consts.Q96), liquidity) + } + if add { if amount.Lte(u256.MustFromDecimal(consts.MAX_UINT160)) { quotient = new(u256.Uint).Lsh(amount, 96) quotient = new(u256.Uint).Div(quotient, liquidity) - } else { - quotient = u256.MulDiv(amount, u256.MustFromDecimal(consts.Q96), liquidity) } quotient = new(u256.Uint).Sub(quotient, u256.One()) return new(u256.Uint).Add(sqrtPX96, quotient) } else { if amount.Lte(u256.MustFromDecimal(consts.MAX_UINT160)) { - value1 := new(u256.Uint).Lsh(amount, 96) - quotient = u256.DivRoundingUp(value1, liquidity) - } else { - quotient = u256.MulDiv(amount, u256.MustFromDecimal(consts.Q96), liquidity) + quotient = new(u256.Uint).Lsh(amount, 96) + quotient = u256.DivRoundingUp(quotient, liquidity) } if !(sqrtPX96.Gt(quotient)) { @@ -117,19 +116,18 @@ func sqrtPriceMathGetAmount0DeltaHelper( sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96 } - numerator1 := new(u256.Uint).Lsh(liquidity, 96) - numerator2 := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96) - if !(sqrtRatioAX96.Gt(u256.Zero())) { panic("pool_sqrt price math #3") } + numerator1 := new(u256.Uint).Lsh(liquidity, 96) + numerator2 := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96) + value := u256.MulDiv(numerator1, numerator2, sqrtRatioBX96) + if roundUp { - value1 := u256.MulDiv(numerator1, numerator2, sqrtRatioBX96) - return u256.DivRoundingUp(value1, sqrtRatioAX96) + return u256.DivRoundingUp(value, sqrtRatioAX96) } else { - value1 := u256.MulDiv(numerator1, numerator2, sqrtRatioBX96) - return new(u256.Uint).Div(value1, sqrtRatioAX96) + return new(u256.Uint).Div(value, sqrtRatioAX96) } } @@ -144,12 +142,12 @@ func sqrtPriceMathGetAmount1DeltaHelper( sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96 } + value := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96) + if roundUp { - value1 := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96) - return u256.MulDiv(liquidity, value1, u256.MustFromDecimal(consts.Q96)) + return u256.MulDiv(liquidity, value, u256.MustFromDecimal(consts.Q96)) } else { - value1 := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96) - return u256.MulDiv(liquidity, value1, u256.MustFromDecimal(consts.Q96)) + return u256.MulDiv(liquidity, value, u256.MustFromDecimal(consts.Q96)) } } diff --git a/position/sqrt_price_math.gno b/position/sqrt_price_math.gno index c6420634..85aa1d11 100644 --- a/position/sqrt_price_math.gno +++ b/position/sqrt_price_math.gno @@ -18,9 +18,9 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp( } numerator1 := new(u256.Uint).Lsh(liquidity, 96) + product := new(u256.Uint).Mul(amount, sqrtPX96) if add { - product := new(u256.Uint).Mul(amount, sqrtPX96) if new(u256.Uint).Div(product, amount).Eq(sqrtPX96) { denominator := new(u256.Uint).Add(numerator1, product) @@ -33,8 +33,6 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount0RoundingUp( _value2 := new(u256.Uint).Add(_value1, amount) return u256.DivRoundingUp(numerator1, _value2) } else { - product := new(u256.Uint).Mul(amount, sqrtPX96) - cond1 := new(u256.Uint).Div(product, amount).Eq(sqrtPX96) cond2 := numerator1.Gt(product) @@ -54,22 +52,23 @@ func sqrtPriceMathGetNextSqrtPriceFromAmount1RoundingDown( add bool, ) *u256.Uint { // uint160 quotient := u256.Zero() + + if !(amount.Lte(u256.MustFromDecimal(consts.MAX_UINT160))) { + quotient = u256.MulDiv(amount, u256.MustFromDecimal(consts.Q96), liquidity) + } + if add { if amount.Lte(u256.MustFromDecimal(consts.MAX_UINT160)) { quotient = new(u256.Uint).Lsh(amount, 96) quotient = new(u256.Uint).Div(quotient, liquidity) - } else { - quotient = u256.MulDiv(amount, u256.MustFromDecimal(consts.Q96), liquidity) } quotient = new(u256.Uint).Sub(quotient, u256.One()) return new(u256.Uint).Add(sqrtPX96, quotient) } else { if amount.Lte(u256.MustFromDecimal(consts.MAX_UINT160)) { - value1 := new(u256.Uint).Lsh(amount, 96) - quotient = u256.DivRoundingUp(value1, liquidity) - } else { - quotient = u256.MulDiv(amount, u256.MustFromDecimal(consts.Q96), liquidity) + quotient = new(u256.Uint).Lsh(amount, 96) + quotient = u256.DivRoundingUp(quotient, liquidity) } if !(sqrtPX96.Gt(quotient)) { @@ -117,19 +116,18 @@ func sqrtPriceMathGetAmount0DeltaHelper( sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96 } - numerator1 := new(u256.Uint).Lsh(liquidity, 96) - numerator2 := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96) - if !(sqrtRatioAX96.Gt(u256.Zero())) { panic("position_sqrt price math #3") } + numerator1 := new(u256.Uint).Lsh(liquidity, 96) + numerator2 := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96) + value := u256.MulDiv(numerator1, numerator2, sqrtRatioBX96) + if roundUp { - value1 := u256.MulDiv(numerator1, numerator2, sqrtRatioBX96) - return u256.DivRoundingUp(value1, sqrtRatioAX96) + return u256.DivRoundingUp(value, sqrtRatioAX96) } else { - value1 := u256.MulDiv(numerator1, numerator2, sqrtRatioBX96) - return new(u256.Uint).Div(value1, sqrtRatioAX96) + return new(u256.Uint).Div(value, sqrtRatioAX96) } } @@ -144,12 +142,12 @@ func sqrtPriceMathGetAmount1DeltaHelper( sqrtRatioAX96, sqrtRatioBX96 = sqrtRatioBX96, sqrtRatioAX96 } + value := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96) + if roundUp { - value1 := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96) - return u256.MulDiv(liquidity, value1, u256.MustFromDecimal(consts.Q96)) + return u256.MulDiv(liquidity, value, u256.MustFromDecimal(consts.Q96)) } else { - value1 := new(u256.Uint).Sub(sqrtRatioBX96, sqrtRatioAX96) - return u256.MulDiv(liquidity, value1, u256.MustFromDecimal(consts.Q96)) + return u256.MulDiv(liquidity, value, u256.MustFromDecimal(consts.Q96)) } }