Skip to content

Commit 3f08c85

Browse files
committed
update test
1 parent cb187e7 commit 3f08c85

File tree

4 files changed

+378
-15
lines changed

4 files changed

+378
-15
lines changed

router/_helper_test.gno

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,19 @@ func CreatePoolWithoutFee(t *testing.T) {
374374
CreatePool(t, barPath, bazPath, fee3000, common.TickMathGetSqrtRatioAtTick(0).ToString(), users.Resolve(admin))
375375
}
376376

377+
func CreateSecondPoolWithoutFee(t *testing.T) {
378+
std.TestSetRealm(adminRealm)
379+
pl.SetPoolCreationFeeByAdmin(0)
380+
381+
CreatePool(t,
382+
bazPath,
383+
quxPath,
384+
fee3000,
385+
common.TickMathGetSqrtRatioAtTick(0).ToString(),
386+
users.Resolve(admin),
387+
)
388+
}
389+
377390
func MakeMintPositionWithoutFee(t *testing.T) (uint64, string, string, string) {
378391
t.Helper()
379392

@@ -399,3 +412,27 @@ func MakeMintPositionWithoutFee(t *testing.T) (uint64, string, string, string) {
399412
users.Resolve(admin),
400413
)
401414
}
415+
416+
func MakeSecondMintPositionWithoutFee(t *testing.T) (uint64, string, string, string) {
417+
t.Helper()
418+
419+
std.TestSetRealm(adminRealm)
420+
421+
TokenApprove(t, bazPath, admin, pool, consts.UINT64_MAX)
422+
TokenApprove(t, quxPath, admin, pool, consts.UINT64_MAX)
423+
424+
return pn.Mint(
425+
bazPath,
426+
quxPath,
427+
fee3000,
428+
-887220,
429+
887220,
430+
"50000",
431+
"50000",
432+
"0",
433+
"0",
434+
max_timeout,
435+
users.Resolve(admin),
436+
users.Resolve(admin),
437+
)
438+
}

router/router_test.gno

Lines changed: 120 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,133 @@
11
package router
22

33
import (
4-
"std"
4+
"strings"
55
"testing"
66

7+
"gno.land/p/demo/uassert"
8+
i256 "gno.land/p/gnoswap/int256"
9+
u256 "gno.land/p/gnoswap/uint256"
10+
"gno.land/r/demo/wugnot"
711
"gno.land/r/gnoswap/v1/consts"
8-
9-
pl "gno.land/r/gnoswap/v1/pool"
1012
)
1113

12-
func setupTestPool(
13-
t *testing.T,
14-
token0Path, token1Path string,
15-
fee uint32,
16-
sqrtPriceX96 string,
17-
) {
18-
t.Helper()
14+
func TestFinalizeSwap(t *testing.T) {
15+
gnot := consts.GNOT
1916

20-
std.TestSetRealm(std.NewUserRealm(consts.ADMIN))
21-
pl.SetPoolCreationFeeByAdmin(1)
17+
newUint256 := func(val string) *u256.Uint {
18+
return u256.MustFromDecimal(val)
19+
}
2220

23-
if token0Path > token1Path {
24-
t.Fatalf("tokens are not sorted: %s > %s", token0Path, token1Path)
21+
tests := []struct {
22+
name string
23+
inputToken string
24+
outputToken string
25+
resultAmountIn *u256.Uint
26+
resultAmountOut *u256.Uint
27+
swapType SwapType
28+
tokenAmountLimit *u256.Uint
29+
userBeforeWugnotBalance uint64
30+
userWrappedWugnot uint64
31+
amountSpecified *u256.Uint
32+
expectError bool
33+
errorMessage string
34+
}{
35+
{
36+
name: "Pass: ExactIn",
37+
inputToken: barPath,
38+
outputToken: bazPath,
39+
resultAmountIn: newUint256("100"),
40+
resultAmountOut: newUint256("90"),
41+
swapType: ExactIn,
42+
tokenAmountLimit: newUint256("85"),
43+
userBeforeWugnotBalance: 0,
44+
userWrappedWugnot: 0,
45+
amountSpecified: newUint256("100"),
46+
expectError: false,
47+
},
48+
{
49+
name: "Pass: ExactOut",
50+
inputToken: barPath,
51+
outputToken: bazPath,
52+
resultAmountIn: newUint256("110"),
53+
resultAmountOut: newUint256("100"),
54+
swapType: ExactOut,
55+
tokenAmountLimit: newUint256("120"),
56+
userBeforeWugnotBalance: 0,
57+
userWrappedWugnot: 0,
58+
amountSpecified: newUint256("100"),
59+
expectError: false,
60+
},
61+
{
62+
name: "ExactOut: Slippage error",
63+
inputToken: barPath,
64+
outputToken: bazPath,
65+
resultAmountIn: newUint256("100"),
66+
resultAmountOut: newUint256("90"),
67+
swapType: ExactOut,
68+
tokenAmountLimit: newUint256("100"),
69+
userBeforeWugnotBalance: 0,
70+
userWrappedWugnot: 0,
71+
amountSpecified: newUint256("100"),
72+
expectError: true,
73+
errorMessage: "too few received for user",
74+
},
75+
{
76+
name: "GNOT: Slippage error",
77+
inputToken: gnot,
78+
outputToken: barPath,
79+
resultAmountIn: newUint256("300"),
80+
resultAmountOut: newUint256("90"),
81+
swapType: ExactIn,
82+
tokenAmountLimit: newUint256("85"),
83+
userBeforeWugnotBalance: 1000,
84+
userWrappedWugnot: 200,
85+
expectError: true,
86+
errorMessage: "too much wugnot spent",
87+
},
2588
}
2689

27-
pl.CreatePool(token0Path, token1Path, fee, sqrtPriceX96)
90+
for _, tt := range tests {
91+
t.Run(tt.name, func(t *testing.T) {
92+
if tt.expectError {
93+
defer func() {
94+
r := recover()
95+
if r == nil {
96+
t.Errorf("Error expected but not occurred")
97+
return
98+
}
99+
errorStr, ok := r.(string)
100+
if !ok {
101+
t.Errorf("Unexpected error type: %v", r)
102+
return
103+
}
104+
if tt.errorMessage != "" && !strings.Contains(errorStr, tt.errorMessage) {
105+
t.Errorf("Expected error message not included. got: %v, want: %v", errorStr, tt.errorMessage)
106+
}
107+
}()
108+
}
109+
110+
amountIn, amountOut := finalizeSwap(
111+
tt.inputToken,
112+
tt.outputToken,
113+
tt.resultAmountIn,
114+
tt.resultAmountOut,
115+
tt.swapType,
116+
tt.tokenAmountLimit,
117+
tt.userBeforeWugnotBalance,
118+
tt.userWrappedWugnot,
119+
tt.amountSpecified,
120+
)
121+
122+
if !tt.expectError {
123+
uassert.NotEqual(t, amountIn, "")
124+
uassert.NotEqual(t, amountOut, "")
125+
126+
outVal := i256.MustFromDecimal(amountOut)
127+
if !outVal.IsNeg() {
128+
t.Error("amountOut is not negative")
129+
}
130+
}
131+
})
132+
}
28133
}

router/swap_multi_test.gno

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package router
2+
3+
import (
4+
"std"
5+
"testing"
6+
7+
i256 "gno.land/p/gnoswap/int256"
8+
9+
"gno.land/r/demo/users"
10+
"gno.land/r/gnoswap/v1/consts"
11+
12+
"gno.land/r/onbloc/bar"
13+
"gno.land/r/onbloc/baz"
14+
"gno.land/r/onbloc/qux"
15+
16+
"gno.land/p/demo/uassert"
17+
)
18+
19+
func TestMultiSwap(t *testing.T) {
20+
user1Realm := std.NewUserRealm(user1Addr)
21+
22+
tests := []struct {
23+
name string
24+
setupFn func(t *testing.T)
25+
params SwapParams
26+
currentPoolIndex int
27+
numPools int
28+
swapPath string
29+
expectedFirstIn string
30+
expectedLastOut string
31+
expectError bool
32+
}{
33+
{
34+
name: "single hop swap BAR -> BAZ",
35+
setupFn: func(t *testing.T) {
36+
CreatePoolWithoutFee(t)
37+
MakeMintPositionWithoutFee(t)
38+
39+
std.TestSetRealm(user1Realm)
40+
bar.Approve(a2u(consts.ROUTER_ADDR), maxApprove)
41+
baz.Approve(a2u(consts.ROUTER_ADDR), maxApprove)
42+
TokenFaucet(t, barPath, a2u(user1Addr))
43+
},
44+
params: SwapParams{
45+
tokenIn: barPath,
46+
tokenOut: bazPath,
47+
fee: 3000,
48+
recipient: users.Resolve(alice),
49+
amountSpecified: i256.MustFromDecimal("100"),
50+
},
51+
currentPoolIndex: 0,
52+
numPools: 1,
53+
swapPath: "",
54+
expectedFirstIn: "100",
55+
expectedLastOut: "98",
56+
expectError: false,
57+
},
58+
{
59+
name: "multi hop swap (BAR -> BAZ -> QUX)",
60+
setupFn: func(t *testing.T) {
61+
// BAR -> BAZ
62+
CreatePoolWithoutFee(t)
63+
MakeMintPositionWithoutFee(t)
64+
65+
// BAZ -> QUX
66+
CreateSecondPoolWithoutFee(t)
67+
MakeSecondMintPositionWithoutFee(t)
68+
69+
std.TestSetRealm(user1Realm)
70+
bar.Approve(a2u(consts.ROUTER_ADDR), maxApprove)
71+
baz.Approve(a2u(consts.ROUTER_ADDR), maxApprove)
72+
qux.Approve(a2u(consts.ROUTER_ADDR), maxApprove)
73+
TokenFaucet(t, barPath, a2u(user1Addr))
74+
},
75+
params: SwapParams{
76+
tokenIn: barPath,
77+
tokenOut: bazPath,
78+
fee: 3000,
79+
recipient: users.Resolve(alice),
80+
amountSpecified: i256.MustFromDecimal("100"),
81+
},
82+
currentPoolIndex: 0,
83+
numPools: 2,
84+
swapPath: "gno.land/r/onbloc/bar:gno.land/r/onbloc/baz:3000*POOL*gno.land/r/onbloc/baz:gno.land/r/onbloc/qux:3000",
85+
expectedFirstIn: "100",
86+
expectedLastOut: "96",
87+
expectError: false,
88+
},
89+
{
90+
name: "multi hop swap with exact output",
91+
setupFn: func(t *testing.T) {
92+
// BAR -> BAZ -> QUX
93+
CreatePoolWithoutFee(t)
94+
MakeMintPositionWithoutFee(t)
95+
CreateSecondPoolWithoutFee(t)
96+
MakeSecondMintPositionWithoutFee(t)
97+
98+
std.TestSetRealm(user1Realm)
99+
bar.Approve(a2u(consts.ROUTER_ADDR), maxApprove)
100+
baz.Approve(a2u(consts.ROUTER_ADDR), maxApprove)
101+
qux.Approve(a2u(consts.ROUTER_ADDR), maxApprove)
102+
TokenFaucet(t, barPath, a2u(user1Addr))
103+
},
104+
params: SwapParams{
105+
tokenIn: barPath,
106+
tokenOut: bazPath,
107+
fee: 3000,
108+
recipient: users.Resolve(alice),
109+
amountSpecified: i256.MustFromDecimal("-96"),
110+
},
111+
currentPoolIndex: 0,
112+
numPools: 2,
113+
swapPath: "gno.land/r/onbloc/bar:gno.land/r/onbloc/baz:3000*POOL*gno.land/r/onbloc/baz:gno.land/r/onbloc/qux:3000",
114+
expectedFirstIn: "98",
115+
expectedLastOut: "94",
116+
expectError: false,
117+
},
118+
}
119+
120+
for _, tt := range tests {
121+
t.Run(tt.name, func(t *testing.T) {
122+
if tt.setupFn != nil {
123+
tt.setupFn(t)
124+
}
125+
126+
firstAmountIn, lastAmountOut := multiSwap(
127+
tt.params,
128+
tt.currentPoolIndex,
129+
tt.numPools,
130+
tt.swapPath,
131+
)
132+
133+
uassert.Equal(t, firstAmountIn.ToString(), tt.expectedFirstIn)
134+
uassert.Equal(t, lastAmountOut.ToString(), tt.expectedLastOut)
135+
})
136+
}
137+
}

0 commit comments

Comments
 (0)