@@ -4,138 +4,138 @@ import (
4
4
"std"
5
5
"testing"
6
6
7
- "gno.land/r/gnoswap/v1/consts"
8
- "gno.land/p/demo/uassert"
9
7
"gno.land/p/demo/testutils"
8
+ "gno.land/p/demo/uassert"
10
9
i256 "gno.land/p/gnoswap/int256"
11
10
u256 "gno.land/p/gnoswap/uint256"
11
+ "gno.land/r/gnoswap/v1/consts"
12
12
)
13
13
14
14
func TestMint(t *testing.T) {
15
- token0Path := "test_token0"
16
- token1Path := "test_token1"
17
- fee := uint32(3000)
18
- recipient := testutils.TestAddress("recipient")
19
- tickLower := int32(-100)
20
- tickUpper := int32(100)
21
- liquidityAmount := "100000"
22
-
23
- t.Run("unauthorized caller mint should fail", func(t *testing.T) {
24
- unauthorized := testutils.TestAddress("unauthorized")
25
- defer func() {
26
- if r := recover(); r == nil {
27
- t.Error("unauthorized caller mint should fail")
28
- }
29
- }()
30
-
31
- Mint(token0Path, token1Path, fee, recipient, tickLower, tickUpper, liquidityAmount, unauthorized)
32
- })
33
-
34
- t.Run("mint with 0 liquidity should fail", func(t *testing.T) {
35
- authorized := consts.POSITION_ADDR
36
- defer func() {
37
- if r := recover(); r == nil {
38
- t.Error("mint with 0 liquidity should fail")
39
- }
40
- }()
41
-
42
- Mint(token0Path, token1Path, fee, recipient, tickLower, tickUpper, "0", authorized)
43
- })
15
+ token0Path := "test_token0"
16
+ token1Path := "test_token1"
17
+ fee := uint32(3000)
18
+ recipient := testutils.TestAddress("recipient")
19
+ tickLower := int32(-100)
20
+ tickUpper := int32(100)
21
+ liquidityAmount := "100000"
22
+
23
+ t.Run("unauthorized caller mint should fail", func(t *testing.T) {
24
+ unauthorized := testutils.TestAddress("unauthorized")
25
+ defer func() {
26
+ if r := recover(); r == nil {
27
+ t.Error("unauthorized caller mint should fail")
28
+ }
29
+ }()
30
+
31
+ Mint(token0Path, token1Path, fee, recipient, tickLower, tickUpper, liquidityAmount, unauthorized)
32
+ })
33
+
34
+ t.Run("mint with 0 liquidity should fail", func(t *testing.T) {
35
+ authorized := consts.POSITION_ADDR
36
+ defer func() {
37
+ if r := recover(); r == nil {
38
+ t.Error("mint with 0 liquidity should fail")
39
+ }
40
+ }()
41
+
42
+ Mint(token0Path, token1Path, fee, recipient, tickLower, tickUpper, "0", authorized)
43
+ })
44
44
}
45
45
46
46
func TestBurn(t *testing.T) {
47
- // Setup
48
- originalGetPool := GetPool
49
- defer func() {
50
- GetPool = originalGetPool
51
- }()
52
-
53
- // Mock data
54
- mockCaller := consts.POSITION_ADDR
55
- mockPosition := PositionInfo{
56
- liquidity: u256.NewUint(1000),
57
- tokensOwed0: u256.NewUint(0),
58
- tokensOwed1: u256.NewUint(0),
59
- }
60
- mockPool := &Pool{
61
- positions: make(map[string]PositionInfo),
62
- }
63
-
64
- GetPool = func(token0Path, token1Path string, fee uint32) *Pool {
65
- return mockPool
66
- }
67
-
68
- tests := []struct {
69
- name string
70
- liquidityAmount string
71
- tickLower int32
72
- tickUpper int32
73
- expectedAmount0 string
74
- expectedAmount1 string
75
- expectPanic bool
76
- }{
77
- {
78
- name: "successful burn",
79
- liquidityAmount: "500",
80
- tickLower: -100,
81
- tickUpper: 100,
82
- expectedAmount0: "100",
83
- expectedAmount1: "200",
84
- },
85
- {
86
- name: "zero liquidity",
87
- liquidityAmount: "0",
88
- tickLower: -100,
89
- tickUpper: 100,
90
- expectPanic: true,
91
- },
92
- }
93
-
94
- for _, tt := range tests {
95
- t.Run(tt.name, func(t *testing.T) {
96
- if tt.name == "successful burn" {
97
- t.Skip("skipping until find better way to test this")
98
- }
99
-
100
- // setup position for this test
101
- posKey := positionGetKey(mockCaller, tt.tickLower, tt.tickUpper)
102
- mockPool.positions[posKey] = mockPosition
103
-
104
- if tt.expectPanic {
105
- defer func() {
106
- if r := recover(); r == nil {
107
- t.Errorf("expected panic but got none")
108
- }
109
- }()
110
- }
111
-
112
- amount0, amount1 := Burn(
113
- "token0",
114
- "token1",
115
- 3000,
116
- tt.tickLower,
117
- tt.tickUpper,
118
- tt.liquidityAmount,
119
- )
120
-
121
- if !tt.expectPanic {
122
- if amount0 != tt.expectedAmount0 {
123
- t.Errorf("expected amount0 %s, got %s", tt.expectedAmount0, amount0)
124
- }
125
- if amount1 != tt.expectedAmount1 {
126
- t.Errorf("expected amount1 %s, got %s", tt.expectedAmount1, amount1)
127
- }
128
-
129
- newPosition := mockPool.positions[posKey]
130
- if newPosition.tokensOwed0.IsZero() {
131
- t.Error("expected tokensOwed0 to be updated")
132
- }
133
- if newPosition.tokensOwed1.IsZero() {
134
- t.Error("expected tokensOwed1 to be updated")
135
- }
136
- }
137
- })
138
- }
47
+ // Setup
48
+ originalGetPool := GetPool
49
+ defer func() {
50
+ GetPool = originalGetPool
51
+ }()
52
+
53
+ // Mock data
54
+ mockCaller := consts.POSITION_ADDR
55
+ mockPosition := PositionInfo{
56
+ liquidity: u256.NewUint(1000),
57
+ tokensOwed0: u256.NewUint(0),
58
+ tokensOwed1: u256.NewUint(0),
59
+ }
60
+ mockPool := &Pool{
61
+ positions: make(map[string]PositionInfo),
62
+ }
63
+
64
+ GetPool = func(token0Path, token1Path string, fee uint32) *Pool {
65
+ return mockPool
66
+ }
67
+
68
+ tests := []struct {
69
+ name string
70
+ liquidityAmount string
71
+ tickLower int32
72
+ tickUpper int32
73
+ expectedAmount0 string
74
+ expectedAmount1 string
75
+ expectPanic bool
76
+ }{
77
+ {
78
+ name: "successful burn",
79
+ liquidityAmount: "500",
80
+ tickLower: -100,
81
+ tickUpper: 100,
82
+ expectedAmount0: "100",
83
+ expectedAmount1: "200",
84
+ },
85
+ {
86
+ name: "zero liquidity",
87
+ liquidityAmount: "0",
88
+ tickLower: -100,
89
+ tickUpper: 100,
90
+ expectPanic: true,
91
+ },
92
+ }
93
+
94
+ for _, tt := range tests {
95
+ t.Run(tt.name, func(t *testing.T) {
96
+ if tt.name == "successful burn" {
97
+ t.Skip("skipping until find better way to test this")
98
+ }
99
+
100
+ // setup position for this test
101
+ posKey := positionGetKey(mockCaller, tt.tickLower, tt.tickUpper)
102
+ mockPool.positions[posKey] = mockPosition
103
+
104
+ if tt.expectPanic {
105
+ defer func() {
106
+ if r := recover(); r == nil {
107
+ t.Errorf("expected panic but got none")
108
+ }
109
+ }()
110
+ }
111
+
112
+ amount0, amount1 := Burn(
113
+ "token0",
114
+ "token1",
115
+ 3000,
116
+ tt.tickLower,
117
+ tt.tickUpper,
118
+ tt.liquidityAmount,
119
+ )
120
+
121
+ if !tt.expectPanic {
122
+ if amount0 != tt.expectedAmount0 {
123
+ t.Errorf("expected amount0 %s, got %s", tt.expectedAmount0, amount0)
124
+ }
125
+ if amount1 != tt.expectedAmount1 {
126
+ t.Errorf("expected amount1 %s, got %s", tt.expectedAmount1, amount1)
127
+ }
128
+
129
+ newPosition := mockPool.positions[posKey]
130
+ if newPosition.tokensOwed0.IsZero() {
131
+ t.Error("expected tokensOwed0 to be updated")
132
+ }
133
+ if newPosition.tokensOwed1.IsZero() {
134
+ t.Error("expected tokensOwed1 to be updated")
135
+ }
136
+ }
137
+ })
138
+ }
139
139
}
140
140
141
141
func TestSaveProtocolFees(t *testing.T) {
@@ -479,7 +479,7 @@ func TestComputeSwap(t *testing.T) {
479
479
}
480
480
481
481
wordPos, _ := tickBitmapPosition(0)
482
- // TODO: use avl
482
+ // TODO: use avl
483
483
mockPool.tickBitmaps[wordPos] = u256.NewUint(1)
484
484
485
485
t.Run("basic swap", func(t *testing.T) {
@@ -623,11 +623,11 @@ func TestTransferFromAndVerify(t *testing.T) {
623
623
defer func() { transferFromByRegisterCall = oldTransferFromByRegisterCall }()
624
624
625
625
transferFromByRegisterCall = func(tokenPath string, from, to std.Address, amount uint64) bool {
626
- // mock the transfer (just return true)
627
- return true
626
+ // mock the transfer (just return true)
627
+ return true
628
628
}
629
629
630
- tt.pool.transferFromAndVerify(tt.from, tt.to, tt.tokenPath, tt.amount, tt.isToken0)
630
+ tt.pool.transferFromAndVerify(tt.from, tt.to, tt.tokenPath, u256.MustFromDecimal( tt.amount.ToString()) , tt.isToken0)
631
631
632
632
if !tt.pool.balances.token0.Eq(tt.expectedBal0) {
633
633
t.Errorf("token0 balance mismatch: expected %s, got %s",
@@ -663,7 +663,7 @@ func TestTransferFromAndVerify(t *testing.T) {
663
663
testutils.TestAddress("from_addr"),
664
664
testutils.TestAddress("to_addr"),
665
665
"token0_path",
666
- negativeAmount,
666
+ u256.MustFromDecimal( negativeAmount.Abs().ToString()) ,
667
667
true,
668
668
)
669
669
@@ -695,7 +695,7 @@ func TestTransferFromAndVerify(t *testing.T) {
695
695
testutils.TestAddress("from_addr"),
696
696
testutils.TestAddress("to_addr"),
697
697
"token0_path",
698
- hugeAmount,
698
+ u256.MustFromDecimal( hugeAmount.ToString()) ,
699
699
true,
700
700
)
701
701
})
0 commit comments