From 23a3bed6dd3be895fab311686b4d14e58e3fb3e7 Mon Sep 17 00:00:00 2001 From: 0xTopaz <60733299+onlyhyde@users.noreply.github.com> Date: Mon, 9 Dec 2024 12:32:08 +0900 Subject: [PATCH] GSW-1838 fix: test errors are fixed (#422) * GSW-1838 fix: test errors are fixed - Integrate helper functions for tests - Change file extensions to prevent test code in the test folder from being executed - Fixing failure errors due to code integration - Known issue : Fixed additional test failure case related to getter * fix: remove time compare in unit test * fix: do not setup data in init * test: pool manger testcase --------- Co-authored-by: n3wbie --- pool/_helper_test.gno | 127 +++++++- pool/api_test.gno | 5 +- pool/getter_test.gno | 4 +- pool/helper_test.gno | 118 -------- pool/pool_manager_test.gno | 282 +++++++++++------- pool/position_test.gno | 6 +- pool/protocol_fee_pool_creation_test.gno | 2 +- pool/protocol_fee_withdrawal_test.gno | 10 +- .../__TEST_0_INIT_TOKEN_REGISTER_test.gnoA | 183 ++++++++++++ ...TEST_0_INIT_VARIABLE_AND_HELPER_test.gnoA} | 0 10 files changed, 482 insertions(+), 255 deletions(-) delete mode 100644 pool/helper_test.gno create mode 100644 pool/tests/__TEST_0_INIT_TOKEN_REGISTER_test.gnoA rename pool/tests/{__TEST_0_INIT_VARIABLE_AND_HELPER_test.gno => __TEST_0_INIT_VARIABLE_AND_HELPER_test.gnoA} (100%) diff --git a/pool/_helper_test.gno b/pool/_helper_test.gno index 3496e643a..ce061a777 100644 --- a/pool/_helper_test.gno +++ b/pool/_helper_test.gno @@ -11,8 +11,10 @@ import ( "gno.land/r/onbloc/foo" "gno.land/r/onbloc/obl" "gno.land/r/onbloc/qux" + "gno.land/r/onbloc/usdc" "gno.land/p/demo/testutils" + "gno.land/p/demo/uassert" pusers "gno.land/p/demo/users" "gno.land/r/demo/users" "gno.land/r/gnoswap/v1/consts" @@ -37,6 +39,12 @@ const ( max_timeout int64 = 9999999999 ) +const ( + // define addresses to use in tests + addr01 = testutils.TestAddress("addr01") + addr02 = testutils.TestAddress("addr02") +) + type WugnotToken struct{} func (WugnotToken) Transfer() func(to pusers.AddressOrName, amount uint64) { @@ -155,20 +163,22 @@ func init() { } var ( - newAdmin = pusers.AddressOrName("g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d") admin = pusers.AddressOrName(consts.ADMIN) alice = pusers.AddressOrName(testutils.TestAddress("alice")) pool = pusers.AddressOrName(consts.POOL_ADDR) protocolFee = pusers.AddressOrName(consts.PROTOCOL_FEE_ADDR) + adminRealm = std.NewUserRealm(users.Resolve(admin)) + posRealm = std.NewCodeRealm(consts.POSITION_PATH) + + // addresses used in tests + addrUsedInTest = []std.Address{addr01, addr02} ) func InitialisePoolTest(t *testing.T) { t.Helper() - ugnotFaucet(t, users.Resolve(newAdmin), 100_000_000_000_000) - ugnotDeposit(t, users.Resolve(newAdmin), 100_000_000_000_000) - TokenFaucet(t, wugnotPath, admin) - TokenFaucet(t, gnsPath, admin) + ugnotFaucet(t, users.Resolve(admin), 100_000_000_000_000) + ugnotDeposit(t, users.Resolve(admin), 100_000_000_000_000) std.TestSetOrigCaller(users.Resolve(admin)) TokenApprove(t, gnsPath, admin, pool, maxApprove) @@ -202,7 +212,7 @@ func InitialisePoolTest(t *testing.T) { func TokenFaucet(t *testing.T, tokenPath string, to pusers.AddressOrName) { t.Helper() - std.TestSetOrigCaller(users.Resolve(newAdmin)) + std.TestSetOrigCaller(users.Resolve(admin)) defaultAmount := uint64(5_000_000_000) switch tokenPath { @@ -367,43 +377,43 @@ func quxApprove(t *testing.T, owner, spender pusers.AddressOrName, amount uint64 func wugnotTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) { t.Helper() - std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin))) + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) wugnot.Transfer(to, amount) } func gnsTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) { t.Helper() - std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin))) + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) gns.Transfer(to, amount) } func barTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) { t.Helper() - std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin))) + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) bar.Transfer(to, amount) } func bazTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) { t.Helper() - std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin))) + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) baz.Transfer(to, amount) } func fooTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) { t.Helper() - std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin))) + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) foo.Transfer(to, amount) } func oblTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) { t.Helper() - std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin))) + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) obl.Transfer(to, amount) } func quxTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) { t.Helper() - std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin))) + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) qux.Transfer(to, amount) } @@ -446,7 +456,7 @@ func ugnotBurn(t *testing.T, addr std.Address, denom string, amount int64) { func ugnotFaucet(t *testing.T, to std.Address, amount uint64) { t.Helper() - faucetAddress := users.Resolve(newAdmin) + faucetAddress := users.Resolve(admin) std.TestSetOrigCaller(faucetAddress) if ugnotBalanceOf(t, faucetAddress) < amount { @@ -465,3 +475,92 @@ func ugnotDeposit(t *testing.T, addr std.Address, amount uint64) { banker.SendCoins(addr, wugnotAddr, std.Coins{{ugnotDenom, int64(amount)}}) wugnot.Deposit() } + +// resetObject resets the object state(clear or make it default values) +func resetObject(t *testing.T) { + pools = make(poolMap) + slot0FeeProtocol = 0 + poolCreationFee = 100_000_000 + withdrawalFeeBPS = 100 +} + +func burnTokens(t *testing.T) { + t.Helper() + + // burn tokens + for _, addr := range addrUsedInTest { + uAddr := a2u(addr) + burnFoo(uAddr) + burnBar(uAddr) + burnBaz(uAddr) + burnQux(uAddr) + burnObl(uAddr) + burnUsdc(uAddr) + } +} + +func burnFoo(addr pusers.AddressOrName) { + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) + foo.Burn(addr, foo.BalanceOf(addr)) +} + +func burnBar(addr pusers.AddressOrName) { + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) + bar.Burn(addr, bar.BalanceOf(addr)) +} + +func burnBaz(addr pusers.AddressOrName) { + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) + baz.Burn(addr, baz.BalanceOf(addr)) +} + +func burnQux(addr pusers.AddressOrName) { + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) + qux.Burn(addr, qux.BalanceOf(addr)) +} + +func burnObl(addr pusers.AddressOrName) { + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) + obl.Burn(addr, obl.BalanceOf(addr)) +} + +func burnUsdc(addr pusers.AddressOrName) { + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) + usdc.Burn(addr, usdc.BalanceOf(addr)) +} + +func TestBeforeResetObject(t *testing.T) { + // make some data + pools = make(poolMap) + pools["gno.land/r/gnoswap/v1/gns:gno.land/r/onbloc/usdc"] = &Pool{ + token0Path: "gno.land/r/gnoswap/v1/gns", + token1Path: "gno.land/r/onbloc/usdc", + } + + slot0FeeProtocol = 1 + poolCreationFee = 100_000_000 + withdrawalFeeBPS = 100 + + // transfer some tokens + std.TestSetRealm(std.NewUserRealm(users.Resolve(admin))) + foo.Transfer(a2u(addr01), 100_000_000) + bar.Transfer(a2u(addr01), 100_000_000) + + uassert.Equal(t, foo.BalanceOf(a2u(addr01)), uint64(100_000_000)) + uassert.Equal(t, bar.BalanceOf(a2u(addr01)), uint64(100_000_000)) +} + +func TestResetObject(t *testing.T) { + resetObject(t) + uassert.Equal(t, len(pools), 0) + uassert.Equal(t, slot0FeeProtocol, uint8(0)) + uassert.Equal(t, poolCreationFee, uint64(100_000_000)) + uassert.Equal(t, withdrawalFeeBPS, uint64(100)) +} + +func TestBurnTokens(t *testing.T) { + burnTokens(t) + + uassert.Equal(t, foo.BalanceOf(a2u(addr01)), uint64(0)) // 100_000_000 -> 0 + uassert.Equal(t, bar.BalanceOf(a2u(addr01)), uint64(0)) // 100_000_000 -> 0 +} diff --git a/pool/api_test.gno b/pool/api_test.gno index 00952a95c..58031e921 100644 --- a/pool/api_test.gno +++ b/pool/api_test.gno @@ -37,7 +37,7 @@ func TestApiGetPools(t *testing.T) { panic(err.Error()) } - uassert.Equal(t, response.Size(), 2) // should be same as the number of pools + uassert.Equal(t, 2, response.Size()) // should be same as the number of pools } func TestApiGetPool(t *testing.T) { @@ -70,9 +70,6 @@ func TestMakeStatNode(t *testing.T) { statHeight, _ := statNode.GetKey("height") uassert.Equal(t, statHeight.String(), "124") - - statTimestamp, _ := statNode.GetKey("timestamp") - uassert.Equal(t, statTimestamp.String(), "1234567895") }) } diff --git a/pool/getter_test.gno b/pool/getter_test.gno index 015abd3d9..1772cc228 100644 --- a/pool/getter_test.gno +++ b/pool/getter_test.gno @@ -7,7 +7,9 @@ import ( u256 "gno.land/p/gnoswap/uint256" ) -func init() { +func TestInitData(t *testing.T) { + resetObject(t) + mockPool := &Pool{ token0Path: "token0", token1Path: "token1", diff --git a/pool/helper_test.gno b/pool/helper_test.gno deleted file mode 100644 index 3668ec1c6..000000000 --- a/pool/helper_test.gno +++ /dev/null @@ -1,118 +0,0 @@ -package pool - -import ( - "std" - "testing" - - "gno.land/p/demo/testutils" - "gno.land/p/demo/uassert" - pusers "gno.land/p/demo/users" - - "gno.land/r/onbloc/bar" - "gno.land/r/onbloc/baz" - "gno.land/r/onbloc/foo" - "gno.land/r/onbloc/obl" - "gno.land/r/onbloc/qux" - "gno.land/r/onbloc/usdc" -) - -// define addresses to use in tests -const ( - addr01 = testutils.TestAddress("addr01") - addr02 = testutils.TestAddress("addr02") -) - -// addresses used in tests -var addrUsedInTest = []std.Address{ - addr01, - addr02, -} - -// resetObject resets the object state(clear or make it default values) -func resetObject(t *testing.T) { - pools = make(poolMap) - slot0FeeProtocol = 0 - poolCreationFee = 100_000_000 - withdrawalFee = 100 -} - -func burnTokens(t *testing.T) { - t.Helper() - - // burn tokens - for _, addr := range addrUsedInTest { - uAddr := a2u(addr) - burnFoo(uAddr) - burnBar(uAddr) - burnBaz(uAddr) - burnQux(uAddr) - burnObl(uAddr) - burnUsdc(uAddr) - } -} - -func burnFoo(addr pusers.AddressOrName) { - std.TestSetRealm(adminRealm) - foo.Burn(addr, foo.BalanceOf(addr)) -} - -func burnBar(addr pusers.AddressOrName) { - std.TestSetRealm(adminRealm) - bar.Burn(addr, bar.BalanceOf(addr)) -} - -func burnBaz(addr pusers.AddressOrName) { - std.TestSetRealm(adminRealm) - baz.Burn(addr, baz.BalanceOf(addr)) -} - -func burnQux(addr pusers.AddressOrName) { - std.TestSetRealm(adminRealm) - qux.Burn(addr, qux.BalanceOf(addr)) -} - -func burnObl(addr pusers.AddressOrName) { - std.TestSetRealm(adminRealm) - obl.Burn(addr, obl.BalanceOf(addr)) -} - -func burnUsdc(addr pusers.AddressOrName) { - std.TestSetRealm(adminRealm) - usdc.Burn(addr, usdc.BalanceOf(addr)) -} - -func TestBeforeResetObject(t *testing.T) { - // make some data - pools = make(poolMap) - pools["gno.land/r/gnoswap/v1/gns:gno.land/r/onbloc/usdc"] = &Pool{ - token0Path: "gno.land/r/gnoswap/v1/gns", - token1Path: "gno.land/r/onbloc/usdc", - } - - slot0FeeProtocol = 1 - poolCreationFee = 100_000_000 - withdrawalFee = 100 - - // transfer some tokens - std.TestSetRealm(adminRealm) - foo.Transfer(a2u(addr01), 100_000_000) - bar.Transfer(a2u(addr01), 100_000_000) - - uassert.Equal(t, foo.BalanceOf(a2u(addr01)), uint64(100_000_000)) - uassert.Equal(t, bar.BalanceOf(a2u(addr01)), uint64(100_000_000)) -} - -func TestResetObject(t *testing.T) { - resetObject(t) - uassert.Equal(t, len(pools), 0) - uassert.Equal(t, slot0FeeProtocol, uint8(0)) - uassert.Equal(t, poolCreationFee, uint64(100_000_000)) - uassert.Equal(t, withdrawalFee, uint64(100)) -} - -func TestBurnTokens(t *testing.T) { - burnTokens(t) - - uassert.Equal(t, foo.BalanceOf(a2u(addr01)), uint64(0)) // 100_000_000 -> 0 - uassert.Equal(t, bar.BalanceOf(a2u(addr01)), uint64(0)) // 100_000_000 -> 0 -} diff --git a/pool/pool_manager_test.gno b/pool/pool_manager_test.gno index 08550608a..da1eed7c1 100644 --- a/pool/pool_manager_test.gno +++ b/pool/pool_manager_test.gno @@ -2,137 +2,201 @@ package pool import ( "std" + "strings" "testing" - "gno.land/p/demo/json" - "gno.land/p/demo/uassert" + "gno.land/r/gnoswap/v1/consts" +) - "gno.land/r/onbloc/bar" - "gno.land/r/onbloc/baz" - "gno.land/r/onbloc/foo" +func TestPoolMapOperations(t *testing.T) { + pm := make(poolMap) - "gno.land/r/gnoswap/v1/common" - "gno.land/r/gnoswap/v1/consts" + poolPath := "token0:token1:500" + params := newPoolParams("token0", "token1", 500, "4295128740") + pool := newPool(params) - "gno.land/r/gnoswap/v1/gns" -) + pm.Set(poolPath, pool) -var ( - test_tickLower = int32(9000) - test_tickUpper = int32(11000) - test_liquidityExpectStr = "100000000" -) + retrieved, exists := pm.Get(poolPath) + if !exists { + t.Error("Expected pool to exist") + } + if retrieved != pool { + t.Error("Retrieved pool doesn't match original") + } -// 1. Create Foo:Bar Pool -func TestCreateFooBarPool(t *testing.T) { - std.TestSkipHeights(100) - - uassert.Equal(t, gns.TotalSupply(), uint64(100000000000000)) - uassert.Equal(t, gnsBalance(t, consts.EMISSION_ADDR), uint64(0)) - uassert.Equal(t, gnsBalance(t, consts.STAKER_ADDR), uint64(0)) - uassert.Equal(t, gnsBalance(t, consts.DEV_OPS), uint64(0)) - - std.TestSetRealm(adminRealm) - gns.Approve(a2u(consts.POOL_ADDR), poolCreationFee) - - t.Run("wrong pool path", func(t *testing.T) { - uassert.PanicsWithMessage( - t, - `[GNOSWAP-POOL-012] tokens must be in lexicographical order || pool_manager.gno__CreatePool() || expected token0Path(gno.land/r/onbloc/foo) < token1Path(gno.land/r/onbloc/bar)`, - func() { - CreatePool(fooPath, barPath, fee500, common.TickMathGetSqrtRatioAtTick(-10000).ToString()) // tick -10000 - }, - ) - }) - - t.Run("create pool", func(t *testing.T) { - CreatePool(barPath, fooPath, fee500, common.TickMathGetSqrtRatioAtTick(-10000).ToString()) // tick -10000 - std.TestSkipHeights(1) - }) - - uassert.Equal(t, gns.TotalSupply(), uint64(100001426940600)) - uassert.Equal(t, gnsBalance(t, consts.EMISSION_ADDR), uint64(0)) - uassert.Equal(t, gnsBalance(t, consts.STAKER_ADDR), uint64(1070205450)) - uassert.Equal(t, gnsBalance(t, consts.DEV_OPS), uint64(285388120)) + _, exists = pm.Get("nonexistent") + if exists { + t.Error("Expected pool to not exist") + } } -// 2. Create Bar:Baz Pool -func TestCreateBarBazPool(t *testing.T) { - std.TestSetRealm(adminRealm) +func TestNewPoolParams(t *testing.T) { + params := newPoolParams( + "token0", + "token1", + 500, // 0.05% fee + "1000000", // example sqrt price + ) - gns.Approve(a2u(consts.POOL_ADDR), poolCreationFee) - std.TestSkipHeights(1) + if params.Token0Path() != "token0" { + t.Errorf("Expected token0Path to be 'token0', got %s", params.Token0Path()) + } - CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick 10000 - std.TestSkipHeights(1) -} + if params.TickSpacing() != 10 { // 500 fee should have 10 tick spacing + t.Errorf("Expected tick spacing 10, got %d", params.TickSpacing()) + } -// 3. Mint Foo:Bar Liquidity by admin -func TestMintFooBarLiquidity(t *testing.T) { - std.TestSetRealm(adminRealm) - - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(4) - - std.TestSetRealm(posRealm) - amount0, amount1 := Mint( - barPath, - fooPath, - fee500, - consts.POSITION_ADDR, - -11000, // -11000 - -9000, // -9000 - test_liquidityExpectStr, - admin, + if !params.isInOrder() { + t.Errorf("Expected token0Path(token0) < token1Path(token1)") + } + + params = newPoolParams( + consts.GNOT, + "token1", + 500, + "1000000", ) - uassert.Equal(t, amount0, "8040316") - uassert.Equal(t, amount1, "2958015") - std.TestSkipHeights(1) -} + token0, token1 := params.wrap() + if token0 != consts.WRAPPED_WUGNOT { + t.Errorf("Expected GNOT to be wrapped to WUGNOT") + } -// 4. Mint Bar:Baz Liquidity by admin -func TestMintBarBazLiquidity(t *testing.T) { - std.TestSetRealm(adminRealm) - - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - std.TestSetRealm(posRealm) - amount0, amount1 := Mint( - barPath, - bazPath, - fee500, - consts.POSITION_ADDR, - test_tickLower, - test_tickUpper, - test_liquidityExpectStr, - admin, + params = newPoolParams( + "token0", + "token0", + 500, + "1000000", ) - uassert.Equal(t, amount0, "2958015") - uassert.Equal(t, amount1, "8040316") - std.TestSkipHeights(1) + if !params.isSameTokenPath() { + t.Errorf("Expected token0Path(token0) == token1Path(token0)") + } } -// GETTER_API TEST -func TestApiGetPools(t *testing.T) { - gpls := ApiGetPools() +func TestGetPoolPath(t *testing.T) { + path := GetPoolPath("tokenA", "tokenB", 500) + expected := "tokenA:tokenB:500" + if path != expected { + t.Errorf("Expected path %s, got %s", expected, path) + } - root, err := json.Unmarshal([]byte(gpls)) - if err != nil { - panic(err.Error()) + path = GetPoolPath("tokenB", "tokenA", 500) + if path != expected { + t.Errorf("Expected tokens to be sorted, expected %s, got %s", expected, path) } +} - response, err := root.GetKey("response") - if err != nil { - panic(err.Error()) +func TestTickSpacingMap(t *testing.T) { + tests := []struct { + fee uint32 + tickSpacing int32 + }{ + {100, 1}, // 0.01% + {500, 10}, // 0.05% + {3000, 60}, // 0.3% + {10000, 200}, // 1% } - uassert.Equal(t, response.Size(), 2) + for _, tt := range tests { + spacing := feeAmountTickSpacing.Get(tt.fee) + if spacing != tt.tickSpacing { + t.Errorf("For fee %d, expected tick spacing %d, got %d", + tt.fee, tt.tickSpacing, spacing) + } + } } -func gnsBalance(t *testing.T, addr std.Address) uint64 { - t.Helper() - return gns.BalanceOf(a2u(addr)) +func TestCreatePool(t *testing.T) { + tests := []struct { + name string + token0Path string + token1Path string + fee uint32 + sqrtPrice string + shouldPanic bool + panicMsg string + }{ + { + name: "success - normal token pair", + token0Path: "test/token0", + token1Path: "test/token1", + fee: 3000, + sqrtPrice: "4295128740", + }, + { + name: "fail - same tokens", + token0Path: "test/token0", + token1Path: "test/token0", + fee: 3000, + sqrtPrice: "4295128740", + shouldPanic: true, + panicMsg: "[GNOSWAP-POOL-011] same token used in single pool || pool_manager.gno__CreatePool() || expected token0Path(test/token0) != token1Path(test/token0", + }, + { + name: "fail - tokens not in order", + token0Path: "test/tokenB", + token1Path: "test/tokenA", + fee: 3000, + sqrtPrice: "4295128740", + shouldPanic: true, + panicMsg: "[GNOSWAP-POOL-012] tokens must be in lexicographical order || pool_manager.gno__CreatePool() || expected token0Path(test/tokenB) < token1Path(test/tokenA)", + }, + { + name: "fail - pool already exists", + token0Path: "test/token0", + token1Path: "test/token1", + fee: 3000, + sqrtPrice: "4295128740", + shouldPanic: true, + panicMsg: "[GNOSWAP-POOL-013] pool already created || pool_manager.gno__CreatePool() || expected poolPath(test/token0:test/token1:3000) not to exist", + }, + } + + std.TestSetRealm(std.NewUserRealm(consts.ADMIN)) + SetPoolCreationFeeByAdmin(0) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.shouldPanic { + defer func() { + r := recover() + if r == nil { + t.Errorf("expected panic but got none") + return + } + errMsg := string(r) + if !strings.Contains(errMsg, tt.panicMsg) { + t.Errorf("expected panic message containing %q but got %q", tt.panicMsg, errMsg) + } + }() + } + + CreatePool(tt.token0Path, tt.token1Path, tt.fee, tt.sqrtPrice) + + if !tt.shouldPanic { + // verify pool was created correctly + poolPath := GetPoolPath(tt.token0Path, tt.token1Path, tt.fee) + pool, exists := pools.Get(poolPath) + if !exists { + t.Errorf("pool was not created") + return + } + + // check if GNOT was properly wrapped + expectedToken0 := tt.token0Path + expectedToken1 := tt.token1Path + if expectedToken0 == consts.GNOT { + expectedToken0 = consts.WRAPPED_WUGNOT + } + if expectedToken1 == consts.GNOT { + expectedToken1 = consts.WRAPPED_WUGNOT + } + + if pool.token0Path != expectedToken0 || pool.token1Path != expectedToken1 { + t.Errorf("incorrect token paths in pool. got %s,%s want %s,%s", + pool.token0Path, pool.token1Path, expectedToken0, expectedToken1) + } + } + }) + } } diff --git a/pool/position_test.gno b/pool/position_test.gno index 93173a0c4..46928b6a8 100644 --- a/pool/position_test.gno +++ b/pool/position_test.gno @@ -47,13 +47,13 @@ func TestPositionUpdateWithKey(t *testing.T) { var positionKey string t.Run("set up initial data for this test function", func(t *testing.T) { - dummyPool = newPool( + poolParams := newPoolParams( "token0", "token1", 100, - 10, - common.TickMathGetSqrtRatioAtTick(0), + common.TickMathGetSqrtRatioAtTick(0).ToString(), ) + dummyPool = newPool(poolParams) positionKey = positionGetKey( testutils.TestAddress("dummyAddr"), diff --git a/pool/protocol_fee_pool_creation_test.gno b/pool/protocol_fee_pool_creation_test.gno index 5ff3e5369..0ce6b43ae 100644 --- a/pool/protocol_fee_pool_creation_test.gno +++ b/pool/protocol_fee_pool_creation_test.gno @@ -82,7 +82,7 @@ func TestSetPoolCreationFee(t *testing.T) { func TestSetPoolCreationFeeByAdmin(t *testing.T) { var ( - admin = pusers.AddressOrName("g1lmvrrrr4er2us84h2732sru76c9zl2nvknha8c") + admin = pusers.AddressOrName(consts.ADMIN) alice = pusers.AddressOrName(testutils.TestAddress("alice")) ) diff --git a/pool/protocol_fee_withdrawal_test.gno b/pool/protocol_fee_withdrawal_test.gno index 0ce69e912..6cbec74e9 100644 --- a/pool/protocol_fee_withdrawal_test.gno +++ b/pool/protocol_fee_withdrawal_test.gno @@ -15,7 +15,7 @@ import ( func TestHandleWithdrawalFee(t *testing.T) { var ( - admin = pusers.AddressOrName("g1lmvrrrr4er2us84h2732sru76c9zl2nvknha8c") + admin = pusers.AddressOrName(consts.ADMIN) position = pusers.AddressOrName(consts.POSITION_ADDR) pool = pusers.AddressOrName(consts.POOL_ADDR) protocolFee = pusers.AddressOrName(consts.PROTOCOL_FEE_ADDR) @@ -35,7 +35,7 @@ func TestHandleWithdrawalFee(t *testing.T) { HandleWithdrawalFee(0, "", "0", "", "0", "", users.Resolve(admin)) }, verify: nil, - expected: "[GNOSWAP-POOL-001] caller has no permission || withdrawal_fee.gno__HandleWithdrawalFee() || only position(g1q646ctzhvn60v492x8ucvyqnrj2w30cwh6efk5) can call this function, called from g1lmvrrrr4er2us84h2732sru76c9zl2nvknha8c", + expected: "[GNOSWAP-POOL-001] caller has no permission || withdrawal_fee.gno__HandleWithdrawalFee() || only position(g1q646ctzhvn60v492x8ucvyqnrj2w30cwh6efk5) can call this function, called from g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d", shouldPanic: true, }, { @@ -143,7 +143,7 @@ func TestHandleWithdrawalFee(t *testing.T) { func TestSetWithdrawalFee(t *testing.T) { var ( - admin = pusers.AddressOrName("g1lmvrrrr4er2us84h2732sru76c9zl2nvknha8c") + admin = pusers.AddressOrName(consts.ADMIN) alice = pusers.AddressOrName(testutils.TestAddress("alice")) ) tests := []struct { @@ -178,7 +178,7 @@ func TestSetWithdrawalFee(t *testing.T) { } }, verify: nil, - expected: "caller(g1lmvrrrr4er2us84h2732sru76c9zl2nvknha8c) has no permission", + expected: "caller(g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d) has no permission", shouldPanic: true, }, { @@ -239,7 +239,7 @@ func TestSetWithdrawalFee(t *testing.T) { func TestSetWithdrawalFeeByAdmin(t *testing.T) { var ( - admin = pusers.AddressOrName("g1lmvrrrr4er2us84h2732sru76c9zl2nvknha8c") + admin = pusers.AddressOrName(consts.ADMIN) alice = pusers.AddressOrName(testutils.TestAddress("alice")) governance = pusers.AddressOrName(consts.GOV_GOVERNANCE_ADDR) ) diff --git a/pool/tests/__TEST_0_INIT_TOKEN_REGISTER_test.gnoA b/pool/tests/__TEST_0_INIT_TOKEN_REGISTER_test.gnoA new file mode 100644 index 000000000..6702bd674 --- /dev/null +++ b/pool/tests/__TEST_0_INIT_TOKEN_REGISTER_test.gnoA @@ -0,0 +1,183 @@ +package pool + +import ( + "std" + "testing" + + "gno.land/p/demo/testutils" + "gno.land/p/demo/uassert" + + "gno.land/r/onbloc/foo" + + "gno.land/r/onbloc/bar" + + "gno.land/r/onbloc/baz" + + "gno.land/r/onbloc/qux" + + "gno.land/r/demo/wugnot" + + "gno.land/r/onbloc/obl" + + "gno.land/r/gnoswap/v1/gns" + + "gno.land/r/gnoswap/v1/consts" + + pusers "gno.land/p/demo/users" +) + +type FooToken struct{} + +func (FooToken) Transfer() func(to pusers.AddressOrName, amount uint64) { + return foo.Transfer +} +func (FooToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) { + return foo.TransferFrom +} +func (FooToken) BalanceOf() func(owner pusers.AddressOrName) uint64 { + return foo.BalanceOf +} +func (FooToken) Approve() func(spender pusers.AddressOrName, amount uint64) { + return foo.Approve +} + +type BarToken struct{} + +func (BarToken) Transfer() func(to pusers.AddressOrName, amount uint64) { + return bar.Transfer +} +func (BarToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) { + return bar.TransferFrom +} +func (BarToken) BalanceOf() func(owner pusers.AddressOrName) uint64 { + return bar.BalanceOf +} +func (BarToken) Approve() func(spender pusers.AddressOrName, amount uint64) { + return bar.Approve +} + +type BazToken struct{} + +func (BazToken) Transfer() func(to pusers.AddressOrName, amount uint64) { + return baz.Transfer +} +func (BazToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) { + return baz.TransferFrom +} +func (BazToken) BalanceOf() func(owner pusers.AddressOrName) uint64 { + return baz.BalanceOf +} +func (BazToken) Approve() func(spender pusers.AddressOrName, amount uint64) { + return baz.Approve +} + +type QuxToken struct{} + +func (QuxToken) Transfer() func(to pusers.AddressOrName, amount uint64) { + return qux.Transfer +} +func (QuxToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) { + return qux.TransferFrom +} +func (QuxToken) BalanceOf() func(owner pusers.AddressOrName) uint64 { + return qux.BalanceOf +} +func (QuxToken) Approve() func(spender pusers.AddressOrName, amount uint64) { + return qux.Approve +} + +type WugnotToken struct{} + +func (WugnotToken) Transfer() func(to pusers.AddressOrName, amount uint64) { + return wugnot.Transfer +} +func (WugnotToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) { + return wugnot.TransferFrom +} +func (WugnotToken) BalanceOf() func(owner pusers.AddressOrName) uint64 { + return wugnot.BalanceOf +} +func (WugnotToken) Approve() func(spender pusers.AddressOrName, amount uint64) { + return wugnot.Approve +} + +type OBLToken struct{} + +func (OBLToken) Transfer() func(to pusers.AddressOrName, amount uint64) { + return obl.Transfer +} +func (OBLToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) { + return obl.TransferFrom +} +func (OBLToken) BalanceOf() func(owner pusers.AddressOrName) uint64 { + return obl.BalanceOf +} +func (OBLToken) Approve() func(spender pusers.AddressOrName, amount uint64) { + return obl.Approve +} + +type GNSToken struct{} + +func (GNSToken) Transfer() func(to pusers.AddressOrName, amount uint64) { + return gns.Transfer +} + +func (GNSToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) { + return gns.TransferFrom +} + +func (GNSToken) BalanceOf() func(owner pusers.AddressOrName) uint64 { + return gns.BalanceOf +} + +func (GNSToken) Approve() func(spender pusers.AddressOrName, amount uint64) { + return gns.Approve +} + +func init() { + std.TestSetRealm(std.NewUserRealm(consts.TOKEN_REGISTER)) + + RegisterGRC20Interface("gno.land/r/onbloc/bar", BarToken{}) + RegisterGRC20Interface("gno.land/r/onbloc/foo", FooToken{}) + RegisterGRC20Interface("gno.land/r/onbloc/baz", BazToken{}) + RegisterGRC20Interface("gno.land/r/onbloc/qux", QuxToken{}) + RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + RegisterGRC20Interface("gno.land/r/onbloc/obl", OBLToken{}) + RegisterGRC20Interface("gno.land/r/gnoswap/v1/gns", GNSToken{}) +} + +func TestGetRegisteredTokens(t *testing.T) { + uassert.Equal(t, len(GetRegisteredTokens()), 7) +} + +func TestRegisterGRC20Interface(t *testing.T) { + uassert.PanicsWithMessage(t, + `[GNOSWAP-POOL-001] caller has no permission || token_register.gno__RegisterGRC20Interface() || only register(g1er355fkjksqpdtwmhf5penwa82p0rhqxkkyhk5) can register token, called from g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm`, + func() { + RegisterGRC20Interface("gno.land/r/onbloc/bar", BarToken{}) + }, + ) +} + +func TestUnregisterGRC20Interface(t *testing.T) { + dummy := testutils.TestAddress("dummy") + std.TestSetRealm(std.NewUserRealm(dummy)) + + uassert.PanicsWithMessage(t, + `[GNOSWAP-POOL-001] caller has no permission || token_register.gno__UnregisterGRC20Interface() || unauthorized address(g1v36k6mteta047h6lta047h6lta047h6lz7gmv8) to unregister`, + func() { + UnregisterGRC20Interface("gno.land/r/onbloc/bar") + }, + ) + + uassert.Equal(t, len(GetRegisteredTokens()), 7) + + std.TestSetRealm(std.NewUserRealm(consts.TOKEN_REGISTER)) + UnregisterGRC20Interface("gno.land/r/onbloc/bar") + uassert.Equal(t, len(GetRegisteredTokens()), 6) + + // re-register to avoid panic in other tests + RegisterGRC20Interface("gno.land/r/onbloc/bar", BarToken{}) + + std.TestSetRealm(adminRealm) +} diff --git a/pool/tests/__TEST_0_INIT_VARIABLE_AND_HELPER_test.gno b/pool/tests/__TEST_0_INIT_VARIABLE_AND_HELPER_test.gnoA similarity index 100% rename from pool/tests/__TEST_0_INIT_VARIABLE_AND_HELPER_test.gno rename to pool/tests/__TEST_0_INIT_VARIABLE_AND_HELPER_test.gnoA