|
| 1 | +package pool |
| 2 | + |
| 3 | +import ( |
| 4 | + "std" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "gno.land/p/demo/testutils" |
| 8 | + "gno.land/p/demo/uassert" |
| 9 | + |
| 10 | + i256 "gno.land/p/gnoswap/int256" |
| 11 | + u256 "gno.land/p/gnoswap/uint256" |
| 12 | + |
| 13 | + "gno.land/r/gnoswap/v1/common" |
| 14 | +) |
| 15 | + |
| 16 | +func TestPositionGetKey(t *testing.T) { |
| 17 | + invalidAddr := std.Address("invalidAddr") |
| 18 | + validAddr := testutils.TestAddress("validAddr") |
| 19 | + |
| 20 | + tests := []struct { |
| 21 | + owner std.Address |
| 22 | + tickLower int32 |
| 23 | + tickUpper int32 |
| 24 | + shouldPanic bool |
| 25 | + panicMsg string |
| 26 | + expectedKey string |
| 27 | + }{ |
| 28 | + {invalidAddr, 100, 200, true, `[GNOSWAP-POOL-023] invalid address || position.gno__positionGetKey() || invalid owner address invalidAddr`, ""}, // invalid address |
| 29 | + {validAddr, 200, 100, true, `[GNOSWAP-POOL-024] tickLower is greater than tickUpper || position.gno__positionGetKey() || tickLower(200) is greater than tickUpper(100)`, ""}, // tickLower > tickUpper |
| 30 | + {validAddr, -100, -200, true, `[GNOSWAP-POOL-024] tickLower is greater than tickUpper || position.gno__positionGetKey() || tickLower(-100) is greater than tickUpper(-200)`, ""}, // tickLower > tickUpper |
| 31 | + {validAddr, 100, 100, false, "", "ZzF3ZXNrYzZ0eWc5anhndWpsdGEwNDdoNmx0YTA0N2g2bGRqbHVkdV9fMTAwX18xMDA="}, // tickLower == tickUpper |
| 32 | + {validAddr, 100, 200, false, "", "ZzF3ZXNrYzZ0eWc5anhndWpsdGEwNDdoNmx0YTA0N2g2bGRqbHVkdV9fMTAwX18yMDA="}, // tickLower < tickUpper |
| 33 | + } |
| 34 | + |
| 35 | + for _, tc := range tests { |
| 36 | + if tc.shouldPanic { |
| 37 | + uassert.PanicsWithMessage(t, tc.panicMsg, func() { positionGetKey(tc.owner, tc.tickLower, tc.tickUpper) }) |
| 38 | + } else { |
| 39 | + key := positionGetKey(tc.owner, tc.tickLower, tc.tickUpper) |
| 40 | + uassert.Equal(t, tc.expectedKey, key) |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestPositionUpdateWithKey(t *testing.T) { |
| 46 | + var dummyPool *Pool |
| 47 | + var positionKey string |
| 48 | + |
| 49 | + t.Run("set up initial data for this test function", func(t *testing.T) { |
| 50 | + dummyPool = newPool( |
| 51 | + "token0", |
| 52 | + "token1", |
| 53 | + 100, |
| 54 | + 10, |
| 55 | + common.TickMathGetSqrtRatioAtTick(0), |
| 56 | + ) |
| 57 | + |
| 58 | + positionKey = positionGetKey( |
| 59 | + testutils.TestAddress("dummyAddr"), |
| 60 | + 100, |
| 61 | + 200, |
| 62 | + ) |
| 63 | + }) |
| 64 | + |
| 65 | + tests := []struct { |
| 66 | + liquidity *i256.Int |
| 67 | + amount0 *u256.Uint |
| 68 | + amount1 *u256.Uint |
| 69 | + shouldPanic bool |
| 70 | + panicMsg string |
| 71 | + expectedLiquidity string |
| 72 | + }{ |
| 73 | + {i256.MustFromDecimal("0"), u256.Zero(), u256.Zero(), true, `[GNOSWAP-POOL-010] zero liquidity || position.gno__positionUpdate() || both liquidityDelta and current position's liquidity are zero`, ""}, |
| 74 | + {i256.MustFromDecimal("100000"), u256.Zero(), u256.Zero(), false, "", "100000"}, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tc := range tests { |
| 78 | + if tc.shouldPanic { |
| 79 | + uassert.PanicsWithMessage(t, tc.panicMsg, func() { dummyPool.positionUpdateWithKey(positionKey, tc.liquidity, tc.amount0, tc.amount1) }) |
| 80 | + } else { |
| 81 | + newPos := dummyPool.positionUpdateWithKey(positionKey, tc.liquidity, tc.amount0, tc.amount1) |
| 82 | + uassert.Equal(t, newPos.liquidity.ToString(), tc.expectedLiquidity) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestPositionUpdate(t *testing.T) { |
| 88 | + tests := []struct { |
| 89 | + initialLiquidity *u256.Uint |
| 90 | + liquidityDelta *i256.Int |
| 91 | + feeGrowthInside0X128 *u256.Uint |
| 92 | + feeGrowthInside1X128 *u256.Uint |
| 93 | + shouldPanic bool |
| 94 | + panicMsg string |
| 95 | + expectedLiquidity string |
| 96 | + expectedFeeGrowthInside0X128 string |
| 97 | + expectedFeeGrowthInside1X128 string |
| 98 | + expectedToken0Owed string |
| 99 | + expectedToken1Owed string |
| 100 | + }{ |
| 101 | + { |
| 102 | + initialLiquidity: u256.Zero(), |
| 103 | + liquidityDelta: i256.MustFromDecimal("0"), |
| 104 | + feeGrowthInside0X128: u256.Zero(), |
| 105 | + feeGrowthInside1X128: u256.Zero(), |
| 106 | + shouldPanic: true, |
| 107 | + panicMsg: `[GNOSWAP-POOL-010] zero liquidity || position.gno__positionUpdate() || both liquidityDelta and current position's liquidity are zero`, |
| 108 | + }, |
| 109 | + { |
| 110 | + initialLiquidity: u256.Zero(), |
| 111 | + liquidityDelta: i256.MustFromDecimal("100000"), |
| 112 | + feeGrowthInside0X128: u256.Zero(), |
| 113 | + feeGrowthInside1X128: u256.Zero(), |
| 114 | + expectedLiquidity: "100000", |
| 115 | + expectedFeeGrowthInside0X128: "0", |
| 116 | + expectedFeeGrowthInside1X128: "0", |
| 117 | + expectedToken0Owed: "0", |
| 118 | + expectedToken1Owed: "0", |
| 119 | + }, |
| 120 | + { |
| 121 | + initialLiquidity: u256.Zero(), |
| 122 | + liquidityDelta: i256.MustFromDecimal("100000"), |
| 123 | + feeGrowthInside0X128: u256.MustFromDecimal("100000000"), |
| 124 | + feeGrowthInside1X128: u256.MustFromDecimal("100000000"), |
| 125 | + expectedLiquidity: "100000", |
| 126 | + expectedFeeGrowthInside0X128: "100000000", |
| 127 | + expectedFeeGrowthInside1X128: "100000000", |
| 128 | + expectedToken0Owed: "0", |
| 129 | + expectedToken1Owed: "0", |
| 130 | + }, |
| 131 | + { |
| 132 | + initialLiquidity: u256.NewUint(100000), |
| 133 | + liquidityDelta: i256.MustFromDecimal("100000"), |
| 134 | + feeGrowthInside0X128: u256.MustFromDecimal("100000000"), |
| 135 | + feeGrowthInside1X128: u256.MustFromDecimal("100000000"), |
| 136 | + expectedLiquidity: "200000", |
| 137 | + expectedFeeGrowthInside0X128: "100000000", |
| 138 | + expectedFeeGrowthInside1X128: "100000000", |
| 139 | + expectedToken0Owed: "0", |
| 140 | + expectedToken1Owed: "0", |
| 141 | + }, |
| 142 | + { |
| 143 | + initialLiquidity: u256.MustFromDecimal("340282366920938463463374607431768211456"), // Q128 value |
| 144 | + liquidityDelta: i256.Zero(), |
| 145 | + feeGrowthInside0X128: u256.MustFromDecimal("100000000"), |
| 146 | + feeGrowthInside1X128: u256.MustFromDecimal("200000000"), |
| 147 | + expectedLiquidity: "340282366920938463463374607431768211456", |
| 148 | + expectedFeeGrowthInside0X128: "100000000", |
| 149 | + expectedFeeGrowthInside1X128: "200000000", |
| 150 | + expectedToken0Owed: "100000000", |
| 151 | + expectedToken1Owed: "200000000", |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + for _, tc := range tests { |
| 156 | + position := PositionInfo{ |
| 157 | + liquidity: tc.initialLiquidity, |
| 158 | + } |
| 159 | + |
| 160 | + if tc.shouldPanic { |
| 161 | + uassert.PanicsWithMessage(t, tc.panicMsg, func() { positionUpdate(position, tc.liquidityDelta, tc.feeGrowthInside0X128, tc.feeGrowthInside1X128) }) |
| 162 | + } else { |
| 163 | + newPos := positionUpdate(position, tc.liquidityDelta, tc.feeGrowthInside0X128, tc.feeGrowthInside1X128) |
| 164 | + uassert.Equal(t, newPos.liquidity.ToString(), tc.expectedLiquidity) |
| 165 | + uassert.Equal(t, newPos.feeGrowthInside0LastX128.ToString(), tc.expectedFeeGrowthInside0X128) |
| 166 | + uassert.Equal(t, newPos.feeGrowthInside1LastX128.ToString(), tc.expectedFeeGrowthInside1X128) |
| 167 | + uassert.Equal(t, newPos.tokensOwed0.ToString(), tc.expectedToken0Owed) |
| 168 | + uassert.Equal(t, newPos.tokensOwed1.ToString(), tc.expectedToken1Owed) |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments