Skip to content

Commit

Permalink
Merge pull request #173 from gnoswap-labs/GSW-873-fix-test-cases-unde…
Browse files Browse the repository at this point in the history
…r-test-folder-≈-based-on-scenario

GSW-873 fix: test cases under `_test` folder ≈ based on scenario
  • Loading branch information
r3v4s authored Feb 26, 2024
2 parents b983d90 + 762d3da commit 5d8ff5a
Show file tree
Hide file tree
Showing 18 changed files with 550 additions and 559 deletions.
15 changes: 14 additions & 1 deletion _test/_TEST_INIT_basic_test.gno
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package swap_scneraio
package swap_scenario

import (
"std"
Expand Down Expand Up @@ -106,6 +106,19 @@ func shouldPanic(t *testing.T, f func()) {
f()
}

func shouldPanicWithMsg(t *testing.T, f func(), msg string) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic")
} else {
if r != msg {
t.Errorf("excepted panic(%v), got(%v)", msg, r)
}
}
}()
f()
}

func balanceOf(token *grc20.AdminToken, addr std.Address) uint64 {
balance, err := token.BalanceOf(addr)
if err != nil {
Expand Down
160 changes: 160 additions & 0 deletions _test/_TEST_INIT_register_tokens_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package swap_scenario

import (
"gno.land/r/demo/foo"

"gno.land/r/demo/bar"

"gno.land/r/demo/baz"

"gno.land/r/demo/qux"

"gno.land/r/demo/wugnot"

"gno.land/r/demo/obl"

"gno.land/r/demo/gns"

"gno.land/r/demo/users"

pl "gno.land/r/demo/pool"
rr "gno.land/r/demo/router"
sr "gno.land/r/demo/staker"
)

type FooToken struct{}

func (FooToken) Transfer() func(to users.AddressOrName, amount uint64) {
return foo.Transfer
}
func (FooToken) TransferFrom() func(from, to users.AddressOrName, amount uint64) {
return foo.TransferFrom
}
func (FooToken) BalanceOf() func(owner users.AddressOrName) uint64 {
return foo.BalanceOf
}
func (FooToken) Approve() func(spender users.AddressOrName, amount uint64) {
return foo.Approve
}

type BarToken struct{}

func (BarToken) Transfer() func(to users.AddressOrName, amount uint64) {
return bar.Transfer
}
func (BarToken) TransferFrom() func(from, to users.AddressOrName, amount uint64) {
return bar.TransferFrom
}
func (BarToken) BalanceOf() func(owner users.AddressOrName) uint64 {
return bar.BalanceOf
}
func (BarToken) Approve() func(spender users.AddressOrName, amount uint64) {
return bar.Approve
}

type BazToken struct{}

func (BazToken) Transfer() func(to users.AddressOrName, amount uint64) {
return baz.Transfer
}
func (BazToken) TransferFrom() func(from, to users.AddressOrName, amount uint64) {
return baz.TransferFrom
}
func (BazToken) BalanceOf() func(owner users.AddressOrName) uint64 {
return baz.BalanceOf
}
func (BazToken) Approve() func(spender users.AddressOrName, amount uint64) {
return baz.Approve
}

type QuxToken struct{}

func (QuxToken) Transfer() func(to users.AddressOrName, amount uint64) {
return qux.Transfer
}
func (QuxToken) TransferFrom() func(from, to users.AddressOrName, amount uint64) {
return qux.TransferFrom
}
func (QuxToken) BalanceOf() func(owner users.AddressOrName) uint64 {
return qux.BalanceOf
}
func (QuxToken) Approve() func(spender users.AddressOrName, amount uint64) {
return qux.Approve
}

type WugnotToken struct{}

func (WugnotToken) Transfer() func(to users.AddressOrName, amount uint64) {
return wugnot.Transfer
}
func (WugnotToken) TransferFrom() func(from, to users.AddressOrName, amount uint64) {
return wugnot.TransferFrom
}
func (WugnotToken) BalanceOf() func(owner users.AddressOrName) uint64 {
return wugnot.BalanceOf
}
func (WugnotToken) Approve() func(spender users.AddressOrName, amount uint64) {
return wugnot.Approve
}

type OBLToken struct{}

func (OBLToken) Transfer() func(to users.AddressOrName, amount uint64) {
return obl.Transfer
}
func (OBLToken) TransferFrom() func(from, to users.AddressOrName, amount uint64) {
return obl.TransferFrom
}
func (OBLToken) BalanceOf() func(owner users.AddressOrName) uint64 {
return obl.BalanceOf
}
func (OBLToken) Approve() func(spender users.AddressOrName, amount uint64) {
return obl.Approve
}

type GNSToken struct{}

func (GNSToken) Transfer() func(to users.AddressOrName, amount uint64) {
return gns.Transfer
}

func (GNSToken) TransferFrom() func(from, to users.AddressOrName, amount uint64) {
return gns.TransferFrom
}

func (GNSToken) BalanceOf() func(owner users.AddressOrName) uint64 {
return gns.BalanceOf
}

func (GNSToken) Approve() func(spender users.AddressOrName, amount uint64) {
return gns.Approve
}

func init() {
// POOL
pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{})
pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{})
pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{})
pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{})
pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{})
pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{})
pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{})

// STAKER
sr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{})
sr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{})
sr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{})
sr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{})
sr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{})
sr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{})
sr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{})

// ROUTER
rr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{})
rr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{})
rr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{})
rr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{})
rr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{})
rr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{})
rr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{})
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// single lp, single swap, normal scenario
package swap_scneraio
package swap_scenario

import (
"std"
Expand All @@ -10,8 +10,6 @@ import (
"gno.land/p/demo/common"
"gno.land/r/demo/consts"

_ "gno.land/r/demo/grc20_wrapper"

pl "gno.land/r/demo/pool"
pn "gno.land/r/demo/position"
rr "gno.land/r/demo/router"
Expand Down
87 changes: 31 additions & 56 deletions _test/_TEST_scenario_02_FAIL_too_small_amount_test.gnoa
Original file line number Diff line number Diff line change
@@ -1,58 +1,30 @@
// single lp, small amount swap
// XXX: SHOULD FAIL DUE TO TOO SMALL AMOUNT
// > if swap uses mutli routes, smallest amount should be larger than 0, else it will panic
package swap_scneraio
package swap_scenario

import (
"std"
"testing"

"gno.land/p/demo/testutils"

_ "gno.land/r/demo/grc20_wrapper"
"gno.land/r/demo/consts"

pl "gno.land/r/demo/pool"
pos "gno.land/r/demo/position"
rou "gno.land/r/demo/router"
pn "gno.land/r/demo/position"
rr "gno.land/r/demo/router"

"gno.land/r/demo/bar"
"gno.land/r/demo/baz"
"gno.land/r/demo/foo"
"gno.land/r/demo/qux"

"gno.land/r/demo/gns"
"gno.land/r/demo/obl"
)

var (
test1 = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
lp01 = testutils.TestAddress("lp01")
tr01 = testutils.TestAddress("tr01")

poolAddr = std.DerivePkgAddr("gno.land/r/demo/pool")
positionAddr = std.DerivePkgAddr("gno.land/r/demo/position")
stakerAddr = std.DerivePkgAddr("gno.land/r/demo/staker")
routerAddr = std.DerivePkgAddr("gno.land/r/demo/router")

barPath = "gno.land/r/demo/bar"
bazPath = "gno.land/r/demo/baz"
fooPath = "gno.land/r/demo/foo"
quxPath = "gno.land/r/demo/qux"

wugnotPath = "gno.land/r/demo/wugnot"

gnsPath = "gno.land/r/demo/gns"
oblPath = "gno.land/r/demo/obl"

barObj = bar.GetGRC20()
bazObj = baz.GetGRC20()
fooObj = foo.GetGRC20()
quxObj = qux.GetGRC20()

gnsObj = gns.GetGRC20()
oblObj = obl.GetGRC20()

max_timeout = bigint(9999999999)
lp01 = testutils.TestAddress("lp01")
tr01 = testutils.TestAddress("tr01")
)

func TestPoolInitByAdmin(t *testing.T) {
Expand All @@ -66,7 +38,7 @@ func TestPoolCreatePools(t *testing.T) {
std.TestSetOrigCaller(test1)

// approve gns to CreatePool ( 500 gns for 1 pool)
gns.Approve(a2u(poolAddr), 5000)
gns.Approve(a2u(consts.POOL_ADDR), 5000)

// bar-baz
pl.CreatePool(barPath, bazPath, 100, 354340008410679467268648495215) // tick 29960 ≈ 1:20
Expand Down Expand Up @@ -99,10 +71,10 @@ func TestPositionMintBarBazInRange(t *testing.T) {
std.TestSetOrigCaller(lp01)

// approve bar, baz to pool ( position.Mint() calls pool.Mint() ≈ so approve to pool )
bar.Approve(a2u(poolAddr), 10000000)
baz.Approve(a2u(poolAddr), 10000000)
bar.Approve(a2u(consts.POOL_ADDR), 10000000)
baz.Approve(a2u(consts.POOL_ADDR), 10000000)

tokenId, liquidity, amount0, amoutn1 := pos.Mint(
tokenId, liquidity, amount0, amoutn1 := pn.Mint(
barPath, // token0
bazPath, // token1
100, // fee
Expand All @@ -112,7 +84,7 @@ func TestPositionMintBarBazInRange(t *testing.T) {
10000000, // amount1Desired
0, // amount0Min
0, // amount1Min
max_timeout, // deadline
MAX_TIMEOUT, // deadline
)

shouldEQ(t, tokenId, bigint(1))
Expand All @@ -127,10 +99,10 @@ func TestPositionMintBazFooInRange(t *testing.T) {
std.TestSetOrigCaller(lp01)

// approve baz, foo to pool
baz.Approve(a2u(poolAddr), 10000000)
foo.Approve(a2u(poolAddr), 10000000)
baz.Approve(a2u(consts.POOL_ADDR), 10000000)
foo.Approve(a2u(consts.POOL_ADDR), 10000000)

tokenId, liquidity, amount0, amoutn1 := pos.Mint(
tokenId, liquidity, amount0, amoutn1 := pn.Mint(
bazPath, // token0
fooPath, // token1
100, // fee
Expand All @@ -140,7 +112,7 @@ func TestPositionMintBazFooInRange(t *testing.T) {
10000000, // amount1Desired
0, // amount0Min
0, // amount1Min
max_timeout, // deadline
MAX_TIMEOUT, // deadline
)

shouldEQ(t, tokenId, bigint(2))
Expand All @@ -167,28 +139,31 @@ func TestFaucetTR01(t *testing.T) {
}

func TestRouterDrySwapBarBazExactIn(t *testing.T) {
dryResult := rou.DrySwapRoute(
dryResult := rr.DrySwapRoute(
barPath, // inputToken
bazPath, // outputToken
10, // amountSpecified
"EXACT_IN", // swapType
"gno.land/r/demo/bar:gno.land/r/demo/baz:100,gno.land/r/demo/bar:gno.land/r/demo/baz:100", // strRouteArr
"95,5", // quoteArr
)
shouldEQ(t, dryResult, bigint(-1))
shouldEQ(t, dryResult, bigint(-1)) // -1 means fail
}

func TestRouterSwapBarBazExactIn(t *testing.T) {
bar.Approve(a2u(poolAddr), 100000)

swapResult := rou.SwapRoute(
barPath, // inputToken
bazPath, // outputToken
10, // amountSpecified
"EXACT_IN", // swapType
"gno.land/r/demo/bar:gno.land/r/demo/baz:100,gno.land/r/demo/bar:gno.land/r/demo/baz:100", // strRouteArr
"95,5", // quoteArr
"1", // tokenAmountLimit
bar.Approve(a2u(consts.POOL_ADDR), 100000)

shouldPanicWithMsg(t, func() {
rr.SwapRoute(
barPath, // inputToken
bazPath, // outputToken
10, // amountSpecified
"EXACT_IN", // swapType
"gno.land/r/demo/bar:gno.land/r/demo/baz:100,gno.land/r/demo/bar:gno.land/r/demo/baz:100", // strRouteArr
"95,5", // quoteArr
"1", // tokenAmountLimit
)
},
"[POOL] pool.gno__Swap() || amountSpecified(0) != 0",
)
shouldEQ(t, swapResult, bigint(19962))
}
Loading

0 comments on commit 5d8ff5a

Please sign in to comment.