Skip to content

Commit d68748e

Browse files
committed
GSW-1838 fix: Modify test code to change transferFromAndVerify function param
1 parent 5b0b453 commit d68748e

File tree

2 files changed

+150
-151
lines changed

2 files changed

+150
-151
lines changed

Diff for: pool/pool_test.gno

+129-129
Original file line numberDiff line numberDiff line change
@@ -4,138 +4,138 @@ import (
44
"std"
55
"testing"
66

7-
"gno.land/r/gnoswap/v1/consts"
8-
"gno.land/p/demo/uassert"
97
"gno.land/p/demo/testutils"
8+
"gno.land/p/demo/uassert"
109
i256 "gno.land/p/gnoswap/int256"
1110
u256 "gno.land/p/gnoswap/uint256"
11+
"gno.land/r/gnoswap/v1/consts"
1212
)
1313

1414
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+
})
4444
}
4545

4646
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+
}
139139
}
140140

141141
func TestSaveProtocolFees(t *testing.T) {
@@ -479,7 +479,7 @@ func TestComputeSwap(t *testing.T) {
479479
}
480480

481481
wordPos, _ := tickBitmapPosition(0)
482-
// TODO: use avl
482+
// TODO: use avl
483483
mockPool.tickBitmaps[wordPos] = u256.NewUint(1)
484484

485485
t.Run("basic swap", func(t *testing.T) {
@@ -623,11 +623,11 @@ func TestTransferFromAndVerify(t *testing.T) {
623623
defer func() { transferFromByRegisterCall = oldTransferFromByRegisterCall }()
624624

625625
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
628628
}
629629

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

632632
if !tt.pool.balances.token0.Eq(tt.expectedBal0) {
633633
t.Errorf("token0 balance mismatch: expected %s, got %s",
@@ -663,7 +663,7 @@ func TestTransferFromAndVerify(t *testing.T) {
663663
testutils.TestAddress("from_addr"),
664664
testutils.TestAddress("to_addr"),
665665
"token0_path",
666-
negativeAmount,
666+
u256.MustFromDecimal(negativeAmount.Abs().ToString()),
667667
true,
668668
)
669669

@@ -695,7 +695,7 @@ func TestTransferFromAndVerify(t *testing.T) {
695695
testutils.TestAddress("from_addr"),
696696
testutils.TestAddress("to_addr"),
697697
"token0_path",
698-
hugeAmount,
698+
u256.MustFromDecimal(hugeAmount.ToString()),
699699
true,
700700
)
701701
})

Diff for: pool/position_modify_test.gno

+21-22
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,47 @@ import (
44
"testing"
55

66
"gno.land/p/demo/uassert"
7-
u256 "gno.land/p/gnoswap/uint256"
87
i256 "gno.land/p/gnoswap/int256"
9-
"gno.land/r/gnoswap/v1/consts"
8+
u256 "gno.land/p/gnoswap/uint256"
109
"gno.land/r/gnoswap/v1/common"
10+
"gno.land/r/gnoswap/v1/consts"
1111
)
1212

13-
1413
func TestModifyPosition(t *testing.T) {
1514
const (
1615
fee500 = uint32(500)
1716
test_liquidityDelta = int64(100000000)
1817
)
1918

2019
tests := []struct {
21-
name string
22-
sqrtPrice string
23-
tickLower int32
24-
tickUpper int32
20+
name string
21+
sqrtPrice string
22+
tickLower int32
23+
tickUpper int32
2524
expectedAmt0 string
2625
expectedAmt1 string
2726
}{
2827
{
29-
name: "current price is lower than range",
30-
sqrtPrice: common.TickMathGetSqrtRatioAtTick(-12000).ToString(),
31-
tickLower: -11000,
32-
tickUpper: -9000,
28+
name: "current price is lower than range",
29+
sqrtPrice: common.TickMathGetSqrtRatioAtTick(-12000).ToString(),
30+
tickLower: -11000,
31+
tickUpper: -9000,
3332
expectedAmt0: "16492846",
3433
expectedAmt1: "0",
3534
},
3635
{
37-
name: "current price is in range",
38-
sqrtPrice: common.TickMathGetSqrtRatioAtTick(-10000).ToString(),
39-
tickLower: -11000,
40-
tickUpper: -9000,
36+
name: "current price is in range",
37+
sqrtPrice: common.TickMathGetSqrtRatioAtTick(-10000).ToString(),
38+
tickLower: -11000,
39+
tickUpper: -9000,
4140
expectedAmt0: "8040316",
4241
expectedAmt1: "2958015",
4342
},
4443
{
45-
name: "current price is higher than range",
46-
sqrtPrice: common.TickMathGetSqrtRatioAtTick(-8000).ToString(),
47-
tickLower: -11000,
48-
tickUpper: -9000,
44+
name: "current price is higher than range",
45+
sqrtPrice: common.TickMathGetSqrtRatioAtTick(-8000).ToString(),
46+
tickLower: -11000,
47+
tickUpper: -9000,
4948
expectedAmt0: "0",
5049
expectedAmt1: "6067683",
5150
},
@@ -86,7 +85,7 @@ func TestModifyPositionEdgeCases(t *testing.T) {
8685
barPath,
8786
fooPath,
8887
fee500,
89-
sp.ToString(),
88+
sqrtPrice,
9089
)
9190
pool := newPool(poolParams)
9291
params := ModifyPositionParams{
@@ -106,7 +105,7 @@ func TestModifyPositionEdgeCases(t *testing.T) {
106105
}
107106
}
108107
}()
109-
108+
110109
pool.modifyPosition(params)
111110
})
112111

@@ -115,7 +114,7 @@ func TestModifyPositionEdgeCases(t *testing.T) {
115114
barPath,
116115
fooPath,
117116
fee500,
118-
sp.ToString(),
117+
sqrtPrice,
119118
)
120119
pool := newPool(poolParams)
121120
params := ModifyPositionParams{

0 commit comments

Comments
 (0)