From 2b017573b744dd210f4496e7a89df1f99e15a1a0 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 30 Apr 2024 18:19:01 +0900 Subject: [PATCH 1/9] fix: terminate functions --- gov/proposal_community_pool_spend.gno | 1 + router/router_dry.gno | 2 ++ 2 files changed, 3 insertions(+) diff --git a/gov/proposal_community_pool_spend.gno b/gov/proposal_community_pool_spend.gno index 2053fb26..f676d86c 100644 --- a/gov/proposal_community_pool_spend.gno +++ b/gov/proposal_community_pool_spend.gno @@ -27,6 +27,7 @@ func (p ProposalCommunityPoolSpend) isValid() bool { return false } + return true } func (p ProposalCommunityPoolSpend) execute() error { diff --git a/router/router_dry.gno b/router/router_dry.gno index 81bf6386..a9dc8c8c 100644 --- a/router/router_dry.gno +++ b/router/router_dry.gno @@ -112,4 +112,6 @@ func DrySwapRoute( } return resultAmountIn.ToString() } + + panic("[ROUTER] router_dry.gno__DrySwapRoute() || unknown swapType") } From 582d6c46c2ed39893cd44d34254ab1024f67ddbe Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 30 Apr 2024 20:54:08 +0900 Subject: [PATCH 2/9] fix: more gas --- __local/test/test_data.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__local/test/test_data.mk b/__local/test/test_data.mk index 03779927..8fc72507 100644 --- a/__local/test/test_data.mk +++ b/__local/test/test_data.mk @@ -483,7 +483,7 @@ swap-exact-in-multi-foo-to-gns-to-wugnot: # approve OUTPUT TOKEN to ROUTER ( as 0.15% fee ) @echo "" | gnokey maketx call -pkgpath gno.land/r/demo/wugnot -func Approve -args $(ADDR_ROUTER) -args $(MAX_UINT64) -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gnoswap_tr01 > /dev/null - @echo "" | gnokey maketx call -pkgpath gno.land/r/demo/router -func SwapRoute -args "gno.land/r/demo/foo" -args "gno.land/r/demo/wugnot" -args 50000 -args "EXACT_IN" -args "gno.land/r/demo/foo:gno.land/r/demo/gns:500*POOL*gno.land/r/demo/gns:gno.land/r/demo/wugnot:500" -args "100" -args "1" -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 10000000 -memo "" gnoswap_tr01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/demo/router -func SwapRoute -args "gno.land/r/demo/foo" -args "gno.land/r/demo/wugnot" -args 50000 -args "EXACT_IN" -args "gno.land/r/demo/foo:gno.land/r/demo/gns:500*POOL*gno.land/r/demo/gns:gno.land/r/demo/wugnot:500" -args "100" -args "1" -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 20000000 -memo "" gnoswap_tr01 > /dev/null @echo @$(MAKE) -f $(MAKEFILE) print-fee-collector From 60379a177446c03cc7f95bdc70fc6e092a2ef9c5 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 30 Apr 2024 20:54:34 +0900 Subject: [PATCH 3/9] test: unit test & integeration test for `gov` --- ...al_test.gn => _TEST_gov_proposal_test.gno} | 0 gov/_TEST_/z_proposal_filetest.gno | 97 +++++++++++++++++++ 2 files changed, 97 insertions(+) rename gov/_TEST_/{_TEST_proposal_test.gn => _TEST_gov_proposal_test.gno} (100%) create mode 100644 gov/_TEST_/z_proposal_filetest.gno diff --git a/gov/_TEST_/_TEST_proposal_test.gn b/gov/_TEST_/_TEST_gov_proposal_test.gno similarity index 100% rename from gov/_TEST_/_TEST_proposal_test.gn rename to gov/_TEST_/_TEST_gov_proposal_test.gno diff --git a/gov/_TEST_/z_proposal_filetest.gno b/gov/_TEST_/z_proposal_filetest.gno new file mode 100644 index 00000000..58b97991 --- /dev/null +++ b/gov/_TEST_/z_proposal_filetest.gno @@ -0,0 +1,97 @@ +package main + +import ( + "fmt" + "time" + + "gno.land/r/demo/gov" +) + +func main() { + TestSubmitProposal() + + // CANNOT TEST THE FOLLOWING DUE TO THE LACK OF `process` FUNCTION in the integration filetest + // TestSubmitProposalFailedDeposit() + // TestSubmitProposalRejected(nil) + // TestSubmitProposalCommunityPoolSpend(nil) + // TestSubmitProposalMint(nil) + // TestProposalParameterChange(nil) + // TestProposalTally(nil) +} + +func TestSubmitProposal() { + gov.Init() + + id := gov.SubmitProposalText( + "title", // title + "summary", // summary + "metadata", // metadata + 0, // initial deposit + ) + + now := uint64(time.Now().Unix()) + + if id != 1 { + panic("proposal id should be 1") + } + + proposal := gov.GetProposalById(id) + + if proposal.Title != "title" { + panic("proposal title should be title") + } + + if proposal.Summary != "summary" { + panic("proposal summary should be summary") + } + + if proposal.Metadata != "metadata" { + panic("proposal metadata should be metadata") + } + + if proposal.ProposalStatus != gov.ProposalStatusDepositPeriod { + panic("proposal status should be deposit period") + } + + if proposal.SubmitTime != now { + panic("proposal submit time should be now") + } + + // 86400 == getDepositPeriod() + if proposal.DepositEndTime != now+86400 { + panic(fmt.Sprintf("proposal deposit end time should be now + deposit period, %d, %d", proposal.DepositEndTime, now+86400)) + } + + if proposal.VotingEndTime != 0 { + panic("proposal voting end time should be 0") + } + + if proposal.TotalDeposit != 0 { + panic("proposal total deposits should be 0") + } + + // 10_000_000 == getDepositMinimum() + // force advance to voting period + proposal.TotalDeposit = 10_000_000 + 1 + + /* + CAN NOT `process` in the integration filetest + + // 86400 == getDepositPeriod() + if proposal.process(now + 86400 + 1) { + panic("proposal process should not halt") + } + + // force advance to voting passed + proposal.YesVotes = 100 + proposal.NoVotes = 0 + + // 259200 == getVotePeriod() + if proposal.process(now + 86400 + 1 + 259200 + 1) { + panic("proposal process should not halt") + } + */ +} + +// OUTPUT: +// From c508d22057c90b11f9754f4f22ab8d6f4c0d677b Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 30 Apr 2024 20:54:54 +0900 Subject: [PATCH 4/9] test: integeration test for `pool` --- pool/_TEST_/_TEST_0_INIT_FAUCET_test.gno | 25 -- .../_TEST_0_INIT_TOKEN_REGISTER_test.gno | 178 -------- .../_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno | 73 ---- .../_TEST_pool_dryswap_and_swap_test.gn | 127 ------ pool/_TEST_/_TEST_pool_multi_token_test.gn | 378 ----------------- pool/_TEST_/_TEST_pool_native_swap_test.gn | 135 ------- pool/_TEST_/_TEST_pool_single_lp_test.gn | 379 ------------------ pool/_TEST_/_TEST_pool_test.gn | 124 ------ pool/_TEST_/z1_single_lp_filetest.gno | 4 +- pool/_TEST_/z2_native_swap_filetest.gno | 307 ++++++++++++++ pool/_TEST_/z3_dryswap_and_swap_filetest.gno | 309 ++++++++++++++ 11 files changed, 618 insertions(+), 1421 deletions(-) delete mode 100644 pool/_TEST_/_TEST_0_INIT_FAUCET_test.gno delete mode 100644 pool/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno delete mode 100644 pool/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno delete mode 100644 pool/_TEST_/_TEST_pool_dryswap_and_swap_test.gn delete mode 100644 pool/_TEST_/_TEST_pool_multi_token_test.gn delete mode 100644 pool/_TEST_/_TEST_pool_native_swap_test.gn delete mode 100644 pool/_TEST_/_TEST_pool_single_lp_test.gn delete mode 100644 pool/_TEST_/_TEST_pool_test.gn create mode 100644 pool/_TEST_/z2_native_swap_filetest.gno create mode 100644 pool/_TEST_/z3_dryswap_and_swap_filetest.gno diff --git a/pool/_TEST_/_TEST_0_INIT_FAUCET_test.gno b/pool/_TEST_/_TEST_0_INIT_FAUCET_test.gno deleted file mode 100644 index 354c35ce..00000000 --- a/pool/_TEST_/_TEST_0_INIT_FAUCET_test.gno +++ /dev/null @@ -1,25 +0,0 @@ -package pool - -import ( - "std" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/foo" - "gno.land/r/demo/obl" - "gno.land/r/demo/qux" -) - -func init() { - std.TestSetPrevAddr(test1) - - // GIVE 100_000_000_000(u) ≈ 100_000 - for i := 0; i < 100; i++ { - foo.Faucet() - bar.Faucet() - baz.Faucet() - qux.Faucet() - obl.Faucet() - //gns.Faucet() - } -} diff --git a/pool/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno b/pool/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno deleted file mode 100644 index 3b7b6068..00000000 --- a/pool/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno +++ /dev/null @@ -1,178 +0,0 @@ -package pool - -import ( - "std" - "testing" - - "gno.land/p/demo/testutils" - - "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/gnoswap/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.TestSetOrigCaller(consts.GNOSWAP_ADMIN) - - RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) - RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) - RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) - RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) - RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) - RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) - RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) -} - -func TestGetRegisteredTokens(t *testing.T) { - shouldEQ(t, len(GetRegisteredTokens()), 7) -} - -func TestRegisterGRC20Interface(t *testing.T) { - shouldPanic(t, - func() { - RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) - }, - ) -} - -func TestUnregisterGRC20Interface(t *testing.T) { - dummy := testutils.TestAddress("dummy") - std.TestSetOrigCaller(dummy) - - shouldPanic(t, - func() { - UnregisterGRC20Interface("gno.land/r/demo/bar") - }, - ) - - shouldEQ(t, len(GetRegisteredTokens()), 7) - - std.TestSetOrigCaller(consts.GNOSWAP_ADMIN) - UnregisterGRC20Interface("gno.land/r/demo/bar") - shouldEQ(t, len(GetRegisteredTokens()), 6) - - // re-register to avoid panic in other tests - RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) -} diff --git a/pool/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno b/pool/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno deleted file mode 100644 index 7ec3977a..00000000 --- a/pool/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno +++ /dev/null @@ -1,73 +0,0 @@ -package pool - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" -) - -var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") - - fooPath string = "gno.land/r/demo/foo" - barPath string = "gno.land/r/demo/bar" - bazPath string = "gno.land/r/demo/baz" - quxPath string = "gno.land/r/demo/qux" - - oblPath string = "gno.land/r/demo/obl" - // wugnotPath string = "gno.land/r/demo/wugnot" // from consts - // gnsPath string = "gno.land/r/demo/gns" // from consts - - fee100 uint32 = 100 - fee500 uint32 = 500 - fee3000 uint32 = 3000 - - maxApprove uint64 = 18446744073709551615 -) - -/* HELPER */ -func shouldEQ(t *testing.T, got, expected interface{}) { - if got != expected { - t.Errorf("got %v, expected %v", got, expected) - } -} - -func shouldNEQ(t *testing.T, got, expected interface{}) { - if got == expected { - t.Errorf("got %v, didn't expected %v", got, expected) - } -} - -func shouldGT(t *testing.T, l, r interface{}) { - if !(l < r) { - t.Errorf("expected %v < %v", l, r) - } -} - -func shouldLT(t *testing.T, l, r interface{}) { - if !(l > r) { - t.Errorf("expected %v > %v", l, r) - } -} - -func shouldPanic(t *testing.T, f func()) { - defer func() { - if r := recover(); r == nil { - t.Errorf("expected panic") - } - }() - f() -} - -func ugnotBalanceOf(addr std.Address) uint64 { - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - - coins := testBanker.GetCoins(addr) - if len(coins) == 0 { - return 0 - } - - return uint64(testBanker.GetCoins(addr)[0].Amount) -} diff --git a/pool/_TEST_/_TEST_pool_dryswap_and_swap_test.gn b/pool/_TEST_/_TEST_pool_dryswap_and_swap_test.gn deleted file mode 100644 index 49f5bb73..00000000 --- a/pool/_TEST_/_TEST_pool_dryswap_and_swap_test.gn +++ /dev/null @@ -1,127 +0,0 @@ -package pool - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - "gno.land/r/demo/bar" - "gno.land/r/demo/foo" - - "gno.land/r/demo/gns" -) - -var ( - sqrtPrice = "130621891405341611593710811006" - - tickLower = int32(9000) - tickUpper = int32(11000) - liquidityExpect = "100000000000" - - currentTick = int32(10000) -) - -func TestCreatePool(t *testing.T) { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - CreatePool(fooPath, barPath, fee500, sqrtPrice) - shouldEQ(t, len(pools), 1) -} - -func TestDrySwap_ZeroForOneTrue_AmountSpecified_Positive_16000(t *testing.T) { - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - - // no mint == no liquidity => swap will fail - _, _, ok := DrySwap(fooPath, barPath, fee500, true, "16000", consts.MIN_PRICE) - shouldEQ(t, ok, false) - - // not enough mint == swap will fail - std.TestSetPrevAddr(test1) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - Mint(fooPath, barPath, fee500, consts.POSITION_ADDR.String(), -tickUpper, -tickLower, "10") - _, _, ok = DrySwap(fooPath, barPath, fee500, true, "16000", consts.MIN_PRICE) - shouldEQ(t, ok, false) - - std.TestSetPrevAddr(test1) - - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - Mint(fooPath, barPath, fee500, consts.POSITION_ADDR.String(), -tickUpper, -tickLower, liquidityExpect) // -11000 ~ -9000 - - // zeroForOne true - // amountSpecified 16000 - poolIn, poolOut, _ := DrySwap( - fooPath, // fooPath - barPath, // barPath - fee500, // fee500 - true, // zeroForOne - "16000", // amountSpecified - consts.MIN_PRICE, // sqrtPriceLimitX96 - ) - shouldEQ(t, poolIn, "16000") - shouldEQ(t, poolOut, "-5883") -} - -func TestDrySwap_ZeroForOneTrue_AmountSpecified_Negative_16000(t *testing.T) { - // zeroForOne true - // amountSpecified -16000 - - poolIn, poolOut, _ := DrySwap( - fooPath, // fooPath - barPath, // barPath - fee500, // fee500 - true, // zeroForOne - "-16000", // amountSpecified - consts.MIN_PRICE, // sqrtPriceLimitX96 - ) - - shouldEQ(t, poolIn, "43512") // r3v4_xxx: ROUNDING ERROR - shouldEQ(t, poolOut, "-16000") -} - -// func TestDrySwap_ZeroForOneFalse_AmountSpecified_Positive_16000(t *testing.T) { -// // zeroForOne false -// // amountSpecified 16000 - -// poolOut, poolIn, _ := DrySwap( -// fooPath, // fooPath -// barPath, // barPath -// fee500, // fee500 -// "_", // recipient -// false, // zeroForOne -// "16000", // amountSpecified -// consts.MAX_PRICE, // sqrtPriceLimitX96 -// ) - -// shouldEQ(t, poolOut, "-43468") -// shouldEQ(t, poolIn, "16000") -// } - -// func TestDrySwap_ZeroForOneFalse_AmountSpecified_Negative_16000(t *testing.T) { -// // zeroForOne false -// // amountSpecified -16000 -// poolOut, poolIn, _ := DrySwap( -// fooPath, // fooPath -// barPath, // barPath -// fee500, // fee500 -// "_", // recipient -// false, // zeroForOne -// "-16000", // amountSpecified -// consts.MAX_PRICE, // sqrtPriceLimitX96 -// ) -// shouldEQ(t, poolOut, "-16000") -// shouldEQ(t, poolIn, "5888") -// } diff --git a/pool/_TEST_/_TEST_pool_multi_token_test.gn b/pool/_TEST_/_TEST_pool_multi_token_test.gn deleted file mode 100644 index e67271a1..00000000 --- a/pool/_TEST_/_TEST_pool_multi_token_test.gn +++ /dev/null @@ -1,378 +0,0 @@ -package pool - -import ( - "std" - "strconv" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/foo" - - "gno.land/r/demo/gns" - - u256 "gno.land/p/demo/gnoswap/uint256" -) - -var ( - test_tickLower = int32(9000) - test_tickUpper = int32(11000) - test_liquidityExpectStr = "100000000" - test_liquidityExpect256 = u256.NewUint(100_000_000) -) - -// Create Foo:Bar Pool -func TestCreateFooBarPool(t *testing.T) { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - - CreatePool(fooPath, barPath, fee500, "130621891405341611593710811006") - shouldEQ(t, len(pools), 1) -} - -// Create Bar:Baz Pool -func TestCreateBarBazPool(t *testing.T) { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - - CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") - shouldEQ(t, len(pools), 2) -} - -// Mint Foo:Bar Liquidity by test1 -func TestMintFooBarLiquidity(t *testing.T) { - std.TestSetPrevAddr(test1) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - amount0, amount1 := Mint( - fooPath, - barPath, - fee500, - consts.POSITION_ADDR.String(), - -test_tickUpper, - -test_tickLower, - test_liquidityExpectStr, - ) - shouldEQ(t, amount0, "8040316") - shouldEQ(t, amount1, "2958014") -} - -// Mint Bar:Baz Liquidity by test1 -func TestMintBarBazLiquidity(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - amount0, amount1 := Mint( - barPath, - bazPath, - fee500, - consts.POSITION_ADDR.String(), - test_tickLower, - test_tickUpper, - test_liquidityExpectStr, - ) - shouldEQ(t, amount0, "2958015") - shouldEQ(t, amount1, "8040315") -} - -// Swap Foo:Bar Foo > Bar by test1 -func TestSwapFooBarFooToBar(t *testing.T) { - oldTest1Bar := balanceOfByRegisterCall(barPath, test1) - oldTest1Foo := balanceOfByRegisterCall(fooPath, test1) - - oldPoolBar := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) - oldPoolFoo := balanceOfByRegisterCall(fooPath, consts.POOL_ADDR) - - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 16000) - - std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) - poolIn, poolOut := Swap( - barPath, - fooPath, - fee500, - test1.String(), - true, - "16000", - consts.MIN_PRICE, - std.GetOrigCaller().String(), - ) - shouldEQ(t, poolIn, "16000") - shouldEQ(t, poolOut, "-5882") - - newTest1Bar := balanceOfByRegisterCall(barPath, test1) - newTest1Foo := balanceOfByRegisterCall(fooPath, test1) - - newPoolBar := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) - newPoolFoo := balanceOfByRegisterCall(fooPath, consts.POOL_ADDR) - - shouldEQ(t, oldTest1Bar-newTest1Bar, 16000) - shouldEQ(t, newTest1Foo-oldTest1Foo, 5882) - - shouldEQ(t, newPoolBar-oldPoolBar, 16000) - shouldEQ(t, oldPoolFoo-newPoolFoo, 5882) -} - -// Swap Bar:Baz Bar > Baz by test1 -func TestSwapBarBazBarToBaz(t *testing.T) { - oldTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - oldtest1BazBalance := balanceOfByRegisterCall(bazPath, test1) - oldPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) - oldPoolBazBalance := balanceOfByRegisterCall(bazPath, consts.POOL_ADDR) - - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 16000) - - std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) - poolIn, poolOut := Swap( - barPath, - bazPath, - fee500, - test1.String(), - true, - "16000", - consts.MIN_PRICE, - std.GetOrigCaller().String(), - ) - shouldEQ(t, poolIn, "16000") - shouldEQ(t, poolOut, "-43457") - - newTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - newtest1BazBalance := balanceOfByRegisterCall(bazPath, test1) - newPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) - newPoolBazBalance := balanceOfByRegisterCall(bazPath, consts.POOL_ADDR) - - shouldEQ(t, oldTest1Token0Balance-newTest1Token0Balance, 16000) - shouldEQ(t, newtest1BazBalance-oldtest1BazBalance, 43457) - shouldEQ(t, newPoolToken0Balance-oldPoolToken0Balance, 16000) - shouldEQ(t, oldPoolBazBalance-newPoolBazBalance, 43457) -} - -// Collect Foo:Bar Fees by test1 -func TestCollectFooBarFees(t *testing.T) { - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - - oldTest1Token1Balance := balanceOfByRegisterCall(fooPath, test1) - oldTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - oldPoolToken1Balance := balanceOfByRegisterCall(fooPath, consts.POOL_ADDR) - oldPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) - - // burn 0 to collect swap fees - Burn(fooPath, barPath, fee500, -test_tickUpper, -test_tickLower, "0") - - c0, c1 := Collect( - fooPath, - barPath, - fee500, - test1.String(), - -test_tickUpper, - -test_tickLower, - "100000", - "100000", - ) - - shouldNEQ(t, c0, "0") // swap was foo > bar, so only foo has fees - shouldEQ(t, c1, "0") // swap was foo > bar, so bar has no fees - - newTest1Token1Balance := balanceOfByRegisterCall(fooPath, test1) - newTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - newPoolToken1Balance := balanceOfByRegisterCall(fooPath, consts.POOL_ADDR) - newPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) - - shouldEQ(t, newTest1Token1Balance-oldTest1Token1Balance, c1) - shouldEQ(t, newTest1Token0Balance-oldTest1Token0Balance, strToUint64(c0)) - shouldEQ(t, oldPoolToken1Balance-newPoolToken1Balance, c1) - shouldEQ(t, oldPoolToken0Balance-newPoolToken0Balance, strToUint64(c0)) -} - -// Collect Bar:Baz Fees by test1 -func TestCollectBarBazFees(t *testing.T) { - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - - oldTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - oldtest1BazBalance := balanceOfByRegisterCall(bazPath, test1) - oldPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) - oldPoolBazBalance := balanceOfByRegisterCall(bazPath, consts.POOL_ADDR) - - // burn 0 to collect swap fees - Burn(barPath, bazPath, fee500, test_tickLower, test_tickUpper, "0") - - c0, c1 := Collect( - barPath, - bazPath, - fee500, - test1.String(), - test_tickLower, - test_tickUpper, - "100000", - "100000", - ) - - shouldNEQ(t, c0, "0") // swap was foo > bar, so only foo has fees - shouldEQ(t, c1, "0") // swap was foo > bar, so bar has no fees - - newTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - newtest1BazBalance := balanceOfByRegisterCall(bazPath, test1) - newPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) - newPoolBazBalance := balanceOfByRegisterCall(bazPath, consts.POOL_ADDR) - - shouldEQ(t, newTest1Token0Balance-oldTest1Token0Balance, strToUint64(c0)) - shouldEQ(t, newtest1BazBalance-oldtest1BazBalance, c1) - shouldEQ(t, oldPoolToken0Balance-newPoolToken0Balance, strToUint64(c0)) - shouldEQ(t, oldPoolBazBalance-newPoolBazBalance, c1) -} - -// Burn Foo:Bar Liquidity by test1 -func TestBurnFooBarLiquidity(t *testing.T) { - std.TestSetOrigCaller(test1) - std.TestSetPrevRealm(consts.POSITION_PATH) - - pool := GetPool(fooPath, barPath, fee500) - poolOldLiquidity := pool.PoolGetLiquidity() - - b0, b1 := Burn( - fooPath, - barPath, - fee500, - -test_tickUpper, - -test_tickLower, - test_liquidityExpectStr, - ) - - shouldNEQ(t, b0, "0") - shouldNEQ(t, b1, "0") - - poolNewLiquidity := pool.PoolGetLiquidity() - - shouldEQ(t, true, new(u256.Uint).Sub(poolOldLiquidity, poolNewLiquidity).Eq(test_liquidityExpect256)) -} - -// Burn Bar:Baz Liquidity by test1 -func TestBurnBarBazLiquidity(t *testing.T) { - std.TestSetOrigCaller(test1) - std.TestSetPrevRealm(consts.POSITION_PATH) - - pool := GetPool(barPath, bazPath, fee500) - poolOldLiquidity := pool.PoolGetLiquidity() - - b0, b1 := Burn( - barPath, - bazPath, - fee500, - test_tickLower, - test_tickUpper, - test_liquidityExpectStr, - ) - - shouldNEQ(t, b0, "0") - shouldNEQ(t, b1, "0") - - poolNewLiquidity := pool.PoolGetLiquidity() - - // shouldEQ(t, poolOldLiquidity-poolNewLiquidity, test_liquidityExpect256) -} - -// Collect Foo:Bar burned Liquidity by test1 -func TestCollectFooBarLiquidity(t *testing.T) { - std.TestSetOrigCaller(test1) - std.TestSetPrevRealm(consts.POSITION_PATH) - - oldTest1Token1Balance := balanceOfByRegisterCall(fooPath, test1) - oldTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - oldPoolToken1Balance := balanceOfByRegisterCall(fooPath, consts.POOL_ADDR) - oldPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) - - c0, c1 := Collect( - fooPath, - barPath, - fee500, - test1.String(), - -test_tickUpper, - -test_tickLower, - "100000", - "100000", - ) - - shouldNEQ(t, c0, "0") - shouldNEQ(t, c1, "0") - - newTest1Token1Balance := balanceOfByRegisterCall(fooPath, test1) - newTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - newPoolToken1Balance := balanceOfByRegisterCall(fooPath, consts.POOL_ADDR) - newPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) - - shouldEQ(t, newTest1Token1Balance-oldTest1Token1Balance, strToUint64(c0)) - shouldEQ(t, newTest1Token0Balance-oldTest1Token0Balance, strToUint64(c1)) - shouldEQ(t, oldPoolToken1Balance-newPoolToken1Balance, strToUint64(c0)) - shouldEQ(t, oldPoolToken0Balance-newPoolToken0Balance, strToUint64(c1)) -} - -// Collect Bar:Baz burned Liquidity by test1 -func TestCollectBarBazLiquidity(t *testing.T) { - std.TestSetOrigCaller(test1) - std.TestSetPrevRealm(consts.POSITION_PATH) - - oldTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - oldtest1BazBalance := balanceOfByRegisterCall(bazPath, test1) - oldPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) - oldPoolBazBalance := balanceOfByRegisterCall(bazPath, consts.POOL_ADDR) - - c0, c1 := Collect( - barPath, - bazPath, - fee500, - test1.String(), - test_tickLower, - test_tickUpper, - "100000", - "100000", - ) - - shouldNEQ(t, c0, "0") - shouldNEQ(t, c1, "0") - - newTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - newtest1BazBalance := balanceOfByRegisterCall(bazPath, test1) - newPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) - newPoolBazBalance := balanceOfByRegisterCall(bazPath, consts.POOL_ADDR) - - shouldEQ(t, newTest1Token0Balance-oldTest1Token0Balance, strToUint64(c0)) - shouldEQ(t, newtest1BazBalance-oldtest1BazBalance, strToUint64(c1)) - shouldEQ(t, oldPoolToken0Balance-newPoolToken0Balance, strToUint64(c0)) - shouldEQ(t, oldPoolBazBalance-newPoolBazBalance, strToUint64(c1)) -} - -// UTILs // -func strToUint64(s string) uint64 { - i, err := strconv.Atoi(s) - if err != nil { - panic("strToUint64") - } - return uint64(i) -} diff --git a/pool/_TEST_/_TEST_pool_native_swap_test.gn b/pool/_TEST_/_TEST_pool_native_swap_test.gn deleted file mode 100644 index b45acd55..00000000 --- a/pool/_TEST_/_TEST_pool_native_swap_test.gn +++ /dev/null @@ -1,135 +0,0 @@ -package pool - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - "gno.land/r/demo/gns" - - "gno.land/r/demo/foo" - "gno.land/r/demo/wugnot" -) - -var ( - test_tickLower = int32(9000) - test_tickUpper = int32(11000) -) - -// 1. Init & Create Pool -func TestPoolInitCreatePool(t *testing.T) { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - CreatePool(fooPath, consts.WRAPPED_WUGNOT, fee500, "130621891405341611593710811006") // x2.7 -} - -// 2. Mint LP and Get GNFT -func TestMint(t *testing.T) { - token0, token1 := fooPath, consts.WRAPPED_WUGNOT - if token1 < token0 { - token0, token1 = token1, token0 - } - - // prepare ugnot - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.IssueCoin(test1, "ugnot", 804_032) - - // simulate transfer & decrase - std.TestSetOrigSend(std.Coins{{"ugnot", 804_032}}, nil) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 804_032) - - // Deposit(wrap) - std.TestSetPrevAddr(test1) - wugnot.Deposit() - - std.TestSetPrevAddr(test1) - foo.Approve(a2u(consts.POOL_ADDR), 295802) - wugnot.Approve(a2u(consts.POOL_ADDR), 804032) - - // Mint - std.TestSetPrevRealm(consts.POSITION_PATH) - amount0, amount1 := Mint( - fooPath, - consts.WRAPPED_WUGNOT, - fee500, - consts.POSITION_ADDR.String(), - test_tickLower, - test_tickUpper, - "10000000", - ) - - shouldEQ(t, amount0, "295802") - shouldEQ(t, amount1, "804031") -} - -func TestSwapBuyNative(t *testing.T) { - pool := GetPool(fooPath, consts.WRAPPED_WUGNOT, fee500) - - test1OldT0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - test1OldT1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - shouldEQ(t, test1OldT0Bal, 9999704198) // foo - shouldEQ(t, test1OldT1Bal, 1) // wugnot - - std.TestSetPrevAddr(test1) - foo.Approve(a2u(consts.POOL_ADDR), 10000) - - std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) - amount0, amount1 := Swap( - fooPath, // token0 - consts.WRAPPED_WUGNOT, // token1 - fee500, - test1.String(), - true, - "10000", - consts.MIN_PRICE, - std.GetOrigCaller().String(), - ) - shouldEQ(t, amount0, "10000") - shouldEQ(t, amount1, "-27123") - - test1NewT0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - test1NewT1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - shouldEQ(t, test1NewT0Bal, 9999694198) // - 10_000 // user sells 10_000 - shouldEQ(t, test1NewT1Bal, 27124) // gets WGNOT -} - -func TestSwapSellNative(t *testing.T) { - pool := GetPool(fooPath, consts.WRAPPED_WUGNOT, fee500) - - test1OldT0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - test1OldT1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - shouldEQ(t, test1OldT0Bal, 9999694198) // foo - shouldEQ(t, test1OldT1Bal, 27124) // wugnot - - std.TestSetPrevAddr(test1) - wugnot.Approve(a2u(consts.POOL_ADDR), 27123) - - std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) - amount0, amount1 := Swap( // ugnot 10_000 > foo ?? - fooPath, // token0 - consts.WRAPPED_WUGNOT, // token1 - fee500, - test1.String(), - false, - "10000", - consts.MAX_PRICE, - std.GetOrigCaller().String(), - ) - shouldEQ(t, amount0, "-3687") // pool sent - shouldEQ(t, amount1, "10000") // pool recv - - test1NewT0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - test1NewT1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - shouldEQ(t, test1NewT0Bal, 9999697885) // foo - shouldEQ(t, test1NewT1Bal, 17124) // wugnot -} diff --git a/pool/_TEST_/_TEST_pool_single_lp_test.gn b/pool/_TEST_/_TEST_pool_single_lp_test.gn deleted file mode 100644 index 342455a2..00000000 --- a/pool/_TEST_/_TEST_pool_single_lp_test.gn +++ /dev/null @@ -1,379 +0,0 @@ -package pool - -import ( - "std" - "strconv" - "testing" - - "gno.land/r/demo/bar" - "gno.land/r/demo/foo" - "gno.land/r/demo/gns" - - "gno.land/r/demo/gnoswap/consts" - - "gno.land/p/demo/json" - - u256 "gno.land/p/demo/gnoswap/uint256" -) - -var ( - test_tickLower = int32(9000) - test_tickUpper = int32(11000) - test_liquidityExpectStr = "1000" - test_liquidityExpect256 = u256.NewUint(1000) - - test_tickLower2 = int32(50000) - test_tickUpper2 = int32(100000) -) - -// 1. reate Pool using Factory Contract by Gnoswap Admin -func TestInitCreatePool(t *testing.T) { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - CreatePool(barPath, fooPath, fee500, "130621891405341611593710811006") // x2.7 -} - -// 2. Mint by test1 -func TestMint(t *testing.T) { - std.TestSetPrevAddr(test1) - - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - amount0, amount1 := Mint( - fooPath, - barPath, - fee500, - consts.POSITION_ADDR.String(), - test_tickLower, - test_tickUpper, - test_liquidityExpectStr, - ) - shouldEQ(t, amount0, "30") - shouldEQ(t, amount1, "80") - - pool := GetPool(barPath, fooPath, fee500) - test_liquidity := pool.PoolGetLiquidity() - shouldEQ(t, test_liquidity.Dec(), test_liquidityExpectStr) - - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - - m81, m82 := Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - m101, m102 := Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - - shouldNEQ(t, m81, "0") - shouldNEQ(t, m82, "0") - shouldEQ(t, m81, m101) - shouldEQ(t, m82, m102) - - test_liquidity = pool.PoolGetLiquidity() - shouldEQ(t, true, test_liquidity.Eq(new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(10)))) - - // tickLower > currentTick == don't add to current liquidity - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower2, test_tickUpper2, test_liquidityExpectStr) - - // tickUpper < current tick == don't add to current liquidity - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), -test_tickUpper2, -test_tickLower2, test_liquidityExpectStr) - - // tickUpper < tickLower == don't add to current liquidity - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), -test_tickUpper, -test_tickLower, test_liquidityExpectStr) - - // test_liquidity = pool.PoolGetLiquidity() - shouldEQ(t, true, test_liquidity.Eq(new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(10)))) - - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, test_liquidityExpectStr) - - test_liquidity = pool.PoolGetLiquidity() - shouldEQ(t, true, test_liquidity.Eq(new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(20)))) - -} - -// 3. Burn by test1 -func TestBurn(t *testing.T) { - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - - b11, b12 := Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, test_liquidityExpectStr) - b21, b22 := Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, test_liquidityExpectStr) - shouldEQ(t, b11, b21) - shouldEQ(t, b12, b22) - - pool := GetPool(barPath, fooPath, fee500) - test_liquidity := pool.PoolGetLiquidity() - shouldEQ(t, true, test_liquidity.Eq(new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(18)))) - - Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(8)).ToString()) - test_liquidity = pool.PoolGetLiquidity() - shouldEQ(t, true, test_liquidity.Eq(new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(10)))) - - Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, "1") - test_liquidity = pool.PoolGetLiquidity() - shouldEQ(t, test_liquidity.ToString(), "9999") - - Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, "999") - test_liquidity = pool.PoolGetLiquidity() - shouldEQ(t, true, test_liquidity.Eq(new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(9)))) - - Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(9)).ToString()) - test_liquidity = pool.PoolGetLiquidity() - shouldEQ(t, test_liquidity.ToString(), "0") -} - -// 4. Collect -func TestCollect(t *testing.T) { - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - - // withdraw all token before test `Collect` - Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") - - // pool should have zero liquidity - pool := GetPool(barPath, fooPath, fee500) - test_liquidity := pool.PoolGetLiquidity() - shouldEQ(t, test_liquidity.ToString(), "0") - - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) - Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) - c11, c12 := Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") - - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) - Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) - c21, c22 := Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") - - shouldEQ(t, c11, c21) // 443 - shouldEQ(t, c12, c22) // 1206 - - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) - Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) - c31, c32 := Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "100", "100") - shouldEQ(t, c31, "100") - shouldEQ(t, c32, "100") - - c41, c42 := Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") - shouldEQ(t, c41, "343") // 343 = c21 - 100 - shouldEQ(t, c42, "1106") // 1106 = c22 - 100 - - // Mint > No Burn => nothing to collect - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) - // Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, test_liquidityExpectStr*15) - c51, c52 := Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") - shouldEQ(t, c51, "0") - shouldEQ(t, c52, "0") - - // Burn Now => something to collect - Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(15)).ToString()) - c61, c62 := Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") - shouldNEQ(t, c61, "0") - shouldNEQ(t, c62, "0") -} - -// 5. Swap by test1 -func TestSwap(t *testing.T) { - pool := GetPool(barPath, fooPath, fee500) - test_liquidity := pool.PoolGetLiquidity() - shouldEQ(t, test_liquidity.ToString(), "0") - - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), test_tickLower, test_tickUpper, new(u256.Uint).Mul(test_liquidityExpect256, u256.NewUint(20000)).ToString()) - - // Swap several times - std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) - test_price := consts.MIN_PRICE - { - poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, consts.POOL_ADDR) - poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, consts.POOL_ADDR) - - userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - - amount0, amount1 := Swap( - fooPath, // token0Path - barPath, // token1Path - fee500, // fee - test1.String(), // recipient - true, // zeroForOne - "10000", // _amountSpecified - test_price, // _sqrtPriceLimitX96 - std.GetOrigCaller().String(), // payer - ) - shouldNEQ(t, amount0, "0") - shouldNEQ(t, amount1, "0") - intAmount0, _ := strconv.Atoi(amount0) - intAmount1, _ := strconv.Atoi(amount1) - - poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, consts.POOL_ADDR) - poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, consts.POOL_ADDR) - - userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - - shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(intAmount0)) - shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-intAmount1)) - shouldEQ(t, poolNewToken0Bal-poolOldToken0Bal, int64(intAmount0)) - shouldEQ(t, poolOldToken1Bal-poolNewToken1Bal, int64(-intAmount1)) - } - - { - poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, consts.POOL_ADDR) - poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, consts.POOL_ADDR) - - userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - - amount0, amount1 := Swap(barPath, fooPath, fee500, test1.String(), true, "5000", test_price, std.GetOrigCaller().String()) // give enough amount to take fees away - intAmount0, _ := strconv.Atoi(amount0) - intAmount1, _ := strconv.Atoi(amount1) - - poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, consts.POOL_ADDR) - poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, consts.POOL_ADDR) - - userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - - shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(intAmount0)) - shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-intAmount1)) - shouldEQ(t, poolNewToken0Bal-poolOldToken0Bal, int64(intAmount0)) - shouldEQ(t, poolOldToken1Bal-poolNewToken1Bal, int64(-intAmount1)) - } - - { - poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, consts.POOL_ADDR) - poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, consts.POOL_ADDR) - - userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - - amount0, amount1 := Swap(barPath, fooPath, fee500, test1.String(), true, "1000", test_price, std.GetOrigCaller().String()) // give enough amount to take fees away - intAmount0, _ := strconv.Atoi(amount0) - intAmount1, _ := strconv.Atoi(amount1) - - poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, consts.POOL_ADDR) - poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, consts.POOL_ADDR) - - userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - - shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(intAmount0)) - shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-intAmount1)) - shouldEQ(t, poolNewToken0Bal-poolOldToken0Bal, int64(intAmount0)) - shouldEQ(t, poolOldToken1Bal-poolNewToken1Bal, int64(-intAmount1)) - } - - // Swap token1 -> token0 - { - poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, consts.POOL_ADDR) - poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, consts.POOL_ADDR) - - userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - - amount0, amount1 := Swap(barPath, fooPath, fee500, test1.String(), false, "16000", consts.MAX_PRICE, std.GetOrigCaller().String()) // give enough amount to take fees away - intAmount0, _ := strconv.Atoi(amount0) - intAmount1, _ := strconv.Atoi(amount1) - - poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, consts.POOL_ADDR) - poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, consts.POOL_ADDR) - - userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - - shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(intAmount0)) - shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-intAmount1)) - shouldEQ(t, poolNewToken0Bal-poolOldToken0Bal, int64(intAmount0)) - shouldEQ(t, poolOldToken1Bal-poolNewToken1Bal, int64(-intAmount1)) - } -} - -// 6. SetFeeProtocol by Gnoswap Admin -func TestSetFeeProtocol(t *testing.T) { - // non admin call - std.TestSetOrigCaller(gsa) - shouldPanic(t, func() { SetFeeProtocol(2, 2) }) - - // admin call - std.TestSetOrigCaller(gsa) - SetFeeProtocol(6, 8) - - for _, pool := range pools { - shouldEQ(t, pool.PoolGetSlot0().feeProtocol, 134) - } -} - -// 7. CollectProtocol by Gnoswap Admin -func TestCollectProtocol(t *testing.T) { - std.TestSetOrigCaller(gsa) - SetFeeProtocol(6, 8) - pool := GetPool(barPath, fooPath, fee500) - test_slot0 := pool.PoolGetSlot0() - shouldEQ(t, test_slot0.feeProtocol, 134) - - // Make ProtocolFee via Swap by test1 ( Mint by test1 ) - std.TestSetOrigCaller(test1) - { - - std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) - Swap(barPath, fooPath, fee500, test1.String(), true, "200000", consts.MIN_PRICE, std.GetOrigCaller().String()) // swap token0 -> token1 => fee only in token0 - - test1OldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - test1OldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - - std.TestSetPrevAddr(gsa) - amount0, amount1 := CollectProtocol(barPath, fooPath, fee500, test1.String(), "100000", "100000") - - test1NewToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - test1NewToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) - - _token0Diff := test1NewToken0Bal - test1OldToken0Bal - _token1Diff := test1NewToken1Bal - test1OldToken1Bal - - token0DiffStr := strconv.Itoa(int(_token0Diff)) - token1DiffStr := strconv.Itoa(int(_token1Diff)) - - shouldEQ(t, token0DiffStr, amount0) - shouldEQ(t, token1DiffStr, amount1) - } -} - -// GETTER_API TEST -func TestApiGetPools(t *testing.T) { - gpls := ApiGetPools() - - root, err := json.Unmarshal([]byte(gpls)) - if err != nil { - panic(err) - } - - response, err := root.GetKey("response") - if err != nil { - panic(err) - } - - shouldEQ(t, response.Size(), 1) -} diff --git a/pool/_TEST_/_TEST_pool_test.gn b/pool/_TEST_/_TEST_pool_test.gn deleted file mode 100644 index cebbcd70..00000000 --- a/pool/_TEST_/_TEST_pool_test.gn +++ /dev/null @@ -1,124 +0,0 @@ -package pool - -import ( - "std" - "testing" - - "gno.land/p/demo/json" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/foo" - - "gno.land/r/demo/gnoswap/consts" - - "gno.land/r/demo/gns" -) - -var ( - test_tickLower = int32(9000) - test_tickUpper = int32(11000) - test_liquidityExpectStr = "100000000" -) - -// 1. Create Foo:Bar Pool -func TestCreateFooBarPool(t *testing.T) { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - std.TestSkipHeights(5) - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - std.TestSkipHeights(1) - - CreatePool(fooPath, barPath, fee500, "130621891405341611593710811006") // tick 10000 - shouldEQ(t, len(pools), 1) - std.TestSkipHeights(1) -} - -// 2. Create Bar:Baz Pool -func TestCreateBarBazPool(t *testing.T) { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - std.TestSkipHeights(5) - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - std.TestSkipHeights(1) - - CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick 10000 - shouldEQ(t, len(pools), 2) - std.TestSkipHeights(1) -} - -// 3. Mint Foo:Bar Liquidity by test1 -func TestMintFooBarLiquidity(t *testing.T) { - std.TestSetPrevAddr(test1) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(4) - - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - amount0, amount1 := Mint( - barPath, - fooPath, - fee500, - consts.POSITION_ADDR.String(), - -11000, // -11000 - -9000, // -9000 - test_liquidityExpectStr, - ) - shouldEQ(t, amount0, "8040316") - shouldEQ(t, amount1, "2958014") - std.TestSkipHeights(1) -} - -// 4. Mint Bar:Baz Liquidity by test1 -func TestMintBarBazLiquidity(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) - amount0, amount1 := Mint( - barPath, - bazPath, - fee500, - consts.POSITION_ADDR.String(), - test_tickLower, - test_tickUpper, - test_liquidityExpectStr, - ) - shouldEQ(t, amount0, "2958015") - shouldEQ(t, amount1, "8040315") - std.TestSkipHeights(1) -} - -// GETTER_API TEST -func TestApiGetPools(t *testing.T) { - gpls := ApiGetPools() - - root, err := json.Unmarshal([]byte(gpls)) - if err != nil { - panic(err) - } - - response, err := root.GetKey("response") - if err != nil { - panic(err) - } - - shouldEQ(t, response.Size(), 2) -} diff --git a/pool/_TEST_/z1_single_lp_filetest.gno b/pool/_TEST_/z1_single_lp_filetest.gno index b6be0e4f..f9c2c61d 100644 --- a/pool/_TEST_/z1_single_lp_filetest.gno +++ b/pool/_TEST_/z1_single_lp_filetest.gno @@ -179,8 +179,8 @@ func init() { // TOKEN REGISTER std.TestSetPrevAddr(gsa) - pl.RegisterGRC20Interface("gno.land/r/demo/bar", FooToken{}) - pl.RegisterGRC20Interface("gno.land/r/demo/foo", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) diff --git a/pool/_TEST_/z2_native_swap_filetest.gno b/pool/_TEST_/z2_native_swap_filetest.gno new file mode 100644 index 00000000..16209d18 --- /dev/null +++ b/pool/_TEST_/z2_native_swap_filetest.gno @@ -0,0 +1,307 @@ +package main + +import ( + "std" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + test_tickLower = int32(9000) + test_tickUpper = int32(11000) + + maxApprove uint64 = 18446744073709551615 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createFooWUGNOT() + mint() + swapBuyNative() + swapSellNative() +} + +func createFooWUGNOT() { + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + gns.Faucet() + gns.Faucet() + gns.Faucet() + gns.Faucet() + gns.Faucet() + std.TestSkipHeights(5) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + pl.CreatePool(fooPath, consts.WRAPPED_WUGNOT, fee500, "130621891405341611593710811006") // tick 10000 x2.7 + if len(pl.PoolGetPoolList()) != 1 { + panic("expected 1 pool") + } + std.TestSkipHeights(1) +} + +func mint() { + token0, token1 := fooPath, consts.WRAPPED_WUGNOT + if token1 < token0 { + token0, token1 = token1, token0 + } + + // prepare ugnot + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.IssueCoin(test1, "ugnot", 804_032) + + // simulate transfer & decrase + std.TestSetOrigSend(std.Coins{{"ugnot", 804_032}}, nil) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 804_032) + + // Deposit(wrap) + std.TestSetPrevAddr(test1) + wugnot.Deposit() + + std.TestSetPrevAddr(test1) + foo.Approve(a2u(consts.POOL_ADDR), maxApprove) + wugnot.Approve(a2u(consts.POOL_ADDR), maxApprove) + + // Mint + std.TestSetPrevRealm(consts.POSITION_PATH) + amount0, amount1 := pl.Mint( + fooPath, + consts.WRAPPED_WUGNOT, + fee500, + consts.POSITION_ADDR.String(), + test_tickLower, + test_tickUpper, + "10000000", + ) + if amount0 != "295802" || amount1 != "804031" { + panic("mint foo wugnot") + } +} + +func swapBuyNative() { + pool := pl.GetPool(fooPath, consts.WRAPPED_WUGNOT, fee500) + + std.TestSetPrevAddr(test1) + foo.Approve(a2u(consts.POOL_ADDR), 10000) + + std.TestSetPrevRealm(consts.ROUTER_PATH) + std.TestSetOrigCaller(test1) + amount0, amount1 := pl.Swap( + fooPath, // token0 + consts.WRAPPED_WUGNOT, // token1 + fee500, + test1.String(), + true, + "10000", + consts.MIN_PRICE, + std.GetOrigCaller().String(), + ) + if amount0 != "10000" && amount1 != "-27123" { + panic("swap sell foo, buy native") + } +} + +func swapSellNative() { + pool := pl.GetPool(fooPath, consts.WRAPPED_WUGNOT, fee500) + + std.TestSetPrevAddr(test1) + foo.Approve(a2u(consts.POOL_ADDR), 10000) + + std.TestSetPrevRealm(consts.ROUTER_PATH) + std.TestSetOrigCaller(test1) + amount0, amount1 := pl.Swap( // ugnot 10_000 > foo ?? + fooPath, // token0 + consts.WRAPPED_WUGNOT, // token1 + fee500, + test1.String(), + false, + "10000", + consts.MAX_PRICE, + std.GetOrigCaller().String(), + ) + + if amount0 != "-3687" && amount1 != "10000" { + panic("swap sell native, buy foo") + } +} + +// Output: +// diff --git a/pool/_TEST_/z3_dryswap_and_swap_filetest.gno b/pool/_TEST_/z3_dryswap_and_swap_filetest.gno new file mode 100644 index 00000000..e1f3de25 --- /dev/null +++ b/pool/_TEST_/z3_dryswap_and_swap_filetest.gno @@ -0,0 +1,309 @@ +package main + +import ( + "std" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + test_tickLower = int32(9000) + test_tickUpper = int32(11000) + + maxApprove uint64 = 18446744073709551615 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + drySwap_ZeroForOneTrue_AmountSpecified_Positive_16000() + drySwap_ZeroForOneTrue_AmountSpecified_Negative_16000() + drySwap_ZeroForOneFalse_AmountSpecified_Positive_16000() + drySwap_ZeroForOneFalse_AmountSpecified_Negative_16000() +} + +func createPool() { + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + gns.Faucet() + gns.Faucet() + gns.Faucet() + gns.Faucet() + gns.Faucet() + std.TestSkipHeights(5) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + pl.CreatePool(fooPath, barPath, fee500, "130621891405341611593710811006") // tick 10000 x2.7 + if len(pl.PoolGetPoolList()) != 1 { + panic("expected 1 pool") + } + std.TestSkipHeights(1) +} + +func drySwap_ZeroForOneTrue_AmountSpecified_Positive_16000() { + std.TestSetPrevRealm(consts.POSITION_PATH) + std.TestSetOrigCaller(test1) + + // no mint == no liquidity => swap will fail + _, _, ok := pl.DrySwap(fooPath, barPath, fee500, true, "16000", consts.MIN_PRICE) + if ok { + panic("expected dry swap to fail #1") + } + + // not enough mint == swap will fail + std.TestSetPrevAddr(test1) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.POSITION_PATH) + std.TestSetOrigCaller(test1) + pl.Mint(fooPath, barPath, fee500, consts.POSITION_ADDR.String(), -test_tickUpper, -test_tickLower, "10") + _, _, ok = pl.DrySwap(fooPath, barPath, fee500, true, "16000", consts.MIN_PRICE) + if ok { + panic("expected dry swap to fail #2") + } + + std.TestSetPrevRealm(consts.POSITION_PATH) + std.TestSetOrigCaller(test1) + pl.Mint(fooPath, barPath, fee500, consts.POSITION_ADDR.String(), -test_tickUpper, -test_tickLower, "100000000000") // -11000 ~ -9000 + // zeroForOne true + // amountSpecified 16000 + poolIn, poolOut, _ := pl.DrySwap( + fooPath, // fooPath + barPath, // barPath + fee500, // fee500 + true, // zeroForOne + "16000", // amountSpecified + consts.MIN_PRICE, // sqrtPriceLimitX96 + ) + if !(poolIn == "16000" && poolOut == "-5883") { + panic("expected 16000, -5883") + } +} + +func drySwap_ZeroForOneTrue_AmountSpecified_Negative_16000() { + // zeroForOne true + // amountSpecified 16000 + poolIn, poolOut, _ := pl.DrySwap( + fooPath, // fooPath + barPath, // barPath + fee500, // fee500 + true, // zeroForOne + "-16000", // amountSpecified + consts.MIN_PRICE, // sqrtPriceLimitX96 + ) + if !(poolIn == "43512" && poolOut == "-16000") { + panic("expected 43512, -16000") + } +} + +func drySwap_ZeroForOneFalse_AmountSpecified_Positive_16000() { + // zeroForOne false + // amountSpecified 16000 + poolIn, poolOut, _ := pl.DrySwap( + fooPath, // fooPath + barPath, // barPath + fee500, // fee500 + false, // zeroForOne + "16000", // amountSpecified + consts.MAX_PRICE, // sqrtPriceLimitX96 + ) + if !(poolIn == "-43468" && poolOut == "16000") { + panic("expected -43468, 16000") + } +} + +func drySwap_ZeroForOneFalse_AmountSpecified_Negative_16000() { + // zeroForOne false + // amountSpecified -16000 + poolIn, poolOut, _ := pl.DrySwap( + fooPath, // fooPath + barPath, // barPath + fee500, // fee500 + false, // zeroForOne + "-16000", // amountSpecified + consts.MAX_PRICE, // sqrtPriceLimitX96 + ) + if !(poolIn == "-16000" && poolOut == "5888") { + panic("expected -16000, 5888") + } +} From 4008bb6743e54066e403f51d0d02d6907376b8ee Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 2 May 2024 15:14:04 +0900 Subject: [PATCH 5/9] test: integeration test for `position` --- position/_TEST_/_TEST_0_INIT_FAUCET_test.gno | 25 -- .../_TEST_0_INIT_TOKEN_REGISTER_test.gno | 145 ------- .../_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno | 94 ----- position/_TEST_/_TEST_position_api_test.gn | 87 ---- .../_TEST_position_increase_decrease_test.gn | 186 --------- position/_TEST_/_TEST_position_test.gn | 167 -------- ...test_two_position_used_single_swap_test.gn | 132 ------ position/_TEST_/z1_position_filetest.gno | 373 +++++++++++++++++ ...z2_position_increase_decrease_filetest.gno | 381 ++++++++++++++++++ ...two_position_used_single_swap_filetest.gno | 335 +++++++++++++++ position/position.gno | 1 - 11 files changed, 1089 insertions(+), 837 deletions(-) delete mode 100644 position/_TEST_/_TEST_0_INIT_FAUCET_test.gno delete mode 100644 position/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno delete mode 100644 position/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno delete mode 100644 position/_TEST_/_TEST_position_api_test.gn delete mode 100644 position/_TEST_/_TEST_position_increase_decrease_test.gn delete mode 100644 position/_TEST_/_TEST_position_test.gn delete mode 100644 position/_TEST_/_TEST_position_test_two_position_used_single_swap_test.gn create mode 100644 position/_TEST_/z1_position_filetest.gno create mode 100644 position/_TEST_/z2_position_increase_decrease_filetest.gno create mode 100644 position/_TEST_/z3_position_two_position_used_single_swap_filetest.gno diff --git a/position/_TEST_/_TEST_0_INIT_FAUCET_test.gno b/position/_TEST_/_TEST_0_INIT_FAUCET_test.gno deleted file mode 100644 index 1991c811..00000000 --- a/position/_TEST_/_TEST_0_INIT_FAUCET_test.gno +++ /dev/null @@ -1,25 +0,0 @@ -package position - -import ( - "std" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/foo" - "gno.land/r/demo/obl" - "gno.land/r/demo/qux" -) - -func init() { - std.TestSetPrevAddr(test1) - - // GIVE 100_000_000_000(u) ≈ 100_000 - for i := 0; i < 100; i++ { - foo.Faucet() - bar.Faucet() - baz.Faucet() - qux.Faucet() - obl.Faucet() - //gns.Faucet() - } -} diff --git a/position/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno b/position/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno deleted file mode 100644 index 6699bba9..00000000 --- a/position/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno +++ /dev/null @@ -1,145 +0,0 @@ -package position - -import ( - "std" - - "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/gnoswap/consts" - - pusers "gno.land/p/demo/users" - - pl "gno.land/r/demo/pool" -) - -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.TestSetOrigCaller(consts.GNOSWAP_ADMIN) - - 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{}) -} diff --git a/position/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno b/position/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno deleted file mode 100644 index 482dcb5e..00000000 --- a/position/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno +++ /dev/null @@ -1,94 +0,0 @@ -package position - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - "gno.land/r/demo/gnft" - - pl "gno.land/r/demo/pool" -) - -var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") - - fooPath string = "gno.land/r/demo/foo" - barPath string = "gno.land/r/demo/bar" - bazPath string = "gno.land/r/demo/baz" - quxPath string = "gno.land/r/demo/qux" - - oblPath string = "gno.land/r/demo/obl" - // wugnotPath string = "gno.land/r/demo/wugnot" // from consts - // gnsPath string = "gno.land/r/demo/gns" // from consts - - fee100 uint32 = 100 - fee500 uint32 = 500 - fee3000 uint32 = 3000 - - max_timeout int64 = 9999999999 -) - -/* HELPER */ -func shouldEQ(t *testing.T, got, expected interface{}) { - if got != expected { - t.Errorf("got %v, expected %v", got, expected) - } -} - -func shouldNEQ(t *testing.T, got, expected interface{}) { - if got == expected { - t.Errorf("got %v, didn't expected %v", got, expected) - } -} - -func shouldGT(t *testing.T, l, r interface{}) { - if !(l < r) { - t.Errorf("expected %v < %v", l, r) - } -} - -func shouldLT(t *testing.T, l, r interface{}) { - if !(l > r) { - t.Errorf("expected %v > %v", l, r) - } -} - -func shouldPanic(t *testing.T, f func()) { - defer func() { - if r := recover(); r == nil { - t.Errorf("expected panic") - } - }() - f() -} - -func ugnotBalanceOf(addr std.Address) uint64 { - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - - coins := testBanker.GetCoins(addr) - if len(coins) == 0 { - return 0 - } - - return uint64(testBanker.GetCoins(addr)[0].Amount) -} - -func isOwner(t *testing.T, tokenId uint64, addr std.Address) bool { - owner := gnft.OwnerOf(tid(tokenId)) - - if owner == addr { - return true - } - - t.Errorf("expected owner %v, got %v", addr, owner) - return false -} - -func getPoolFromLpTokenId(lpTokenId uint64) *pl.Pool { - position := positions[lpTokenId] - - return pl.GetPoolFromPoolPath(position.poolKey) -} diff --git a/position/_TEST_/_TEST_position_api_test.gn b/position/_TEST_/_TEST_position_api_test.gn deleted file mode 100644 index f64ad780..00000000 --- a/position/_TEST_/_TEST_position_api_test.gn +++ /dev/null @@ -1,87 +0,0 @@ -package position - -import ( - "gno.land/p/demo/json" - - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - "gno.land/r/demo/bar" - "gno.land/r/demo/foo" - "gno.land/r/demo/gns" - - pl "gno.land/r/demo/pool" -) - -// 1. Init & Create Pool -func TestPoolInitCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - pl.CreatePool(fooPath, barPath, fee500, "130621891405341611593710811006") // tick = 10000 -} - -// 2. Mint LP and Get GNFT -func TestMint(t *testing.T) { - std.TestSetPrevAddr(test1) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - // test1 mints => will get tid 1 nft - std.TestSetOrigCaller(test1) - Mint(fooPath, barPath, fee500, int32(9000), int32(11000), "1000000", "1000000", "1", "1", max_timeout, test1.String()) - - // test1 mints => will get tid 2 nft - std.TestSetOrigCaller(test1) - Mint(fooPath, barPath, fee500, int32(4000), int32(6000), "1000000", "1000000", "0", "0", max_timeout, test1.String()) -} - -// 3. Swap to make position fee -func TestSwap(t *testing.T) { - std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) - pl.Swap( - fooPath, - barPath, - fee500, - test1.String(), - true, - "123456", - consts.MIN_PRICE, - test1.String(), - ) -} - -func TestCollectFeeAfterSwap(t *testing.T) { - // burn 0 to collect swap fee - std.TestSetOrigCaller(test1) - tokneId, fee0, fee1, fromPath := CollectFee(1) - shouldEQ(t, tokneId, uint64(1)) - shouldEQ(t, fee0, "60") - shouldEQ(t, fee1, "0") - shouldEQ(t, fromPath, "gno.land/r/demo/bar:gno.land/r/demo/foo:500") -} - -// RPCs -func TestApiGetPositions(t *testing.T) { - gpss := ApiGetPositions() - - root, err := json.Unmarshal([]byte(gpss)) - if err != nil { - panic(err) - } - - response, err := root.GetKey("response") - if err != nil { - panic(err) - } - - shouldEQ(t, response.Size(), 2) -} diff --git a/position/_TEST_/_TEST_position_increase_decrease_test.gn b/position/_TEST_/_TEST_position_increase_decrease_test.gn deleted file mode 100644 index d3225d06..00000000 --- a/position/_TEST_/_TEST_position_increase_decrease_test.gn +++ /dev/null @@ -1,186 +0,0 @@ -package position - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/common" - "gno.land/r/demo/gnoswap/consts" - - "gno.land/r/demo/bar" - "gno.land/r/demo/foo" - "gno.land/r/demo/gns" - - pl "gno.land/r/demo/pool" -) - -// 1. Create Pool -func TestPoolInitCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - pl.CreatePool(barPath, fooPath, fee500, common.TickMathGetSqrtRatioAtTick(10000).ToString()) // x2.71814592682522526700950038502924144268035888671875 -} - -func TestMintPosition(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 18394892) - foo.Approve(a2u(consts.POOL_ADDR), 50000000) - - tokenId, liquidity, amount0, amount1 := Mint( - barPath, - fooPath, - fee500, - 8000, - 12000, - "50000000", - "50000000", - "0", - "0", - max_timeout, - test1.String(), - ) - shouldEQ(t, tokenId, 1) - shouldEQ(t, getNextId(), 2) - shouldEQ(t, amount0, "18394892") - shouldEQ(t, amount1, "49999999") -} - -func TestIncreaseLiquidity(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 3678979) - foo.Approve(a2u(consts.POOL_ADDR), 10000000) - - pool := getPoolFromLpTokenId(uint64(1)) - oldLiquidity := pool.PoolGetLiquidity() - - _, _, m0, m1, _ := IncreaseLiquidity( - uint64(1), // tokenId - "10000000", // amount0Desired - "10000000", // amount1Desired - "0", // amount0Min - "0", // amount1Min - max_timeout, // deadline - ) - shouldEQ(t, m0, "3678979") - shouldEQ(t, m1, "9999999") - - newLiquidity := pool.PoolGetLiquidity() - - shouldEQ(t, newLiquidity.Gt(oldLiquidity), true) -} - -func TestCollectFeeBeforeSwap(t *testing.T) { - std.TestSetPrevAddr(test1) - - tokenId, fee0, fee1, poolPath := CollectFee(1) - shouldEQ(t, tokenId, uint64(1)) - shouldEQ(t, fee0, "0") - shouldEQ(t, fee1, "0") - shouldEQ(t, poolPath, "gno.land/r/demo/bar:gno.land/r/demo/foo:500") -} - -func TestSwap(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 1_234_567) - - std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) - amount0, amount1 := pl.Swap( - barPath, - fooPath, - fee500, - test1.String(), - true, - "1234567", - consts.MIN_PRICE, - test1.String(), - ) -} - -func TestCollectFeeAfterSwap(t *testing.T) { - std.TestSetPrevAddr(test1) - - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - tokenId, fee0, fee1, poolPath := CollectFee(1) - shouldEQ(t, tokenId, uint64(1)) - shouldEQ(t, fee0, "610") // this is input token - shouldEQ(t, fee1, "0") // this it output token - shouldEQ(t, poolPath, "gno.land/r/demo/bar:gno.land/r/demo/foo:500") -} - -func TestCollectFee2(t *testing.T) { - std.TestSetPrevRealm("") - std.TestSetOrigCaller(test1) - - tokenId, fee0, fee1, poolPath := CollectFee(1) - shouldEQ(t, tokenId, uint64(1)) - shouldEQ(t, fee0, "0") // this is input token - shouldEQ(t, fee1, "0") // this it output token - shouldEQ(t, poolPath, "gno.land/r/demo/bar:gno.land/r/demo/foo:500") - - afterFee0, afterFee1 := unclaimedFee(uint64(1)) - shouldEQ(t, afterFee0.ToString(), "0") - shouldEQ(t, afterFee1.ToString(), "0") -} - -func TestSwap2(t *testing.T) { - std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) - amount0, amount1 := pl.Swap( - barPath, - fooPath, - fee500, - test1.String(), - true, - "1000000", - consts.MIN_PRICE, - test1.String(), - ) - - // UNCLAIMED_FEE - std.TestSetPrevRealm("") - std.TestSetOrigCaller(test1) - - oldFee0, oldFee1 := unclaimedFee(uint64(1)) - shouldEQ(t, oldFee0.ToString(), "498") - shouldEQ(t, oldFee1.ToString(), "0") - - tokenId, fee0, fee1, poolPath := CollectFee(1) - shouldEQ(t, tokenId, uint64(1)) - shouldEQ(t, fee0, "494") // decreased cause of fee - shouldEQ(t, fee1, "0") - shouldEQ(t, poolPath, "gno.land/r/demo/bar:gno.land/r/demo/foo:500") -} - -func TestDecreaseLiquidity(t *testing.T) { - std.TestSetPrevRealm("") - std.TestSetOrigCaller(test1) - - oldLiquidity := getPoolFromLpTokenId(uint64(1)).PoolGetLiquidity() - - DecreaseLiquidity( - uint64(1), // tokenId - 50, // liquidityRatio - "0", // amount0Min - "0", // amount1Min - max_timeout, // deadline - false, // unwrapResult - ) - - newLiquidity := getPoolFromLpTokenId(uint64(1)).PoolGetLiquidity() - shouldEQ(t, true, newLiquidity.Lt(oldLiquidity)) - - // check fee left - tokenId, fee0, fee1, poolPath := CollectFee(1) - shouldEQ(t, tokenId, uint64(1)) - shouldEQ(t, fee0, "0") - shouldEQ(t, fee1, "0") -} diff --git a/position/_TEST_/_TEST_position_test.gn b/position/_TEST_/_TEST_position_test.gn deleted file mode 100644 index eb91be70..00000000 --- a/position/_TEST_/_TEST_position_test.gn +++ /dev/null @@ -1,167 +0,0 @@ -package position - -import ( - // @notJoon JSON - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - - "gno.land/r/demo/bar" - "gno.land/r/demo/foo" - - "gno.land/r/demo/gnft" - "gno.land/r/demo/gns" -) - -func TestPoolInitCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - pl.CreatePool(barPath, fooPath, fee500, "130621891405341611593710811006") // tick = 10000 -} - -func TestMintPosition01InRange(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 18394892) - foo.Approve(a2u(consts.POOL_ADDR), 50000000) - - tokenId, liquidity, amount0, amount1 := Mint( - barPath, - fooPath, - fee500, - 8000, - 12000, - "50000000", - "50000000", - "0", - "0", - max_timeout, - test1.String(), - ) - shouldEQ(t, tokenId, 1) - shouldEQ(t, getNextId(), 2) - shouldEQ(t, amount0, "18394892") - shouldEQ(t, amount1, "49999999") -} - -func TestMintPosition02LowerRange(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 0) - foo.Approve(a2u(consts.POOL_ADDR), 50000000) - - tokenId, liquidity, amount0, amount1 := Mint( - barPath, - fooPath, - fee500, - 5000, - 8000, - "50000000", - "50000000", - "0", - "0", - max_timeout, - test1.String(), - ) - shouldEQ(t, tokenId, 2) - shouldEQ(t, getNextId(), 3) - shouldEQ(t, amount0, "0") - shouldEQ(t, amount1, "49999999") -} - -func TestMintPosition03UpperRange(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 50000000) - foo.Approve(a2u(consts.POOL_ADDR), 0) - - tokenId, liquidity, amount0, amount1 := Mint( - barPath, - fooPath, - fee500, - 12000, - 14000, - "50000000", - "50000000", - "0", - "0", - max_timeout, - test1.String(), - ) - shouldEQ(t, tokenId, 3) - shouldEQ(t, getNextId(), 4) - shouldEQ(t, amount0, "50000000") - shouldEQ(t, amount1, "0") -} - -func TestCollectFeeBeforeSwap(t *testing.T) { - tokenId, fee0, fee1, poolPath := CollectFee(1) - shouldEQ(t, tokenId, uint64(1)) - shouldEQ(t, fee0, "0") - shouldEQ(t, fee1, "0") - shouldEQ(t, poolPath, "gno.land/r/demo/bar:gno.land/r/demo/foo:500") -} - -func TestSwap(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 1234567) - - std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) - amount0, amount1 := pl.Swap( - barPath, - fooPath, - fee500, - test1.String(), - true, - "1234567", - consts.MIN_PRICE, - test1.String(), - ) - shouldEQ(t, amount0, "1234567") - shouldEQ(t, amount1, "-3332779") -} - -func TestCollectFeeAfterSwap(t *testing.T) { - std.TestSetPrevAddr(test1) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE - - tokenId, fee0, fee1, poolPath := CollectFee(1) - shouldEQ(t, tokenId, uint64(1)) - shouldNEQ(t, fee0, "0") // this is input token - shouldEQ(t, fee1, "0") // this it output token - shouldEQ(t, poolPath, "gno.land/r/demo/bar:gno.land/r/demo/foo:500") -} - -func TestDecreaseLiquidityUpperPosition(t *testing.T) { - std.TestSetPrevAddr(test1) - - ownerOfPosition := gnft.OwnerOf(tid(3)) - shouldEQ(t, ownerOfPosition, std.GetOrigCaller()) - - tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath := DecreaseLiquidity(uint64(3), 100, "0", "0", max_timeout, false) - shouldEQ(t, tokenId, uint64(3)) - shouldEQ(t, amount0, "49999999") - shouldEQ(t, amount1, "0") - - ownerOfPosition = gnft.OwnerOf(tid(3)) - shouldEQ(t, ownerOfPosition, std.GetOrigCaller()) -} - -// @notJoon JSON -// func TestApiGetPositions(t *testing.T) { -// gpss := ApiGetPositions() -// jsonStr := gjson.Parse(gpss) -// jsonArr := jsonStr.Get("response").Array() -// shouldEQ(t, len(jsonArr), 3) - -// shouldEQ(t, jsonArr[0].Get("burned").Bool(), false) -// shouldEQ(t, jsonArr[2].Get("burned").Bool(), true) -// } diff --git a/position/_TEST_/_TEST_position_test_two_position_used_single_swap_test.gn b/position/_TEST_/_TEST_position_test_two_position_used_single_swap_test.gn deleted file mode 100644 index 889ab6d8..00000000 --- a/position/_TEST_/_TEST_position_test_two_position_used_single_swap_test.gn +++ /dev/null @@ -1,132 +0,0 @@ -package position - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - - "gno.land/r/demo/bar" - "gno.land/r/demo/foo" - "gno.land/r/demo/gns" -) - -func TestPoolInitCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - pl.CreatePool(barPath, fooPath, fee500, "130621891405341611593710811006") // tick = 10000 -} - -func TestMintPosition01WideInRange(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 18394892) - foo.Approve(a2u(consts.POOL_ADDR), 50000000) - - tokenId, liquidity, amount0, amount1 := Mint( - barPath, - fooPath, - fee500, - 8000, - 12000, - "50000000", - "50000000", - "0", - "0", - max_timeout, - test1.String(), - ) - shouldEQ(t, tokenId, 1) - shouldEQ(t, getNextId(), 2) - shouldEQ(t, liquidity, "318704392") - shouldEQ(t, amount0, "18394892") - shouldEQ(t, amount1, "49999999") -} - -func TestMintPositionTightInRange(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 18394892) - foo.Approve(a2u(consts.POOL_ADDR), 50000000) - - tokenId, liquidity, amount0, amount1 := Mint( - barPath, - fooPath, - fee500, - 9500, - 10500, - "50000000", - "50000000", - "0", - "0", - max_timeout, - test1.String(), - ) - shouldEQ(t, tokenId, 2) - shouldEQ(t, getNextId(), 3) - shouldEQ(t, liquidity, "1228379121") - shouldEQ(t, amount0, "18394892") - shouldEQ(t, amount1, "49999999") -} - -func TestCollectFeeBeforeSwapPos1(t *testing.T) { - tokenId, fee0, fee1, poolPath := CollectFee(1) - shouldEQ(t, tokenId, uint64(1)) - shouldEQ(t, fee0, "0") - shouldEQ(t, fee1, "0") - shouldEQ(t, poolPath, "gno.land/r/demo/bar:gno.land/r/demo/foo:500") -} - -func TestCollectFeeBeforeSwapPos2(t *testing.T) { - tokenId, fee0, fee1, poolPath := CollectFee(2) - shouldEQ(t, tokenId, uint64(2)) - shouldEQ(t, fee0, "0") - shouldEQ(t, fee1, "0") - shouldEQ(t, poolPath, "gno.land/r/demo/bar:gno.land/r/demo/foo:500") -} - -func TestSwap(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 1234567) - - std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) - amount0, amount1 := pl.Swap( - barPath, - fooPath, - fee500, - test1.String(), - true, - "1234567", - consts.MIN_PRICE, - test1.String(), - ) -} - -// FYI, IF POSITION 2 DOESN'T EXIST, POSITION 1 WILL EARN '617' as fee -func TestCollectFeeAfterSwapPos1(t *testing.T) { - std.TestSetPrevAddr(test1) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE - - tokenId, fee0, fee1, poolPath := CollectFee(1) - shouldEQ(t, tokenId, uint64(1)) - // shouldEQ(t, fee0, uint64(617)) - shouldEQ(t, fee0, "126") - shouldEQ(t, fee1, "0") - shouldEQ(t, poolPath, "gno.land/r/demo/bar:gno.land/r/demo/foo:500") -} - -func TestCollectFeeAfterSwapPos2(t *testing.T) { - tokenId, fee0, fee1, poolPath := CollectFee(2) - shouldEQ(t, tokenId, uint64(2)) - shouldEQ(t, fee0, "485") - shouldEQ(t, fee1, "0") - shouldEQ(t, poolPath, "gno.land/r/demo/bar:gno.land/r/demo/foo:500") -} diff --git a/position/_TEST_/z1_position_filetest.gno b/position/_TEST_/z1_position_filetest.gno new file mode 100644 index 00000000..a7a1abf0 --- /dev/null +++ b/position/_TEST_/z1_position_filetest.gno @@ -0,0 +1,373 @@ +package main + +import ( + "std" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" + pn "gno.land/r/demo/position" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + max_timeout int64 = 9999999999 + + maxApprove uint64 = 18446744073709551615 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + mint01InRange() + mint02LowerRange() + mint03UpperRange() + swap() + collectFeeAfterSwap() + decreaseLiquidityUpperPosition() +} + +func createPool() { + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + gns.Faucet() + gns.Faucet() + gns.Faucet() + gns.Faucet() + gns.Faucet() + std.TestSkipHeights(5) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + pl.CreatePool(barPath, fooPath, fee500, "130621891405341611593710811006") // tick 10000 x2.7 + if len(pl.PoolGetPoolList()) != 1 { + panic("expected 1 pool") + } + std.TestSkipHeights(1) +} + +func mint01InRange() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), 18394892) + foo.Approve(a2u(consts.POOL_ADDR), 50000000) + + std.TestSetPrevAddr(test1) + std.TestSetPrevRealm(consts.STAKER_PATH) + tokenId, liquidity, amount0, amount1 := pn.Mint( + barPath, + fooPath, + fee500, + 8000, + 12000, + "50000000", + "50000000", + "0", + "0", + max_timeout, + test1.String(), + ) + if tokenId != 1 { + panic("expected tokenId 1") + } + if amount0 != "18394892" { + panic("expected amount0 18394892") + } + if amount1 != "49999999" { + panic("expected amount1 49999999") + } +} + +func mint02LowerRange() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), 0) + foo.Approve(a2u(consts.POOL_ADDR), 50000000) + + std.TestSetPrevAddr(test1) + std.TestSetPrevRealm(consts.STAKER_PATH) + tokenId, liquidity, amount0, amount1 := pn.Mint( + barPath, + fooPath, + fee500, + 5000, + 8000, + "50000000", + "50000000", + "0", + "0", + max_timeout, + test1.String(), + ) + if tokenId != 2 { + panic("expected tokenId 2") + } + if amount0 != "0" { + panic("expected amount0 0") + } + if amount1 != "49999999" { + panic("expected amount1 49999999") + } +} + +func mint03UpperRange() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), 50000000) + foo.Approve(a2u(consts.POOL_ADDR), 0) + + std.TestSetPrevAddr(test1) + std.TestSetPrevRealm(consts.STAKER_PATH) + tokenId, liquidity, amount0, amount1 := pn.Mint( + barPath, + fooPath, + fee500, + 12000, + 14000, + "50000000", + "50000000", + "0", + "0", + max_timeout, + test1.String(), + ) + if tokenId != 3 { + panic("expected tokenId 3") + } + if amount0 != "50000000" { + panic("expected amount0 50000000") + } + if amount1 != "0" { + panic("expected amount1 0") + } +} + +func swap() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), 1234567) + + std.TestSetPrevRealm(consts.ROUTER_PATH) + std.TestSetOrigCaller(test1) + amount0, amount1 := pl.Swap( + barPath, + fooPath, + fee500, + test1.String(), + true, + "1234567", + consts.MIN_PRICE, + test1.String(), + ) + if amount0 != "1234567" { + panic("expected amount0 1234567") + } + if amount1 != "-3332779" { + panic("expected amount1 -3332779") + } +} + +func collectFeeAfterSwap() { + std.TestSetPrevAddr(test1) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE + + tokenId, fee0, fee1, poolPath := pn.CollectFee(1) + if tokenId != 1 { + panic("expected tokenId 1") + } + if fee0 != "610" { + panic("expected fee0 610") + } + if fee1 != "0" { + panic("expected fee1 0") + } + +} + +func decreaseLiquidityUpperPosition() { + std.TestSetPrevAddr(test1) + + tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath := pn.DecreaseLiquidity(uint64(3), 100, "0", "0", max_timeout, false) + if tokenId != 3 { + panic("expected tokenId 3") + } + if amount0 != "49999999" { + panic("expected liquidity 49999999") + } + if amount1 != "0" { + panic("expected liquidity 0") + } + +} diff --git a/position/_TEST_/z2_position_increase_decrease_filetest.gno b/position/_TEST_/z2_position_increase_decrease_filetest.gno new file mode 100644 index 00000000..4801f557 --- /dev/null +++ b/position/_TEST_/z2_position_increase_decrease_filetest.gno @@ -0,0 +1,381 @@ +package main + +import ( + "std" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" + pn "gno.land/r/demo/position" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + max_timeout int64 = 9999999999 + + maxApprove uint64 = 18446744073709551615 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + mint() + increase() + decreasePartial() + swap() + decreaseFull() +} + +func createPool() { + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + gns.Faucet() + gns.Faucet() + gns.Faucet() + gns.Faucet() + gns.Faucet() + std.TestSkipHeights(5) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + pl.CreatePool(barPath, fooPath, fee500, "130621891405341611593710811006") // tick 10000 x2.7 + if len(pl.PoolGetPoolList()) != 1 { + panic("expected 1 pool") + } + std.TestSkipHeights(1) +} + +func mint() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), 18394892) + foo.Approve(a2u(consts.POOL_ADDR), 50000000) + + std.TestSetPrevAddr(test1) + std.TestSetPrevRealm(consts.STAKER_PATH) + tokenId, liquidity, amount0, amount1 := pn.Mint( + barPath, + fooPath, + fee500, + 8000, + 12000, + "50000000", + "50000000", + "0", + "0", + max_timeout, + test1.String(), + ) + if tokenId != 1 { + panic("expected tokenId 1") + } + if amount0 != "18394892" { + panic("expected amount0 18394892") + } + if amount1 != "49999999" { + panic("expected amount1 49999999") + } +} + +func increase() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), 3678979) + foo.Approve(a2u(consts.POOL_ADDR), 10000000) + + pool := pl.GetPool(barPath, fooPath, fee500) + oldLiquidity := pool.PoolGetLiquidity() + + if oldLiquidity.ToString() != "318704392" { + panic("expected liquidity 318704392") + } + + _, _, amount0, amount1, _ := pn.IncreaseLiquidity( + uint64(1), // tokenId + "10000000", // amount0Desired + "10000000", // amount1Desired + "0", // amount0Min + "0", // amount1Min + max_timeout, // deadline + ) + if amount0 != "3678979" { + panic("expected liquidity 3678979") + } + if amount1 != "9999999" { + panic("expected liquidity 9999999") + } + + newLiquidity := pool.PoolGetLiquidity() + if newLiquidity.ToString() != "382445270" { + panic("expected liquidity 382445270") + } + // 318704392 -> 382445270 +} + +func decreasePartial() { + std.TestSetPrevRealm("") + std.TestSetOrigCaller(test1) + + pool := pl.GetPool(barPath, fooPath, fee500) + oldLiquidity := pool.PoolGetLiquidity() + if oldLiquidity.ToString() != "382445270" { + panic("expected liquidity 382445270") + } + + // remove 50% of liquidity + _, liquidity, fee0, fee1, amount0, amount1, _ := pn.DecreaseLiquidity( + uint64(1), // tokenId + 50, // liquidityRatio + "0", // amount0Min + "0", // amount1Min + max_timeout, // deadline + false, // unwrapResult + ) + + if liquidity != "191222635" { + panic("expected liquidity 191222635") + } + if fee0 != "0" { + panic("expected fee0 0") + } + if fee1 != "0" { + panic("expected fee1 0") + } + if amount0 != "11036934" { + panic("expected amount0 11036934") + } + if amount1 != "29999999" { + panic("expected amount1 29999999") + } +} + +func swap() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), 1_234_567) + + std.TestSetPrevRealm(consts.ROUTER_PATH) + std.TestSetOrigCaller(test1) + amount0, amount1 := pl.Swap( + barPath, + fooPath, + fee500, + test1.String(), + true, + "1234567", + consts.MIN_PRICE, + test1.String(), + ) +} + +func decreaseFull() { + std.TestSetPrevAddr(test1) + + // protocol fee approval + bar.Approve(a2u(consts.POOL_ADDR), 10000000) + foo.Approve(a2u(consts.POOL_ADDR), 10000000) + + pool := pl.GetPool(barPath, fooPath, fee500) + oldLiquidity := pool.PoolGetLiquidity() + if oldLiquidity.ToString() != "191222635" { + panic("expected liquidity 191222635") + } + + // remove all + _, liquidity, fee0, fee1, amount0, amount1, _ := pn.DecreaseLiquidity( + uint64(1), // tokenId + 100, // liquidityRatio + "0", // amount0Min + "0", // amount1Min + max_timeout, // deadline + false, // unwrapResult + ) + if fee0 != "610" { + panic("expected fee0 610") + } + if fee1 != "0" { + panic("expected fee1 0") + } + + newLiquidity := pool.PoolGetLiquidity() + if newLiquidity.ToString() != "0" { + panic("expected liquidity 0") + } + + positionLiquidity := pn.PositionGetPositionLiquidity(uint64(1)) + if positionLiquidity.ToString() != "0" { + panic("expected position liquidity 0") + } +} diff --git a/position/_TEST_/z3_position_two_position_used_single_swap_filetest.gno b/position/_TEST_/z3_position_two_position_used_single_swap_filetest.gno new file mode 100644 index 00000000..a2e95c10 --- /dev/null +++ b/position/_TEST_/z3_position_two_position_used_single_swap_filetest.gno @@ -0,0 +1,335 @@ +package main + +import ( + "std" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" + pn "gno.land/r/demo/position" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + max_timeout int64 = 9999999999 + + maxApprove uint64 = 18446744073709551615 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + mint01Wide() + mint02Tight() + swap() + collect01() + collect02() +} + +func createPool() { + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + gns.Faucet() + gns.Faucet() + gns.Faucet() + gns.Faucet() + gns.Faucet() + std.TestSkipHeights(5) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + pl.CreatePool(barPath, fooPath, fee500, "130621891405341611593710811006") // tick 10000 x2.7 + if len(pl.PoolGetPoolList()) != 1 { + panic("expected 1 pool") + } + std.TestSkipHeights(1) +} + +func mint01Wide() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), 18394892) + foo.Approve(a2u(consts.POOL_ADDR), 50000000) + + std.TestSetPrevAddr(test1) + std.TestSetPrevRealm(consts.STAKER_PATH) + tokenId, liquidity, amount0, amount1 := pn.Mint( + barPath, + fooPath, + fee500, + 8000, + 12000, + "50000000", + "50000000", + "0", + "0", + max_timeout, + test1.String(), + ) + if tokenId != 1 { + panic("expected tokenId 1") + } + if liquidity != "318704392" { + panic("expected liquidity 318704392") + } + if amount0 != "18394892" { + panic("expected amount0 18394892") + } + if amount1 != "49999999" { + panic("expected amount1 49999999") + } +} + +func mint02Tight() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), 18394892) + foo.Approve(a2u(consts.POOL_ADDR), 50000000) + + std.TestSetPrevAddr(test1) + std.TestSetPrevRealm(consts.STAKER_PATH) + tokenId, liquidity, amount0, amount1 := pn.Mint( + barPath, + fooPath, + fee500, + 9500, + 10500, + "50000000", + "50000000", + "0", + "0", + max_timeout, + test1.String(), + ) + if tokenId != 2 { + panic("expected tokenId 2") + } + if liquidity != "1228379121" { + panic("expected liquidity 1228379121") + } + if amount0 != "18394892" { + panic("expected amount0 18394892") + } + if amount1 != "49999999" { + panic("expected amount1 49999999") + } +} + +func swap() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), 1_234_567) + + std.TestSetPrevRealm(consts.ROUTER_PATH) + std.TestSetOrigCaller(test1) + amount0, amount1 := pl.Swap( + barPath, + fooPath, + fee500, + test1.String(), + true, + "1234567", + consts.MIN_PRICE, + test1.String(), + ) +} + +func collect01() { + std.TestSetPrevAddr(test1) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE + + tokenId, fee0, fee1, poolPath := pn.CollectFee(1) + if fee0 != "126" { + panic("expected fee0 126") + } + if fee1 != "0" { + panic("expected fee1 0") + } +} + +func collect02() { + std.TestSetPrevAddr(test1) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE + + tokenId, fee0, fee1, poolPath := pn.CollectFee(2) + if fee0 != "485" { + panic("expected fee0 485") + } + if fee1 != "0" { + panic("expected fee1 0") + } +} diff --git a/position/position.gno b/position/position.gno index 34e9ef67..a10684da 100644 --- a/position/position.gno +++ b/position/position.gno @@ -37,7 +37,6 @@ func Mint( deadline int64, mintTo string, ) (uint64, string, string, string) { // tokenId, liquidity, amount0, amount1 ( *u256.Uint x3 ) - if common.GetLimitCaller() { // only user or staker can call isUserCalled := std.IsOriginCall() From 70ec9d8f9ffc28adbe97ed1afc875a561a6e0df3 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 2 May 2024 15:14:20 +0900 Subject: [PATCH 6/9] test: integeration test for `router` --- router/_TEST_/_TEST_0_INIT_FAUCET_test.gno | 25 -- .../_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno | 62 --- router/_TEST_/_TEST_router_ratio_test.gn | 123 ------ ...ap_route_1route_1hop_native_in_out_test.gn | 145 ------- ...r_swap_route_1route_1hop_out_range_test.gn | 86 ---- ...TEST_router_swap_route_1route_1hop_test.gn | 180 -------- ..._1route_2hop_wrapped_native_in_out_test.gn | 136 ------ ..._1route_3hop_wrapped_native_middle_test.gn | 112 ----- ...TEST_router_swap_route_2route_2hop_test.gn | 179 -------- .../_TEST_routes_find_swap_path_test.gn | 134 ------ router/_TEST_/z1_ratio_filetest.gno | 336 +++++++++++++++ .../z2_swap_route_1route_1hop_filetest.gno | 398 ++++++++++++++++++ ..._route_1route_1hop_out_range_filetest.gno} | 162 ++++++- ...ute_1route_1hop_native_in_out_filetest.gno | 351 +++++++++++++++ ...te_1route_2hop_wrapped_middle_filetest.gno | 314 ++++++++++++++ 15 files changed, 1539 insertions(+), 1204 deletions(-) delete mode 100644 router/_TEST_/_TEST_0_INIT_FAUCET_test.gno delete mode 100644 router/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno delete mode 100644 router/_TEST_/_TEST_router_ratio_test.gn delete mode 100644 router/_TEST_/_TEST_router_swap_route_1route_1hop_native_in_out_test.gn delete mode 100644 router/_TEST_/_TEST_router_swap_route_1route_1hop_out_range_test.gn delete mode 100644 router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gn delete mode 100644 router/_TEST_/_TEST_router_swap_route_1route_2hop_wrapped_native_in_out_test.gn delete mode 100644 router/_TEST_/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gn delete mode 100644 router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gn delete mode 100644 router/_TEST_/_TEST_routes_find_swap_path_test.gn create mode 100644 router/_TEST_/z1_ratio_filetest.gno create mode 100644 router/_TEST_/z2_swap_route_1route_1hop_filetest.gno rename router/_TEST_/{_TEST_0_INIT_TOKEN_REGISTER_test.gno => z3_swap_route_1route_1hop_out_range_filetest.gno} (51%) create mode 100644 router/_TEST_/z4_swap_route_1route_1hop_native_in_out_filetest.gno create mode 100644 router/_TEST_/z5_swap_route_1route_2hop_wrapped_middle_filetest.gno diff --git a/router/_TEST_/_TEST_0_INIT_FAUCET_test.gno b/router/_TEST_/_TEST_0_INIT_FAUCET_test.gno deleted file mode 100644 index 586e33ff..00000000 --- a/router/_TEST_/_TEST_0_INIT_FAUCET_test.gno +++ /dev/null @@ -1,25 +0,0 @@ -package router - -import ( - "std" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/foo" - "gno.land/r/demo/obl" - "gno.land/r/demo/qux" -) - -func init() { - std.TestSetPrevAddr(test1) - - // GIVE 100_000_000_000(u) ≈ 100_000 - for i := 0; i < 100; i++ { - foo.Faucet() - bar.Faucet() - baz.Faucet() - qux.Faucet() - obl.Faucet() - //gns.Faucet() - } -} diff --git a/router/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno b/router/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno deleted file mode 100644 index a7402d4c..00000000 --- a/router/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno +++ /dev/null @@ -1,62 +0,0 @@ -package router - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" -) - -var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") - - fooPath string = "gno.land/r/demo/foo" - barPath string = "gno.land/r/demo/bar" - bazPath string = "gno.land/r/demo/baz" - quxPath string = "gno.land/r/demo/qux" - - oblPath string = "gno.land/r/demo/obl" - // wugnotPath string = "gno.land/r/demo/wugnot" // from consts - // gnsPath string = "gno.land/r/demo/gns" // from consts - - fee100 uint32 = 100 - fee500 uint32 = 500 - fee3000 uint32 = 3000 - - max_timeout int64 = 9999999999 -) - -/* HELPER */ -func shouldEQ(t *testing.T, got, expected interface{}) { - if got != expected { - t.Errorf("got %v, expected %v", got, expected) - } -} - -func shouldNEQ(t *testing.T, got, expected interface{}) { - if got == expected { - t.Errorf("got %v, didn't expected %v", got, expected) - } -} - -func shouldGT(t *testing.T, l, r interface{}) { - if !(l < r) { - t.Errorf("expected %v < %v", l, r) - } -} - -func shouldLT(t *testing.T, l, r interface{}) { - if !(l > r) { - t.Errorf("expected %v > %v", l, r) - } -} - -func shouldPanic(t *testing.T, f func()) { - defer func() { - if r := recover(); r == nil { - t.Errorf("expected panic") - } - }() - f() -} diff --git a/router/_TEST_/_TEST_router_ratio_test.gn b/router/_TEST_/_TEST_router_ratio_test.gn deleted file mode 100644 index d9b032f2..00000000 --- a/router/_TEST_/_TEST_router_ratio_test.gn +++ /dev/null @@ -1,123 +0,0 @@ -package router - -import ( - "std" - "testing" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/p/demo/json" - - "gno.land/r/demo/gnoswap/common" - "gno.land/r/demo/gnoswap/consts" - - "gno.land/r/demo/bar" - "gno.land/r/demo/foo" - "gno.land/r/demo/gns" - "gno.land/r/demo/qux" - "gno.land/r/demo/wugnot" -) - -func TestPoolCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 10); i++ { - gns.Faucet() - } - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*10) - - // 1 HOPS - pl.CreatePool(barPath, consts.WRAPPED_WUGNOT, fee100, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) - pl.CreatePool(barPath, consts.WRAPPED_WUGNOT, fee500, common.TickMathGetSqrtRatioAtTick(20_000).ToString()) - pl.CreatePool(barPath, consts.WRAPPED_WUGNOT, fee3000, common.TickMathGetSqrtRatioAtTick(60_000).ToString()) // NOT USED BY SMALL LIQ - // -10_000 0.367897 - // -20_000 0.135348 - // 0.367897 + 0.135348 = 0.503245 - // 0.503245 / 2 = 0.2516225 - // 1 WGNOT = 0.2516225 BAR - - // 2 HOPS - pl.CreatePool(consts.WRAPPED_WUGNOT, quxPath, fee100, common.TickMathGetSqrtRatioAtTick(0).ToString()) // 1:1 - pl.CreatePool(quxPath, fooPath, fee100, common.TickMathGetSqrtRatioAtTick(50_000).ToString()) - pl.CreatePool(quxPath, fooPath, fee500, common.TickMathGetSqrtRatioAtTick(60_000).ToString()) - pl.CreatePool(quxPath, fooPath, fee3000, common.TickMathGetSqrtRatioAtTick(100_000).ToString()) // NOT USED BY SMALL LIQ - // 50_000 148.376062 - // 60_000 403.307791 - // 148.376062 + 403.307791 = 551.683853 - // 551.683853 / 2 = 275.8419265 - // 1 WGNOT = 275.8419265 FOO - - // 3 HOPS - pl.CreatePool(consts.WRAPPED_WUGNOT, bazPath, fee100, common.TickMathGetSqrtRatioAtTick(6932).ToString()) // 1:2 - pl.CreatePool(bazPath, oblPath, fee100, common.TickMathGetSqrtRatioAtTick(6932).ToString()) // 1:2 - pl.CreatePool(oblPath, consts.GNS_PATH, fee100, common.TickMathGetSqrtRatioAtTick(6932).ToString()) // 1:2 - // 1 WGNOT = 8 GNS -} - -func TestPositionMint(t *testing.T) { - std.TestSetOrigCaller(test1) - - // prepare ugnot - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.IssueCoin(test1, "ugnot", 100_000_000_000) - std.TestSetOrigSend(std.Coins{{"ugnot", 100_000_000_000}}, nil) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 100_000_000_000) - - // Deposit(wrap) - std.TestSetPrevAddr(test1) - wugnot.Deposit() - - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - wugnot.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - pn.Mint(barPath, consts.WRAPPED_WUGNOT, fee100, 8000, 12000, "100000000", "100000000", "0", "0", max_timeout, test1.String()) - pn.Mint(barPath, consts.WRAPPED_WUGNOT, fee500, 18000, 22000, "80000000", "80000000", "0", "0", max_timeout, test1.String()) - pn.Mint(barPath, consts.WRAPPED_WUGNOT, fee3000, 58020, 62040, "10000000", "10000000", "0", "0", max_timeout, test1.String()) - - pn.Mint(quxPath, fooPath, fee100, 48000, 52000, "100000000", "100000000", "0", "0", max_timeout, test1.String()) - pn.Mint(quxPath, fooPath, fee500, 58000, 62000, "80000000", "80000000", "0", "0", max_timeout, test1.String()) - pn.Mint(quxPath, fooPath, fee3000, 98040, 10020, "100000", "100000", "0", "0", max_timeout, test1.String()) - - // pn.Mint(fooPath, consts.GNS_PATH, fee100, 48000, 52000, "100000000", "100000000","0", "0" max_timeout, test1.String()) -} - -func TestApiGetRatiosFromBase(t *testing.T) { - std.TestSetOrigCaller(test1) - - ratios := ApiGetRatiosFromBase() - - root, err := json.Unmarshal([]byte(ratios)) - if err != nil { - panic(err) - } - - response, err := root.GetKey("response") - if err != nil { - panic(err) - } - shouldEQ(t, response.Size(), 7) - - arr, err := response.GetArray() - if err != nil { - panic(err) - } - - token0, _ := response.GetIndex(0) - token1, _ := response.GetIndex(1) - token2, _ := response.GetIndex(2) - token3, _ := response.GetIndex(3) - token4, _ := response.GetIndex(4) - token5, _ := response.GetIndex(5) - token6, _ := response.GetIndex(6) - - shouldEQ(t, token0.String(), "{\"token\":\"gno.land/r/demo/wugnot\",\"ratio\":\"79228162514264337593543950336\"}") // WGNOT: 1 - shouldEQ(t, token1.String(), "{\"token\":\"gno.land/r/demo/bar\",\"ratio\":\"19935653721785907863909200771\"}") // BAR: 0.2516233255 - shouldEQ(t, token2.String(), "{\"token\":\"gno.land/r/demo/qux\",\"ratio\":\"79228162514264337593543950338\"}") // QUX: 1 - shouldEQ(t, token3.String(), "{\"token\":\"gno.land/r/demo/foo\",\"ratio\":\"21854449020437189859372345675086\"}") // FOO: 275.8419269979 - shouldEQ(t, token4.String(), "{\"token\":\"gno.land/r/demo/baz\",\"ratio\":\"158459202898910110285447649633\"}") // BAZ: 2.0000363238 - shouldEQ(t, token5.String(), "{\"token\":\"gno.land/r/demo/obl\",\"ratio\":\"316924161643118367991168631862\"}") // OBL: 4.0001452966 - shouldEQ(t, token6.String(), "{\"token\":\"gno.land/r/demo/gns\",\"ratio\":\"633859835185907382391729498085\"}") // GNS: 8.0004358939 -} diff --git a/router/_TEST_/_TEST_router_swap_route_1route_1hop_native_in_out_test.gn b/router/_TEST_/_TEST_router_swap_route_1route_1hop_native_in_out_test.gn deleted file mode 100644 index d8de441e..00000000 --- a/router/_TEST_/_TEST_router_swap_route_1route_1hop_native_in_out_test.gn +++ /dev/null @@ -1,145 +0,0 @@ -package router - -import ( - "std" - "testing" - - "gno.land/p/demo/json" - - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/gns" - "gno.land/r/demo/qux" - "gno.land/r/demo/wugnot" -) - -func TestCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 3); i++ { - gns.Faucet() - } - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*3) - - pl.CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - pl.CreatePool(bazPath, quxPath, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - pl.CreatePool(quxPath, consts.WRAPPED_WUGNOT, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - // 1 bar ≈ 19.683 gnot - - jsonStr := pl.ApiGetPools() - root, err := json.Unmarshal([]byte(jsonStr)) - if err != nil { - panic(err) - } - - response, err := root.GetKey("response") - if err != nil { - panic(err) - } - - shouldEQ(t, response.Size(), 3) -} - -func TestPositionMintBarBaz(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - tokenId, liquidity, amount0, amount1 := pn.Mint(barPath, bazPath, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, test1.String()) - shouldEQ(t, tokenId, uint64(1)) - shouldEQ(t, amount0, "36790") // bar - shouldEQ(t, amount1, "99999") // baz -} - -func TestPositionMintBazQux(t *testing.T) { - std.TestSetPrevAddr(test1) - baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - tokenId, liquidity, amount0, amount1 := pn.Mint(bazPath, quxPath, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, test1.String()) - shouldEQ(t, tokenId, uint64(2)) - shouldEQ(t, amount0, "36790") - shouldEQ(t, amount1, "99999") - -} - -func TestPositionMintQuxGnot(t *testing.T) { - std.TestSetPrevAddr(test1) - - // send - std.TestIssueCoins(test1, std.Coins{{"ugnot", 1000009}}) - std.TestSetOrigSend(std.Coins{{"ugnot", 1000009}}, nil) - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 1000009) - - // Deposit(wrap) - std.TestSetPrevAddr(test1) - wugnot.Deposit() - - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - wugnot.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - tokenId, liquidity, amount0, amount1 := pn.Mint(quxPath, consts.WRAPPED_WUGNOT, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, test1.String()) - shouldEQ(t, tokenId, uint64(3)) - shouldEQ(t, amount0, "36790") - shouldEQ(t, amount1, "99999") -} - -func TestDrySwapRouteBarGnotExactIn(t *testing.T) { - std.TestSetOrigCaller(test1) - - dryResult := DrySwapRoute( - barPath, // inputToken - consts.WRAPPED_WUGNOT, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500*POOL*gno.land/r/demo/qux:gno.land/r/demo/wugnot:500", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, "19740") -} - -func TestDrySwapRouteBarGnotExactOut(t *testing.T) { - std.TestSetOrigCaller(test1) - - dryResult := DrySwapRoute( - barPath, // inputToken - consts.WRAPPED_WUGNOT, // outputToken - "20000", // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500*POOL*gno.land/r/demo/qux:gno.land/r/demo/wugnot:500", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, "1012") -} - -func TestDrySwapRouteGnotBarExactIn(t *testing.T) { - std.TestSetOrigCaller(test1) - - dryResult := DrySwapRoute( - consts.WRAPPED_WUGNOT, // intputToken - barPath, // outputToken - "5000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/wugnot:gno.land/r/demo/qux:500*POOL*gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, "247") -} - -func TestDrySwapRouteGnotBarExactOut(t *testing.T) { - std.TestSetOrigCaller(test1) - - dryResult := DrySwapRoute( - consts.WRAPPED_WUGNOT, // intputToken - barPath, // outputToken - "100", // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/demo/wugnot:gno.land/r/demo/qux:500*POOL*gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, "2003") -} diff --git a/router/_TEST_/_TEST_router_swap_route_1route_1hop_out_range_test.gn b/router/_TEST_/_TEST_router_swap_route_1route_1hop_out_range_test.gn deleted file mode 100644 index f234f698..00000000 --- a/router/_TEST_/_TEST_router_swap_route_1route_1hop_out_range_test.gn +++ /dev/null @@ -1,86 +0,0 @@ -package router - -import ( - "std" - "testing" - - "gno.land/p/demo/json" - - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/gns" -) - -func TestCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 1); i++ { - gns.Faucet() - } - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - pl.CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - - jsonStr := pl.ApiGetPools() - root, err := json.Unmarshal([]byte(jsonStr)) - if err != nil { - panic(err) - } - - response, err := root.GetKey("response") - if err != nil { - panic(err) - } - - shouldEQ(t, response.Size(), 1) -} - -func TestPositionMint(t *testing.T) { - // bar_baz_500 by test1 - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - // Mint - tokenId, liquidity, amount0, amount1 := pn.Mint(barPath, bazPath, fee500, int32(12000), int32(15000), "100000", "100000", "0", "0", max_timeout, test1.String()) - shouldEQ(t, tokenId, uint64(1)) - // shouldEQ(t, liquidity, "1308151") - shouldEQ(t, amount0, "100000") // ONLY BAR - shouldEQ(t, amount1, "0") // NO BAZ -} - -func TestDrySwapRouteBarBazExactIn(t *testing.T) { - std.TestSetOrigCaller(test1) - - dryResult := DrySwapRoute( - barPath, // inputToken - bazPath, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500", // strRouteArr - "100", // quoteArr - ) - - shouldEQ(t, dryResult, "-1") -} - -func TestSwapRouteBarBazExactIn(t *testing.T) { - std.TestSetOrigCaller(test1) - - shouldPanic( - t, - func() { - SwapRoute( - barPath, // inputToken - bazPath, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "100", // quoteArr - "2710", // tokenAmountLimit ( too few recieved (expected 2710, got 300)) - ) - }) -} diff --git a/router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gn b/router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gn deleted file mode 100644 index f079d7f3..00000000 --- a/router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gn +++ /dev/null @@ -1,180 +0,0 @@ -package router - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - - "gno.land/r/demo/gns" -) - -func TestCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 1); i++ { - gns.Faucet() - } - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - pl.CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - - // jsonOutput := pl.ApiGetPools() - // jsonStr := gjson.Parse(jsonOutput) - // shouldEQ(t, len(jsonStr.Get("response").Array()), 1) -} - -func TestPositionMint(t *testing.T) { - // bar_baz_500 by test1 - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), 36790) - baz.Approve(a2u(consts.POOL_ADDR), 100000) - - // Mint - tokenId, liquidity, amount0, amount1 := pn.Mint(barPath, bazPath, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, test1.String()) - shouldEQ(t, tokenId, uint64(1)) - shouldEQ(t, amount0, "36790") - shouldEQ(t, amount1, "99999") -} - -func TestDrySwapRouteBarBazExactIn(t *testing.T) { - std.TestSetPrevAddr(test1) - - dryResult := DrySwapRoute( - barPath, // inputToken - bazPath, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500", // strRouteArr - "100", // quoteArr - ) - - shouldEQ(t, dryResult, "2711") -} - -func TestSwapRouteBarBazExactIn(t *testing.T) { - std.TestSetPrevAddr(test1) - - bar.Approve(a2u(consts.POOL_ADDR), uint64(1000)) - baz.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee - - amountIn, amountOut := SwapRoute( - barPath, // inputToken - bazPath, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500", // strRouteArr - "100", // quoteArr - "2700", // tokenAmountLimit - ) - shouldEQ(t, amountIn, "1000") - shouldEQ(t, amountOut, "-2707") -} - -func TestDrySwapRouteBarBazExactOut(t *testing.T) { - std.TestSetPrevAddr(test1) - - dryResult := DrySwapRoute( - barPath, // inputToken - bazPath, // outputToken - "1000", // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500", // strRouteArr - "100", // quoteArr - ) - - shouldEQ(t, dryResult, "370") -} - -func TestSwapRouteBarBazExactOut(t *testing.T) { - std.TestSetPrevAddr(test1) - - bar.Approve(a2u(consts.POOL_ADDR), uint64(1000)) - baz.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee - - amountIn, amountOut := SwapRoute( - barPath, // inputToken - bazPath, // outputToken - "1000", // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500", // strRouteArr - "100", // quoteArr - "370", // tokenAmountLimit - ) - - shouldEQ(t, amountIn, "370") - shouldEQ(t, amountOut, "-999") -} - -func TestDrySwapRouteBazBarExactIn(t *testing.T) { - std.TestSetPrevAddr(test1) - - dryResult := DrySwapRoute( - bazPath, // inputToken - barPath, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "100", // quoteArr - ) - - shouldEQ(t, dryResult, "368") -} - -func TestSwapRouteBazBarExactIn(t *testing.T) { - std.TestSetPrevAddr(test1) - - baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - bar.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee - - amountIn, amountOut := SwapRoute( - bazPath, // inputToken - barPath, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "100", // quoteArr - "360", // tokenAmountLimit - ) - - shouldEQ(t, amountIn, "1000") - shouldEQ(t, amountOut, "-368") -} - -func TestDrySwapRouteBazBarExactOut(t *testing.T) { - std.TestSetPrevAddr(test1) - - dryResult := DrySwapRoute( - bazPath, // inputToken - barPath, // outputToken - "3000", // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "100", // quoteArr - ) - - shouldEQ(t, dryResult, "8169") -} - -func TestSwapRouteBazBarExactOut(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) - - amountIn, amountOut := SwapRoute( - bazPath, // inputToken - barPath, // outputToken - "3000", // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "100", // quoteArr - "8200", // tokenAmountLimit - ) - - shouldEQ(t, amountIn, "8169") - shouldEQ(t, amountOut, "-2996") -} diff --git a/router/_TEST_/_TEST_router_swap_route_1route_2hop_wrapped_native_in_out_test.gn b/router/_TEST_/_TEST_router_swap_route_1route_2hop_wrapped_native_in_out_test.gn deleted file mode 100644 index 672269f2..00000000 --- a/router/_TEST_/_TEST_router_swap_route_1route_2hop_wrapped_native_in_out_test.gn +++ /dev/null @@ -1,136 +0,0 @@ -package router - -import ( - // @notJoon JSON - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/gns" - "gno.land/r/demo/qux" - "gno.land/r/demo/wugnot" -) - -func TestCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 3); i++ { - gns.Faucet() - } - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*3) - - pl.CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - pl.CreatePool(bazPath, quxPath, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - pl.CreatePool(quxPath, consts.WRAPPED_WUGNOT, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - // 1 bar ≈ 19.683 gnot - - // jsonOutput := pl.ApiGetPools() - // jsonStr := gjson.Parse(jsonOutput) - // shouldEQ(t, len(jsonStr.Get("response").Array()), 3) -} - -func TestPositionMintBarBaz(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - tokenId, liquidity, amount0, amount1 := pn.Mint(barPath, bazPath, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, test1.String()) - shouldEQ(t, tokenId, uint64(1)) - shouldEQ(t, amount0, "36790") // bar - shouldEQ(t, amount1, "99999") // baz -} - -func TestPositionMintBazQux(t *testing.T) { - std.TestSetPrevAddr(test1) - baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - tokenId, liquidity, amount0, amount1 := pn.Mint(bazPath, quxPath, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, test1.String()) - shouldEQ(t, tokenId, uint64(2)) - shouldEQ(t, amount0, "36790") - shouldEQ(t, amount1, "99999") - -} - -func TestPositionMintQuxGnot(t *testing.T) { - std.TestSetPrevAddr(test1) - - // send - std.TestIssueCoins(test1, std.Coins{{"ugnot", 1000009}}) - std.TestSetOrigSend(std.Coins{{"ugnot", 1000009}}, nil) - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 1000009) - - // Deposit(wrap) - std.TestSetPrevAddr(test1) - wugnot.Deposit() - - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - wugnot.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - tokenId, liquidity, amount0, amount1 := pn.Mint(quxPath, consts.WRAPPED_WUGNOT, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, test1.String()) - shouldEQ(t, tokenId, uint64(3)) - shouldEQ(t, amount0, "36790") - shouldEQ(t, amount1, "99999") -} - -func TestDrySwapRouteBarGnotExactIn(t *testing.T) { - std.TestSetOrigCaller(test1) - - dryResult := DrySwapRoute( - barPath, // inputToken - consts.WRAPPED_WUGNOT, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500*POOL*gno.land/r/demo/qux:gno.land/r/demo/wugnot:500", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, "19740") -} - -func TestDrySwapRouteBarGnotExactOut(t *testing.T) { - std.TestSetOrigCaller(test1) - - dryResult := DrySwapRoute( - barPath, // inputToken - consts.WRAPPED_WUGNOT, // outputToken - "20000", // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500*POOL*gno.land/r/demo/qux:gno.land/r/demo/wugnot:500", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, "1012") -} - -func TestDrySwapRouteGnotBarExactIn(t *testing.T) { - std.TestSetOrigCaller(test1) - - dryResult := DrySwapRoute( - consts.WRAPPED_WUGNOT, // intputToken - barPath, // outputToken - "5000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/wugnot:gno.land/r/demo/qux:500*POOL*gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, "247") -} - -func TestDrySwapRouteGnotBarExactOut(t *testing.T) { - std.TestSetOrigCaller(test1) - - dryResult := DrySwapRoute( - consts.WRAPPED_WUGNOT, // intputToken - barPath, // outputToken - "100", // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/demo/wugnot:gno.land/r/demo/qux:500*POOL*gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, "2003") -} diff --git a/router/_TEST_/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gn b/router/_TEST_/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gn deleted file mode 100644 index 1c97a991..00000000 --- a/router/_TEST_/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gn +++ /dev/null @@ -1,112 +0,0 @@ -package router - -import ( - // @notJoon JSON - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/gns" - "gno.land/r/demo/wugnot" -) - -func TestCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 2); i++ { - gns.Faucet() - } - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*2) - - pl.CreatePool(consts.GNS_PATH, consts.WRAPPED_WUGNOT, fee100, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - pl.CreatePool(consts.WRAPPED_WUGNOT, barPath, fee100, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - // 1 gns ≈ 7.29 bar - - // jsonOutput := pl.ApiGetPools() - // jsonStr := gjson.Parse(jsonOutput) - // shouldEQ(t, len(jsonStr.Get("response").Array()), 2) -} - -func TestPositionMintGnsGnot(t *testing.T) { - std.TestSetPrevAddr(test1) - - // send - std.TestIssueCoins(test1, std.Coins{{"ugnot", 999999}}) - std.TestSetOrigSend(std.Coins{{"ugnot", 999999}}, nil) - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 999999) - - // Deposit(wrap) - std.TestSetPrevAddr(test1) - wugnot.Deposit() - - gns.Faucet() - - gns.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - wugnot.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - tokenId, liquidity, amount0, amount1 := pn.Mint(consts.GNS_PATH, consts.WRAPPED_WUGNOT, fee100, int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, test1.String()) - shouldEQ(t, tokenId, uint64(1)) - shouldEQ(t, amount0, "36790") - shouldEQ(t, amount1, "99999") -} - -func TestPositionMintGnotBar(t *testing.T) { - std.TestSetOrigCaller(test1) - - // send - std.TestIssueCoins(test1, std.Coins{{"ugnot", 999999}}) - std.TestSetOrigSend(std.Coins{{"ugnot", 999999}}, nil) - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 999999) - - // Deposit(wrap) - std.TestSetPrevAddr(test1) - wugnot.Deposit() - - wugnot.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - tokenId, liquidity, amount0, amount1 := pn.Mint(consts.WRAPPED_WUGNOT, barPath, fee100, int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, test1.String()) - shouldEQ(t, tokenId, uint64(2)) - shouldEQ(t, amount0, "100000") // 100000 - shouldEQ(t, amount1, "36789") // 36789 -} - -func TestDrySwapRouteGnsBarExactIn(t *testing.T) { - std.TestSetOrigCaller(test1) - - dryResult := DrySwapRoute( - consts.GNS_PATH, // inputToken - barPath, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/gns:gno.land/r/demo/wugnot:100*POOL*gno.land/r/demo/wugnot:gno.land/r/demo/bar:100", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, "7339") -} - -func TestSwapRouteGnsBarExactIn(t *testing.T) { - std.TestSetPrevAddr(test1) - - gns.Approve(a2u(consts.POOL_ADDR), 1000) // swap input amount - bar.Approve(a2u(consts.ROUTER_ADDR), 7328) // 0.15% fee - - amountIn, amountOut := SwapRoute( - consts.GNS_PATH, // inputToken - barPath, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/gns:gno.land/r/demo/wugnot:100*POOL*gno.land/r/demo/wugnot:gno.land/r/demo/bar:100", // strRouteArr - "100", // quoteArr - "0", // tokenAmountLimit - ) - shouldEQ(t, amountIn, "1000") - shouldEQ(t, amountOut, "-7328") -} diff --git a/router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gn b/router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gn deleted file mode 100644 index c0e5f824..00000000 --- a/router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gn +++ /dev/null @@ -1,179 +0,0 @@ -package router - -import ( - // @notJoon JSON - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/gns" - "gno.land/r/demo/qux" -) - -func TestCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 2); i++ { - gns.Faucet() - } - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*2) - - pl.CreatePool(barPath, bazPath, uint16(500), "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - pl.CreatePool(bazPath, quxPath, uint16(500), "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - - // jsonOutput := pl.ApiGetPools() - // jsonStr := gjson.Parse(jsonOutput) - // shouldEQ(t, len(jsonStr.Get("response").Array()), 2) -} - -func TestPositionMint(t *testing.T) { - // bar_baz_500 by test1 - std.TestSetPrevAddr(test1) - - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - // Mint - pn.Mint(barPath, bazPath, uint16(500), int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, test1.String()) - pn.Mint(bazPath, quxPath, uint16(500), int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, test1.String()) -} - -func TestDrySwapRouteBarQuxExactIn(t *testing.T) { - std.TestSetOrigCaller(test1) - - dryResult := DrySwapRoute( - barPath, // inputToken - quxPath, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500,gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500", // strRouteArr - "50,50", // quoteArr - ) - - shouldEQ(t, dryResult, "7346") -} - -func TestSwapRouteBarQuxExactIn(t *testing.T) { - std.TestSetPrevAddr(test1) - - bar.Approve(a2u(consts.POOL_ADDR), 10000) - qux.Approve(a2u(consts.ROUTER_ADDR), 10000) - - amountIn, amountOut := SwapRoute( - barPath, // inputToken - quxPath, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500,gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500", // strRouteArr - "50,50", // quoteArr - "1", // tokenAmountLimit - ) - - shouldEQ(t, amountIn, "1000") - shouldEQ(t, amountOut, "-7318") -} - -func TestDrySwapRouteBarQuxExactOut(t *testing.T) { - std.TestSetPrevAddr(test1) - - dryResult := DrySwapRoute( - barPath, // inputToken - quxPath, // outputToken - "1000", // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500,gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500", // strRouteArr - "50,50", // quoteArr - ) - - shouldEQ(t, dryResult, "138") -} - -func TestSwapRouteBarQuxExactOut(t *testing.T) { - std.TestSetPrevAddr(test1) - - amountIn, amountOut := SwapRoute( - barPath, // inputToken - quxPath, // outputToken - "1000", // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500,gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500", // strRouteArr - "50,50", // quoteArr - "99999", // tokenAmountLimit - ) - - shouldEQ(t, amountIn, "138") - shouldEQ(t, amountOut, "-996") -} - -func TestDrySwapRouteQuxBarExactIn(t *testing.T) { - std.TestSetPrevAddr(test1) - - dryResult := DrySwapRoute( - quxPath, // inputToken - barPath, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500,gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "30,70", // quoteArr - ) - - shouldEQ(t, dryResult, "135") -} - -func TestSwapRouteQuxBarExactIn(t *testing.T) { - std.TestSetPrevAddr(test1) - - amountIn, amountOut := SwapRoute( - quxPath, // inputToken - barPath, // outputToken - "1000", // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500,gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "30,70", // quoteArr - "1", // tokenAmountLimit - ) - - shouldEQ(t, amountIn, "1000") - shouldEQ(t, amountOut, "-135") -} - -func TestDrySwapRouteQuxBarExactOut(t *testing.T) { - std.TestSetPrevAddr(test1) - - dryResult := DrySwapRoute( - quxPath, // inputToken - barPath, // outputToken - "1000", // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500,gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "30,70", // quoteArr - ) - - shouldEQ(t, dryResult, "7336") -} - -func TestwapRouteQuxBarExactOut(t *testing.T) { - std.TestSetPrevAddr(test1) - - qux.Approve(a2u(consts.POOL_ADDR), 10000) - bar.Approve(a2u(consts.ROUTER_ADDR), 10000) - - amountIn, amountOut := SwapRoute( - quxPath, // inputToken - barPath, // outputToken - "1000", // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500,gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr - "30,70", // quoteArr - "99999", // tokenAmountLimit - ) - - shouldEQ(t, amountIn, "7350") - shouldEQ(t, amountOut, "-997") -} diff --git a/router/_TEST_/_TEST_routes_find_swap_path_test.gn b/router/_TEST_/_TEST_routes_find_swap_path_test.gn deleted file mode 100644 index 99731e9d..00000000 --- a/router/_TEST_/_TEST_routes_find_swap_path_test.gn +++ /dev/null @@ -1,134 +0,0 @@ -package router - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/common" - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/foo" - "gno.land/r/demo/gns" - "gno.land/r/demo/qux" - "gno.land/r/demo/wugnot" -) - -func TestPoolCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 10); i++ { - gns.Faucet() - } - - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*(3+4+3)) - - // 1 HOPS - pl.CreatePool(barPath, consts.WRAPPED_WUGNOT, fee100, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) - pl.CreatePool(barPath, consts.WRAPPED_WUGNOT, fee500, common.TickMathGetSqrtRatioAtTick(20_000).ToString()) - pl.CreatePool(barPath, consts.WRAPPED_WUGNOT, fee3000, common.TickMathGetSqrtRatioAtTick(60_000).ToString()) // NOT USED BY SMALL LIQ - // -10_000 0.367897 - // -20_000 0.135348 - // 0.367897 + 0.135348 = 0.503245 - // 0.503245 / 2 = 0.2516225 - // 1 WGNOT = 0.2516225 BAR - - // 2 HOPS - pl.CreatePool(consts.WRAPPED_WUGNOT, quxPath, fee100, common.TickMathGetSqrtRatioAtTick(0).ToString()) // 1:1 - pl.CreatePool(quxPath, fooPath, fee100, common.TickMathGetSqrtRatioAtTick(50_000).ToString()) - pl.CreatePool(quxPath, fooPath, fee500, common.TickMathGetSqrtRatioAtTick(60_000).ToString()) - pl.CreatePool(quxPath, fooPath, fee3000, common.TickMathGetSqrtRatioAtTick(100_000).ToString()) // NOT USED BY SMALL LIQ - // 50_000 148.376062 - // 60_000 403.307791 - // 148.376062 + 403.307791 = 551.683853 - // 551.683853 / 2 = 275.8419265 - // 1 WGNOT = 275.8419265 BAR - - // 3 HOPS - pl.CreatePool(consts.WRAPPED_WUGNOT, bazPath, fee100, common.TickMathGetSqrtRatioAtTick(6932).ToString()) // 1:2 - pl.CreatePool(bazPath, oblPath, fee100, common.TickMathGetSqrtRatioAtTick(6932).ToString()) // 1:2 - pl.CreatePool(oblPath, consts.GNS_PATH, fee100, common.TickMathGetSqrtRatioAtTick(6932).ToString()) // 1:2 - // 1 GNOT = 8 GNS -} - -func TestPositionMint(t *testing.T) { - std.TestSetPrevAddr(test1) - - // wugnot prepare - std.TestIssueCoins(test1, std.Coins{{"ugnot", 100_000_000_000}}) - std.TestSetOrigSend(std.Coins{{"ugnot", 100_000_000_000}}, nil) - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 100_000_000_000) - - // Deposit(wrap) - std.TestSetPrevAddr(test1) - wugnot.Deposit() - - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - wugnot.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - - pn.Mint(barPath, consts.WRAPPED_WUGNOT, fee100, 8000, 12000, "100000000", "100000000", "0", "0", max_timeout, test1.String()) - pn.Mint(barPath, consts.WRAPPED_WUGNOT, fee500, 18000, 22000, "80000000", "80000000", "0", "0", max_timeout, test1.String()) - pn.Mint(barPath, consts.WRAPPED_WUGNOT, fee3000, 58020, 62040, "10000000", "10000000", "0", "0", max_timeout, test1.String()) - - pn.Mint(quxPath, fooPath, fee100, 48000, 52000, "100000000", "100000000", "0", "0", max_timeout, test1.String()) - pn.Mint(quxPath, fooPath, fee500, 58000, 62000, "80000000", "80000000", "0", "0", max_timeout, test1.String()) - pn.Mint(quxPath, fooPath, fee3000, 98040, 10020, "10000000", "10000000", "0", "0", max_timeout, test1.String()) - - // pn.Mint(fooPath, consts.GNS_PATH, fee100, 48000, 52000, "100000000", "100000000", "0", "0", max_timeout, test1.String()) -} - -func TestFindRoutes1(t *testing.T) { - std.TestSetPrevAddr(test1) - - pools := findCandidatePools() - - inputTokenPath := consts.WRAPPED_WUGNOT // FIXED - outputTokenPath := "gno.land/r/demo/bar" - maxHops := 3 - routes := computeAllRoutes( - inputTokenPath, - outputTokenPath, - maxHops, - pools, - ) - shouldEQ(t, len(routes), 2) -} - -func TestFindRoutes2(t *testing.T) { - std.TestSetPrevAddr(test1) - - pools := findCandidatePools() - - inputTokenPath := consts.WRAPPED_WUGNOT // FIXED - outputTokenPath := "gno.land/r/demo/foo" - maxHops := 3 - routes := computeAllRoutes( - inputTokenPath, - outputTokenPath, - maxHops, - pools, - ) - shouldEQ(t, len(routes), 2) -} - -func TestFindRoutes3(t *testing.T) { - std.TestSetPrevAddr(test1) - - pools := findCandidatePools() - - inputTokenPath := consts.WRAPPED_WUGNOT // FIXED - outputTokenPath := "gno.land/r/demo/gns" - maxHops := 3 - routes := computeAllRoutes( - inputTokenPath, - outputTokenPath, - maxHops, - pools, - ) - shouldEQ(t, len(routes), 1) -} diff --git a/router/_TEST_/z1_ratio_filetest.gno b/router/_TEST_/z1_ratio_filetest.gno new file mode 100644 index 00000000..7f7f96ef --- /dev/null +++ b/router/_TEST_/z1_ratio_filetest.gno @@ -0,0 +1,336 @@ +package main + +import ( + "gno.land/p/demo/json" + + "std" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/common" + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" + pn "gno.land/r/demo/position" + rr "gno.land/r/demo/router" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + max_timeout int64 = 9999999999 + + maxApprove uint64 = 18446744073709551615 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + rr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + mint() + ratio() +} + +func createPool() { + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + for i := 0; i < (5 * 10); i++ { + gns.Faucet() + std.TestSkipHeights(1) + } + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*10) + std.TestSkipHeights(1) + + // 1 HOPS + pl.CreatePool(barPath, consts.WRAPPED_WUGNOT, fee100, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) + pl.CreatePool(barPath, consts.WRAPPED_WUGNOT, fee500, common.TickMathGetSqrtRatioAtTick(20_000).ToString()) + pl.CreatePool(barPath, consts.WRAPPED_WUGNOT, fee3000, common.TickMathGetSqrtRatioAtTick(60_000).ToString()) // NOT USED BY SMALL LIQ + // -10_000 0.367897 + // -20_000 0.135348 + // 0.367897 + 0.135348 = 0.503245 + // 0.503245 / 2 = 0.2516225 + // 1 WGNOT = 0.2516225 BAR + + // 2 HOPS + pl.CreatePool(consts.WRAPPED_WUGNOT, quxPath, fee100, common.TickMathGetSqrtRatioAtTick(0).ToString()) // 1:1 + pl.CreatePool(quxPath, fooPath, fee100, common.TickMathGetSqrtRatioAtTick(50_000).ToString()) + pl.CreatePool(quxPath, fooPath, fee500, common.TickMathGetSqrtRatioAtTick(60_000).ToString()) + pl.CreatePool(quxPath, fooPath, fee3000, common.TickMathGetSqrtRatioAtTick(100_000).ToString()) // NOT USED BY SMALL LIQ + // 50_000 148.376062 + // 60_000 403.307791 + // 148.376062 + 403.307791 = 551.683853 + // 551.683853 / 2 = 275.8419265 + // 1 WGNOT = 275.8419265 FOO + + // 3 HOPS + pl.CreatePool(consts.WRAPPED_WUGNOT, bazPath, fee100, common.TickMathGetSqrtRatioAtTick(6932).ToString()) // 1:2 + pl.CreatePool(bazPath, oblPath, fee100, common.TickMathGetSqrtRatioAtTick(6932).ToString()) // 1:2 + pl.CreatePool(oblPath, consts.GNS_PATH, fee100, common.TickMathGetSqrtRatioAtTick(6932).ToString()) // 1:2 + // 1 WGNOT = 8 GNS +} + +func mint() { + std.TestSetOrigCaller(test1) + + // prepare ugnot + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.IssueCoin(test1, "ugnot", 100_000_000_000) + std.TestSetOrigSend(std.Coins{{"ugnot", 100_000_000_000}}, nil) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 100_000_000_000) + + // Deposit(wrap) + std.TestSetPrevAddr(test1) + wugnot.Deposit() + + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + wugnot.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + pn.Mint(barPath, consts.WRAPPED_WUGNOT, fee100, 8000, 12000, "100000000", "100000000", "0", "0", max_timeout, test1.String()) + pn.Mint(barPath, consts.WRAPPED_WUGNOT, fee500, 18000, 22000, "80000000", "80000000", "0", "0", max_timeout, test1.String()) + pn.Mint(barPath, consts.WRAPPED_WUGNOT, fee3000, 58020, 62040, "10000000", "10000000", "0", "0", max_timeout, test1.String()) + + pn.Mint(quxPath, fooPath, fee100, 48000, 52000, "100000000", "100000000", "0", "0", max_timeout, test1.String()) + pn.Mint(quxPath, fooPath, fee500, 58000, 62000, "80000000", "80000000", "0", "0", max_timeout, test1.String()) + pn.Mint(quxPath, fooPath, fee3000, 98040, 10020, "100000", "100000", "0", "0", max_timeout, test1.String()) + + // pn.Mint(fooPath, consts.GNS_PATH, fee100, 48000, 52000, "100000000", "100000000","0", "0" max_timeout, test1.String()) +} + +func ratio() { + ratios := rr.ApiGetRatiosFromBase() + + root, err := json.Unmarshal([]byte(ratios)) + if err != nil { + panic(err) + } + + response, err := root.GetKey("response") + if err != nil { + panic(err) + } + if response.Size() != 7 { + panic("response size should be 7") + } + + arr, err := response.GetArray() + if err != nil { + panic(err) + } + + token0, _ := response.GetIndex(0) + if token0.String() != "{\"token\":\"gno.land/r/demo/wugnot\",\"ratio\":\"79228162514264337593543950336\"}" { + panic("token0 wrong") + } + + token1, _ := response.GetIndex(1) + if token1.String() != "{\"token\":\"gno.land/r/demo/bar\",\"ratio\":\"19935653721785907863909200771\"}" { + panic("token1 wrong") + } + + token2, _ := response.GetIndex(2) + if token2.String() != "{\"token\":\"gno.land/r/demo/qux\",\"ratio\":\"79228162514264337593543950338\"}" { + panic("token2 wrong") + } + + token3, _ := response.GetIndex(3) + if token3.String() != "{\"token\":\"gno.land/r/demo/foo\",\"ratio\":\"21854449020437189859372345675086\"}" { + panic("token3 wrong") + } + + token4, _ := response.GetIndex(4) + if token4.String() != "{\"token\":\"gno.land/r/demo/baz\",\"ratio\":\"158459202898910110285447649633\"}" { + panic("token4 wrong") + } + + token5, _ := response.GetIndex(5) + if token5.String() != "{\"token\":\"gno.land/r/demo/obl\",\"ratio\":\"316924161643118367991168631862\"}" { + panic("token5 wrong") + } + + token6, _ := response.GetIndex(6) + if token6.String() != "{\"token\":\"gno.land/r/demo/gns\",\"ratio\":\"633859835185907382391729498085\"}" { + panic("token6 wrong") + } +} diff --git a/router/_TEST_/z2_swap_route_1route_1hop_filetest.gno b/router/_TEST_/z2_swap_route_1route_1hop_filetest.gno new file mode 100644 index 00000000..64a078ce --- /dev/null +++ b/router/_TEST_/z2_swap_route_1route_1hop_filetest.gno @@ -0,0 +1,398 @@ +package main + +import ( + "std" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/common" + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" + pn "gno.land/r/demo/position" + rr "gno.land/r/demo/router" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + maxTimeout int64 = 9999999999 + + maxApprove uint64 = 18446744073709551615 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + rr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + mint() + drySwapBarBazExactIn() + swapBarBazExactIn() + drySwapBarBazExactOut() + swapBarBazExactOut() + drySwapBazBarExactIn() + swapBazBarExactIn() + drySwapBazBarExactOut() + swapBazBarExactOut() +} + +func createPool() { + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + for i := 0; i < (5 * 1); i++ { + gns.Faucet() + std.TestSkipHeights(1) + } + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + // 1 HOPS + pl.CreatePool(barPath, bazPath, fee500, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) + // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 +} + +func mint() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + _, _, amount0, amount1 := pn.Mint(barPath, bazPath, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", maxTimeout, test1.String()) + if !(amount0 == "36790" && amount1 == "99999") { + panic("amount0 and amount1 should be 36790 and 99999") + } +} + +func drySwapBarBazExactIn() { + std.TestSetPrevAddr(test1) + + dryResult := rr.DrySwapRoute( + barPath, // inputToken + bazPath, // outputToken + "1000", // amountSpecified + "EXACT_IN", // swapType + "gno.land/r/demo/bar:gno.land/r/demo/baz:500", // strRouteArr + "100", // quoteArr + ) + if dryResult != "2711" { + panic("dryResult should be 2711") + } +} + +func swapBarBazExactIn() { + std.TestSetPrevAddr(test1) + + bar.Approve(a2u(consts.POOL_ADDR), uint64(1000)) + baz.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee + + amountIn, amountOut := rr.SwapRoute( + barPath, // inputToken + bazPath, // outputToken + "1000", // amountSpecified + "EXACT_IN", // swapType + "gno.land/r/demo/bar:gno.land/r/demo/baz:500", // strRouteArr + "100", // quoteArr + "2700", // tokenAmountLimit + ) + + if amountIn != "1000" { + panic("amountIn should be 1000") + } + if amountOut != "-2707" { + panic("amountOut should be -2707") + } +} + +func drySwapBarBazExactOut() { + std.TestSetPrevAddr(test1) + + dryResult := rr.DrySwapRoute( + barPath, // inputToken + bazPath, // outputToken + "1000", // amountSpecified + "EXACT_OUT", // swapType + "gno.land/r/demo/bar:gno.land/r/demo/baz:500", // strRouteArr + "100", // quoteArr + ) + if dryResult != "370" { + panic("dryResult should be 370") + } +} + +func swapBarBazExactOut() { + std.TestSetPrevAddr(test1) + + bar.Approve(a2u(consts.POOL_ADDR), uint64(1000)) + baz.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee + + amountIn, amountOut := rr.SwapRoute( + barPath, // inputToken + bazPath, // outputToken + "1000", // amountSpecified + "EXACT_OUT", // swapType + "gno.land/r/demo/bar:gno.land/r/demo/baz:500", // strRouteArr + "100", // quoteArr + "370", // tokenAmountLimit + ) + if amountIn != "370" { + panic("amountIn should be 370") + } + if amountOut != "-999" { + panic("amountOut should be -999") + } +} + +func drySwapBazBarExactIn() { + std.TestSetPrevAddr(test1) + + dryResult := rr.DrySwapRoute( + bazPath, // inputToken + barPath, // outputToken + "1000", // amountSpecified + "EXACT_IN", // swapType + "gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr + "100", // quoteArr + ) + if dryResult != "368" { + panic("dryResult should be 368") + } +} + +func swapBazBarExactIn() { + std.TestSetPrevAddr(test1) + + baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + bar.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee + + amountIn, amountOut := rr.SwapRoute( + bazPath, // inputToken + barPath, // outputToken + "1000", // amountSpecified + "EXACT_IN", // swapType + "gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr + "100", // quoteArr + "360", // tokenAmountLimit + ) + if amountIn != "1000" { + panic("amountIn should be 1000") + } + if amountOut != "-368" { + panic("amountOut should be -368") + } +} + +func drySwapBazBarExactOut() { + std.TestSetPrevAddr(test1) + + dryResult := rr.DrySwapRoute( + bazPath, // inputToken + barPath, // outputToken + "3000", // amountSpecified + "EXACT_OUT", // swapType + "gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr + "100", // quoteArr + ) + if dryResult != "8169" { + panic("dryResult should be 8169") + } +} + +func swapBazBarExactOut() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) + + amountIn, amountOut := rr.SwapRoute( + bazPath, // inputToken + barPath, // outputToken + "3000", // amountSpecified + "EXACT_OUT", // swapType + "gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr + "100", // quoteArr + "8200", // tokenAmountLimit + ) + if amountIn != "8169" { + panic("amountIn should be 8169") + } + if amountOut != "-2996" { + panic("amountOut should be -2996") + } +} diff --git a/router/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno b/router/_TEST_/z3_swap_route_1route_1hop_out_range_filetest.gno similarity index 51% rename from router/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno rename to router/_TEST_/z3_swap_route_1route_1hop_out_range_filetest.gno index d36776af..ca1c8e99 100644 --- a/router/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno +++ b/router/_TEST_/z3_swap_route_1route_1hop_out_range_filetest.gno @@ -1,27 +1,48 @@ -package router +package main import ( "std" + "testing" + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" - "gno.land/r/demo/bar" + pusers "gno.land/p/demo/users" - "gno.land/r/demo/baz" + "gno.land/r/demo/gnoswap/common" + "gno.land/r/demo/gnoswap/consts" - "gno.land/r/demo/qux" + pl "gno.land/r/demo/pool" + pn "gno.land/r/demo/position" + rr "gno.land/r/demo/router" +) - "gno.land/r/demo/wugnot" +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") - "gno.land/r/demo/obl" + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" - "gno.land/r/demo/gns" + oblPath string = "gno.land/r/demo/obl" - "gno.land/r/demo/gnoswap/consts" + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 - pusers "gno.land/p/demo/users" + maxTimeout int64 = 9999999999 - pl "gno.land/r/demo/pool" + maxApprove uint64 = 18446744073709551615 + + // TESTING + t *testing.T ) type FooToken struct{} @@ -132,24 +153,121 @@ func (GNSToken) Approve() func(spender pusers.AddressOrName, amount uint64) { return gns.Approve } +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + func init() { - std.TestSetOrigCaller(consts.GNOSWAP_ADMIN) + // FAUCET + std.TestSetPrevAddr(test1) - // POOL - pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) 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/wugnot", WugnotToken{}) pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) - // ROUTER - RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) - RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) - RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) - RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) - RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) - RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) - RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + mint() + drySwapBarBazExactIn() + swapBarBazExactIn() +} + +func createPool() { + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + for i := 0; i < (5 * 1); i++ { + gns.Faucet() + std.TestSkipHeights(1) + } + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + // 1 HOPS + pl.CreatePool(barPath, bazPath, fee500, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) + // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 +} + +func mint() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + _, _, amount0, amount1 := pn.Mint(barPath, bazPath, fee500, int32(12000), int32(15000), "100000", "100000", "0", "0", maxTimeout, test1.String()) + if !(amount0 == "100000" && amount1 == "0") { // only bar, no baz + panic("amount0 and amount1 should be 100000 and 0") + } +} + +func drySwapBarBazExactIn() { + std.TestSetPrevAddr(test1) + + dryResult := rr.DrySwapRoute( + barPath, // inputToken + bazPath, // outputToken + "1000", // amountSpecified + "EXACT_IN", // swapType + "gno.land/r/demo/bar:gno.land/r/demo/baz:500", // strRouteArr + "100", // quoteArr + ) + if dryResult != "-1" { + panic("dryResult should be -1") + } +} + +func swapBarBazExactIn() { + std.TestSetPrevAddr(test1) + + bar.Approve(a2u(consts.POOL_ADDR), uint64(1000)) + baz.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee + + rr.SwapRoute( + barPath, // inputToken + bazPath, // outputToken + "1000", // amountSpecified + "EXACT_IN", // swapType + "gno.land/r/demo/bar:gno.land/r/demo/baz:500", // strRouteArr + "100", // quoteArr + "2700", // tokenAmountLimit + ) } + +// Error: +// [ROUTER] router.gno__SwapRoute() || too few received for user (expected minimum received:2700, actual received:0) diff --git a/router/_TEST_/z4_swap_route_1route_1hop_native_in_out_filetest.gno b/router/_TEST_/z4_swap_route_1route_1hop_native_in_out_filetest.gno new file mode 100644 index 00000000..79010e83 --- /dev/null +++ b/router/_TEST_/z4_swap_route_1route_1hop_native_in_out_filetest.gno @@ -0,0 +1,351 @@ +package main + +import ( + "std" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/common" + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" + pn "gno.land/r/demo/position" + rr "gno.land/r/demo/router" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + maxTimeout int64 = 9999999999 + + maxApprove uint64 = 18446744073709551615 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + rr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + mintBarBaz() + mintBazQux() + mintQuxGnot() + drySwapBarGnotExactIn() + drySwapBarGnotExactOut() + drySwapGnotBarExactIn() + drySwapGnotBarExactOut() +} + +func createPool() { + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + for i := 0; i < (5 * 3); i++ { + gns.Faucet() + std.TestSkipHeights(1) + } + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*3) + std.TestSkipHeights(1) + + // 3 HOPS, 1 BAR ≈ 19.683 GNOT + pl.CreatePool(barPath, bazPath, fee500, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) + pl.CreatePool(bazPath, quxPath, fee500, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) + pl.CreatePool(quxPath, consts.WRAPPED_WUGNOT, fee500, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) + // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 +} + +func mintBarBaz() { + std.TestSetPrevAddr(test1) + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + _, _, amount0, amount1 := pn.Mint(barPath, bazPath, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", maxTimeout, test1.String()) + if amount0 != "36790" { + panic("amount0 should be 36790") + } + if amount1 != "99999" { + panic("amount1 should be 99999") + } +} + +func mintBazQux() { + std.TestSetPrevAddr(test1) + baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + _, _, amount0, amount1 := pn.Mint(bazPath, quxPath, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", maxTimeout, test1.String()) + if amount0 != "36790" { + panic("amount0 should be 36790") + } + if amount1 != "99999" { + panic("amount1 should be 99999") + } +} + +func mintQuxGnot() { + std.TestSetPrevAddr(test1) + + // send + std.TestIssueCoins(test1, std.Coins{{"ugnot", 1000009}}) + std.TestSetOrigSend(std.Coins{{"ugnot", 1000009}}, nil) + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 1000009) + + // Deposit(wrap) + std.TestSetPrevAddr(test1) + wugnot.Deposit() + + qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + wugnot.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + _, _, amount0, amount1 := pn.Mint(quxPath, consts.WRAPPED_WUGNOT, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", maxTimeout, test1.String()) + if amount0 != "36790" { + panic("amount0 should be 36790") + } + if amount1 != "99999" { + panic("amount1 should be 99999") + } +} + +func drySwapBarGnotExactIn() { + std.TestSetPrevAddr(test1) + + dryResult := rr.DrySwapRoute( + barPath, // inputToken + consts.WRAPPED_WUGNOT, // outputToken + "1000", // amountSpecified + "EXACT_IN", // swapType + "gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500*POOL*gno.land/r/demo/qux:gno.land/r/demo/wugnot:500", // strRouteArr + "100", // quoteArr + ) + if dryResult != "19740" { + panic("dryResult should be 19740") + } +} + +func drySwapBarGnotExactOut() { + std.TestSetPrevAddr(test1) + + dryResult := rr.DrySwapRoute( + barPath, // inputToken + consts.WRAPPED_WUGNOT, // outputToken + "20000", // amountSpecified + "EXACT_OUT", // swapType + "gno.land/r/demo/bar:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/qux:500*POOL*gno.land/r/demo/qux:gno.land/r/demo/wugnot:500", // strRouteArr + "100", // quoteArr + ) + if dryResult != "1012" { + panic("dryResult should be 1012") + } +} + +func drySwapGnotBarExactIn() { + std.TestSetPrevAddr(test1) + + dryResult := rr.DrySwapRoute( + consts.WRAPPED_WUGNOT, // intputToken + barPath, // outputToken + "5000", // amountSpecified + "EXACT_IN", // swapType + "gno.land/r/demo/wugnot:gno.land/r/demo/qux:500*POOL*gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr + "100", // quoteArr + ) + if dryResult != "247" { + panic("dryResult should be 247") + } +} + +func drySwapGnotBarExactOut() { + std.TestSetPrevAddr(test1) + + dryResult := rr.DrySwapRoute( + consts.WRAPPED_WUGNOT, // intputToken + barPath, // outputToken + "100", // amountSpecified + "EXACT_OUT", // swapType + "gno.land/r/demo/wugnot:gno.land/r/demo/qux:500*POOL*gno.land/r/demo/qux:gno.land/r/demo/baz:500*POOL*gno.land/r/demo/baz:gno.land/r/demo/bar:500", // strRouteArr + "100", // quoteArr + ) + if dryResult != "2003" { + panic("dryResult should be 2003") + } +} diff --git a/router/_TEST_/z5_swap_route_1route_2hop_wrapped_middle_filetest.gno b/router/_TEST_/z5_swap_route_1route_2hop_wrapped_middle_filetest.gno new file mode 100644 index 00000000..91e72094 --- /dev/null +++ b/router/_TEST_/z5_swap_route_1route_2hop_wrapped_middle_filetest.gno @@ -0,0 +1,314 @@ +package main + +import ( + "std" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/common" + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" + pn "gno.land/r/demo/position" + rr "gno.land/r/demo/router" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + maxTimeout int64 = 9999999999 + + maxApprove uint64 = 18446744073709551615 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + rr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + mintGnsGnot() + mintGnotBar() + drySwapGnsBarExactIn() + swapGnsBarExactIn() +} + +func createPool() { + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + for i := 0; i < (5 * 2); i++ { + gns.Faucet() + std.TestSkipHeights(1) + } + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*2) + std.TestSkipHeights(1) + + // 2 HOPS, 1 GNS ≈ 7.29 BAR + pl.CreatePool(consts.GNS_PATH, consts.WRAPPED_WUGNOT, fee100, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) + pl.CreatePool(consts.WRAPPED_WUGNOT, barPath, fee100, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) + // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 +} + +func mintGnsGnot() { + std.TestSetPrevAddr(test1) + + // send + std.TestIssueCoins(test1, std.Coins{{"ugnot", 999999}}) + std.TestSetOrigSend(std.Coins{{"ugnot", 999999}}, nil) + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 999999) + + // Deposit(wrap) + std.TestSetPrevAddr(test1) + wugnot.Deposit() + + gns.Faucet() + + gns.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + wugnot.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + _, _, amount0, amount1 := pn.Mint(consts.GNS_PATH, consts.WRAPPED_WUGNOT, fee100, int32(9000), int32(11000), "100000", "100000", "0", "0", maxTimeout, test1.String()) + if amount0 != "36790" { + panic("amount0 should be 36790") + } + if amount1 != "99999" { + panic("amount1 should be 99999") + } +} + +func mintGnotBar() { + std.TestSetPrevAddr(test1) + + // send + std.TestIssueCoins(test1, std.Coins{{"ugnot", 999999}}) + std.TestSetOrigSend(std.Coins{{"ugnot", 999999}}, nil) + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 999999) + + // Deposit(wrap) + std.TestSetPrevAddr(test1) + wugnot.Deposit() + + wugnot.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + tokenId, liquidity, amount0, amount1 := pn.Mint(consts.WRAPPED_WUGNOT, barPath, fee100, int32(9000), int32(11000), "100000", "100000", "0", "0", maxTimeout, test1.String()) +} + +func drySwapGnsBarExactIn() { + std.TestSetPrevAddr(test1) + + dryResult := rr.DrySwapRoute( + consts.GNS_PATH, // inputToken + barPath, // outputToken + "1000", // amountSpecified + "EXACT_IN", // swapType + "gno.land/r/demo/gns:gno.land/r/demo/wugnot:100*POOL*gno.land/r/demo/wugnot:gno.land/r/demo/bar:100", // strRouteArr + "100", // quoteArr + ) + if dryResult != "7339" { + panic("dryResult should be 7339") + } +} + +func swapGnsBarExactIn() { + std.TestSetPrevAddr(test1) + + gns.Approve(a2u(consts.POOL_ADDR), 1000) // swap input amount + bar.Approve(a2u(consts.ROUTER_ADDR), 7328) // 0.15% fee + + amountIn, amountOut := rr.SwapRoute( + consts.GNS_PATH, // inputToken + barPath, // outputToken + "1000", // amountSpecified + "EXACT_IN", // swapType + "gno.land/r/demo/gns:gno.land/r/demo/wugnot:100*POOL*gno.land/r/demo/wugnot:gno.land/r/demo/bar:100", // strRouteArr + "100", // quoteArr + "0", // tokenAmountLimit + ) + if amountIn != "1000" { + panic("amountIn should be 1000") + } + if amountOut != "-7328" { + panic("amountOut should be -7328") + } +} From 0c5b383c96f4a550963eff26b94c52045fb6afff Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 2 May 2024 15:14:36 +0900 Subject: [PATCH 7/9] test: integeration test for `staker` --- staker/_TEST_/_TEST_0_INIT_FAUCET_test.gno | 28 -- .../_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno | 75 ---- .../_TEST_staker_collect_reward_test.gn | 204 --------- staker/_TEST_/_TEST_staker_get_test.gn | 139 ------ .../_TEST_staker_internal_external_test.gn | 270 ------------ .../_TEST_staker_manage_pool_tiers_test.gn | 53 --- .../_TEST_staker_mint_and_stake_test.gn | 92 ---- .../_TEST_staker_one_external_native_test.gn | 191 --------- .../_TEST_/_TEST_staker_one_external_test.gn | 193 --------- ..._TEST_staker_one_increase_external_test.gn | 225 ---------- staker/_TEST_/_TEST_staker_rpc_get_test.gn | 245 ----------- .../z1_one_increase_external_filetest.gno | 323 ++++++++++++++ staker/_TEST_/z2_one_external_filetest.gno | 345 +++++++++++++++ .../_TEST_/z3_internal_external_filetest.gno | 397 ++++++++++++++++++ ...est.gno => z4_mint_and_stake_filetest.gno} | 172 +++++++- .../_TEST_/z5_manage_pool_tier_filetest.gno | 283 +++++++++++++ 16 files changed, 1498 insertions(+), 1737 deletions(-) delete mode 100644 staker/_TEST_/_TEST_0_INIT_FAUCET_test.gno delete mode 100644 staker/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno delete mode 100644 staker/_TEST_/_TEST_staker_collect_reward_test.gn delete mode 100644 staker/_TEST_/_TEST_staker_get_test.gn delete mode 100644 staker/_TEST_/_TEST_staker_internal_external_test.gn delete mode 100644 staker/_TEST_/_TEST_staker_manage_pool_tiers_test.gn delete mode 100644 staker/_TEST_/_TEST_staker_mint_and_stake_test.gn delete mode 100644 staker/_TEST_/_TEST_staker_one_external_native_test.gn delete mode 100644 staker/_TEST_/_TEST_staker_one_external_test.gn delete mode 100644 staker/_TEST_/_TEST_staker_one_increase_external_test.gn delete mode 100644 staker/_TEST_/_TEST_staker_rpc_get_test.gn create mode 100644 staker/_TEST_/z1_one_increase_external_filetest.gno create mode 100644 staker/_TEST_/z2_one_external_filetest.gno create mode 100644 staker/_TEST_/z3_internal_external_filetest.gno rename staker/_TEST_/{_TEST_0_INIT_TOKEN_REGISTER_test.gno => z4_mint_and_stake_filetest.gno} (50%) create mode 100644 staker/_TEST_/z5_manage_pool_tier_filetest.gno diff --git a/staker/_TEST_/_TEST_0_INIT_FAUCET_test.gno b/staker/_TEST_/_TEST_0_INIT_FAUCET_test.gno deleted file mode 100644 index 3bc62ddd..00000000 --- a/staker/_TEST_/_TEST_0_INIT_FAUCET_test.gno +++ /dev/null @@ -1,28 +0,0 @@ -package staker - -import ( - "std" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/foo" - "gno.land/r/demo/obl" - "gno.land/r/demo/qux" - - _ "gno.land/p/demo/users" -) - -func init() { - std.TestSetPrevAddr(test1) - - // GIVE 10_000_000_000(u) ≈ 10_000 - // 10000000000 - for i := 0; i < 100; i++ { - foo.Faucet() - bar.Faucet() - baz.Faucet() - qux.Faucet() - obl.Faucet() - //gns.Faucet() - } -} diff --git a/staker/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno b/staker/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno deleted file mode 100644 index 7783d719..00000000 --- a/staker/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno +++ /dev/null @@ -1,75 +0,0 @@ -package staker - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" -) - -var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") - - fooPath string = "gno.land/r/demo/foo" - barPath string = "gno.land/r/demo/bar" - bazPath string = "gno.land/r/demo/baz" - quxPath string = "gno.land/r/demo/qux" - - oblPath string = "gno.land/r/demo/obl" - // wugnotPath string = "gno.land/r/demo/wugnot" // from consts - // gnsPath string = "gno.land/r/demo/gns" // from consts - - fee100 uint32 = 100 - fee500 uint32 = 500 - fee3000 uint32 = 3000 - - max_timeout int64 = 9999999999 -) - -/* HELPER */ -func shouldEQ(t *testing.T, got, expected interface{}) { - if got != expected { - t.Errorf("got %v, expected %v", got, expected) - } -} - -func shouldNEQ(t *testing.T, got, expected interface{}) { - if got == expected { - t.Errorf("got %v, didn't expected %v", got, expected) - } -} - -func shouldGT(t *testing.T, l, r interface{}) { - if !(l < r) { - t.Errorf("expected %v < %v", l, r) - } -} - -func shouldLT(t *testing.T, l, r interface{}) { - if !(l > r) { - t.Errorf("expected %v > %v", l, r) - } -} - -func shouldPanic(t *testing.T, f func()) { - defer func() { - if r := recover(); r == nil { - t.Errorf("expected panic") - } - }() - f() -} - -func shouldPanicWithMsg(t *testing.T, f func(), msg string) { - defer func() { - if r := recover(); r == nil { - t.Errorf("The code did not panic") - } else { - if r != msg { - t.Errorf("excepted panic(%v), got(%v)", msg, r) - } - } - }() - f() -} diff --git a/staker/_TEST_/_TEST_staker_collect_reward_test.gn b/staker/_TEST_/_TEST_staker_collect_reward_test.gn deleted file mode 100644 index 1d7ad6fb..00000000 --- a/staker/_TEST_/_TEST_staker_collect_reward_test.gn +++ /dev/null @@ -1,204 +0,0 @@ -package staker - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/gnft" - - "gno.land/r/demo/bar" - "gno.land/r/demo/qux" - - "gno.land/r/demo/gns" - "gno.land/r/demo/obl" -) - -func init() { - // init pool tiers - // tier 1 - poolTiers["gno.land/r/demo/bar:gno.land/r/demo/qux:500"] = 1 // DEV - - // tier 2 - poolTiers["GNS/USDT_500"] = 2 - poolTiers["ATOM/GNS_500"] = 2 - - // tier 3 - poolTiers["ATOM/GNOT_500"] = 3 - poolTiers["ATOM/USDT_500"] = 3 - poolTiers["ATOM/WETH_500"] = 3 -} - -func TestPoolInitCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 1); i++ { - gns.Faucet() - } - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - - pl.CreatePool(barPath, quxPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 - std.TestSkipHeights(1) -} - -func TestPositionMint01(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1 := pn.Mint( - barPath, // token0 - quxPath, // token1 - uint16(500), // fee - int32(9000), // tickLower - int32(11000), // tickUpper - "1000", // amount0Desired - "1000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, // deadline - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, lpTokenId, 1) - shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "368") - shouldEQ(t, amount1, "999") - - // approve nft to staker - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) - std.TestSkipHeights(1) -} - -func TestPositionMint02(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1 := pn.Mint( - barPath, // token0 - quxPath, // token1 - uint16(500), // fee - int32(9100), // tickLower - int32(12000), // tickUpper - "5000", // amount0Desired - "5000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, // deadline - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, lpTokenId, 2) - shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "3979") - shouldEQ(t, amount1, "4999") - - // approve nft to staker - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) - std.TestSkipHeights(1) -} - -func TestCreateExternalIncentive(t *testing.T) { - std.TestSetPrevAddr(test1) - obl.Approve(a2u(consts.STAKER_ADDR), uint64(10_000_000_000)) - std.TestSkipHeights(1) - - CreateExternalIncentive( - "gno.land/r/demo/bar:gno.land/r/demo/qux:500", // targetPoolPath - "gno.land/r/demo/obl", // rewardToken - "1000000000", // rewardAmount 10_000_000_000 - 1234569600, // startTimestamp - 1234569600+TIMESTAMP_90DAYS, // endTimestamp - ) - std.TestSkipHeights(1) - - obl.Approve(a2u(consts.STAKER_ADDR), uint64(10_000_000_000)) - std.TestSkipHeights(1) - - CreateExternalIncentive("gno.land/r/demo/bar:gno.land/r/demo/qux:500", "gno.land/r/demo/obl", "1000000000", 1234569600, 1234569600+TIMESTAMP_90DAYS) - std.TestSkipHeights(1) -} - -func TestStakeToken01(t *testing.T) { - std.TestSetPrevAddr(test1) - StakeToken(1) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker - shouldEQ(t, len(deposits), 1) -} - -func TestStakeToken02(t *testing.T) { - std.TestSetPrevAddr(test1) - StakeToken(2) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(2)), GetOrigPkgAddr()) // staker - shouldEQ(t, len(deposits), 2) -} - -func TestCollectReward(t *testing.T) { - std.TestSetPrevAddr(consts.INTERNAL_REWARD_ACCOUNT) - gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) // internal reward distribution - std.TestSkipHeights(1) - - // before claim - gnsOld := gns.BalanceOf(a2u(test1)) - std.TestSkipHeights(1) - shouldEQ(t, gnsOld, 0) - - std.TestSetPrevRealm(consts.STAKER_PATH) - std.TestSetOrigCaller(test1) - CollectReward(1) // GNFT tokenId - std.TestSkipHeights(1) - - gnsNew := gns.BalanceOf(a2u(test1)) - std.TestSkipHeights(1) - shouldEQ(t, gnsNew, 24502) -} - -func TestUnstakeToken01(t *testing.T) { - std.TestSetPrevAddr(test1) - std.TestSkipHeights(400) // spend more time - - UnstakeToken(1) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(1)), test1) - - // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 2487004) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 8000004851) // external -} - -func TestUnstakeToken02(t *testing.T) { - std.TestSetPrevAddr(test1) - UnstakeToken(2) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(2)), test1) - - // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 19067029) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 8000037608) // external -} - -func TestEndExternalIncentive(t *testing.T) { - std.TestSetPrevAddr(test1) - std.TestSkipHeights(9999999) - EndExternalIncentive(std.GetOrigCaller().String(), "gno.land/r/demo/bar:gno.land/r/demo/qux:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() - std.TestSkipHeights(1) - - shouldEQ(t, len(incentives), 0) - shouldEQ(t, len(poolIncentives["gno.land/r/demo/bar:gno.land/r/demo/qux:500"]), 0) -} diff --git a/staker/_TEST_/_TEST_staker_get_test.gn b/staker/_TEST_/_TEST_staker_get_test.gn deleted file mode 100644 index ea758b80..00000000 --- a/staker/_TEST_/_TEST_staker_get_test.gn +++ /dev/null @@ -1,139 +0,0 @@ -package staker - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/gnft" - - "gno.land/r/demo/bar" - "gno.land/r/demo/qux" - - "gno.land/r/demo/gns" - "gno.land/r/demo/obl" -) - -func TestPoolInitCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 1); i++ { - gns.Faucet() - } - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - std.TestSkipHeights(2) - - pl.CreatePool(barPath, quxPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 - std.TestSkipHeights(1) -} - -func TestPositionMint01(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1 := pn.Mint( - barPath, // token0 - quxPath, // token1 - uint16(500), // fee - int32(9000), // tickLower - int32(11000), // tickUpper - "1000", // amount0Desired - "1000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, // deadline - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, lpTokenId, 1) - shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "368") - shouldEQ(t, amount1, "999") - - // approve nft to staker - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) - std.TestSkipHeights(1) -} - -func TestPositionMint02(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1 := pn.Mint( - barPath, // token0 - quxPath, // token1 - uint16(500), // fee - int32(9100), // tickLower - int32(12000), // tickUpper - "5000", // amount0Desired - "5000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, // deadline - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, lpTokenId, 2) - shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "3979") - shouldEQ(t, amount1, "4999") - - // approve nft to staker - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) - std.TestSkipHeights(1) -} - -func TestCreateExternalIncentive(t *testing.T) { - std.TestSetPrevAddr(test1) - obl.Approve(a2u(consts.STAKER_ADDR), uint64(10_000_000_000)) - std.TestSkipHeights(1) - - CreateExternalIncentive( - "gno.land/r/demo/bar:gno.land/r/demo/qux:500", // targetPoolPath - "gno.land/r/demo/obl", // rewardToken - "1000000000", // rewardAmount - 1234569600, // startTimestamp - 1234569600+TIMESTAMP_90DAYS, // endTimestamp - ) - std.TestSkipHeights(1) - - obl.Approve(a2u(consts.STAKER_ADDR), uint64(10_000_000_000)) - std.TestSkipHeights(1) - - CreateExternalIncentive("gno.land/r/demo/bar:gno.land/r/demo/qux:500", "gno.land/r/demo/obl", "1000000000", 1234569600, 1234569600+TIMESTAMP_90DAYS) - std.TestSkipHeights(1) -} - -func TestStakeToken01(t *testing.T) { - std.TestSetPrevAddr(test1) - StakeToken(1) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker - shouldEQ(t, len(deposits), 1) -} - -// func TestStakeToken02(t *testing.T) { -// std.TestSetPrevAddr(test1) -// StakeToken(2) // GNFT tokenId -// std.TestSkipHeights(1) - -// shouldEQ(t, gnft.OwnerOf(tid(2)), GetOrigPkgAddr()) // staker -// shouldEQ(t, len(deposits), 2) -// } - -func TestIsStaked(t *testing.T) { - shouldEQ(t, IsStaked(1), true) - shouldEQ(t, IsStaked(2), false) -} diff --git a/staker/_TEST_/_TEST_staker_internal_external_test.gn b/staker/_TEST_/_TEST_staker_internal_external_test.gn deleted file mode 100644 index 2c059805..00000000 --- a/staker/_TEST_/_TEST_staker_internal_external_test.gn +++ /dev/null @@ -1,270 +0,0 @@ -package staker - -import ( - "std" - "testing" - - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/foo" - "gno.land/r/demo/gns" - "gno.land/r/demo/obl" - "gno.land/r/demo/qux" - - "gno.land/r/demo/gnft" -) - -func init() { - // init pool tiers - // tier 1 - poolTiers["gno.land/r/demo/bar:gno.land/r/demo/qux:500"] = 1 - - // tier 2 - poolTiers["gno.land/r/demo/bar:gno.land/r/demo/baz:100"] = 2 - poolTiers["ATOM/GNS_500"] = 2 - - // tier 3 - poolTiers["ATOM/GNOT_500"] = 3 - poolTiers["ATOM/USDT_500"] = 3 - poolTiers["ATOM/WETH_500"] = 3 -} - -func TestPoolInitCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 3); i++ { - gns.Faucet() - } - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*3) - std.TestSkipHeights(1) - - pl.CreatePool(barPath, quxPath, 500, "130621891405341611593710811006") // internal, tier 1 // tick 10_000 ≈ x2.7 - pl.CreatePool(barPath, bazPath, 100, "79228162514264337593543950337") // internal, tier 2 // tick 0 ≈ x1 - pl.CreatePool(fooPath, quxPath, 500, "79228162514264337593543950337") // external // tick 0 ≈ x1 - std.TestSkipHeights(3) -} - -func TestMintBarQux500(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - tokenId, liquidity, amount0, amount1 := pn.Mint( - barPath, // token0 - quxPath, // token1 - fee500, // fee - int32(9000), // tickLower - int32(11000), // tickUpper - "100000", // amount0Desired - "100000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, tokenId, 1) - shouldEQ(t, gnft.OwnerOf(tid(tokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "36790") - shouldEQ(t, amount1, "99999") -} - -func TestMintBarBaz100(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - tokenId, liquidity, amount0, amount1 := pn.Mint( - barPath, // token0 - bazPath, // token1 - fee100, // fee - int32(-1000), // tickLower - int32(1000), // tickUpper - "100000", // amount0Desired - "100000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, tokenId, 2) - shouldEQ(t, gnft.OwnerOf(tid(tokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "100000") - shouldEQ(t, amount1, "99999") -} - -func TestMintFooQux500(t *testing.T) { - std.TestSetPrevAddr(test1) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - tokenId, liquidity, amount0, amount1 := pn.Mint( - fooPath, // token0 - quxPath, // token1 - fee500, // fee - int32(-1000), // tickLower - int32(1000), // tickUpper - "100000", // amount0Desired - "100000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, tokenId, 3) - shouldEQ(t, gnft.OwnerOf(tid(tokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "100000") - shouldEQ(t, amount1, "99999") -} - -func TestCreateExternalIncentive(t *testing.T) { - std.TestSetPrevAddr(test1) - obl.Approve(a2u(consts.STAKER_ADDR), uint64(100_000_000)) - std.TestSkipHeights(1) - - CreateExternalIncentive( - "gno.land/r/demo/foo:gno.land/r/demo/qux:500", // targetPoolPath - "gno.land/r/demo/obl", // rewardToken - "100000000", // rewardAmount - 1234569600, // startTimestamp - 1234569600+TIMESTAMP_90DAYS, // endTimestamp - ) - std.TestSkipHeights(1) -} - -func TestStakeInternalTier1(t *testing.T) { - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(1)) - StakeToken(1) - std.TestSkipHeights(2) - - shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) - shouldEQ(t, len(deposits), 1) -} - -func TestStakeInternalTier2(t *testing.T) { - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(2)) - StakeToken(2) - std.TestSkipHeights(2) - - shouldEQ(t, gnft.OwnerOf(tid(2)), GetOrigPkgAddr()) - shouldEQ(t, len(deposits), 2) -} - -func TestStakeExternal(t *testing.T) { - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(3)) - StakeToken(3) - std.TestSkipHeights(2) - - shouldEQ(t, gnft.OwnerOf(tid(3)), GetOrigPkgAddr()) - shouldEQ(t, len(deposits), 3) -} - -func TestCollectRewardToken1(t *testing.T) { - std.TestSetPrevAddr(consts.INTERNAL_REWARD_ACCOUNT) - gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) // internal reward distribution - std.TestSkipHeights(1) - - // before claim - gnsOld := gns.BalanceOf(a2u(test1)) - shouldEQ(t, gnsOld, 0) - - std.TestSetPrevAddr(test1) - CollectReward(1) - std.TestSkipHeights(1) - - gnsNew := gns.BalanceOf(a2u(test1)) - shouldEQ(t, gnsNew, 285863) -} - -func TestCollectRewardToken2(t *testing.T) { - std.TestSetPrevAddr(test1) - - // before claim - gnsOld := gns.BalanceOf(a2u(test1)) - shouldEQ(t, gnsOld, 285863) - - CollectReward(2) - std.TestSkipHeights(1) - - gnsNew := gns.BalanceOf(a2u(test1)) - shouldEQ(t, gnsNew, 432878) -} - -func TestCollectRewardToken1Again(t *testing.T) { - std.TestSetPrevAddr(test1) - - // before claim - gnsOld := gns.BalanceOf(a2u(test1)) - shouldEQ(t, gnsOld, 432878) - - // ACCELERATE TIME - std.TestSkipHeights(1000) - - CollectReward(1) - std.TestSkipHeights(1) - - gnsNew := gns.BalanceOf(a2u(test1)) - shouldEQ(t, gnsNew, 41352053) -} - -func TestCollectExternalReward(t *testing.T) { - std.TestSetPrevAddr(test1) - - // before claim - oblOld := obl.BalanceOf(a2u(test1)) - shouldEQ(t, oblOld, 9900000000) - - CollectReward(3) - std.TestSkipHeights(1) - - oblNew := obl.BalanceOf(a2u(test1)) - shouldEQ(t, oblNew, 9900014200) -} - -func TestCollectAll(t *testing.T) { - std.TestSetPrevAddr(test1) - - CollectReward(1) - CollectReward(2) - CollectReward(3) - std.TestSkipHeights(3) - // claimed all remaining reward -} - -func TestUnstakeToken1(t *testing.T) { - std.TestSetPrevAddr(test1) - gnsOld := gns.BalanceOf(a2u(test1)) - oblOld := obl.BalanceOf(a2u(test1)) - shouldEQ(t, gnsOld, 66009736) - shouldEQ(t, oblOld, 9900014221) - - std.TestSkipHeights(100000) - UnstakeToken(1) - gnsNew := gns.BalanceOf(a2u(test1)) - shouldEQ(t, gnsNew, 6899988399) - - UnstakeToken(2) - gnsNew = gns.BalanceOf(a2u(test1)) - shouldEQ(t, gnsNew, 11000342927) - - UnstakeToken(3) - oblNew := obl.BalanceOf(a2u(test1)) - shouldEQ(t, oblNew, 9903488806) - - std.TestSkipHeights(1) -} diff --git a/staker/_TEST_/_TEST_staker_manage_pool_tiers_test.gn b/staker/_TEST_/_TEST_staker_manage_pool_tiers_test.gn deleted file mode 100644 index febdbe78..00000000 --- a/staker/_TEST_/_TEST_staker_manage_pool_tiers_test.gn +++ /dev/null @@ -1,53 +0,0 @@ -package staker - -import ( - "std" - "testing" - - pl "gno.land/r/demo/pool" - - "gno.land/r/demo/gnoswap/consts" - - "gno.land/r/demo/gns" -) - -func TestPoolInitCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 1); i++ { - gns.Faucet() - } - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - std.TestSkipHeights(1) - - pl.CreatePool(fooPath, barPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 - std.TestSkipHeights(1) -} - -func TestSetPoolTier(t *testing.T) { - std.TestSetOrigCaller(consts.GNOSWAP_ADMIN) - SetPoolTier("gno.land/r/demo/bar:gno.land/r/demo/foo:500", 2) -} - -func TestGetPoolWithTiers(t *testing.T) { - poolTiers := GetPoolWithTiers() - if len(poolTiers) != 2 { - t.Error("Expected 2 pools") - } -} - -func TestChangePoolTier(t *testing.T) { - std.TestSetOrigCaller(consts.GNOSWAP_ADMIN) - ChangePoolTier("gno.land/r/demo/bar:gno.land/r/demo/foo:500", 3) - if poolTiers["gno.land/r/demo/bar:gno.land/r/demo/foo:500"] != 3 { - t.Error("Expected tier to be 3") - } -} - -func TestRemovePoolTier(t *testing.T) { - std.TestSetOrigCaller(consts.GNOSWAP_ADMIN) - RemovePoolTier("gno.land/r/demo/bar:gno.land/r/demo/foo:500") - if len(poolTiers) != 1 { - t.Error("Expected 1 pool") - } - -} diff --git a/staker/_TEST_/_TEST_staker_mint_and_stake_test.gn b/staker/_TEST_/_TEST_staker_mint_and_stake_test.gn deleted file mode 100644 index ed19a6e0..00000000 --- a/staker/_TEST_/_TEST_staker_mint_and_stake_test.gn +++ /dev/null @@ -1,92 +0,0 @@ -package staker - -import ( - "std" - "testing" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/gnft" - "gno.land/r/demo/gns" - "gno.land/r/demo/qux" - - "gno.land/r/demo/gnoswap/consts" -) - -func init() { - // init pool tiers - // tier 1 - poolTiers["gno.land/r/demo/bar:gno.land/r/demo/qux:500"] = 1 // DEV - - // tier 2 - poolTiers["GNS/USDT_500"] = 2 - poolTiers["ATOM/GNS_500"] = 2 - - // tier 3 - poolTiers["ATOM/GNOT_500"] = 3 - poolTiers["ATOM/USDT_500"] = 3 - poolTiers["ATOM/WETH_500"] = 3 -} - -func TestPoolInitCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 1); i++ { - gns.Faucet() - } - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - std.TestSkipHeights(1) - - pl.CreatePool(barPath, quxPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 - std.TestSkipHeights(1) -} - -func TestMintAndStake(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1, poolPath := MintAndStake( - barPath, // token0 - quxPath, // token1 - fee500, // fee - int32(9000), // tickLower - int32(11000), // tickUpper - "1000", // amount0Desired - "1000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, - ) - std.TestSkipHeights(1) -} - -func TestPositionCollectFee(t *testing.T) { - std.TestSetPrevAddr(test1) - pn.CollectFee(1) // lpTokenId - std.TestSkipHeights(1) -} - -func TestCollectReward(t *testing.T) { - // internal reward distribution - std.TestSetPrevAddr(consts.INTERNAL_REWARD_ACCOUNT) - gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) - - std.TestSetPrevAddr(test1) - CollectReward(1) // lpTokenId - std.TestSkipHeights(1) -} - -func TestUnstakeToken(t *testing.T) { - ownerOfLp1 := gnft.OwnerOf(tid(1)) - shouldEQ(t, ownerOfLp1, a2u(consts.STAKER_ADDR)) - - std.TestSetPrevAddr(test1) - UnstakeToken(1) // lpTokenId - std.TestSkipHeights(1) - - ownerOfLp1 = gnft.OwnerOf(tid(1)) - shouldEQ(t, ownerOfLp1, a2u(test1)) -} diff --git a/staker/_TEST_/_TEST_staker_one_external_native_test.gn b/staker/_TEST_/_TEST_staker_one_external_native_test.gn deleted file mode 100644 index d083bacd..00000000 --- a/staker/_TEST_/_TEST_staker_one_external_native_test.gn +++ /dev/null @@ -1,191 +0,0 @@ -package staker - -import ( - "std" - "testing" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/foo" - "gno.land/r/demo/gns" - "gno.land/r/demo/wugnot" - - "gno.land/r/demo/gnft" - - "gno.land/r/demo/gnoswap/consts" -) - -func init() { - // init pool tiers - // tier 1 - poolTiers["gno.land/r/demo/bar:gno.land/r/demo/foo:500"] = 1 // DEV - - // tier 2 - poolTiers["GNS/USDT_500"] = 2 - poolTiers["ATOM/GNS_500"] = 2 - - // tier 3 - poolTiers["ATOM/GNOT_500"] = 3 - poolTiers["ATOM/USDT_500"] = 3 - poolTiers["ATOM/WETH_500"] = 3 -} - -func TestPoolInitCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 1); i++ { - gns.Faucet() - } - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - std.TestSkipHeights(1) - - pl.CreatePool(barPath, fooPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 - std.TestSkipHeights(1) -} - -func TestPositionMint01(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1 := pn.Mint( - barPath, // token0 - fooPath, // token1 - uint16(500), // fee - int32(9000), // tickLower - int32(11000), // tickUpper - "1000", // amount0Desired - "1000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, // deadline - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, lpTokenId, 1) - shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "368") - shouldEQ(t, amount1, "999") - - // approve nft to staker for staking - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) - std.TestSkipHeights(1) -} - -func TestPositionMint02(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1 := pn.Mint( - barPath, // token0 - fooPath, // token1 - uint16(500), // fee - int32(9100), // tickLower - int32(12000), // tickUpper - "5000", // amount0Desired - "5000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, // deadline - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, lpTokenId, 2) - shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "3979") - shouldEQ(t, amount1, "4999") - - // approve nft to staker - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) - std.TestSkipHeights(1) -} - -func TestCreateExternalIncentive(t *testing.T) { - std.TestSetPrevAddr(test1) - - // send - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.IssueCoin(test1, "ugnot", 10_000_000_000) - std.TestSetOrigSend(std.Coins{{"ugnot", 10_000_000_000}}, nil) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 10_000_000_000) - - // Deposit(wrap) - wugnot.Deposit() - std.TestSkipHeights(1) - - wugnot.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) - std.TestSkipHeights(1) - - CreateExternalIncentive( - "gno.land/r/demo/bar:gno.land/r/demo/foo:500", // targetPoolPath - consts.WRAPPED_WUGNOT, // rewardToken - "10000000000", // rewardAmount - 1234569600, // startTimestamp - 1234569600+TIMESTAMP_90DAYS, // endTimestamp - ) - std.TestSkipHeights(1) -} - -func TestStakeToken01(t *testing.T) { - std.TestSetPrevAddr(test1) - StakeToken(1) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker - shouldEQ(t, len(deposits), 1) -} - -func TestStakeToken02(t *testing.T) { - std.TestSetPrevAddr(test1) - StakeToken(2) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(2)), GetOrigPkgAddr()) // staker - shouldEQ(t, len(deposits), 2) -} - -func TestUnstakeToken01(t *testing.T) { - std.TestSetPrevAddr(consts.INTERNAL_REWARD_ACCOUNT) - gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) // internal reward distribution - std.TestSkipHeights(335) // skip times - - std.TestSetPrevAddr(test1) - UnstakeToken(1) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(1)), test1) - - // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 2064336) // internal - shouldEQ(t, wugnot.BalanceOf(a2u(test1)), 2521) // external -} - -func TestUnstakeToken02(t *testing.T) { - std.TestSetPrevAddr(test1) - UnstakeToken(2) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(2)), test1) - - // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 15826574) // internal - shouldEQ(t, wugnot.BalanceOf(a2u(test1)), 21428) // external -} - -func TestEndExternalIncentive(t *testing.T) { - std.TestSetOrigCaller(test1) - std.TestSkipHeights(9999999) - EndExternalIncentive(std.GetOrigCaller().String(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", consts.WRAPPED_WUGNOT) // use same parameter as CreateExternalIncentive() - std.TestSkipHeights(1) - - shouldEQ(t, len(incentives), 0) - shouldEQ(t, len(poolIncentives["gno.land/r/demo/bar:gno.land/r/demo/foo:500"]), 0) -} diff --git a/staker/_TEST_/_TEST_staker_one_external_test.gn b/staker/_TEST_/_TEST_staker_one_external_test.gn deleted file mode 100644 index d8d0124b..00000000 --- a/staker/_TEST_/_TEST_staker_one_external_test.gn +++ /dev/null @@ -1,193 +0,0 @@ -package staker - -import ( - "std" - "testing" - - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/foo" - "gno.land/r/demo/gns" - "gno.land/r/demo/obl" - - "gno.land/r/demo/gnft" - - "gno.land/r/demo/gnoswap/consts" -) - -func init() { - // init pool tiers - // tier 1 - poolTiers["gno.land/r/demo/bar:gno.land/r/demo/foo:500"] = 1 // DEV - - // tier 2 - poolTiers["GNS/USDT_500"] = 2 - poolTiers["ATOM/GNS_500"] = 2 - - // tier 3 - poolTiers["ATOM/GNOT_500"] = 3 - poolTiers["ATOM/USDT_500"] = 3 - poolTiers["ATOM/WETH_500"] = 3 -} - -func TestPoolInitCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 1); i++ { - gns.Faucet() - } - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - std.TestSkipHeights(1) - - pl.CreatePool(fooPath, barPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 - std.TestSkipHeights(1) -} - -func TestPositionMint01(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1 := pn.Mint( - fooPath, // token0 - barPath, // token1 - uint16(500), // fee - int32(9000), // tickLower - int32(11000), // tickUpper - "1000", // amount0Desired - "1000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, // deadline - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, lpTokenId, 1) - shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "1000") - shouldEQ(t, amount1, "367") - - // approve nft to staker for staking - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) - std.TestSkipHeights(1) -} - -func TestPositionMint02(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1 := pn.Mint( - fooPath, // token0 - barPath, // token1 - uint16(500), // fee - int32(9100), // tickLower - int32(12000), // tickUpper - "5000", // amount0Desired - "5000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, // deadline - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, lpTokenId, 2) - shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "5000") - shouldEQ(t, amount1, "3978") - - // approve nft to staker - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) - std.TestSkipHeights(1) -} - -func TestCreateExternalIncentive(t *testing.T) { - std.TestSetPrevAddr(test1) - obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) - std.TestSkipHeights(1) - - CreateExternalIncentive( - "gno.land/r/demo/bar:gno.land/r/demo/foo:500", // targetPoolPath - oblPath, // rewardToken - "10000000000", // rewardAmount - 1234569600, // startTimestamp - 1234569600+TIMESTAMP_90DAYS, // endTimestamp - ) - std.TestSkipHeights(1) -} - -func TestStakeToken01(t *testing.T) { - std.TestSetPrevAddr(test1) - StakeToken(1) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker - shouldEQ(t, len(deposits), 1) -} - -func TestStakeToken02(t *testing.T) { - std.TestSetPrevAddr(test1) - StakeToken(2) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(2)), GetOrigPkgAddr()) // staker - shouldEQ(t, len(deposits), 2) -} - -// func TestApiGetStakes(t *testing.T) { -// gss := ApiGetStakes() -// jsonStr := gjson.Parse(gss) -// shouldEQ(t, len(jsonStr.Get("response").Array()), 2) -// } - -// func TestApiGetRewardTokens(t *testing.T) { -// grt := ApiGetRewardTokens() -// jsonStr := gjson.Parse(grt) -// shouldEQ(t, len(jsonStr.Get("response").Array()), 1) -// } - -func TestUnstakeToken01(t *testing.T) { - std.TestSetPrevAddr(consts.INTERNAL_REWARD_ACCOUNT) - gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) // internal reward distribution - std.TestSkipHeights(330) - - std.TestSetPrevAddr(test1) - UnstakeToken(1) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(1)), test1) - - // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 2033707) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 630) // external -} - -func TestUnstakeToken02(t *testing.T) { - std.TestSetPrevAddr(test1) - UnstakeToken(2) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(2)), test1) - - // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 15591757) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 6932) // external -} - -func TestEndExternalIncentive(t *testing.T) { - std.TestSetOrigCaller(test1) - std.TestSkipHeights(9999999) - EndExternalIncentive(std.GetOrigCaller().String(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() - std.TestSkipHeights(1) - - shouldEQ(t, len(incentives), 0) - shouldEQ(t, len(poolIncentives["gno.land/r/demo/bar:gno.land/r/demo/foo:500"]), 0) -} diff --git a/staker/_TEST_/_TEST_staker_one_increase_external_test.gn b/staker/_TEST_/_TEST_staker_one_increase_external_test.gn deleted file mode 100644 index a4934530..00000000 --- a/staker/_TEST_/_TEST_staker_one_increase_external_test.gn +++ /dev/null @@ -1,225 +0,0 @@ -package staker - -import ( - "std" - "testing" - - "gno.land/p/demo/json" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/foo" - "gno.land/r/demo/gns" - "gno.land/r/demo/obl" - - "gno.land/r/demo/gnft" - "gno.land/r/demo/gnoswap/consts" -) - -func init() { - // init pool tiers - // tier 1 - poolTiers["gno.land/r/demo/bar:gno.land/r/demo/foo:500"] = 1 // DEV - - // tier 2 - poolTiers["GNS/USDT_500"] = 2 - poolTiers["ATOM/GNS_500"] = 2 - - // tier 3 - poolTiers["ATOM/GNOT_500"] = 3 - poolTiers["ATOM/USDT_500"] = 3 - poolTiers["ATOM/WETH_500"] = 3 -} - -func TestPoolCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 1); i++ { - gns.Faucet() - } - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - std.TestSkipHeights(1) - - pl.CreatePool(fooPath, barPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 - std.TestSkipHeights(1) -} - -func TestPositionMint01(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1 := pn.Mint( - fooPath, // token0 - barPath, // token1 - uint16(500), // fee - int32(9000), // tickLower - int32(11000), // tickUpper - "1000", // amount0Desired - "1000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, // deadline - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, lpTokenId, 1) - shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "1000") - shouldEQ(t, amount1, "367") - - // approve nft to staker for staking - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) - std.TestSkipHeights(1) -} - -func TestPositionMint02(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1 := pn.Mint( - fooPath, // token0 - barPath, // token1 - uint16(500), // fee - int32(9100), // tickLower - int32(12000), // tickUpper - "5000", // amount0Desired - "5000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, // deadline - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, lpTokenId, 2) - shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "5000") - shouldEQ(t, amount1, "3978") - - // approve nft to staker - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) - std.TestSkipHeights(1) -} - -func TestCreateExternalIncentive(t *testing.T) { - std.TestSetPrevAddr(test1) - obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) - std.TestSkipHeights(1) - - CreateExternalIncentive( - "gno.land/r/demo/bar:gno.land/r/demo/foo:500", // targetPoolPath - oblPath, // rewardToken - "1000000000", // rewardAmount - 1234569600, // startTimestamp - 1234569600+TIMESTAMP_90DAYS, // endTimestamp - ) - std.TestSkipHeights(1) - - obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) - std.TestSkipHeights(1) - CreateExternalIncentive("gno.land/r/demo/bar:gno.land/r/demo/foo:500", oblPath, "100000000", 1234569600, 1234569600+TIMESTAMP_90DAYS) - std.TestSkipHeights(1) -} - -func TestStakeToken01(t *testing.T) { - std.TestSetPrevAddr(test1) - StakeToken(1) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker - shouldEQ(t, len(deposits), 1) -} - -func TestStakeToken02(t *testing.T) { - std.TestSetPrevAddr(test1) - StakeToken(2) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(2)), GetOrigPkgAddr()) // staker - shouldEQ(t, len(deposits), 2) -} - -func TestApiGetStakes(t *testing.T) { - gss := ApiGetStakes() - - root, err := json.Unmarshal([]byte(gss)) - if err != nil { - panic(err) - } - - response, err := root.GetKey("response") - if err != nil { - panic(err) - } - - shouldEQ(t, response.Size(), 2) -} - -func TestApiGetRewardTokens(t *testing.T) { - grt := ApiGetRewardTokens() - - root, err := json.Unmarshal([]byte(grt)) - if err != nil { - panic(err) - } - - response, err := root.GetKey("response") - if err != nil { - panic(err) - } - - shouldEQ(t, response.Size(), 1) -} - -func TestUnstakeToken01(t *testing.T) { - std.TestSetPrevAddr(consts.INTERNAL_REWARD_ACCOUNT) - gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) // internal reward distribution - std.TestSkipHeights(350) - - std.TestSetPrevAddr(test1) - UnstakeToken(1) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(1)), test1) - - // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 2156220) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 8900000831) // external -} - -func TestUnstakeToken02(t *testing.T) { - std.TestSetPrevAddr(test1) - UnstakeToken(2) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(2)), test1) - - // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 16531020) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 8900006600) // external -} - -func TestEndExternalIncentive(t *testing.T) { - shouldEQ(t, len(incentives), 1) - - std.TestSetOrigCaller(test1) - - // incentive time isn't over yet - shouldPanicWithMsg(t, func() { - EndExternalIncentive(test1.String(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", "gno.land/r/demo/obl") - }, "[STAKER] staker.gno__EndExternalIncentive() || cannot end incentive before endTimestamp(1242345600), current(1234569730)") - - std.TestSkipHeights(9999999) - EndExternalIncentive(test1.String(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() - - shouldEQ(t, len(incentives), 0) - shouldEQ(t, len(poolIncentives["gno.land/r/demo/bar:gno.land/r/demo/foo:500"]), 0) -} diff --git a/staker/_TEST_/_TEST_staker_rpc_get_test.gn b/staker/_TEST_/_TEST_staker_rpc_get_test.gn deleted file mode 100644 index 0b1396b0..00000000 --- a/staker/_TEST_/_TEST_staker_rpc_get_test.gn +++ /dev/null @@ -1,245 +0,0 @@ -package staker - -import ( - "gno.land/p/demo/json" - "std" - "testing" - - pl "gno.land/r/demo/pool" - pn "gno.land/r/demo/position" - - "gno.land/r/demo/bar" - "gno.land/r/demo/gns" - "gno.land/r/demo/obl" - "gno.land/r/demo/qux" - - "gno.land/r/demo/gnft" - - "gno.land/r/demo/gnoswap/consts" -) - -func init() { - // init pool tiers - // tier 1 - poolTiers["gno.land/r/demo/bar:gno.land/r/demo/qux:500"] = 1 // DEV - - // tier 2 - poolTiers["GNS/USDT_500"] = 2 - poolTiers["ATOM/GNS_500"] = 2 - - // tier 3 - poolTiers["ATOM/GNOT_500"] = 3 - poolTiers["ATOM/USDT_500"] = 3 - poolTiers["ATOM/WETH_500"] = 3 -} - -func TestPoolCreatePool(t *testing.T) { - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 1); i++ { - gns.Faucet() - } - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) - std.TestSkipHeights(1) - - pl.CreatePool(barPath, quxPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 - std.TestSkipHeights(1) -} - -func TestPositionMint01(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1 := pn.Mint( - barPath, // token0 - quxPath, // token1 - fee500, // fee - int32(9000), // tickLower - int32(11000), // tickUpper - "1000", // amount0Desired - "1000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, lpTokenId, 1) - shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "368") - shouldEQ(t, amount1, "999") - - // approve nft to staker for staking - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) - std.TestSkipHeights(1) -} - -func TestPositionMint02(t *testing.T) { - std.TestSetPrevAddr(test1) - bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) - std.TestSkipHeights(2) - - lpTokenId, liquidity, amount0, amount1 := pn.Mint( - barPath, // token0 - quxPath, // token1 - fee500, // fee - int32(9100), // tickLower - int32(12000), // tickUpper - "5000", // amount0Desired - "5000", // amount1Desired - "1", // amount0Min - "1", // amount1Min - max_timeout, - test1.String(), - ) - std.TestSkipHeights(1) - - shouldEQ(t, lpTokenId, 2) - shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // test1 - shouldEQ(t, amount0, "3979") - shouldEQ(t, amount1, "4999") - - // approve nft to staker - std.TestSetPrevAddr(test1) - gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) - std.TestSkipHeights(1) -} - -func TestCreateExternalIncentive(t *testing.T) { - std.TestSetPrevAddr(test1) - obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) - std.TestSkipHeights(1) - - CreateExternalIncentive( - "gno.land/r/demo/bar:gno.land/r/demo/qux:500", // targetPoolPath - oblPath, // rewardToken - "1000000000", // rewardAmount - 1234569600, // startTimestamp - 1234569600+TIMESTAMP_90DAYS, // endTimestamp - ) - std.TestSkipHeights(1) - - obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) - std.TestSkipHeights(1) - CreateExternalIncentive("gno.land/r/demo/bar:gno.land/r/demo/qux:500", oblPath, "1000000000", 1234569600, 1234569600+TIMESTAMP_90DAYS) - std.TestSkipHeights(1) -} - -func TestStakeToken01(t *testing.T) { - std.TestSetPrevAddr(test1) - StakeToken(1) // GNFT tokenId - std.TestSkipHeights(500) - - shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker - shouldEQ(t, len(deposits), 1) -} - -func TestStakeToken02(t *testing.T) { - std.TestSetPrevAddr(test1) - StakeToken(2) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(2)), GetOrigPkgAddr()) // staker - shouldEQ(t, len(deposits), 2) -} - -func TestApiGetRewards(t *testing.T) { - agrs := ApiGetRewards() - root, err := json.Unmarshal([]byte(agrs)) - if err != nil { - panic(err) - } - - response, err := root.GetKey("response") - if err != nil { - panic(err) - } - - shouldEQ(t, response.Size(), 2) // lpTokenId 1, 2 -} - -func TestApiGetStakes(t *testing.T) { - ags := ApiGetStakes() - root, err := json.Unmarshal([]byte(ags)) - if err != nil { - panic(err) - } - - response, err := root.GetKey("response") - if err != nil { - panic(err) - } - - shouldEQ(t, response.Size(), 2) // lpTokenId 1, 2 -} - -func TestApiGetIncentives(t *testing.T) { - agi := ApiGetIncentives() - root, err := json.Unmarshal([]byte(agi)) - if err != nil { - panic(err) - } - - response, err := root.GetKey("response") - if err != nil { - panic(err) - } - - shouldEQ(t, response.Size(), 1) // bar:qux:500, obl -} - -func TestCollectReward(t *testing.T) { - std.TestSetPrevAddr(consts.INTERNAL_REWARD_ACCOUNT) - gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) // internal reward distribution - std.TestSkipHeights(1) - - // before claim - gnsOld := gns.BalanceOf(a2u(test1)) - shouldEQ(t, gnsOld, 0) - - std.TestSetPrevAddr(test1) - CollectReward(1) // GNFT tokenId - std.TestSkipHeights(1) - - gnsNew := gns.BalanceOf(a2u(test1)) - shouldEQ(t, gnsNew, 3075063) -} - -func TestUnstakeToken01(t *testing.T) { - std.TestSetPrevAddr(test1) - UnstakeToken(1) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(1)), test1) - - // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 3081190) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 8000011025) // external -} - -func TestUnstakeToken02(t *testing.T) { - std.TestSetPrevAddr(test1) - UnstakeToken(2) // GNFT tokenId - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(2)), test1) - - // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 3244540) - // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 8000012705) // external -} - -func TestEndExternalIncentive(t *testing.T) { - std.TestSetOrigCaller(test1) - std.TestSkipHeights(9999999) - EndExternalIncentive(std.GetOrigCaller().String(), "gno.land/r/demo/bar:gno.land/r/demo/qux:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() - std.TestSkipHeights(1) - - shouldEQ(t, len(incentives), 0) - shouldEQ(t, len(poolIncentives["gno.land/r/demo/bar:gno.land/r/demo/qux:500"]), 0) -} diff --git a/staker/_TEST_/z1_one_increase_external_filetest.gno b/staker/_TEST_/z1_one_increase_external_filetest.gno new file mode 100644 index 00000000..22e0e70a --- /dev/null +++ b/staker/_TEST_/z1_one_increase_external_filetest.gno @@ -0,0 +1,323 @@ +package main + +import ( + "std" + "strconv" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + "gno.land/p/demo/grc/grc721" + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/common" + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" + pn "gno.land/r/demo/position" + rr "gno.land/r/demo/router" + sr "gno.land/r/demo/staker" + + "gno.land/r/demo/gnft" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + max_timeout int64 = 9999999999 + + maxApprove uint64 = 18446744073709551615 + + TIMESTAMP_90DAYS int64 = 7776000 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func tid(tokenId interface{}) grc721.TokenID { + switch tokenId.(type) { + case string: + return grc721.TokenID(tokenId.(string)) + case int: + return grc721.TokenID(strconv.Itoa(tokenId.(int))) + case uint64: + return grc721.TokenID(strconv.Itoa(int(tokenId.(uint64)))) + case grc721.TokenID: + return tokenId.(grc721.TokenID) + default: + panic("tid() || unsupported tokenId type") + } +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + rr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + sr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + mint01() + mint02() + createExternalIncentiveInit() + createExternalIncentiveIncrease() + stakeToken01() + stakeToken02() + +} + +func createPool() { + numPool := uint64(1) + + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + for i := uint64(0); i < (5 * numPool); i++ { + gns.Faucet() + std.TestSkipHeights(1) + } + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*numPool) + std.TestSkipHeights(1) + + pl.CreatePool(barPath, fooPath, fee100, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) +} + +func mint01() { + std.TestSetOrigCaller(test1) + + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + pn.Mint(barPath, fooPath, fee100, 9000, 11000, "1000", "1000", "0", "0", max_timeout, test1.String()) +} + +func mint02() { + std.TestSetOrigCaller(test1) + + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + pn.Mint(barPath, fooPath, fee100, 9100, 12000, "5000", "5000", "0", "0", max_timeout, test1.String()) +} + +func createExternalIncentiveInit() { + std.TestSetPrevAddr(test1) + obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) + std.TestSkipHeights(1) + + sr.CreateExternalIncentive( + "gno.land/r/demo/bar:gno.land/r/demo/foo:100", // targetPoolPath + oblPath, // rewardToken + "1000000000", // rewardAmount + 1234569600, // startTimestamp + 1234569600+TIMESTAMP_90DAYS, // endTimestamp + ) + std.TestSkipHeights(1) +} + +func createExternalIncentiveIncrease() { + std.TestSetPrevAddr(test1) + obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) + std.TestSkipHeights(1) + + sr.CreateExternalIncentive( + "gno.land/r/demo/bar:gno.land/r/demo/foo:100", // targetPoolPath + oblPath, // rewardToken + "1000000000", // rewardAmount + 1234569600, // startTimestamp + 1234569600+TIMESTAMP_90DAYS, // endTimestamp + ) + std.TestSkipHeights(1) +} + +func stakeToken01() { + std.TestSetPrevAddr(test1) + gnft.Approve(a2u(consts.STAKER_ADDR), tid(1)) + sr.StakeToken(1) + std.TestSkipHeights(2) +} + +func stakeToken02() { + std.TestSetPrevAddr(test1) + gnft.Approve(a2u(consts.STAKER_ADDR), tid(2)) + sr.StakeToken(2) + std.TestSkipHeights(2) +} diff --git a/staker/_TEST_/z2_one_external_filetest.gno b/staker/_TEST_/z2_one_external_filetest.gno new file mode 100644 index 00000000..1e5b0edb --- /dev/null +++ b/staker/_TEST_/z2_one_external_filetest.gno @@ -0,0 +1,345 @@ +package main + +import ( + "std" + "strconv" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + "gno.land/r/demo/gnft" + + "gno.land/p/demo/grc/grc721" + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/common" + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" + pn "gno.land/r/demo/position" + rr "gno.land/r/demo/router" + sr "gno.land/r/demo/staker" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + max_timeout int64 = 9999999999 + + maxApprove uint64 = 18446744073709551615 + + TIMESTAMP_90DAYS int64 = 7776000 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func tid(tokenId interface{}) grc721.TokenID { + switch tokenId.(type) { + case string: + return grc721.TokenID(tokenId.(string)) + case int: + return grc721.TokenID(strconv.Itoa(tokenId.(int))) + case uint64: + return grc721.TokenID(strconv.Itoa(int(tokenId.(uint64)))) + case grc721.TokenID: + return tokenId.(grc721.TokenID) + default: + panic("tid() || unsupported tokenId type") + } +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + rr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + sr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + mint01() + mint02() + createExternalIncentive() + stakeToken01() + stakeToken02() + unStakeToken01() + unStakeToken02() + endExternalIncentive() +} + +func createPool() { + numPool := uint64(1) + + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + for i := uint64(0); i < (5 * numPool); i++ { + gns.Faucet() + std.TestSkipHeights(1) + } + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*numPool) + std.TestSkipHeights(1) + + pl.CreatePool(barPath, fooPath, fee100, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) +} + +func mint01() { + std.TestSetOrigCaller(test1) + + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + pn.Mint(barPath, fooPath, fee100, 9000, 11000, "1000", "1000", "0", "0", max_timeout, test1.String()) +} + +func mint02() { + std.TestSetOrigCaller(test1) + + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + pn.Mint(barPath, fooPath, fee100, 9100, 12000, "5000", "5000", "0", "0", max_timeout, test1.String()) +} + +func createExternalIncentive() { + std.TestSetPrevAddr(test1) + obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) + std.TestSkipHeights(1) + + sr.CreateExternalIncentive( + "gno.land/r/demo/bar:gno.land/r/demo/foo:100", // targetPoolPath + oblPath, // rewardToken + "1000000000", // rewardAmount + 1234569600, // startTimestamp + 1234569600+TIMESTAMP_90DAYS, // endTimestamp + ) + std.TestSkipHeights(1) +} + +func stakeToken01() { + std.TestSetPrevAddr(test1) + gnft.Approve(a2u(consts.STAKER_ADDR), tid(1)) + sr.StakeToken(1) + std.TestSkipHeights(2) +} + +func stakeToken02() { + std.TestSetPrevAddr(test1) + gnft.Approve(a2u(consts.STAKER_ADDR), tid(2)) + sr.StakeToken(2) + std.TestSkipHeights(2) +} + +func unStakeToken01() { + std.TestSetPrevAddr(consts.INTERNAL_REWARD_ACCOUNT) + gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) // internal reward distribution + std.TestSkipHeights(360) + + std.TestSetPrevAddr(test1) + sr.UnstakeToken(1) // GNFT tokenId + std.TestSkipHeights(1) + + gnsBal := gns.BalanceOf(a2u(test1)) // 0 + oblBal := obl.BalanceOf(a2u(test1)) + if oblBal != 9000000945 { + panic("oblBal != 9000000945") + } +} + +func unStakeToken02() { + std.TestSetPrevAddr(test1) + sr.UnstakeToken(2) // GNFT tokenId + std.TestSkipHeights(1) + + gnsBal := gns.BalanceOf(a2u(test1)) // 0 + oblBal := obl.BalanceOf(a2u(test1)) + if oblBal != 9000007455 { + panic("oblBal != 9000007455") + } +} + +func endExternalIncentive() { + std.TestSetOrigCaller(test1) + std.TestSkipHeights(9999999) + + sr.EndExternalIncentive(std.GetOrigCaller().String(), "gno.land/r/demo/bar:gno.land/r/demo/foo:100", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() + std.TestSkipHeights(1) +} diff --git a/staker/_TEST_/z3_internal_external_filetest.gno b/staker/_TEST_/z3_internal_external_filetest.gno new file mode 100644 index 00000000..572d95f2 --- /dev/null +++ b/staker/_TEST_/z3_internal_external_filetest.gno @@ -0,0 +1,397 @@ +package main + +import ( + "std" + "strconv" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + "gno.land/r/demo/gnft" + + "gno.land/p/demo/grc/grc721" + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/common" + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" + pn "gno.land/r/demo/position" + rr "gno.land/r/demo/router" + sr "gno.land/r/demo/staker" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + maxTimeout int64 = 9999999999 + + maxApprove uint64 = 18446744073709551615 + + TIMESTAMP_90DAYS int64 = 7776000 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func tid(tokenId interface{}) grc721.TokenID { + switch tokenId.(type) { + case string: + return grc721.TokenID(tokenId.(string)) + case int: + return grc721.TokenID(strconv.Itoa(tokenId.(int))) + case uint64: + return grc721.TokenID(strconv.Itoa(int(tokenId.(uint64)))) + case grc721.TokenID: + return tokenId.(grc721.TokenID) + default: + panic("tid() || unsupported tokenId type") + } +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + rr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + sr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + setPoolTier() + mintBarQux() + mintBarBaz() + mintFooQux() + createExternalIncentive() + stakeTier01() + stakeTier02() + stakeExternal() + collectReward01() + collectReward01Again() + collectExternalReward() +} + +func createPool() { + numPool := uint64(4) + + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + for i := uint64(0); i < (5 * numPool); i++ { + gns.Faucet() + std.TestSkipHeights(1) + } + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*numPool) + std.TestSkipHeights(1) + + pl.CreatePool(barPath, quxPath, fee500, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) + pl.CreatePool(barPath, bazPath, fee100, common.TickMathGetSqrtRatioAtTick(0).ToString()) + pl.CreatePool(fooPath, quxPath, fee500, common.TickMathGetSqrtRatioAtTick(0).ToString()) + std.TestSkipHeights(3) +} + +func setPoolTier() { + std.TestSetPrevAddr(consts.GNOSWAP_ADMIN) + sr.SetPoolTier("gno.land/r/demo/bar:gno.land/r/demo/qux:500", 1) + sr.SetPoolTier("gno.land/r/demo/bar:gno.land/r/demo/baz:100", 1) + + sr.SetPoolTier("gno.land/r/demo/foo:gno.land/r/demo/qux:500", 2) +} + +func mintBarQux() { + std.TestSetPrevAddr(test1) + + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + pn.Mint(barPath, quxPath, fee500, 9000, 11000, "100000", "100000", "0", "0", maxTimeout, test1.String()) +} + +func mintBarBaz() { + std.TestSetPrevAddr(test1) + + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + pn.Mint(barPath, bazPath, fee100, -1000, 1000, "100000", "100000", "0", "0", maxTimeout, test1.String()) +} + +func mintFooQux() { + std.TestSetPrevAddr(test1) + + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + std.TestSetPrevRealm(consts.STAKER_PATH) + pn.Mint(fooPath, quxPath, fee500, -1000, 11000, "100000", "100000", "0", "0", maxTimeout, test1.String()) +} + +func createExternalIncentive() { + std.TestSetPrevAddr(test1) + obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) + std.TestSkipHeights(1) + + sr.CreateExternalIncentive( + "gno.land/r/demo/foo:gno.land/r/demo/qux:500", // targetPoolPath + oblPath, // rewardToken + "1000000000", // rewardAmount + 1234569600, // startTimestamp + 1234569600+TIMESTAMP_90DAYS, // endTimestamp + ) + std.TestSkipHeights(1) +} + +func stakeTier01() { + std.TestSetPrevAddr(test1) + gnft.Approve(a2u(consts.STAKER_ADDR), tid(1)) + sr.StakeToken(1) + + std.TestSkipHeights(2) +} + +func stakeTier02() { + std.TestSetPrevAddr(test1) + gnft.Approve(a2u(consts.STAKER_ADDR), tid(2)) + sr.StakeToken(2) + + std.TestSkipHeights(2) +} + +func stakeExternal() { + std.TestSetPrevAddr(test1) + gnft.Approve(a2u(consts.STAKER_ADDR), tid(3)) + sr.StakeToken(3) + + std.TestSkipHeights(2) +} + +func collectReward01() { + std.TestSetPrevAddr(consts.INTERNAL_REWARD_ACCOUNT) + gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) // internal reward distribution + std.TestSkipHeights(1) + + gnsBal := gns.BalanceOf(a2u(test1)) + if gnsBal != 500000000 { + panic("collectReward01() || gnsBal != 500000000") + } + + std.TestSetPrevAddr(test1) + sr.CollectReward(1) + + gnsBal = gns.BalanceOf(a2u(test1)) + if gnsBal != 500266805 { + panic("collectReward01() || gnsBal != 500266805") + } +} + +func collectReward01Again() { + std.TestSetPrevAddr(test1) + + gnsBal := gns.BalanceOf(a2u(test1)) + if gnsBal != 500266805 { + panic("collectReward01Again() || gnsBal != 500266805") + } + + // ACCELERATE TIME + std.TestSkipHeights(1000) + + sr.CollectReward(1) + std.TestSkipHeights(1) + + gnsBal = gns.BalanceOf(a2u(test1)) + if gnsBal != 538381805 { + panic("collectReward01() || gnsBal != 538381805") + } +} + +func collectExternalReward() { + std.TestSetPrevAddr(test1) + + oblBal := obl.BalanceOf(a2u(test1)) + if oblBal != 9000000000 { + panic("collectExternalReward() || oblBal != 9000000000") + } + + sr.CollectReward(3) + oblBal = obl.BalanceOf(a2u(test1)) + if oblBal != 9000145306 { + panic("collectExternalReward() || oblBal != 9000145306") + } +} diff --git a/staker/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno b/staker/_TEST_/z4_mint_and_stake_filetest.gno similarity index 50% rename from staker/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno rename to staker/_TEST_/z4_mint_and_stake_filetest.gno index f181db81..3d2b4b88 100644 --- a/staker/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno +++ b/staker/_TEST_/z4_mint_and_stake_filetest.gno @@ -1,27 +1,53 @@ -package staker +package main import ( "std" + "strconv" + "testing" + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" - "gno.land/r/demo/bar" + "gno.land/p/demo/grc/grc721" + pusers "gno.land/p/demo/users" - "gno.land/r/demo/baz" + "gno.land/r/demo/gnoswap/common" + "gno.land/r/demo/gnoswap/consts" - "gno.land/r/demo/qux" + pl "gno.land/r/demo/pool" + pn "gno.land/r/demo/position" + rr "gno.land/r/demo/router" + sr "gno.land/r/demo/staker" +) - "gno.land/r/demo/wugnot" +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") - "gno.land/r/demo/obl" + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" - "gno.land/r/demo/gns" + oblPath string = "gno.land/r/demo/obl" - "gno.land/r/demo/gnoswap/consts" + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 - pusers "gno.land/p/demo/users" + maxTimeout int64 = 9999999999 - pl "gno.land/r/demo/pool" + maxApprove uint64 = 18446744073709551615 + + TIMESTAMP_90DAYS int64 = 7776000 + + // TESTING + t *testing.T ) type FooToken struct{} @@ -132,24 +158,126 @@ func (GNSToken) Approve() func(spender pusers.AddressOrName, amount uint64) { return gns.Approve } +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func tid(tokenId interface{}) grc721.TokenID { + switch tokenId.(type) { + case string: + return grc721.TokenID(tokenId.(string)) + case int: + return grc721.TokenID(strconv.Itoa(tokenId.(int))) + case uint64: + return grc721.TokenID(strconv.Itoa(int(tokenId.(uint64)))) + case grc721.TokenID: + return tokenId.(grc721.TokenID) + default: + panic("tid() || unsupported tokenId type") + } +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + func init() { - std.TestSetOrigCaller(consts.GNOSWAP_ADMIN) + // FAUCET + std.TestSetPrevAddr(test1) - // POOL - pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) 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/wugnot", WugnotToken{}) pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) - // STAKER - RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) - RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) - RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) - RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) - RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) - RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) - RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + sr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + mintAndStake() + collectFee() + collectReward() + unstakeToken() +} + +func createPool() { + numPool := uint64(1) + + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + for i := uint64(0); i < (5 * numPool); i++ { + gns.Faucet() + std.TestSkipHeights(1) + } + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*numPool) + std.TestSkipHeights(1) + + pl.CreatePool(barPath, quxPath, fee500, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) + std.TestSkipHeights(1) +} + +func mintAndStake() { + std.TestSetPrevAddr(test1) + + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + sr.MintAndStake(barPath, quxPath, fee500, 9000, 11000, "100000", "100000", "0", "0", maxTimeout) +} + +func collectFee() { + std.TestSetPrevAddr(test1) + pn.CollectFee(1) +} + +func collectReward() { + // internal reward distribution + std.TestSetPrevAddr(consts.INTERNAL_REWARD_ACCOUNT) + gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) + + std.TestSetPrevAddr(test1) + sr.CollectReward(1) +} + +func unstakeToken() { + std.TestSetPrevAddr(test1) + sr.UnstakeToken(1) } diff --git a/staker/_TEST_/z5_manage_pool_tier_filetest.gno b/staker/_TEST_/z5_manage_pool_tier_filetest.gno new file mode 100644 index 00000000..e83c4121 --- /dev/null +++ b/staker/_TEST_/z5_manage_pool_tier_filetest.gno @@ -0,0 +1,283 @@ +package main + +import ( + "std" + "strconv" + "testing" + + "gno.land/r/demo/bar" + "gno.land/r/demo/baz" + "gno.land/r/demo/foo" + "gno.land/r/demo/gns" + "gno.land/r/demo/obl" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" + + "gno.land/p/demo/grc/grc721" + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gnoswap/common" + "gno.land/r/demo/gnoswap/consts" + + pl "gno.land/r/demo/pool" + rr "gno.land/r/demo/router" + sr "gno.land/r/demo/staker" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + fooPath string = "gno.land/r/demo/foo" + barPath string = "gno.land/r/demo/bar" + bazPath string = "gno.land/r/demo/baz" + quxPath string = "gno.land/r/demo/qux" + + oblPath string = "gno.land/r/demo/obl" + + fee100 uint32 = 100 + fee500 uint32 = 500 + fee3000 uint32 = 3000 + + maxTimeout int64 = 9999999999 + + maxApprove uint64 = 18446744073709551615 + + TIMESTAMP_90DAYS int64 = 7776000 + + // TESTING + t *testing.T +) + +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 +} + +/* HELPER FUNCTIONS */ +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} + +func tid(tokenId interface{}) grc721.TokenID { + switch tokenId.(type) { + case string: + return grc721.TokenID(tokenId.(string)) + case int: + return grc721.TokenID(strconv.Itoa(tokenId.(int))) + case uint64: + return grc721.TokenID(strconv.Itoa(int(tokenId.(uint64)))) + case grc721.TokenID: + return tokenId.(grc721.TokenID) + default: + panic("tid() || unsupported tokenId type") + } +} + +func ugnotBalanceOf(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + + coins := testBanker.GetCoins(addr) + if len(coins) == 0 { + return 0 + } + + return uint64(testBanker.GetCoins(addr)[0].Amount) +} + +func init() { + // FAUCET + std.TestSetPrevAddr(test1) + + for i := 0; i < 100; i++ { + foo.Faucet() + bar.Faucet() + baz.Faucet() + qux.Faucet() + obl.Faucet() + } + + // TOKEN REGISTER + std.TestSetPrevAddr(gsa) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + rr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + sr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) +} + +/* START TEST */ +func main() { + createPool() + setPoolTier() + changePoolTier() + removePoolTier() +} + +func createPool() { + numPool := uint64(1) + + // call gns faucet to get creation fee + std.TestSetPrevAddr(test1) + for i := uint64(0); i < (5 * numPool); i++ { + gns.Faucet() + std.TestSkipHeights(1) + } + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*numPool) + std.TestSkipHeights(1) + + pl.CreatePool(barPath, quxPath, fee500, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) + std.TestSkipHeights(1) +} + +func setPoolTier() { + std.TestSetPrevAddr(consts.GNOSWAP_ADMIN) + sr.SetPoolTier("gno.land/r/demo/bar:gno.land/r/demo/qux:500", 2) + + poolTier := sr.GetPoolWithTiers()["gno.land/r/demo/bar:gno.land/r/demo/qux:500"] + if poolTier != 2 { + panic("pool tier not set") + } +} + +func changePoolTier() { + std.TestSetPrevAddr(consts.GNOSWAP_ADMIN) + sr.ChangePoolTier("gno.land/r/demo/bar:gno.land/r/demo/qux:500", 3) + + poolTier := sr.GetPoolWithTiers()["gno.land/r/demo/bar:gno.land/r/demo/qux:500"] + if poolTier != 3 { + panic("pool tier not updated") + } +} + +func removePoolTier() { + std.TestSetPrevAddr(consts.GNOSWAP_ADMIN) + sr.RemovePoolTier("gno.land/r/demo/bar:gno.land/r/demo/qux:500") + + _, exist := sr.GetPoolWithTiers()["gno.land/r/demo/bar:gno.land/r/demo/qux:500"] + if exist { + panic("pool tier not removed") + } +} From d1d39592bc8e5d4332afe42f3f652151da13b7a2 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 2 May 2024 15:15:19 +0900 Subject: [PATCH 8/9] fix: userCalled check condition --- .../r/demo/gnoswap/common/allow_non_gnoswap_contracts.gno | 4 ++-- router/router.gno | 5 ++++- staker/staker.gno | 5 ++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/_deploy/r/demo/gnoswap/common/allow_non_gnoswap_contracts.gno b/_deploy/r/demo/gnoswap/common/allow_non_gnoswap_contracts.gno index 6f190cb9..59147e60 100644 --- a/_deploy/r/demo/gnoswap/common/allow_non_gnoswap_contracts.gno +++ b/_deploy/r/demo/gnoswap/common/allow_non_gnoswap_contracts.gno @@ -27,8 +27,8 @@ func MustCallFromAdmin() { } func DisallowCallFromUser() { - isOrigin := std.IsOriginCall() - if isOrigin { + prevRealmPath := std.PrevRealm().PkgPath() + if prevRealmPath == "" { panic("must be called by realm, not user") } } diff --git a/router/router.gno b/router/router.gno index 40346094..8c7f4867 100644 --- a/router/router.gno +++ b/router/router.gno @@ -38,7 +38,10 @@ func SwapRoute( _tokenAmountLimit string, // uint256 ) (string, string) { // tokneIn, tokenOut if common.GetLimitCaller() { - std.AssertOriginCall() + isUserCalled := std.PrevRealm().PkgPath() == "" + if !isUserCalled { + panic("[ROUTER] router.gno__SwapRoute() || only user can call this function") + } } amountSpecified := i256.MustFromDecimal(_amountSpecified) diff --git a/staker/staker.gno b/staker/staker.gno index a5125598..9a5ce763 100644 --- a/staker/staker.gno +++ b/staker/staker.gno @@ -57,7 +57,10 @@ func CreateExternalIncentive( endTimestamp int64, ) { if common.GetLimitCaller() { - std.AssertOriginCall() + isUserCalled := std.PrevRealm().PkgPath() == "" + if !isUserCalled { + panic("[STAKER] staker.gno__CreateExternalIncentive() || only user can call this function") + } } rewardAmount := u256.MustFromDecimal(_rewardAmount) From d7c47ae9c2ece8ccb70d6c4918899ae548964031 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 2 May 2024 15:15:44 +0900 Subject: [PATCH 9/9] chore --- gov/api.gno | 4 ++++ pool/_RPC_dry.gno | 3 ++- pool/pool.gno | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/gov/api.gno b/gov/api.gno index b4bea401..2022e923 100644 --- a/gov/api.gno +++ b/gov/api.gno @@ -43,6 +43,10 @@ func GetActiveProposals() []Proposal { return nil } +func GetProposalById(proposalID uint64) *Proposal { + return &proposalsMap[proposalID] +} + func SubmitProposalText( title string, summary string, diff --git a/pool/_RPC_dry.gno b/pool/_RPC_dry.gno index 028be9ce..0798cfda 100644 --- a/pool/_RPC_dry.gno +++ b/pool/_RPC_dry.gno @@ -182,6 +182,8 @@ func DrySwap( amount1 = i256.Zero().Sub(amountSpecified, state.amountSpecifiedRemaining) } + pool.slot0.unlocked = true + if zeroForOne { if pool.balances.token1.Lt(amount1.Abs()) { // NOT ENOUGH BALANCE for output token1 @@ -199,6 +201,5 @@ func DrySwap( return "0", "0", false } - pool.slot0.unlocked = true return amount0.ToString(), amount1.ToString(), true } diff --git a/pool/pool.gno b/pool/pool.gno index 04bbcfef..cee41e48 100644 --- a/pool/pool.gno +++ b/pool/pool.gno @@ -484,6 +484,10 @@ func (pool *Pool) transferAndVerify( amount *i256.Int, isToken0 bool, ) { + if amount.IsZero() { + return + } + // must be negative to send token from pool to user // as point of view from pool, it is negative if !amount.IsNeg() {