From 8ae495aaef9c253ebc03ecb6ed8eb0d46a3e5a70 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 15 May 2024 21:30:17 +0900 Subject: [PATCH 01/10] test: update test grc20 tokens --- __local/grc20_tokens/bar/bar.gno | 27 +--- __local/grc20_tokens/baz/baz.gno | 27 +--- __local/grc20_tokens/foo/foo.gno | 27 +--- __local/grc20_tokens/obl/obl.gno | 29 +--- __local/grc20_tokens/qux/qux.gno | 6 +- __local/grc20_tokens/register_gnodev/gno.mod | 1 + .../register_gnodev/register_gnodev.gno | 19 +++ __local/grc20_tokens/usdc/gno.mod | 8 ++ __local/grc20_tokens/usdc/usdc.gno | 130 ++++++++++++++++++ 9 files changed, 170 insertions(+), 104 deletions(-) create mode 100644 __local/grc20_tokens/usdc/gno.mod create mode 100644 __local/grc20_tokens/usdc/usdc.gno diff --git a/__local/grc20_tokens/bar/bar.gno b/__local/grc20_tokens/bar/bar.gno index 615cc815..5b93f2dd 100644 --- a/__local/grc20_tokens/bar/bar.gno +++ b/__local/grc20_tokens/bar/bar.gno @@ -13,12 +13,12 @@ import ( var ( bar *grc20.AdminToken - admin std.Address = "g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq" // TODO: helper to change admin + admin std.Address = "g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq" ) func init() { bar = grc20.NewAdminToken("Bar", "BAR", 6) - bar.Mint(admin, 500_000_000_000_000) + bar.Mint(admin, 100_000_000_000_000) } // method proxies as public functions. @@ -72,29 +72,6 @@ func TransferFrom(from, to pusers.AddressOrName, amount uint64) { } } -// faucet. - -func Faucet() { - // FIXME: add limits? - // FIXME: add payment in gnot? - caller := std.PrevRealm().Addr() - err := bar.Mint(caller, 100*1_000_000) // 100M - if err != nil { - panic(err.Error()) - } -} - -// administration. - -func Mint(address pusers.AddressOrName, amount uint64) { - caller := std.PrevRealm().Addr() - assertIsAdmin(caller) - err := bar.Mint(users.Resolve(address), amount) - if err != nil { - panic(err.Error()) - } -} - func Burn(address pusers.AddressOrName, amount uint64) { caller := std.PrevRealm().Addr() assertIsAdmin(caller) diff --git a/__local/grc20_tokens/baz/baz.gno b/__local/grc20_tokens/baz/baz.gno index bd090b7a..cbe85dbe 100644 --- a/__local/grc20_tokens/baz/baz.gno +++ b/__local/grc20_tokens/baz/baz.gno @@ -13,12 +13,12 @@ import ( var ( baz *grc20.AdminToken - admin std.Address = "g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq" // TODO: helper to change admin + admin std.Address = "g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq" ) func init() { baz = grc20.NewAdminToken("Baz", "BAZ", 6) - baz.Mint(admin, 500_000_000_000_000) + baz.Mint(admin, 100_000_000_000_000) } // method proxies as public functions. @@ -72,29 +72,6 @@ func TransferFrom(from, to pusers.AddressOrName, amount uint64) { } } -// faucet. - -func Faucet() { - // FIXME: add limits? - // FIXME: add payment in gnot? - caller := std.PrevRealm().Addr() - err := baz.Mint(caller, 100*1_000_000) // 100M - if err != nil { - panic(err.Error()) - } -} - -// administration. - -func Mint(address pusers.AddressOrName, amount uint64) { - caller := std.PrevRealm().Addr() - assertIsAdmin(caller) - err := baz.Mint(users.Resolve(address), amount) - if err != nil { - panic(err.Error()) - } -} - func Burn(address pusers.AddressOrName, amount uint64) { caller := std.PrevRealm().Addr() assertIsAdmin(caller) diff --git a/__local/grc20_tokens/foo/foo.gno b/__local/grc20_tokens/foo/foo.gno index 9ffc7824..fbaa4bf9 100644 --- a/__local/grc20_tokens/foo/foo.gno +++ b/__local/grc20_tokens/foo/foo.gno @@ -13,12 +13,12 @@ import ( var ( foo *grc20.AdminToken - admin std.Address = "g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq" // TODO: helper to change admin + admin std.Address = "g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq" ) func init() { foo = grc20.NewAdminToken("Foo", "FOO", 6) - foo.Mint(admin, 500_000_000_000_000) + foo.Mint(admin, 100_000_000_000_000) } // method proxies as public functions. @@ -72,29 +72,6 @@ func TransferFrom(from, to pusers.AddressOrName, amount uint64) { } } -// faucet. - -func Faucet() { - // FIXME: add limits? - // FIXME: add payment in gnot? - caller := std.PrevRealm().Addr() - err := foo.Mint(caller, 100*1_000_000) // 100M - if err != nil { - panic(err.Error()) - } -} - -// administration. - -func Mint(address pusers.AddressOrName, amount uint64) { - caller := std.PrevRealm().Addr() - assertIsAdmin(caller) - err := foo.Mint(users.Resolve(address), amount) - if err != nil { - panic(err.Error()) - } -} - func Burn(address pusers.AddressOrName, amount uint64) { caller := std.PrevRealm().Addr() assertIsAdmin(caller) diff --git a/__local/grc20_tokens/obl/obl.gno b/__local/grc20_tokens/obl/obl.gno index d76269c0..0a646665 100644 --- a/__local/grc20_tokens/obl/obl.gno +++ b/__local/grc20_tokens/obl/obl.gno @@ -13,12 +13,12 @@ import ( var ( obl *grc20.AdminToken - admin std.Address = "g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq" // TODO: helper to change admin + admin std.Address = "g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq" ) func init() { - obl = grc20.NewAdminToken("Obl", "OBL", 4) - obl.Mint(admin, 500_000_000_000_000) + obl = grc20.NewAdminToken("Obl", "OBL", 6) + obl.Mint(admin, 100_000_000_000_000) } // method proxies as public functions. @@ -72,29 +72,6 @@ func TransferFrom(from, to pusers.AddressOrName, amount uint64) { } } -// faucet. - -func Faucet() { - // FIXME: add limits? - // FIXME: add payment in gnot? - caller := std.PrevRealm().Addr() - err := obl.Mint(caller, 100*1_000_000) // 100M - if err != nil { - panic(err.Error()) - } -} - -// administration. - -func Mint(address pusers.AddressOrName, amount uint64) { - caller := std.PrevRealm().Addr() - assertIsAdmin(caller) - err := obl.Mint(users.Resolve(address), amount) - if err != nil { - panic(err.Error()) - } -} - func Burn(address pusers.AddressOrName, amount uint64) { caller := std.PrevRealm().Addr() assertIsAdmin(caller) diff --git a/__local/grc20_tokens/qux/qux.gno b/__local/grc20_tokens/qux/qux.gno index 2c425b56..73e6002f 100644 --- a/__local/grc20_tokens/qux/qux.gno +++ b/__local/grc20_tokens/qux/qux.gno @@ -13,12 +13,12 @@ import ( var ( qux *grc20.AdminToken - admin std.Address = "g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq" // TODO: helper to change admin + admin std.Address = "g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq" ) func init() { - qux = grc20.NewAdminToken("Qux", "QUX", 4) - qux.Mint(admin, 500_000_000_000_000) + qux = grc20.NewAdminToken("Qux", "QUX", 6) + qux.Mint(admin, 100_000_000_000_000) } // method proxies as public functions. diff --git a/__local/grc20_tokens/register_gnodev/gno.mod b/__local/grc20_tokens/register_gnodev/gno.mod index 21671cfd..70f31a6b 100644 --- a/__local/grc20_tokens/register_gnodev/gno.mod +++ b/__local/grc20_tokens/register_gnodev/gno.mod @@ -10,5 +10,6 @@ require ( gno.land/r/demo/obl v0.0.0-latest gno.land/r/demo/pool v0.0.0-latest gno.land/r/demo/qux v0.0.0-latest + gno.land/r/demo/usdc v0.0.0-latest gno.land/r/demo/wugnot v0.0.0-latest ) diff --git a/__local/grc20_tokens/register_gnodev/register_gnodev.gno b/__local/grc20_tokens/register_gnodev/register_gnodev.gno index 426deb6d..3fd5e2b5 100644 --- a/__local/grc20_tokens/register_gnodev/register_gnodev.gno +++ b/__local/grc20_tokens/register_gnodev/register_gnodev.gno @@ -10,6 +10,7 @@ import ( "gno.land/r/demo/gns" "gno.land/r/demo/obl" "gno.land/r/demo/qux" + "gno.land/r/demo/usdc" "gno.land/r/demo/wugnot" pl "gno.land/r/demo/pool" @@ -137,6 +138,21 @@ func (WugnotToken) Approve() func(spender pusers.AddressOrName, amount uint64) { return wugnot.Approve } +type UsdcToken struct{} + +func (UsdcToken) Transfer() func(to pusers.AddressOrName, amount uint64) { + return usdc.Transfer +} +func (UsdcToken) TransferFrom() func(from, to pusers.AddressOrName, amount uint64) { + return usdc.TransferFrom +} +func (UsdcToken) BalanceOf() func(owner pusers.AddressOrName) uint64 { + return usdc.BalanceOf +} +func (UsdcToken) Approve() func(spender pusers.AddressOrName, amount uint64) { + return usdc.Approve +} + func init() { pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) @@ -146,6 +162,7 @@ func init() { pl.RegisterGRC20Interface("gno.land/r/demo/obl", OblToken{}) pl.RegisterGRC20Interface("gno.land/r/demo/foo20", Foo20Token{}) pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/usdc", UsdcToken{}) sr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) sr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) @@ -155,6 +172,7 @@ func init() { sr.RegisterGRC20Interface("gno.land/r/demo/obl", OblToken{}) sr.RegisterGRC20Interface("gno.land/r/demo/foo20", Foo20Token{}) sr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + sr.RegisterGRC20Interface("gno.land/r/demo/usdc", UsdcToken{}) rr.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) rr.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) @@ -164,4 +182,5 @@ func init() { rr.RegisterGRC20Interface("gno.land/r/demo/obl", OblToken{}) rr.RegisterGRC20Interface("gno.land/r/demo/foo20", Foo20Token{}) rr.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + rr.RegisterGRC20Interface("gno.land/r/demo/usdc", UsdcToken{}) } diff --git a/__local/grc20_tokens/usdc/gno.mod b/__local/grc20_tokens/usdc/gno.mod new file mode 100644 index 00000000..52e772a5 --- /dev/null +++ b/__local/grc20_tokens/usdc/gno.mod @@ -0,0 +1,8 @@ +module gno.land/r/demo/usdc + +require ( + gno.land/p/demo/grc/grc20 v0.0.0-latest + gno.land/p/demo/ufmt v0.0.0-latest + gno.land/p/demo/users v0.0.0-latest + gno.land/r/demo/users v0.0.0-latest +) \ No newline at end of file diff --git a/__local/grc20_tokens/usdc/usdc.gno b/__local/grc20_tokens/usdc/usdc.gno new file mode 100644 index 00000000..69da22e3 --- /dev/null +++ b/__local/grc20_tokens/usdc/usdc.gno @@ -0,0 +1,130 @@ +package usdc + +import ( + "std" + "strings" + + "gno.land/p/demo/grc/grc20" + "gno.land/p/demo/ufmt" + "gno.land/r/demo/users" + + pusers "gno.land/p/demo/users" +) + +var ( + usdc *grc20.AdminToken + admin std.Address = "g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq" +) + +func init() { + usdc = grc20.NewAdminToken("Usd Coin", "USC", 6) + usdc.Mint(admin, 100_000_000_000_000) +} + +// method proxies as public functions. +// + +// getters. + +func TotalSupply() uint64 { + return usdc.TotalSupply() +} + +func BalanceOf(owner pusers.AddressOrName) uint64 { + balance, err := usdc.BalanceOf(users.Resolve(owner)) + if err != nil { + panic(err.Error()) + } + return balance +} + +func Allowance(owner, spender pusers.AddressOrName) uint64 { + allowance, err := usdc.Allowance(users.Resolve(owner), users.Resolve(spender)) + if err != nil { + panic(err.Error()) + } + return allowance +} + +// setters. + +func Transfer(to pusers.AddressOrName, amount uint64) { + caller := std.PrevRealm().Addr() + err := usdc.Transfer(caller, users.Resolve(to), amount) + if err != nil { + panic(err.Error()) + } +} + +func Approve(spender pusers.AddressOrName, amount uint64) { + caller := std.PrevRealm().Addr() + err := usdc.Approve(caller, users.Resolve(spender), amount) + if err != nil { + panic(err.Error()) + } +} + +func TransferFrom(from, to pusers.AddressOrName, amount uint64) { + caller := std.PrevRealm().Addr() + err := usdc.TransferFrom(caller, users.Resolve(from), users.Resolve(to), amount) + if err != nil { + panic(err.Error()) + } +} + +// faucet. + +func Faucet() { + // FIXME: add limits? + // FIXME: add payment in gnot? + caller := std.PrevRealm().Addr() + err := usdc.Mint(caller, 100*1_000_000) // 100M + if err != nil { + panic(err.Error()) + } +} + +// administration. + +func Mint(address pusers.AddressOrName, amount uint64) { + caller := std.PrevRealm().Addr() + assertIsAdmin(caller) + err := usdc.Mint(users.Resolve(address), amount) + if err != nil { + panic(err.Error()) + } +} + +func Burn(address pusers.AddressOrName, amount uint64) { + caller := std.PrevRealm().Addr() + assertIsAdmin(caller) + err := usdc.Burn(users.Resolve(address), amount) + if err != nil { + panic(err.Error()) + } +} + +// render. +// + +func Render(path string) string { + parts := strings.Split(path, "/") + c := len(parts) + + switch { + case path == "": + return usdc.RenderHome() + case c == 2 && parts[0] == "balance": + owner := pusers.AddressOrName(parts[1]) + balance, _ := usdc.BalanceOf(users.Resolve(owner)) + return ufmt.Sprintf("%d\n", balance) + default: + return "404\n" + } +} + +func assertIsAdmin(address std.Address) { + if address != admin { + panic("restricted access") + } +} From f4df612cc65fdc83ff9c85fd229fe8bc7a26d395 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 15 May 2024 21:30:38 +0900 Subject: [PATCH 02/10] feat: update gns token --- _deploy/r/demo/gns/gns.gno | 73 ++------------------------------------ 1 file changed, 3 insertions(+), 70 deletions(-) diff --git a/_deploy/r/demo/gns/gns.gno b/_deploy/r/demo/gns/gns.gno index d98b6c88..6d029349 100644 --- a/_deploy/r/demo/gns/gns.gno +++ b/_deploy/r/demo/gns/gns.gno @@ -16,17 +16,13 @@ import ( const MAXIMUM_SUPPLY = uint64(1_000_000_000_000_000) // 1B var ( - gns *grc20.AdminToken - admins []string + gns *grc20.AdminToken ) func init() { - gns = grc20.NewAdminToken("Gnoswap", "GNS", 4) + gns = grc20.NewAdminToken("Gnoswap", "GNS", 6) gns.Mint(consts.INTERNAL_REWARD_ACCOUNT, 500_000_000_000_000) // 500_000_000 GNS ≈ 0.5B - gns.Mint(consts.GNOSWAP_ADMIN, 100_000_000_000) // 100_000 GNS - - admins = append(admins, string(consts.GNOSWAP_ADMIN)) - admins = append(admins, string(consts.STAKER_ADDR)) + gns.Mint(consts.GNOSWAP_ADMIN, 500_000_000_000_000) // 500_000_000 GNS ≈ 0.5B gns.Approve(consts.INTERNAL_REWARD_ACCOUNT, consts.STAKER_ADDR, consts.UINT64_MAX) } @@ -82,55 +78,6 @@ func TransferFrom(from, to pusers.AddressOrName, amount uint64) { } } -// faucet. - -func Faucet() { - // FIXME: add limits? - // FIXME: add payment in gnot? - caller := std.PrevRealm().Addr() - err := gns.Mint(caller, 100*1_000_000) // 100M - if err != nil { - panic(err.Error()) - } -} - -// administration. - -func Mint(address pusers.AddressOrName, amount uint64) { - caller := std.PrevRealm().Addr() - assertIsAdmin(caller) - - if gns.TotalSupply()+amount <= MAXIMUM_SUPPLY { - gns.Mint(users.Resolve(address), amount) - } -} - -func Burn(address pusers.AddressOrName, amount uint64) { - caller := std.PrevRealm().Addr() - assertIsAdmin(caller) - err := gns.Burn(users.Resolve(address), amount) - if err != nil { - panic(err.Error()) - } -} - -func AppendAdmin(address pusers.AddressOrName) { - caller := std.PrevRealm().Addr() - assertIsAdmin(caller) - admins = append(admins, string(users.Resolve(address))) -} - -func RemoveAdmin(address pusers.AddressOrName) { - caller := std.PrevRealm().Addr() - assertIsAdmin(caller) - for i, v := range admins { - if v == string(users.Resolve(address)) { - admins = append(admins[:i], admins[i+1:]...) - return - } - } -} - // render. // @@ -149,17 +96,3 @@ func Render(path string) string { return "404\n" } } - -func assertIsAdmin(address std.Address) { - if !contains(admins, string(address)) { - panic("not one of admins") - } -} -func contains(s []string, str string) bool { - for _, v := range s { - if v == str { - return true - } - } - return false -} From 646ddbcf1d8269ef54b0316070c56c1c661c5a19 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 15 May 2024 21:31:08 +0900 Subject: [PATCH 03/10] feat: disable pool creation fee, update: latest wugnot min deposit --- _deploy/r/demo/gnoswap/consts/consts.gno | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/_deploy/r/demo/gnoswap/consts/consts.gno b/_deploy/r/demo/gnoswap/consts/consts.gno index 956fe54a..e60aad64 100644 --- a/_deploy/r/demo/gnoswap/consts/consts.gno +++ b/_deploy/r/demo/gnoswap/consts/consts.gno @@ -11,14 +11,13 @@ var ( // GNOSWAP SERVICE const ( - // r3v4_xxx: for txtar testing, change below address GNOSWAP_ADMIN std.Address = "g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq" // GSA FEE_COLLECTOR std.Address = "g18sp3hq6zqfxw88ffgz773gvaqgzjhxy62l9906" // FCL INTERNAL_REWARD_ACCOUNT std.Address = "g1jms5fx2raq4qfkq3502mfh25g54nyl5qeuvz5y" // IRA for GNS BLOCK_GENERATION_INTERVAL int64 = 5 // 5 seconds - POOL_CREATION_FEE uint64 = 500_000_000 + POOL_CREATION_FEE uint64 = 0 ) // WRAP & UNWRAP @@ -26,7 +25,7 @@ const ( GNOT string = "gnot" WRAPPED_WUGNOT string = "gno.land/r/demo/wugnot" - UGNOT_MINT_DEPOSIT_TO_WRAP uint64 = 1 // defined in gno.land/r/demo/wugnot + UGNOT_MINT_DEPOSIT_TO_WRAP uint64 = 1000 // defined in gno.land/r/demo/wugnot ) // CONTRACT PATH & ADDRESS From 91584892904d2aa6ab74b0a1dbf3d83264f96569 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 15 May 2024 21:31:24 +0900 Subject: [PATCH 04/10] feat: remove gns.mint from gov --- gov/gns_helper.gno | 4 ---- gov/proposal_mint.gno | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/gov/gns_helper.gno b/gov/gns_helper.gno index 78a79f01..4bbb3282 100644 --- a/gov/gns_helper.gno +++ b/gov/gns_helper.gno @@ -16,10 +16,6 @@ func transfer(to std.Address, amount uint64) { gns.Transfer(a2u(to), amount) } -func mint(recipient std.Address, amount uint64) { - gns.Mint(a2u(recipient), amount) -} - func a2u(address std.Address) pusers.AddressOrName { return pusers.AddressOrName(address) } diff --git a/gov/proposal_mint.gno b/gov/proposal_mint.gno index 9ab75efb..c7df4477 100644 --- a/gov/proposal_mint.gno +++ b/gov/proposal_mint.gno @@ -37,7 +37,7 @@ func (p ProposalMint) execute() error { return errors.New("unimplemented: only GNS token is supported") } - mint(p.Recipient, p.Amount) + // mint(p.Recipient, p.Amount) return nil } From 3f9715682e482c6075cedb22b7f2625cd7ddaaaa Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 15 May 2024 21:31:56 +0900 Subject: [PATCH 05/10] feat: apply emit & event in pool --- pool/_TEST_/_TEST_0_INIT_FAUCET_test.gno | 25 ---- .../_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno | 3 +- pool/_TEST_/_TEST_pool_all_test.gnoA | 111 ++++++++++++++ .../_TEST_pool_dryswap_and_swap_test.gnoA | 18 +-- ...st.gn => _TEST_pool_multi_token_test.gnoA} | 137 +++++++++--------- pool/_TEST_/_TEST_pool_native_swap_test.gn | 135 ----------------- pool/_TEST_/_TEST_pool_native_swap_test.gnoA | 135 +++++++++++++++++ ...test.gn => _TEST_pool_single_lp_test.gnoA} | 117 ++++++++------- ..._TEST_pool_test.gn => _TEST_pool_test.gno} | 38 +++-- pool/_TEST_/z0_rpc_filetest.gno | 40 +---- pool/_TEST_/z1_single_lp_filetest.gno | 63 +++----- pool/_TEST_/z2_native_swap_filetest.gno | 41 ++---- pool/_TEST_/z3_dryswap_and_swap_filetest.gno | 31 +--- pool/pool.gno | 83 ++++++++++- pool/pool_manager.gno | 12 +- pool/utils.gno | 29 ++++ pool/withdrawal_fee.gno | 18 +++ 17 files changed, 587 insertions(+), 449 deletions(-) delete mode 100644 pool/_TEST_/_TEST_0_INIT_FAUCET_test.gno create mode 100644 pool/_TEST_/_TEST_pool_all_test.gnoA rename pool/_TEST_/{_TEST_pool_multi_token_test.gn => _TEST_pool_multi_token_test.gnoA} (67%) delete mode 100644 pool/_TEST_/_TEST_pool_native_swap_test.gn create mode 100644 pool/_TEST_/_TEST_pool_native_swap_test.gnoA rename pool/_TEST_/{_TEST_pool_single_lp_test.gn => _TEST_pool_single_lp_test.gnoA} (66%) rename pool/_TEST_/{_TEST_pool_test.gn => _TEST_pool_test.gno} (76%) 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_VARIABLE_AND_HELPER_test.gno b/pool/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno index 7ec3977a..5272a42f 100644 --- a/pool/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno +++ b/pool/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno @@ -8,8 +8,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" diff --git a/pool/_TEST_/_TEST_pool_all_test.gnoA b/pool/_TEST_/_TEST_pool_all_test.gnoA new file mode 100644 index 00000000..6424bd82 --- /dev/null +++ b/pool/_TEST_/_TEST_pool_all_test.gnoA @@ -0,0 +1,111 @@ +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) { + std.TestSetPrevAddr(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + CreatePool(fooPath, barPath, fee500, "130621891405341611593710811006") // tick 10000 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + shouldEQ(t, len(pools), 1) + std.TestSkipHeights(1) +} + +// 2. Create Bar:Baz Pool +func TestCreateBarBazPool(t *testing.T) { + std.TestSetPrevAddr(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick 10000 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500}]} + shouldEQ(t, len(pools), 2) + std.TestSkipHeights(1) +} + +// 3. Mint Foo:Bar Liquidity by gsa +func TestMintFooBarLiquidity(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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(gsa) + amount0, amount1 := Mint( + barPath, + fooPath, + fee500, + consts.POSITION_ADDR.String(), + -11000, // -11000 + -9000, // -9000 + test_liquidityExpectStr, + ) + shouldEQ(t, amount0, "8040316") + shouldEQ(t, amount1, "2958015") + std.TestSkipHeights(1) +} + +// 4. Mint Bar:Baz Liquidity by gsa +func TestMintBarBazLiquidity(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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(gsa) + amount0, amount1 := Mint( + barPath, + bazPath, + fee500, + consts.POSITION_ADDR.String(), + test_tickLower, + test_tickUpper, + test_liquidityExpectStr, + ) + shouldEQ(t, amount0, "2958015") + shouldEQ(t, amount1, "8040316") + 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_/_TEST_pool_dryswap_and_swap_test.gnoA b/pool/_TEST_/_TEST_pool_dryswap_and_swap_test.gnoA index 3a1e3f4d..65860a96 100644 --- a/pool/_TEST_/_TEST_pool_dryswap_and_swap_test.gnoA +++ b/pool/_TEST_/_TEST_pool_dryswap_and_swap_test.gnoA @@ -24,41 +24,37 @@ var ( 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() + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) CreatePool(fooPath, barPath, fee500, sqrtPrice) + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} shouldEQ(t, len(pools), 1) } func TestDrySwap_ZeroForOneTrue_AmountSpecified_Positive_16000(t *testing.T) { std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) // 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) + std.TestSetPrevAddr(gsa) 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) + std.TestSetOrigCaller(gsa) 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.TestSetPrevAddr(gsa) std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) Mint(fooPath, barPath, fee500, consts.POSITION_ADDR.String(), -tickUpper, -tickLower, liquidityExpect) // -11000 ~ -9000 // zeroForOne true diff --git a/pool/_TEST_/_TEST_pool_multi_token_test.gn b/pool/_TEST_/_TEST_pool_multi_token_test.gnoA similarity index 67% rename from pool/_TEST_/_TEST_pool_multi_token_test.gn rename to pool/_TEST_/_TEST_pool_multi_token_test.gnoA index e67271a1..59f09c37 100644 --- a/pool/_TEST_/_TEST_pool_multi_token_test.gn +++ b/pool/_TEST_/_TEST_pool_multi_token_test.gnoA @@ -26,43 +26,35 @@ var ( // 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.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) CreatePool(fooPath, barPath, fee500, "130621891405341611593710811006") + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} 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() + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500}]} shouldEQ(t, len(pools), 2) } -// Mint Foo:Bar Liquidity by test1 +// Mint Foo:Bar Liquidity by gsa func TestMintFooBarLiquidity(t *testing.T) { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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) + std.TestSetOrigCaller(gsa) amount0, amount1 := Mint( fooPath, barPath, @@ -73,17 +65,17 @@ func TestMintFooBarLiquidity(t *testing.T) { test_liquidityExpectStr, ) shouldEQ(t, amount0, "8040316") - shouldEQ(t, amount1, "2958014") + shouldEQ(t, amount1, "2958015") } -// Mint Bar:Baz Liquidity by test1 +// Mint Bar:Baz Liquidity by gsa func TestMintBarBazLiquidity(t *testing.T) { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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) + std.TestSetOrigCaller(gsa) amount0, amount1 := Mint( barPath, bazPath, @@ -94,37 +86,38 @@ func TestMintBarBazLiquidity(t *testing.T) { test_liquidityExpectStr, ) shouldEQ(t, amount0, "2958015") - shouldEQ(t, amount1, "8040315") + shouldEQ(t, amount1, "8040316") } -// Swap Foo:Bar Foo > Bar by test1 +// Swap Foo:Bar Foo > Bar by gsa func TestSwapFooBarFooToBar(t *testing.T) { - oldTest1Bar := balanceOfByRegisterCall(barPath, test1) - oldTest1Foo := balanceOfByRegisterCall(fooPath, test1) + oldTest1Bar := balanceOfByRegisterCall(barPath, gsa) + oldTest1Foo := balanceOfByRegisterCall(fooPath, gsa) oldPoolBar := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) oldPoolFoo := balanceOfByRegisterCall(fooPath, consts.POOL_ADDR) - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), 16000) std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) poolIn, poolOut := Swap( barPath, fooPath, fee500, - test1.String(), + gsa.String(), true, "16000", consts.MIN_PRICE, std.GetOrigCaller().String(), ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_zeroForOne true} {p_amountSpecified 16000} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 16000} {amount1 -5882} {protocol_fee0 0} {protocol_fee1 0}]} shouldEQ(t, poolIn, "16000") shouldEQ(t, poolOut, "-5882") - newTest1Bar := balanceOfByRegisterCall(barPath, test1) - newTest1Foo := balanceOfByRegisterCall(fooPath, test1) + newTest1Bar := balanceOfByRegisterCall(barPath, gsa) + newTest1Foo := balanceOfByRegisterCall(fooPath, gsa) newPoolBar := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) newPoolFoo := balanceOfByRegisterCall(fooPath, consts.POOL_ADDR) @@ -136,49 +129,51 @@ func TestSwapFooBarFooToBar(t *testing.T) { shouldEQ(t, oldPoolFoo-newPoolFoo, 5882) } -// Swap Bar:Baz Bar > Baz by test1 +// Swap Bar:Baz Bar > Baz by gsa func TestSwapBarBazBarToBaz(t *testing.T) { - oldTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - oldtest1BazBalance := balanceOfByRegisterCall(bazPath, test1) + oldTest1Token0Balance := balanceOfByRegisterCall(barPath, gsa) + oldgsaBazBalance := balanceOfByRegisterCall(bazPath, gsa) oldPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) oldPoolBazBalance := balanceOfByRegisterCall(bazPath, consts.POOL_ADDR) - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), 16000) std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) poolIn, poolOut := Swap( barPath, bazPath, fee500, - test1.String(), + gsa.String(), true, "16000", consts.MIN_PRICE, std.GetOrigCaller().String(), ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne true} {p_amountSpecified 16000} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 16000} {amount1 -43457} {protocol_fee0 0} {protocol_fee1 0}]} + shouldEQ(t, poolIn, "16000") shouldEQ(t, poolOut, "-43457") - newTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - newtest1BazBalance := balanceOfByRegisterCall(bazPath, test1) + newTest1Token0Balance := balanceOfByRegisterCall(barPath, gsa) + newgsaBazBalance := balanceOfByRegisterCall(bazPath, gsa) newPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) newPoolBazBalance := balanceOfByRegisterCall(bazPath, consts.POOL_ADDR) shouldEQ(t, oldTest1Token0Balance-newTest1Token0Balance, 16000) - shouldEQ(t, newtest1BazBalance-oldtest1BazBalance, 43457) + shouldEQ(t, newgsaBazBalance-oldgsaBazBalance, 43457) shouldEQ(t, newPoolToken0Balance-oldPoolToken0Balance, 16000) shouldEQ(t, oldPoolBazBalance-newPoolBazBalance, 43457) } -// Collect Foo:Bar Fees by test1 +// Collect Foo:Bar Fees by gsa func TestCollectFooBarFees(t *testing.T) { std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) - oldTest1Token1Balance := balanceOfByRegisterCall(fooPath, test1) - oldTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) + oldTest1Token1Balance := balanceOfByRegisterCall(fooPath, gsa) + oldTest1Token0Balance := balanceOfByRegisterCall(barPath, gsa) oldPoolToken1Balance := balanceOfByRegisterCall(fooPath, consts.POOL_ADDR) oldPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) @@ -189,7 +184,7 @@ func TestCollectFooBarFees(t *testing.T) { fooPath, barPath, fee500, - test1.String(), + gsa.String(), -test_tickUpper, -test_tickLower, "100000", @@ -199,8 +194,8 @@ func TestCollectFooBarFees(t *testing.T) { 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) + newTest1Token1Balance := balanceOfByRegisterCall(fooPath, gsa) + newTest1Token0Balance := balanceOfByRegisterCall(barPath, gsa) newPoolToken1Balance := balanceOfByRegisterCall(fooPath, consts.POOL_ADDR) newPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) @@ -210,13 +205,13 @@ func TestCollectFooBarFees(t *testing.T) { shouldEQ(t, oldPoolToken0Balance-newPoolToken0Balance, strToUint64(c0)) } -// Collect Bar:Baz Fees by test1 +// Collect Bar:Baz Fees by gsa func TestCollectBarBazFees(t *testing.T) { std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) - oldTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - oldtest1BazBalance := balanceOfByRegisterCall(bazPath, test1) + oldTest1Token0Balance := balanceOfByRegisterCall(barPath, gsa) + oldgsaBazBalance := balanceOfByRegisterCall(bazPath, gsa) oldPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) oldPoolBazBalance := balanceOfByRegisterCall(bazPath, consts.POOL_ADDR) @@ -227,7 +222,7 @@ func TestCollectBarBazFees(t *testing.T) { barPath, bazPath, fee500, - test1.String(), + gsa.String(), test_tickLower, test_tickUpper, "100000", @@ -237,20 +232,20 @@ func TestCollectBarBazFees(t *testing.T) { 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) + newTest1Token0Balance := balanceOfByRegisterCall(barPath, gsa) + newgsaBazBalance := balanceOfByRegisterCall(bazPath, gsa) 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, newgsaBazBalance-oldgsaBazBalance, c1) shouldEQ(t, oldPoolToken0Balance-newPoolToken0Balance, strToUint64(c0)) shouldEQ(t, oldPoolBazBalance-newPoolBazBalance, c1) } -// Burn Foo:Bar Liquidity by test1 +// Burn Foo:Bar Liquidity by gsa func TestBurnFooBarLiquidity(t *testing.T) { - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) std.TestSetPrevRealm(consts.POSITION_PATH) pool := GetPool(fooPath, barPath, fee500) @@ -273,9 +268,9 @@ func TestBurnFooBarLiquidity(t *testing.T) { shouldEQ(t, true, new(u256.Uint).Sub(poolOldLiquidity, poolNewLiquidity).Eq(test_liquidityExpect256)) } -// Burn Bar:Baz Liquidity by test1 +// Burn Bar:Baz Liquidity by gsa func TestBurnBarBazLiquidity(t *testing.T) { - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) std.TestSetPrevRealm(consts.POSITION_PATH) pool := GetPool(barPath, bazPath, fee500) @@ -298,13 +293,13 @@ func TestBurnBarBazLiquidity(t *testing.T) { // shouldEQ(t, poolOldLiquidity-poolNewLiquidity, test_liquidityExpect256) } -// Collect Foo:Bar burned Liquidity by test1 +// Collect Foo:Bar burned Liquidity by gsa func TestCollectFooBarLiquidity(t *testing.T) { - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) std.TestSetPrevRealm(consts.POSITION_PATH) - oldTest1Token1Balance := balanceOfByRegisterCall(fooPath, test1) - oldTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) + oldTest1Token1Balance := balanceOfByRegisterCall(fooPath, gsa) + oldTest1Token0Balance := balanceOfByRegisterCall(barPath, gsa) oldPoolToken1Balance := balanceOfByRegisterCall(fooPath, consts.POOL_ADDR) oldPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) @@ -312,7 +307,7 @@ func TestCollectFooBarLiquidity(t *testing.T) { fooPath, barPath, fee500, - test1.String(), + gsa.String(), -test_tickUpper, -test_tickLower, "100000", @@ -322,8 +317,8 @@ func TestCollectFooBarLiquidity(t *testing.T) { shouldNEQ(t, c0, "0") shouldNEQ(t, c1, "0") - newTest1Token1Balance := balanceOfByRegisterCall(fooPath, test1) - newTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) + newTest1Token1Balance := balanceOfByRegisterCall(fooPath, gsa) + newTest1Token0Balance := balanceOfByRegisterCall(barPath, gsa) newPoolToken1Balance := balanceOfByRegisterCall(fooPath, consts.POOL_ADDR) newPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) @@ -333,13 +328,13 @@ func TestCollectFooBarLiquidity(t *testing.T) { shouldEQ(t, oldPoolToken0Balance-newPoolToken0Balance, strToUint64(c1)) } -// Collect Bar:Baz burned Liquidity by test1 +// Collect Bar:Baz burned Liquidity by gsa func TestCollectBarBazLiquidity(t *testing.T) { - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) std.TestSetPrevRealm(consts.POSITION_PATH) - oldTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - oldtest1BazBalance := balanceOfByRegisterCall(bazPath, test1) + oldTest1Token0Balance := balanceOfByRegisterCall(barPath, gsa) + oldgsaBazBalance := balanceOfByRegisterCall(bazPath, gsa) oldPoolToken0Balance := balanceOfByRegisterCall(barPath, consts.POOL_ADDR) oldPoolBazBalance := balanceOfByRegisterCall(bazPath, consts.POOL_ADDR) @@ -347,7 +342,7 @@ func TestCollectBarBazLiquidity(t *testing.T) { barPath, bazPath, fee500, - test1.String(), + gsa.String(), test_tickLower, test_tickUpper, "100000", @@ -357,13 +352,13 @@ func TestCollectBarBazLiquidity(t *testing.T) { shouldNEQ(t, c0, "0") shouldNEQ(t, c1, "0") - newTest1Token0Balance := balanceOfByRegisterCall(barPath, test1) - newtest1BazBalance := balanceOfByRegisterCall(bazPath, test1) + newTest1Token0Balance := balanceOfByRegisterCall(barPath, gsa) + newgsaBazBalance := balanceOfByRegisterCall(bazPath, gsa) 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, newgsaBazBalance-oldgsaBazBalance, strToUint64(c1)) shouldEQ(t, oldPoolToken0Balance-newPoolToken0Balance, strToUint64(c0)) shouldEQ(t, oldPoolBazBalance-newPoolBazBalance, strToUint64(c1)) } 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_native_swap_test.gnoA b/pool/_TEST_/_TEST_pool_native_swap_test.gnoA new file mode 100644 index 00000000..367bbc1f --- /dev/null +++ b/pool/_TEST_/_TEST_pool_native_swap_test.gnoA @@ -0,0 +1,135 @@ +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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + CreatePool(fooPath, consts.WRAPPED_WUGNOT, fee500, "130621891405341611593710811006") // x2.7 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/foo:gno.land/r/demo/wugnot:500}]} +} + +// 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(gsa, "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(gsa) + wugnot.Deposit() + + std.TestSetPrevAddr(gsa) + 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, "804032") +} + +func TestSwapBuyNative(t *testing.T) { + pool := GetPool(fooPath, consts.WRAPPED_WUGNOT, fee500) + + gsaOldT0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaOldT1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) + shouldEQ(t, gsaOldT0Bal, 99999999704198) // foo + shouldEQ(t, gsaOldT1Bal, 0) // wugnot + + std.TestSetPrevAddr(gsa) + foo.Approve(a2u(consts.POOL_ADDR), 10000) + + std.TestSetPrevRealm(consts.ROUTER_PATH) + std.TestSetOrigCaller(gsa) + amount0, amount1 := Swap( + fooPath, // token0 + consts.WRAPPED_WUGNOT, // token1 + fee500, + gsa.String(), + true, + "10000", + consts.MIN_PRICE, + std.GetOrigCaller().String(), + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/foo:gno.land/r/demo/wugnot:500} {p_zeroForOne true} {p_amountSpecified 10000} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 10000} {amount1 -27123} {protocol_fee0 0} {protocol_fee1 0}]} + + shouldEQ(t, amount0, "10000") + shouldEQ(t, amount1, "-27123") + + gsaNewT0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaNewT1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) + shouldEQ(t, gsaNewT0Bal, 99999999694198) // - 10_000 // user sells 10_000 + shouldEQ(t, gsaNewT1Bal, 27123) // gets WGNOT +} + +func TestSwapSellNative(t *testing.T) { + pool := GetPool(fooPath, consts.WRAPPED_WUGNOT, fee500) + + gsaOldT0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaOldT1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) + shouldEQ(t, gsaOldT0Bal, 99999999694198) // foo + shouldEQ(t, gsaOldT1Bal, 27123) // wugnot + + std.TestSetPrevAddr(gsa) + wugnot.Approve(a2u(consts.POOL_ADDR), 27123) + + std.TestSetPrevRealm(consts.ROUTER_PATH) + std.TestSetOrigCaller(gsa) + amount0, amount1 := Swap( // ugnot 10_000 > foo ?? + fooPath, // token0 + consts.WRAPPED_WUGNOT, // token1 + fee500, + gsa.String(), + false, + "10000", + consts.MAX_PRICE, + std.GetOrigCaller().String(), + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/foo:gno.land/r/demo/wugnot:500} {p_zeroForOne false} {p_amountSpecified 10000} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 -3687} {amount1 10000} {protocol_fee0 0} {protocol_fee1 0}]} + + shouldEQ(t, amount0, "-3687") // pool sent + shouldEQ(t, amount1, "10000") // pool recv + + gsaNewT0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaNewT1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) + shouldEQ(t, gsaNewT0Bal, 99999999697885) // foo + shouldEQ(t, gsaNewT1Bal, 17123) // wugnot +} diff --git a/pool/_TEST_/_TEST_pool_single_lp_test.gn b/pool/_TEST_/_TEST_pool_single_lp_test.gnoA similarity index 66% rename from pool/_TEST_/_TEST_pool_single_lp_test.gn rename to pool/_TEST_/_TEST_pool_single_lp_test.gnoA index 342455a2..fb3da4e3 100644 --- a/pool/_TEST_/_TEST_pool_single_lp_test.gn +++ b/pool/_TEST_/_TEST_pool_single_lp_test.gnoA @@ -29,26 +29,22 @@ var ( // 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() + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) CreatePool(barPath, fooPath, fee500, "130621891405341611593710811006") // x2.7 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} } -// 2. Mint by test1 +// 2. Mint by gsa func TestMint(t *testing.T) { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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) + std.TestSetOrigCaller(gsa) amount0, amount1 := Mint( fooPath, barPath, @@ -59,7 +55,7 @@ func TestMint(t *testing.T) { test_liquidityExpectStr, ) shouldEQ(t, amount0, "30") - shouldEQ(t, amount1, "80") + shouldEQ(t, amount1, "81") pool := GetPool(barPath, fooPath, fee500) test_liquidity := pool.PoolGetLiquidity() @@ -112,10 +108,10 @@ func TestMint(t *testing.T) { } -// 3. Burn by test1 +// 3. Burn by gsa func TestBurn(t *testing.T) { std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) b11, b12 := Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, test_liquidityExpectStr) b21, b22 := Burn(barPath, fooPath, fee500, test_tickLower, test_tickUpper, test_liquidityExpectStr) @@ -146,10 +142,10 @@ func TestBurn(t *testing.T) { // 4. Collect func TestCollect(t *testing.T) { std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) // withdraw all token before test `Collect` - Collect(barPath, fooPath, fee500, test1.String(), test_tickLower, test_tickUpper, "50000000", "50000000") + Collect(barPath, fooPath, fee500, gsa.String(), test_tickLower, test_tickUpper, "50000000", "50000000") // pool should have zero liquidity pool := GetPool(barPath, fooPath, fee500) @@ -158,70 +154,72 @@ func TestCollect(t *testing.T) { 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") + c11, c12 := Collect(barPath, fooPath, fee500, gsa.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") + c21, c22 := Collect(barPath, fooPath, fee500, gsa.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") + c31, c32 := Collect(barPath, fooPath, fee500, gsa.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") + c41, c42 := Collect(barPath, fooPath, fee500, gsa.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") + c51, c52 := Collect(barPath, fooPath, fee500, gsa.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") + c61, c62 := Collect(barPath, fooPath, fee500, gsa.String(), test_tickLower, test_tickUpper, "50000000", "50000000") shouldNEQ(t, c61, "0") shouldNEQ(t, c62, "0") } -// 5. Swap by test1 +// 5. Swap by gsa 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) + std.TestSetOrigCaller(gsa) 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) + std.TestSetOrigCaller(gsa) 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) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) amount0, amount1 := Swap( fooPath, // token0Path barPath, // token1Path fee500, // fee - test1.String(), // recipient + gsa.String(), // recipient true, // zeroForOne "10000", // _amountSpecified test_price, // _sqrtPriceLimitX96 std.GetOrigCaller().String(), // payer ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_zeroForOne true} {p_amountSpecified 10000} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 10000} {amount1 -27145} {protocol_fee0 0} {protocol_fee1 0}]} + shouldNEQ(t, amount0, "0") shouldNEQ(t, amount1, "0") intAmount0, _ := strconv.Atoi(amount0) @@ -230,8 +228,8 @@ func TestSwap(t *testing.T) { poolNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, consts.POOL_ADDR) poolNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, consts.POOL_ADDR) - userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(intAmount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-intAmount1)) @@ -243,18 +241,20 @@ func TestSwap(t *testing.T) { poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, consts.POOL_ADDR) poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, consts.POOL_ADDR) - userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) + + amount0, amount1 := Swap(barPath, fooPath, fee500, gsa.String(), true, "5000", test_price, std.GetOrigCaller().String()) // give enough amount to take fees away + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_zeroForOne true} {p_amountSpecified 5000} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 5000} {amount1 -13554} {protocol_fee0 0} {protocol_fee1 0}]} - 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) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(intAmount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-intAmount1)) @@ -266,18 +266,20 @@ func TestSwap(t *testing.T) { poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, consts.POOL_ADDR) poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, consts.POOL_ADDR) - userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) + + amount0, amount1 := Swap(barPath, fooPath, fee500, gsa.String(), true, "1000", test_price, std.GetOrigCaller().String()) // give enough amount to take fees away + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_zeroForOne true} {p_amountSpecified 1000} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 1000} {amount1 -2708} {protocol_fee0 0} {protocol_fee1 0}]} - 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) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(intAmount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-intAmount1)) @@ -290,18 +292,20 @@ func TestSwap(t *testing.T) { poolOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, consts.POOL_ADDR) poolOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, consts.POOL_ADDR) - userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) + userOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + userOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) + + amount0, amount1 := Swap(barPath, fooPath, fee500, gsa.String(), false, "16000", consts.MAX_PRICE, std.GetOrigCaller().String()) // give enough amount to take fees away + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_zeroForOne false} {p_amountSpecified 16000} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 -5896} {amount1 16000} {protocol_fee0 0} {protocol_fee1 0}]} - 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) + userNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + userNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) shouldEQ(t, userOldToken0Bal-userNewToken0Bal, int64(intAmount0)) shouldEQ(t, userNewToken1Bal-userOldToken1Bal, int64(-intAmount1)) @@ -319,6 +323,7 @@ func TestSetFeeProtocol(t *testing.T) { // admin call std.TestSetOrigCaller(gsa) SetFeeProtocol(6, 8) + // {GNOSWAP gno.land/r/demo/pool SetFeeProtocol [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_feeProtocol0 6} {p_feeProtocol1 8} {newFee 134}]} for _, pool := range pools { shouldEQ(t, pool.PoolGetSlot0().feeProtocol, 134) @@ -329,29 +334,33 @@ func TestSetFeeProtocol(t *testing.T) { func TestCollectProtocol(t *testing.T) { std.TestSetOrigCaller(gsa) SetFeeProtocol(6, 8) + // event: {GNOSWAP gno.land/r/demo/pool SetFeeProtocol [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_feeProtocol0 6} {p_feeProtocol1 8} {newFee 134}]} + 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) + // Make ProtocolFee via Swap by gsa ( Mint by gsa ) + std.TestSetOrigCaller(gsa) { 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 + std.TestSetOrigCaller(gsa) + Swap(barPath, fooPath, fee500, gsa.String(), true, "200000", consts.MIN_PRICE, std.GetOrigCaller().String()) // swap token0 -> token1 => fee only in token0 + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_zeroForOne true} {p_amountSpecified 200000} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 200000} {amount1 -533667} {protocol_fee0 16} {protocol_fee1 0}]} - test1OldToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - test1OldToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) + gsaOldToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaOldToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) std.TestSetPrevAddr(gsa) - amount0, amount1 := CollectProtocol(barPath, fooPath, fee500, test1.String(), "100000", "100000") + amount0, amount1 := CollectProtocol(barPath, fooPath, fee500, gsa.String(), "100000", "100000") + // event: {GNOSWAP gno.land/r/demo/pool CollectProtocol [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 15} {amount1 0}]} - test1NewToken0Bal := balanceOfByRegisterCall(pool.token0Path, test1) - test1NewToken1Bal := balanceOfByRegisterCall(pool.token1Path, test1) + gsaNewToken0Bal := balanceOfByRegisterCall(pool.token0Path, gsa) + gsaNewToken1Bal := balanceOfByRegisterCall(pool.token1Path, gsa) - _token0Diff := test1NewToken0Bal - test1OldToken0Bal - _token1Diff := test1NewToken1Bal - test1OldToken1Bal + _token0Diff := gsaNewToken0Bal - gsaOldToken0Bal + _token1Diff := gsaNewToken1Bal - gsaOldToken1Bal token0DiffStr := strconv.Itoa(int(_token0Diff)) token1DiffStr := strconv.Itoa(int(_token1Diff)) diff --git a/pool/_TEST_/_TEST_pool_test.gn b/pool/_TEST_/_TEST_pool_test.gno similarity index 76% rename from pool/_TEST_/_TEST_pool_test.gn rename to pool/_TEST_/_TEST_pool_test.gno index cebbcd70..b51c1463 100644 --- a/pool/_TEST_/_TEST_pool_test.gn +++ b/pool/_TEST_/_TEST_pool_test.gno @@ -24,18 +24,16 @@ var ( // 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.TestSetPrevAddr(gsa) + std.TestSkipHeights(5) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) std.TestSkipHeights(1) CreatePool(fooPath, barPath, fee500, "130621891405341611593710811006") // tick 10000 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + shouldEQ(t, len(pools), 1) std.TestSkipHeights(1) } @@ -43,32 +41,30 @@ func TestCreateFooBarPool(t *testing.T) { // 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.TestSetPrevAddr(gsa) + std.TestSetPrevAddr(gsa) + std.TestSkipHeights(5) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) std.TestSkipHeights(1) CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick 10000 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500}]} + shouldEQ(t, len(pools), 2) std.TestSkipHeights(1) } -// 3. Mint Foo:Bar Liquidity by test1 +// 3. Mint Foo:Bar Liquidity by gsa func TestMintFooBarLiquidity(t *testing.T) { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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) + std.TestSetOrigCaller(gsa) amount0, amount1 := Mint( barPath, fooPath, @@ -79,19 +75,19 @@ func TestMintFooBarLiquidity(t *testing.T) { test_liquidityExpectStr, ) shouldEQ(t, amount0, "8040316") - shouldEQ(t, amount1, "2958014") + shouldEQ(t, amount1, "2958015") std.TestSkipHeights(1) } -// 4. Mint Bar:Baz Liquidity by test1 +// 4. Mint Bar:Baz Liquidity by gsa func TestMintBarBazLiquidity(t *testing.T) { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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) + std.TestSetOrigCaller(gsa) amount0, amount1 := Mint( barPath, bazPath, @@ -102,7 +98,7 @@ func TestMintBarBazLiquidity(t *testing.T) { test_liquidityExpectStr, ) shouldEQ(t, amount0, "2958015") - shouldEQ(t, amount1, "8040315") + shouldEQ(t, amount1, "8040316") std.TestSkipHeights(1) } diff --git a/pool/_TEST_/z0_rpc_filetest.gno b/pool/_TEST_/z0_rpc_filetest.gno index c05f4452..b60be435 100644 --- a/pool/_TEST_/z0_rpc_filetest.gno +++ b/pool/_TEST_/z0_rpc_filetest.gno @@ -21,8 +21,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -150,16 +149,7 @@ func (GNSToken) Approve() func(spender pusers.AddressOrName, amount uint64) { } func init() { - // FAUCET - std.TestSetPrevAddr(test1) - - for i := 0; i < 100; i++ { - foo.Faucet() - bar.Faucet() - baz.Faucet() - qux.Faucet() - obl.Faucet() - } + std.TestSetPrevAddr(gsa) // TOKEN REGISTER std.TestSetPrevAddr(gsa) @@ -183,14 +173,7 @@ func main() { } func createFooBarPool() { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - std.TestSkipHeights(5) + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) std.TestSkipHeights(1) @@ -203,14 +186,7 @@ func createFooBarPool() { } func createBarBazPool() { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - std.TestSkipHeights(5) + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) std.TestSkipHeights(1) @@ -223,13 +199,13 @@ func createBarBazPool() { } func mintFooBar() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) std.TestSkipHeights(2) //std.TestSetPrevRealm(consts.POSITION_PATH) - //std.TestSetOrigCaller(test1) + //std.TestSetOrigCaller(gsa) amount0, amount1 := pl.Mint( barPath, fooPath, @@ -248,13 +224,13 @@ func mintFooBar() { } func mintBarBaz() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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) + //std.TestSetOrigCaller(gsa) amount0, amount1 := pl.Mint( barPath, bazPath, diff --git a/pool/_TEST_/z1_single_lp_filetest.gno b/pool/_TEST_/z1_single_lp_filetest.gno index 38994a7b..ebdfcdfe 100644 --- a/pool/_TEST_/z1_single_lp_filetest.gno +++ b/pool/_TEST_/z1_single_lp_filetest.gno @@ -21,8 +21,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -166,16 +165,7 @@ func ugnotBalanceOf(addr std.Address) uint64 { } func init() { - // FAUCET - std.TestSetPrevAddr(test1) - - for i := 0; i < 100; i++ { - foo.Faucet() - bar.Faucet() - baz.Faucet() - qux.Faucet() - obl.Faucet() - } + std.TestSetPrevAddr(gsa) // TOKEN REGISTER std.TestSetPrevAddr(gsa) @@ -201,14 +191,7 @@ func main() { } func createBarFooPool() { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - gns.Faucet() - std.TestSkipHeights(5) + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) std.TestSkipHeights(1) @@ -220,13 +203,13 @@ func createBarFooPool() { std.TestSkipHeights(1) } func mint() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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) + std.TestSetOrigCaller(gsa) amount0, amount1 := pl.Mint( fooPath, barPath, @@ -305,7 +288,7 @@ func mint() { } func burn() { std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) b11, b12 := pl.Burn(barPath, fooPath, fee500, 9000, 11000, "1000") b21, b22 := pl.Burn(barPath, fooPath, fee500, 9000, 11000, "1000") @@ -345,10 +328,10 @@ func burn() { } func collect() { std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) // withdraw all token before test `Collect` - pl.Collect(barPath, fooPath, fee500, test1.String(), 9000, 11000, "50000000", "50000000") + pl.Collect(barPath, fooPath, fee500, gsa.String(), 9000, 11000, "50000000", "50000000") pool := pl.GetPool(barPath, fooPath, fee500) testLiquidity := pool.PoolGetLiquidity() @@ -358,11 +341,11 @@ func collect() { pl.Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), 9000, 11000, "15000") pl.Burn(barPath, fooPath, fee500, 9000, 11000, "15000") - c11, c12 := pl.Collect(barPath, fooPath, fee500, test1.String(), 9000, 11000, "50000000", "50000000") + c11, c12 := pl.Collect(barPath, fooPath, fee500, gsa.String(), 9000, 11000, "50000000", "50000000") pl.Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), 9000, 11000, "15000") pl.Burn(barPath, fooPath, fee500, 9000, 11000, "15000") - c21, c22 := pl.Collect(barPath, fooPath, fee500, test1.String(), 9000, 11000, "50000000", "50000000") + c21, c22 := pl.Collect(barPath, fooPath, fee500, gsa.String(), 9000, 11000, "50000000", "50000000") if c11 != c21 || c12 != c22 { panic("collect foo bar 1") @@ -374,12 +357,12 @@ func collect() { pl.Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), 9000, 11000, "15000") pl.Burn(barPath, fooPath, fee500, 9000, 11000, "15000") - c31, c32 := pl.Collect(barPath, fooPath, fee500, test1.String(), 9000, 11000, "100", "100") + c31, c32 := pl.Collect(barPath, fooPath, fee500, gsa.String(), 9000, 11000, "100", "100") if c31 != "100" || c32 != "100" { panic("collect foo bar 3") } - c41, c42 := pl.Collect(barPath, fooPath, fee500, test1.String(), 9000, 11000, "50000000", "50000000") + c41, c42 := pl.Collect(barPath, fooPath, fee500, gsa.String(), 9000, 11000, "50000000", "50000000") if c41 != "343" || c42 != "1106" { panic("collect foo bar 4") } @@ -387,14 +370,14 @@ func collect() { // Just Mint > No Burn => nothing to collect pl.Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), 9000, 11000, "15000") // Burn(barPath, fooPath, fee500, 9000, 11000, "15000") - c51, c52 := pl.Collect(barPath, fooPath, fee500, test1.String(), 9000, 11000, "50000000", "50000000") + c51, c52 := pl.Collect(barPath, fooPath, fee500, gsa.String(), 9000, 11000, "50000000", "50000000") if c51 != "0" || c52 != "0" { panic("collect foo bar 5") } // Burn Now => something to collect pl.Burn(barPath, fooPath, fee500, 9000, 11000, "15000") - c61, c62 := pl.Collect(barPath, fooPath, fee500, test1.String(), 9000, 11000, "50000000", "50000000") + c61, c62 := pl.Collect(barPath, fooPath, fee500, gsa.String(), 9000, 11000, "50000000", "50000000") if c61 == "0" || c62 == "0" { panic("collect foo bar 6") } @@ -407,17 +390,17 @@ func swap() { } std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) pl.Mint(barPath, fooPath, fee500, consts.POSITION_ADDR.String(), 9000, 11000, "2000000") std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) { amount0, amount1 := pl.Swap( fooPath, // token0Path barPath, // token1Path fee500, // fee - test1.String(), // recipient + gsa.String(), // recipient true, // zeroForOne "10000", // _amountSpecified consts.MIN_PRICE, // _sqrtPriceLimitX96 @@ -429,14 +412,14 @@ func swap() { } { - amount0, amount1 := pl.Swap(barPath, fooPath, fee500, test1.String(), true, "5000", consts.MIN_PRICE, std.GetOrigCaller().String()) // give enough amount to take fees away + amount0, amount1 := pl.Swap(barPath, fooPath, fee500, gsa.String(), true, "5000", consts.MIN_PRICE, std.GetOrigCaller().String()) // give enough amount to take fees away if amount0 != "5000" || amount1 != "-13307" { panic("swap foo bar 2") } } { - amount0, amount1 := pl.Swap(barPath, fooPath, fee500, test1.String(), true, "1000", consts.MIN_PRICE, std.GetOrigCaller().String()) // give enough amount to take fees away + amount0, amount1 := pl.Swap(barPath, fooPath, fee500, gsa.String(), true, "1000", consts.MIN_PRICE, std.GetOrigCaller().String()) // give enough amount to take fees away if amount0 != "1000" || amount1 != "-2647" { panic("swap foo bar 3") } @@ -444,7 +427,7 @@ func swap() { // Token0 -> Token1 { - amount0, amount1 := pl.Swap(barPath, fooPath, fee500, test1.String(), false, "16000", consts.MAX_PRICE, std.GetOrigCaller().String()) // give enough amount to take fees away + amount0, amount1 := pl.Swap(barPath, fooPath, fee500, gsa.String(), false, "16000", consts.MAX_PRICE, std.GetOrigCaller().String()) // give enough amount to take fees away if amount0 != "-6010" || amount1 != "16000" { panic("swap foo bar 4") } @@ -467,12 +450,12 @@ func collectProtocol() { pl.SetFeeProtocol(6, 8) std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) pl.Swap( barPath, fooPath, fee500, - test1.String(), + gsa.String(), true, "200000", consts.MIN_PRICE, @@ -480,7 +463,7 @@ func collectProtocol() { ) std.TestSetPrevAddr(gsa) - protocolFee0, protocolFee1 := pl.CollectProtocol(barPath, fooPath, fee500, test1.String(), "100000", "100000") + protocolFee0, protocolFee1 := pl.CollectProtocol(barPath, fooPath, fee500, gsa.String(), "100000", "100000") if protocolFee0 != "8" { panic(protocolFee0) } diff --git a/pool/_TEST_/z2_native_swap_filetest.gno b/pool/_TEST_/z2_native_swap_filetest.gno index 255a29f9..4ed17665 100644 --- a/pool/_TEST_/z2_native_swap_filetest.gno +++ b/pool/_TEST_/z2_native_swap_filetest.gno @@ -20,8 +20,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -168,17 +167,6 @@ func ugnotBalanceOf(addr std.Address) uint64 { } 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{}) @@ -199,14 +187,7 @@ func main() { } 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) + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) std.TestSkipHeights(1) @@ -226,17 +207,17 @@ func mint() { // prepare ugnot testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.IssueCoin(test1, "ugnot", 804_032) + testBanker.IssueCoin(gsa, "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) + std.TestSetPrevAddr(gsa) wugnot.Deposit() - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) foo.Approve(a2u(consts.POOL_ADDR), maxApprove) wugnot.Approve(a2u(consts.POOL_ADDR), maxApprove) @@ -262,16 +243,16 @@ func mint() { func swapBuyNative() { pool := pl.GetPool(fooPath, consts.WRAPPED_WUGNOT, fee500) - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) foo.Approve(a2u(consts.POOL_ADDR), 10000) std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) amount0, amount1 := pl.Swap( fooPath, // token0 consts.WRAPPED_WUGNOT, // token1 fee500, - test1.String(), + gsa.String(), true, "10000", consts.MIN_PRICE, @@ -285,16 +266,16 @@ func swapBuyNative() { func swapSellNative() { pool := pl.GetPool(fooPath, consts.WRAPPED_WUGNOT, fee500) - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) foo.Approve(a2u(consts.POOL_ADDR), 10000) std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) amount0, amount1 := pl.Swap( // ugnot 10_000 > foo ?? fooPath, // token0 consts.WRAPPED_WUGNOT, // token1 fee500, - test1.String(), + gsa.String(), false, "10000", consts.MAX_PRICE, diff --git a/pool/_TEST_/z3_dryswap_and_swap_filetest.gno b/pool/_TEST_/z3_dryswap_and_swap_filetest.gno index 6d09aa6b..3a5e9edc 100644 --- a/pool/_TEST_/z3_dryswap_and_swap_filetest.gno +++ b/pool/_TEST_/z3_dryswap_and_swap_filetest.gno @@ -20,8 +20,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -168,16 +167,7 @@ func ugnotBalanceOf(addr std.Address) uint64 { } func init() { - // FAUCET - std.TestSetPrevAddr(test1) - - for i := 0; i < 100; i++ { - foo.Faucet() - bar.Faucet() - baz.Faucet() - qux.Faucet() - obl.Faucet() - } + std.TestSetPrevAddr(gsa) // TOKEN REGISTER std.TestSetPrevAddr(gsa) @@ -200,14 +190,7 @@ func main() { } 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) + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) std.TestSkipHeights(1) @@ -221,7 +204,7 @@ func createPool() { func drySwap_ZeroForOneTrue_AmountSpecified_Positive_16000() { std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) // no mint == no liquidity => swap will fail _, _, ok := pl.DrySwap(fooPath, barPath, fee500, true, "16000", consts.MIN_PRICE) @@ -230,12 +213,12 @@ func drySwap_ZeroForOneTrue_AmountSpecified_Positive_16000() { } // not enough mint == swap will fail - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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) + std.TestSetOrigCaller(gsa) 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 { @@ -243,7 +226,7 @@ func drySwap_ZeroForOneTrue_AmountSpecified_Positive_16000() { } std.TestSetPrevRealm(consts.POSITION_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) pl.Mint(fooPath, barPath, fee500, consts.POSITION_ADDR.String(), -test_tickUpper, -test_tickLower, "100000000000") // -11000 ~ -9000 // zeroForOne true // amountSpecified 16000 diff --git a/pool/pool.gno b/pool/pool.gno index 49cde9aa..ff19421d 100644 --- a/pool/pool.gno +++ b/pool/pool.gno @@ -53,6 +53,20 @@ func Mint( pool.transferFromAndVerify(std.GetOrigCaller(), consts.POOL_ADDR, pool.token1Path, amount1, false) } + /* + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_poolPath", GetPoolPath(token0Path, token1Path, fee), + "p_tickLower", int32ToStr(tickLower), + "p_tickUpper", int32ToStr(tickUpper), + "p_liquidityAmount", _liquidityAmount, + "amount0", amount0.ToString(), + "amount1", amount1.ToString(), + ) + */ + return amount0.ToString(), amount1.ToString() } @@ -96,6 +110,20 @@ func Burn( positionKey := positionGetKey(std.PrevRealm().Addr(), tickLower, tickUpper) pool.positions[positionKey] = position + /* + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_poolPath", GetPoolPath(token0Path, token1Path, fee), + "p_tickLower", int32ToStr(tickLower), + "p_tickUpper", int32ToStr(tickUpper), + "p_liquidityAmount", _liquidityAmount, + "amount0", amount0.ToString(), + "amount1", amount1.ToString(), + ) + */ + // actual token transfer happens in Collect() return amount0.ToString(), amount1.ToString() } @@ -151,6 +179,20 @@ func Collect( pool.positions[positionKey] = position + /* + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_poolPath", GetPoolPath(token0Path, token1Path, fee), + "p_tickLower", int32ToStr(tickLower), + "p_tickUpper", int32ToStr(tickUpper), + "p_recipient", _recipient, + "amount0", amount0.ToString(), + "amount1", amount1.ToString(), + ) + */ + return amount0.ToString(), amount1.ToString() } @@ -383,6 +425,22 @@ func Swap( } + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_poolPath", GetPoolPath(token0Path, token1Path, fee), + "p_zeroForOne", boolToStr(zeroForOne), + "p_amountSpecified", _amountSpecified, + "p_sqrtPriceLimitX96", _sqrtPriceLimitX96, + "p_payer", _payer, + "p_recipient", _recipient, + "amount0", amount0.ToString(), + "amount1", amount1.ToString(), + "protocol_fee0", pool.protocolFees.token0.ToString(), + "protocol_fee1", pool.protocolFees.token1.ToString(), + ) + pool.slot0.unlocked = true return amount0.ToString(), amount1.ToString() } @@ -404,13 +462,24 @@ func SetFeeProtocol( panic(ufmt.Sprintf("[POOL] pool.gno__SetFeeProtocol() || expected (feeProtocol0(%d) == 0 || (feeProtocol0(%d) >= 4 && feeProtocol0(%d) <= 10)) && (feeProtocol1(%d) == 0 || (feeProtocol1(%d) >= 4 && feeProtocol1(%d) <= 10))", feeProtocol0, feeProtocol0, feeProtocol0, feeProtocol1, feeProtocol1, feeProtocol1)) } + newFee := feeProtocol0 + (feeProtocol1 << 4) // ( << 4 ) = ( * 16 ) + // iterate all pool for _, pool := range pools { - pool.slot0.feeProtocol = feeProtocol0 + (feeProtocol1 << 4) // ( << 4 ) = ( * 16 ) + pool.slot0.feeProtocol = newFee } // update governance value - gv.SetGovParameter("protocoL_fees", feeProtocol0+(feeProtocol1<<4)) + gv.SetGovParameter("protocoL_fees", newFee) + + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_feeProtocol0", uint8ToStr(feeProtocol0), + "p_feeProtocol1", uint8ToStr(feeProtocol1), + "newFee", uint8ToStr(newFee), + ) } // CollectProtocol collects protocol fees from the pool @@ -453,6 +522,16 @@ func CollectProtocol( panic(ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || transferByRegisterCall(pool.token1Path(%s), recipient(%s), uAmount1)(%d) failed", pool.token1Path, recipient.String(), uAmount1)) } + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_poolPath", GetPoolPath(token0Path, token1Path, fee), + "p_recipient", _recipient, + "amount0", amount0.ToString(), + "amount1", amount1.ToString(), + ) + return amount0.ToString(), amount1.ToString() } diff --git a/pool/pool_manager.gno b/pool/pool_manager.gno index 02e3943f..968410f2 100644 --- a/pool/pool_manager.gno +++ b/pool/pool_manager.gno @@ -70,11 +70,19 @@ func CreatePool( } if !exist { + gns.TransferFrom(a2u(std.GetOrigCaller()), a2u(consts.FEE_COLLECTOR), consts.POOL_CREATION_FEE) + pool = newPool(token0Path, token1Path, fee, tickSpacing, sqrtPriceX96) pools[poolPath] = pool - gns.TransferFrom(a2u(std.GetOrigCaller()), a2u(consts.FEE_COLLECTOR), consts.POOL_CREATION_FEE) -1 } + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_poolPath", poolPath, + ) + + } } // DoesPoolPathExist reports whether the pool exists with the given poolPath diff --git a/pool/utils.gno b/pool/utils.gno index a6ed72b3..f3087a11 100644 --- a/pool/utils.gno +++ b/pool/utils.gno @@ -2,6 +2,7 @@ package pool import ( "std" + "strconv" pusers "gno.land/p/demo/users" @@ -19,3 +20,31 @@ func u256Min(num1, num2 *u256.Uint) *u256.Uint { return num2 } + +func uint8ToStr(i uint8) string { + return strconv.FormatInt(int64(i), 10) +} + +func int32ToStr(i int32) string { + return strconv.FormatInt(int64(i), 10) +} + +func uint64ToStr(i uint64) string { + return strconv.FormatInt(int64(i), 10) +} + +func boolToStr(b bool) string { + if b { + return "true" + } + + return "false" +} + +func origCaller() string { + return std.GetOrigCaller().String() +} + +func prevRealm() string { + return std.PrevRealm().PkgPath() +} diff --git a/pool/withdrawal_fee.gno b/pool/withdrawal_fee.gno index 87aa2424..2110f9dd 100644 --- a/pool/withdrawal_fee.gno +++ b/pool/withdrawal_fee.gno @@ -59,6 +59,17 @@ func HandleWithdrawalFee( afterAmount0 := new(u256.Uint).Sub(amount0, feeAmount0) afterAmount1 := new(u256.Uint).Sub(amount1, feeAmount1) + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_tokenId", uint64ToStr(tokenId), + "p_token0Path", token0Path, + "p_token1Path", token1Path, + "fee0Amount", feeAmount0.ToString(), + "fee1Amount", feeAmount1.ToString(), + ) + return afterAmount0.ToString(), afterAmount1.ToString() } @@ -77,4 +88,11 @@ func SetWithdrawalFee(fee uint64) { } withdrawalFee = fee + + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_fee", uint64ToStr(fee), + ) } From 662e1e652c22db3ebf369a70b519f3fce3b3a4cd Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 15 May 2024 21:32:08 +0900 Subject: [PATCH 06/10] feat: apply emit & event in position --- .../_TEST_0_INIT_TOKEN_REGISTER_test.gno | 0 .../_TEST_0_INIT_VARS_HELPERS_test.gno | 0 position/_TEST_/_TEST_position_api_test.gnoA | 88 ++++++++ position/_TEST_/_TEST_position_full_test.gnoA | 92 ++++++++ ..._TEST_position_increase_decrease_test.gnoA | 205 ++++++++++++++++++ position/_TEST_/_TEST_position_test.gnoA | 170 +++++++++++++++ ...st_two_position_used_single_swap_test.gnoA | 146 +++++++++++++ position/_TEST_/z1_position_filetest.gno | 53 ++--- ...z2_position_increase_decrease_filetest.gno | 42 ++-- ...two_position_used_single_swap_filetest.gno | 46 ++-- position/_TEST_0_INIT_FAUCET_test.gno | 25 --- position/_TEST_position_full_test.gno | 66 ------ position/position.gno | 50 ++++- position/utils.gno | 16 ++ 14 files changed, 811 insertions(+), 188 deletions(-) rename position/{ => _TEST_}/_TEST_0_INIT_TOKEN_REGISTER_test.gno (100%) rename position/{ => _TEST_}/_TEST_0_INIT_VARS_HELPERS_test.gno (100%) create mode 100644 position/_TEST_/_TEST_position_api_test.gnoA create mode 100644 position/_TEST_/_TEST_position_full_test.gnoA create mode 100644 position/_TEST_/_TEST_position_increase_decrease_test.gnoA create mode 100644 position/_TEST_/_TEST_position_test.gnoA create mode 100644 position/_TEST_/_TEST_position_test_two_position_used_single_swap_test.gnoA delete mode 100644 position/_TEST_0_INIT_FAUCET_test.gno delete mode 100644 position/_TEST_position_full_test.gno diff --git a/position/_TEST_0_INIT_TOKEN_REGISTER_test.gno b/position/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno similarity index 100% rename from position/_TEST_0_INIT_TOKEN_REGISTER_test.gno rename to position/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno diff --git a/position/_TEST_0_INIT_VARS_HELPERS_test.gno b/position/_TEST_/_TEST_0_INIT_VARS_HELPERS_test.gno similarity index 100% rename from position/_TEST_0_INIT_VARS_HELPERS_test.gno rename to position/_TEST_/_TEST_0_INIT_VARS_HELPERS_test.gno diff --git a/position/_TEST_/_TEST_position_api_test.gnoA b/position/_TEST_/_TEST_position_api_test.gnoA new file mode 100644 index 00000000..dc5ed623 --- /dev/null +++ b/position/_TEST_/_TEST_position_api_test.gnoA @@ -0,0 +1,88 @@ +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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + pl.CreatePool(fooPath, barPath, fee500, "130621891405341611593710811006") // tick = 10000 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} +} + +// 2. Mint LP and Get GNFT +func TestMint(t *testing.T) { + std.TestSetPrevAddr(gsa) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + // gsa mints => will get tid 1 nft + std.TestSetOrigCaller(gsa) + Mint(fooPath, barPath, fee500, int32(9000), int32(11000), "1000000", "1000000", "1", "1", max_timeout, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower -11000} {p_tickUpper -9000} {tokenId 1} {liquidity 12437322} {amount0 1000000} {amount1 367898}]} + + // gsa mints => will get tid 2 nft + std.TestSetOrigCaller(gsa) + Mint(fooPath, barPath, fee500, int32(4000), int32(6000), "1000000", "1000000", "0", "0", max_timeout, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower -6000} {p_tickUpper -4000} {tokenId 2} {liquidity 7785250} {amount0 1000000} {amount1 0}]} +} + +// 3. Swap to make position fee +func TestSwap(t *testing.T) { + std.TestSetPrevRealm(consts.ROUTER_PATH) + std.TestSetOrigCaller(gsa) + pl.Swap( + fooPath, + barPath, + fee500, + gsa.String(), + true, + "123456", + consts.MIN_PRICE, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_zeroForOne true} {p_amountSpecified 123456} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 123456} {amount1 -45124} {protocol_fee0 0} {protocol_fee1 0}]} +} + +func TestCollectFeeAfterSwap(t *testing.T) { + // burn 0 to collect swap fee + std.TestSetOrigCaller(gsa) + tokneId, fee0, fee1, fromPath := CollectFee(1) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 0} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 61} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + shouldEQ(t, tokneId, uint64(1)) + shouldEQ(t, fee0, "61") + 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_full_test.gnoA b/position/_TEST_/_TEST_position_full_test.gnoA new file mode 100644 index 00000000..6bef5f64 --- /dev/null +++ b/position/_TEST_/_TEST_position_full_test.gnoA @@ -0,0 +1,92 @@ +package position + +import ( + "std" + "testing" + + "gno.land/r/demo/gnoswap/common" + "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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + pl.CreatePool(barPath, fooPath, fee500, common.TickMathGetSqrtRatioAtTick(1).ToString()) + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} +} + +func TestMintPositionFullInRange(t *testing.T) { + std.TestSetPrevAddr(gsa) + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + tokenId, liquidity, amount0, amount1 := Mint( + barPath, + fooPath, + fee500, + -887270, + 887270, + "50000", + "50000", + "0", + "0", + max_timeout, + gsa.String(), + ) + // event {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower -887270} {p_tickUpper 887270} {tokenId 1} {liquidity 49997} {amount0 49995} {amount1 50000}]} + +} + +func TestIncreaseLiquidity(t *testing.T) { + std.TestSetPrevAddr(gsa) + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + tokenId, liquidity, amount0, amount1, poolPath := IncreaseLiquidity( + uint64(1), + "50000", + "50000", + "0", + "0", + max_timeout, + ) + // event: {GNOSWAP gno.land/r/demo/position IncreaseLiquidity [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {liquidity 49997} {amount0 49995} {amount1 50000}]} +} + +func TestDecreaseLiquidity(t *testing.T) { + std.TestSetPrevAddr(gsa) + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath := DecreaseLiquidity( + uint64(1), + uint64(50), + "0", + "0", + max_timeout, + false, + ) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 0} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + // event: {GNOSWAP gno.land/r/demo/position DecreaseLiquidity [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {p_liquidityRatio 50} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {liquidity 49997} {fee0 0} {fee1 0} {amount0 49994} {amount1 49999}]} + +} + +func TestCollectFee(t *testing.T) { + std.TestSetPrevAddr(gsa) + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + tokenId, fee0, fee1, poolPath := CollectFee(uint64(1)) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 0} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + +} diff --git a/position/_TEST_/_TEST_position_increase_decrease_test.gnoA b/position/_TEST_/_TEST_position_increase_decrease_test.gnoA new file mode 100644 index 00000000..b306c69f --- /dev/null +++ b/position/_TEST_/_TEST_position_increase_decrease_test.gnoA @@ -0,0 +1,205 @@ +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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + pl.CreatePool(barPath, fooPath, fee500, common.TickMathGetSqrtRatioAtTick(10000).ToString()) // x2.71814592682522526700950038502924144268035888671875 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} +} + +func TestMintPosition(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower 8000} {p_tickUpper 12000} {tokenId 1} {liquidity 318704392} {amount0 18394892} {amount1 50000000}]} + shouldEQ(t, tokenId, 1) + shouldEQ(t, getNextId(), 2) + shouldEQ(t, amount0, "18394892") + shouldEQ(t, amount1, "50000000") +} + +func TestIncreaseLiquidity(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + ) + // event: {GNOSWAP gno.land/r/demo/position IncreaseLiquidity [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {liquidity 63740878} {amount0 3678979} {amount1 10000000}]} + + shouldEQ(t, m0, "3678979") + shouldEQ(t, m1, "10000000") + + newLiquidity := pool.PoolGetLiquidity() + + shouldEQ(t, newLiquidity.Gt(oldLiquidity), true) +} + +func TestCollectFeeBeforeSwap(t *testing.T) { + std.TestSetPrevAddr(gsa) + + tokenId, fee0, fee1, poolPath := CollectFee(1) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 0} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + 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(gsa) + bar.Approve(a2u(consts.POOL_ADDR), 1_234_567) + + std.TestSetPrevRealm(consts.ROUTER_PATH) + std.TestSetOrigCaller(gsa) + amount0, amount1 := pl.Swap( + barPath, + fooPath, + fee500, + gsa.String(), + true, + "1234567", + consts.MIN_PRICE, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_zeroForOne true} {p_amountSpecified 1234567} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 1234567} {amount1 -3336306} {protocol_fee0 0} {protocol_fee1 0}]} +} + +func TestCollectFeeAfterSwap(t *testing.T) { + std.TestSetPrevAddr(gsa) + + bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) + + tokenId, fee0, fee1, poolPath := CollectFee(1) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 6} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 617} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + shouldEQ(t, tokenId, uint64(1)) + shouldEQ(t, fee0, "611") // 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(gsa) + + tokenId, fee0, fee1, poolPath := CollectFee(1) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 0} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + 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(gsa) + amount0, amount1 := pl.Swap( + barPath, + fooPath, + fee500, + gsa.String(), + true, + "1000000", + consts.MIN_PRICE, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_zeroForOne true} {p_amountSpecified 1000000} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 1000000} {amount1 -2676640} {protocol_fee0 0} {protocol_fee1 0}]} + + // UNCLAIMED_FEE + std.TestSetPrevRealm("") + std.TestSetOrigCaller(gsa) + + oldFee0, oldFee1 := unclaimedFee(uint64(1)) + shouldEQ(t, oldFee0.ToString(), "499") + shouldEQ(t, oldFee1.ToString(), "0") + + tokenId, fee0, fee1, poolPath := CollectFee(1) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 4} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 499} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + shouldEQ(t, tokenId, uint64(1)) + shouldEQ(t, fee0, "495") // 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(gsa) + + oldLiquidity := getPoolFromLpTokenId(uint64(1)).PoolGetLiquidity() + + DecreaseLiquidity( + uint64(1), // tokenId + 50, // liquidityRatio + "0", // amount0Min + "0", // amount1Min + max_timeout, // deadline + false, // unwrapResult + ) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 0} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + // event: {GNOSWAP gno.land/r/demo/position DecreaseLiquidity [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {p_liquidityRatio 50} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {liquidity 191222635} {fee0 0} {fee1 0} {amount0 12153659} {amount1 26993526}]} + + newLiquidity := getPoolFromLpTokenId(uint64(1)).PoolGetLiquidity() + shouldEQ(t, true, newLiquidity.Lt(oldLiquidity)) + + // check fee left + tokenId, fee0, fee1, poolPath := CollectFee(1) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 0} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + shouldEQ(t, tokenId, uint64(1)) + shouldEQ(t, fee0, "0") + shouldEQ(t, fee1, "0") +} diff --git a/position/_TEST_/_TEST_position_test.gnoA b/position/_TEST_/_TEST_position_test.gnoA new file mode 100644 index 00000000..b4e9a024 --- /dev/null +++ b/position/_TEST_/_TEST_position_test.gnoA @@ -0,0 +1,170 @@ +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/gnft" + "gno.land/r/demo/gns" +) + +func TestPoolInitCreatePool(t *testing.T) { + std.TestSetPrevAddr(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + pl.CreatePool(barPath, fooPath, fee500, "130621891405341611593710811006") // tick = 10000 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + +} + +func TestMintPosition01InRange(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower 8000} {p_tickUpper 12000} {tokenId 1} {liquidity 318704392} {amount0 18394892} {amount1 50000000}]} + + shouldEQ(t, tokenId, 1) + shouldEQ(t, getNextId(), 2) + shouldEQ(t, amount0, "18394892") + shouldEQ(t, amount1, "50000000") +} + +func TestMintPosition02LowerRange(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower 5000} {p_tickUpper 8000} {tokenId 2} {liquidity 240632772} {amount0 0} {amount1 50000000}]} + + shouldEQ(t, tokenId, 2) + shouldEQ(t, getNextId(), 3) + shouldEQ(t, amount0, "0") + shouldEQ(t, amount1, "50000000") +} + +func TestMintPosition03UpperRange(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower 12000} {p_tickUpper 14000} {tokenId 3} {liquidity 957388253} {amount0 50000000} {amount1 0}]} + + 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) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 0} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + 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(gsa) + bar.Approve(a2u(consts.POOL_ADDR), 1234567) + + std.TestSetPrevRealm(consts.ROUTER_PATH) + std.TestSetOrigCaller(gsa) + amount0, amount1 := pl.Swap( + barPath, + fooPath, + fee500, + gsa.String(), + true, + "1234567", + consts.MIN_PRICE, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_zeroForOne true} {p_amountSpecified 1234567} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 1234567} {amount1 -3332779} {protocol_fee0 0} {protocol_fee1 0}]} + + shouldEQ(t, amount0, "1234567") + shouldEQ(t, amount1, "-3332779") +} + +func TestCollectFeeAfterSwap(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 6} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 617} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + 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(gsa) + + 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) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 3} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 0} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 3} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + // event: {GNOSWAP gno.land/r/demo/position DecreaseLiquidity [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 3} {p_liquidityRatio 100} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {liquidity 957388253} {fee0 0} {fee1 0} {amount0 49999999} {amount1 0}]} + + shouldEQ(t, tokenId, uint64(3)) + shouldEQ(t, amount0, "49999999") + shouldEQ(t, amount1, "0") + + ownerOfPosition = gnft.OwnerOf(tid(3)) + shouldEQ(t, ownerOfPosition, std.GetOrigCaller()) +} diff --git a/position/_TEST_/_TEST_position_test_two_position_used_single_swap_test.gnoA b/position/_TEST_/_TEST_position_test_two_position_used_single_swap_test.gnoA new file mode 100644 index 00000000..5ecbc7a8 --- /dev/null +++ b/position/_TEST_/_TEST_position_test_two_position_used_single_swap_test.gnoA @@ -0,0 +1,146 @@ +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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + pl.CreatePool(barPath, fooPath, fee500, "130621891405341611593710811006") // tick = 10000 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} +} + +func TestMintPosition01WideInRange(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower 8000} {p_tickUpper 12000} {tokenId 1} {liquidity 318704392} {amount0 18394892} {amount1 50000000}]} + + shouldEQ(t, tokenId, 1) + shouldEQ(t, getNextId(), 2) + shouldEQ(t, liquidity, "318704392") + shouldEQ(t, amount0, "18394892") + shouldEQ(t, amount1, "50000000") +} + +func TestMintPositionTightInRange(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower 9500} {p_tickUpper 10500} {tokenId 2} {liquidity 1228379121} {amount0 18394892} {amount1 50000000}]} + + shouldEQ(t, tokenId, 2) + shouldEQ(t, getNextId(), 3) + shouldEQ(t, liquidity, "1228379121") + shouldEQ(t, amount0, "18394892") + shouldEQ(t, amount1, "50000000") +} + +func TestCollectFeeBeforeSwapPos1(t *testing.T) { + tokenId, fee0, fee1, poolPath := CollectFee(1) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 0} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + 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) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 2} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 0} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 2} {fee0 0} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + 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(gsa) + bar.Approve(a2u(consts.POOL_ADDR), 1234567) + + std.TestSetPrevRealm(consts.ROUTER_PATH) + std.TestSetOrigCaller(gsa) + amount0, amount1 := pl.Swap( + barPath, + fooPath, + fee500, + gsa.String(), + true, + "1234567", + consts.MIN_PRICE, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_zeroForOne true} {p_amountSpecified 1234567} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 1234567} {amount1 -3349648} {protocol_fee0 0} {protocol_fee1 0}]} + +} + +// FYI, IF POSITION 2 DOESN'T EXIST, POSITION 1 WILL EARN '617' as fee +func TestCollectFeeAfterSwapPos1(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 1} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 1} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 1} {fee0 127} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + 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) + // event: {GNOSWAP gno.land/r/demo/pool HandleWithdrawalFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/position} {p_tokenId 2} {p_token0Path gno.land/r/demo/bar} {p_token1Path gno.land/r/demo/foo} {fee0Amount 4} {fee1Amount 0}]} + // event: {GNOSWAP gno.land/r/demo/position CollectFee [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_tokenId 2} {fee0 490} {fee1 0} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + shouldEQ(t, tokenId, uint64(2)) + shouldEQ(t, fee0, "486") + 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 index 5291fe6a..ca9fbdae 100644 --- a/position/_TEST_/z1_position_filetest.gno +++ b/position/_TEST_/z1_position_filetest.gno @@ -21,8 +21,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -168,16 +167,7 @@ func ugnotBalanceOf(addr std.Address) uint64 { } func init() { - // FAUCET - std.TestSetPrevAddr(test1) - - for i := 0; i < 100; i++ { - foo.Faucet() - bar.Faucet() - baz.Faucet() - qux.Faucet() - obl.Faucet() - } + std.TestSetPrevAddr(gsa) // TOKEN REGISTER std.TestSetPrevAddr(gsa) @@ -202,14 +192,7 @@ func main() { } 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) + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) std.TestSkipHeights(1) @@ -222,11 +205,11 @@ func createPool() { } func mint01InRange() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), 18394892) foo.Approve(a2u(consts.POOL_ADDR), 50000000) - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) std.TestSetPrevRealm(consts.STAKER_PATH) tokenId, liquidity, amount0, amount1 := pn.Mint( barPath, @@ -239,7 +222,7 @@ func mint01InRange() { "0", "0", max_timeout, - test1.String(), + gsa.String(), ) if tokenId != 1 { panic("expected tokenId 1") @@ -253,11 +236,11 @@ func mint01InRange() { } func mint02LowerRange() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), 0) foo.Approve(a2u(consts.POOL_ADDR), 50000000) - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) std.TestSetPrevRealm(consts.STAKER_PATH) tokenId, liquidity, amount0, amount1 := pn.Mint( barPath, @@ -270,7 +253,7 @@ func mint02LowerRange() { "0", "0", max_timeout, - test1.String(), + gsa.String(), ) if tokenId != 2 { panic("expected tokenId 2") @@ -284,11 +267,11 @@ func mint02LowerRange() { } func mint03UpperRange() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), 50000000) foo.Approve(a2u(consts.POOL_ADDR), 0) - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) std.TestSetPrevRealm(consts.STAKER_PATH) tokenId, liquidity, amount0, amount1 := pn.Mint( barPath, @@ -301,7 +284,7 @@ func mint03UpperRange() { "0", "0", max_timeout, - test1.String(), + gsa.String(), ) if tokenId != 3 { panic("expected tokenId 3") @@ -315,20 +298,20 @@ func mint03UpperRange() { } func swap() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), 1234567) std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) amount0, amount1 := pl.Swap( barPath, fooPath, fee500, - test1.String(), + gsa.String(), true, "1234567", consts.MIN_PRICE, - test1.String(), + gsa.String(), ) if amount0 != "1234567" { panic("expected amount0 1234567") @@ -339,7 +322,7 @@ func swap() { } func collectFeeAfterSwap() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE @@ -357,7 +340,7 @@ func collectFeeAfterSwap() { } func decreaseLiquidityUpperPosition() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath := pn.DecreaseLiquidity(uint64(3), 100, "0", "0", max_timeout, false) if tokenId != 3 { diff --git a/position/_TEST_/z2_position_increase_decrease_filetest.gno b/position/_TEST_/z2_position_increase_decrease_filetest.gno index 91ac7273..f80273f7 100644 --- a/position/_TEST_/z2_position_increase_decrease_filetest.gno +++ b/position/_TEST_/z2_position_increase_decrease_filetest.gno @@ -21,8 +21,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -168,16 +167,7 @@ func ugnotBalanceOf(addr std.Address) uint64 { } func init() { - // FAUCET - std.TestSetPrevAddr(test1) - - for i := 0; i < 100; i++ { - foo.Faucet() - bar.Faucet() - baz.Faucet() - qux.Faucet() - obl.Faucet() - } + std.TestSetPrevAddr(gsa) // TOKEN REGISTER std.TestSetPrevAddr(gsa) @@ -202,13 +192,7 @@ func main() { 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) + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) std.TestSkipHeights(1) @@ -221,11 +205,11 @@ func createPool() { } func mint() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), 18394892) foo.Approve(a2u(consts.POOL_ADDR), 50000000) - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) std.TestSetPrevRealm(consts.STAKER_PATH) tokenId, liquidity, amount0, amount1 := pn.Mint( barPath, @@ -238,7 +222,7 @@ func mint() { "0", "0", max_timeout, - test1.String(), + gsa.String(), ) if tokenId != 1 { panic("expected tokenId 1") @@ -252,7 +236,7 @@ func mint() { } func increase() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), 3678979) foo.Approve(a2u(consts.POOL_ADDR), 10000000) @@ -287,7 +271,7 @@ func increase() { func decreasePartial() { std.TestSetPrevRealm("") - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) pool := pl.GetPool(barPath, fooPath, fee500) oldLiquidity := pool.PoolGetLiquidity() @@ -323,25 +307,25 @@ func decreasePartial() { } func swap() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), 1_234_567) std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) amount0, amount1 := pl.Swap( barPath, fooPath, fee500, - test1.String(), + gsa.String(), true, "1234567", consts.MIN_PRICE, - test1.String(), + gsa.String(), ) } func decreaseFull() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) // protocol fee approval bar.Approve(a2u(consts.POOL_ADDR), 10000000) 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 index bf86ecea..807a920b 100644 --- a/position/_TEST_/z3_position_two_position_used_single_swap_filetest.gno +++ b/position/_TEST_/z3_position_two_position_used_single_swap_filetest.gno @@ -21,8 +21,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -168,17 +167,6 @@ func ugnotBalanceOf(addr std.Address) uint64 { } 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{}) @@ -202,13 +190,7 @@ func main() { 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) + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) std.TestSkipHeights(1) @@ -221,11 +203,11 @@ func createPool() { } func mint01Wide() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), 18394892) foo.Approve(a2u(consts.POOL_ADDR), 50000000) - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) std.TestSetPrevRealm(consts.STAKER_PATH) tokenId, liquidity, amount0, amount1 := pn.Mint( barPath, @@ -238,7 +220,7 @@ func mint01Wide() { "0", "0", max_timeout, - test1.String(), + gsa.String(), ) if tokenId != 1 { panic("expected tokenId 1") @@ -255,11 +237,11 @@ func mint01Wide() { } func mint02Tight() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), 18394892) foo.Approve(a2u(consts.POOL_ADDR), 50000000) - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) std.TestSetPrevRealm(consts.STAKER_PATH) tokenId, liquidity, amount0, amount1 := pn.Mint( barPath, @@ -272,7 +254,7 @@ func mint02Tight() { "0", "0", max_timeout, - test1.String(), + gsa.String(), ) if tokenId != 2 { panic("expected tokenId 2") @@ -289,25 +271,25 @@ func mint02Tight() { } func swap() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), 1_234_567) std.TestSetPrevRealm(consts.ROUTER_PATH) - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) amount0, amount1 := pl.Swap( barPath, fooPath, fee500, - test1.String(), + gsa.String(), true, "1234567", consts.MIN_PRICE, - test1.String(), + gsa.String(), ) } func collect01() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE @@ -321,7 +303,7 @@ func collect01() { } func collect02() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // COLLECT_FEE diff --git a/position/_TEST_0_INIT_FAUCET_test.gno b/position/_TEST_0_INIT_FAUCET_test.gno deleted file mode 100644 index 1991c811..00000000 --- a/position/_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_position_full_test.gno b/position/_TEST_position_full_test.gno deleted file mode 100644 index c897d64e..00000000 --- a/position/_TEST_position_full_test.gno +++ /dev/null @@ -1,66 +0,0 @@ -package position - -import ( - // @notJoon JSON - "std" - "testing" - - "gno.land/r/demo/gnoswap/common" - "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" - - i256 "gno.land/p/demo/gnoswap/int256" -) - -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(1).ToString()) -} - -func TestMintPositionFullInRange(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, liquidity, amount0, amount1 := Mint( - barPath, - fooPath, - fee500, - -887270, - 887270, - "50000", - "50000", - "0", - "0", - max_timeout, - test1.String(), - ) - shouldEQ(t, tokenId, 1) - shouldEQ(t, getNextId(), 2) - - position := positions[tokenId] - - currentX96 := common.TickMathGetSqrtRatioAtTick(1) - lowerX96 := common.TickMathGetSqrtRatioAtTick(position.tickLower) - upperX96 := common.TickMathGetSqrtRatioAtTick(position.tickUpper) - - token0Balance, token1Balance := common.GetAmountsForLiquidity( - currentX96, - lowerX96, - upperX96, - i256.FromUint256(position.liquidity), - ) -} diff --git a/position/position.gno b/position/position.gno index a10684da..e1040845 100644 --- a/position/position.gno +++ b/position/position.gno @@ -131,6 +131,19 @@ func Mint( } } + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_poolPath", poolPath, + "p_tickLower", int32ToStr(tickLower), + "p_tickUpper", int32ToStr(tickUpper), + "tokenId", uint64ToStr(tokenId), + "liquidity", liquidity.ToString(), + "amount0", amount0.ToString(), + "amount1", amount1.ToString(), + ) + return tokenId, liquidity.ToString(), amount0.ToString(), amount1.ToString() } @@ -205,6 +218,18 @@ func IncreaseLiquidity( } tokneId, liquidity, amount0, amount1, poolPath := increaseLiquidity(increaseLiquidityParams) + + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_tokenId", uint64ToStr(tokenId), + "poolPath", poolPath, + "liquidity", liquidity.ToString(), + "amount0", amount0.ToString(), + "amount1", amount1.ToString(), + ) + return tokenId, liquidity.ToString(), amount0.ToString(), amount1.ToString(), poolPath } @@ -299,6 +324,20 @@ func DecreaseLiquidity( tokenId, liquidity, fee0, fee1, amount0, amount1, poolPath := decreaseLiquidity(decreaseLiquidityParams) + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_tokenId", uint64ToStr(tokenId), + "p_liquidityRatio", uint64ToStr(liquidityRatio), + "poolPath", poolPath, + "liquidity", liquidity.ToString(), + "fee0", fee0.ToString(), + "fee1", fee1.ToString(), + "amount0", amount0.ToString(), + "amount1", amount1.ToString(), + ) + return tokenId, liquidity.ToString(), fee0.ToString(), fee1.ToString(), amount0.ToString(), amount1.ToString(), poolPath } @@ -498,6 +537,16 @@ func CollectFee(tokenId uint64) (uint64, string, string, string) { // tokenId, t // handle withdrawal fee withoutFee0, withoutFee1 := pl.HandleWithdrawalFee(tokenId, token0, amount0, token1, amount1) + std.Emit( + "GNOSWAP", + "m_origCaller", origCaller(), + "m_prevRealm", prevRealm(), + "p_tokenId", uint64ToStr(tokenId), + "fee0", tokensOwed0.ToString(), + "fee1", tokensOwed1.ToString(), + "poolPath", position.poolKey, + ) + return tokenId, withoutFee0, withoutFee1, position.poolKey } @@ -527,5 +576,4 @@ func isAuthorizedForToken(tokenId uint64) { if !(isApprovedOrOwner(std.PrevRealm().Addr(), tokenId)) { panic(ufmt.Sprintf("[POSITION] position.gno__isAuthorizedForToken() || caller(%s) is not approved or owner of tokenId(%d)", std.PrevRealm().Addr(), tokenId)) } - } diff --git a/position/utils.gno b/position/utils.gno index 8725c92b..cf1608ed 100644 --- a/position/utils.gno +++ b/position/utils.gno @@ -32,3 +32,19 @@ func poolKeyDivide(poolKey string) (string, string, uint32) { pFee, _ := strconv.Atoi(pFeeStr) return pToken0, pToken1, uint32(pFee) } + +func int32ToStr(i int32) string { + return strconv.FormatInt(int64(i), 10) +} + +func uint64ToStr(i uint64) string { + return strconv.FormatInt(int64(i), 10) +} + +func origCaller() string { + return std.GetOrigCaller().String() +} + +func prevRealm() string { + return std.PrevRealm().PkgPath() +} From 923dbd7fd31ee1456bef0a4a061f7f667f178b50 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 15 May 2024 21:32:27 +0900 Subject: [PATCH 07/10] feat: apply emit & event in router --- router/_RPC_api.gno | 191 ---------- .../_TEST_0_INIT_TOKEN_REGISTER_test.gno | 155 ++++++++ .../_TEST_/_TEST_0_INIT_VARS_HELPERS_test.gno | 62 ++++ .../_TEST_router_all_2_route_2_hop_test.gnoA | 196 ++++++++++ ..._route_1route_1hop_native_in_out_test.gnoA | 155 ++++++++ ...swap_route_1route_1hop_out_range_test.gnoA | 88 +++++ ...ST_router_swap_route_1route_1hop_test.gnoA | 189 ++++++++++ ...route_2hop_wrapped_native_in_out_test.gnoA | 144 ++++++++ ...route_3hop_wrapped_native_middle_test.gnoA | 118 ++++++ ...ST_router_swap_route_2route_2hop_test.gnoA | 201 +++++++++++ router/_TEST_/z1_ratio_filetest.gno | 336 ------------------ ...=> z1_swap_route_1route_1hop_filetest.gno} | 41 +-- ..._route_1route_1hop_out_range_filetest.gno} | 29 +- ...te_1route_1hop_native_in_out_filetest.gno} | 45 +-- ...e_1route_2hop_wrapped_middle_filetest.gno} | 43 +-- ...etest.gno => z5_dry_and_swap_filetest.gno} | 59 +-- router/router.gno | 14 +- router/utils.gno | 8 + 18 files changed, 1391 insertions(+), 683 deletions(-) delete mode 100644 router/_RPC_api.gno create mode 100644 router/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno create mode 100644 router/_TEST_/_TEST_0_INIT_VARS_HELPERS_test.gno create mode 100644 router/_TEST_/_TEST_router_all_2_route_2_hop_test.gnoA create mode 100644 router/_TEST_/_TEST_router_swap_route_1route_1hop_native_in_out_test.gnoA create mode 100644 router/_TEST_/_TEST_router_swap_route_1route_1hop_out_range_test.gnoA create mode 100644 router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gnoA create mode 100644 router/_TEST_/_TEST_router_swap_route_1route_2hop_wrapped_native_in_out_test.gnoA create mode 100644 router/_TEST_/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gnoA create mode 100644 router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gnoA delete mode 100644 router/_TEST_/z1_ratio_filetest.gno rename router/_TEST_/{z2_swap_route_1route_1hop_filetest.gno => z1_swap_route_1route_1hop_filetest.gno} (93%) rename router/_TEST_/{z3_swap_route_1route_1hop_out_range_filetest.gno => z2_swap_route_1route_1hop_out_range_filetest.gno} (93%) rename router/_TEST_/{z4_swap_route_1route_1hop_native_in_out_filetest.gno => z3_swap_route_1route_1hop_native_in_out_filetest.gno} (92%) rename router/_TEST_/{z5_swap_route_1route_2hop_wrapped_middle_filetest.gno => z4_swap_route_1route_2hop_wrapped_middle_filetest.gno} (90%) rename router/_TEST_/{z6_dry_and_swap_filetest.gno => z5_dry_and_swap_filetest.gno} (93%) diff --git a/router/_RPC_api.gno b/router/_RPC_api.gno deleted file mode 100644 index 25b65d43..00000000 --- a/router/_RPC_api.gno +++ /dev/null @@ -1,191 +0,0 @@ -package router - -import ( - "std" - "time" - - "gno.land/p/demo/json" - "gno.land/p/demo/ufmt" - - "gno.land/r/demo/gnoswap/common" - "gno.land/r/demo/gnoswap/consts" - - pl "gno.land/r/demo/pool" - - u256 "gno.land/p/demo/gnoswap/uint256" -) - -type TokenRatio struct { - Token string `json:"token"` - Ratio string `json:"ratio"` -} - -type ResponseQueryBase struct { - Height int64 `json:"height"` - Timestamp int64 `json:"timestamp"` -} - -type ResponseGetRatiosFromBase struct { - Stat ResponseQueryBase `json:"stat"` - Response []TokenRatio `json:"response"` -} - -func ApiGetRatiosFromBase() string { - qb := ResponseQueryBase{ - Height: std.GetHeight(), - Timestamp: time.Now().Unix(), - } - - ratios := findRatios(3) - r := ResponseGetRatiosFromBase{ - Stat: qb, - Response: ratios, - } - - // STAT NODE - _stat := json.ObjectNode("", map[string]*json.Node{ - "height": json.NumberNode("height", float64(std.GetHeight())), - "timestamp": json.NumberNode("timestamp", float64(time.Now().Unix())), - }) - - // RESPONSE (ARRAY) NODE - responses := json.ArrayNode("", []*json.Node{}) - for i, pool := range r.Response { - _routerNode := json.ObjectNode("", map[string]*json.Node{ - "token": json.StringNode("token", pool.Token), - "ratio": json.StringNode("ratio", pool.Ratio), - }) - responses.AppendArray(_routerNode) - } - - node := json.ObjectNode("", map[string]*json.Node{ - "stat": _stat, - "response": responses, - }) - - b, err := json.Marshal(node) - if err != nil { - panic(ufmt.Sprintf("[ROUTER] _RPC_api.gno__ApiGetRatiosFromBase() || error marshalling json: %s", err.Error())) - } - - return string(b) -} - -func findRatios(maxHops int) []TokenRatio { - var tokenRatio = make(map[string]*u256.Uint, 0) - // WGNOT - tokenRatio[consts.WRAPPED_WUGNOT] = u256.MustFromDecimal(consts.Q96) // ~= 1 - - tokens := getTokenList() - - pools := findCandidatePools() - - for _, token := range tokens { - if token == consts.WRAPPED_WUGNOT { - continue - } - - routes := computeAllRoutes(consts.WRAPPED_WUGNOT, token, maxHops, pools) - - if len(routes) == 0 { - // NO ROUTES FOUND => SET RATIO TO 0 - tokenRatio[token] = u256.Zero() - } else { - numRoutes := uint64(len(routes)) - - _tokenRatioX96 := u256.Zero() - - for _, route := range routes { - numHops := uint64(len(route.route)) - - switch numHops { - case 1, 2, 3: - priceRatio := calculateTokenRatio(token, route.route, 0, u256.One()) - // _tokenRatioX96 += priceRatio - _tokenRatioX96 = new(u256.Uint).Add(_tokenRatioX96, priceRatio) - default: - _tokenRatioX96 = u256.Zero() - } - } - avgPriceX96 := new(u256.Uint).Div(_tokenRatioX96, u256.NewUint(numRoutes)) - tokenRatio[token] = avgPriceX96 - } - } - // LOOP FIN - - var tokenRatios = []TokenRatio{} - for token, ratio := range tokenRatio { - tokenRatios = append(tokenRatios, TokenRatio{ - Token: token, - Ratio: ratio.ToString(), - }) - } - - return tokenRatios -} - -func calculateTokenRatio(currentToken string, routes []PoolWithMeta, proceed int, priceX96 *u256.Uint) *u256.Uint { - poolPath := routes[len(routes)-proceed-1].poolPath - pool := pl.GetPoolFromPoolPath(poolPath) - - token0Path := pool.PoolGetToken0Path() - token1Path := pool.PoolGetToken1Path() - - if token1Path == currentToken { - poolSqrtPriceX96 := pool.PoolGetSlot0SqrtPriceX96() - - sqrtX96X96 := new(u256.Uint).Mul(poolSqrtPriceX96, poolSqrtPriceX96) - sqrtX96 := new(u256.Uint).Div(sqrtX96X96, u256.MustFromDecimal(consts.Q96)) - - priceX96 = new(u256.Uint).Mul(priceX96, sqrtX96) - currentToken = token0Path - } else if token0Path == currentToken { - poolTick := pool.PoolGetSlot0Tick() - oppositeTick := -poolTick - oppositeSqrtPriceX96 := common.TickMathGetSqrtRatioAtTick(oppositeTick) - - oppSqrtX96X96 := new(u256.Uint).Mul(oppositeSqrtPriceX96, oppositeSqrtPriceX96) - oppSqrtX96 := new(u256.Uint).Div(oppSqrtX96X96, u256.MustFromDecimal(consts.Q96)) - - priceX96 = new(u256.Uint).Mul(priceX96, oppSqrtX96) - currentToken = token1Path - } else { - // wrong condition - panic("[ROUTER] _RPC_api.gno__calculateTokenRatio() || wrong condition") - return u256.Zero() - } - - proceed += 1 - - for { // remove as much X96 as possible - tempPriceX96 := priceX96.Clone() - priceX96 = new(u256.Uint).Div(priceX96, u256.MustFromDecimal(consts.Q96)) - - if priceX96.Lt(u256.MustFromDecimal(consts.MIN_PRICE)) { - if proceed == len(routes) { - return tempPriceX96 - } - return calculateTokenRatio(currentToken, routes, proceed, tempPriceX96) - } - } -} - -func getTokenList() []string { - seen := make(map[string]bool) - uniqueTokenList := []string{} - poolList := pl.PoolGetPoolList() - - for _, poolPath := range poolList { - token0Path, token1Path, _ := poolPathWithFeeDivide(poolPath) - if _, exists := seen[token0Path]; !exists { - seen[token0Path] = true - uniqueTokenList = append(uniqueTokenList, token0Path) - } - if _, exists := seen[token1Path]; !exists { - seen[token1Path] = true - uniqueTokenList = append(uniqueTokenList, token1Path) - } - } - - return uniqueTokenList -} diff --git a/router/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno b/router/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno new file mode 100644 index 00000000..d36776af --- /dev/null +++ b/router/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno @@ -0,0 +1,155 @@ +package router + +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) + + // POOL + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + // 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{}) +} diff --git a/router/_TEST_/_TEST_0_INIT_VARS_HELPERS_test.gno b/router/_TEST_/_TEST_0_INIT_VARS_HELPERS_test.gno new file mode 100644 index 00000000..a7402d4c --- /dev/null +++ b/router/_TEST_/_TEST_0_INIT_VARS_HELPERS_test.gno @@ -0,0 +1,62 @@ +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_all_2_route_2_hop_test.gnoA b/router/_TEST_/_TEST_router_all_2_route_2_hop_test.gnoA new file mode 100644 index 00000000..1a7dd7d5 --- /dev/null +++ b/router/_TEST_/_TEST_router_all_2_route_2_hop_test.gnoA @@ -0,0 +1,196 @@ +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" + "gno.land/r/demo/qux" +) + +func TestCreatePool(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500}]} + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500}]} +} + +func TestPositionMint(t *testing.T) { + // bar_baz_500 by gsa + std.TestSetPrevAddr(gsa) + + 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, gsa.String()) + pn.Mint(bazPath, quxPath, uint16(500), int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 1} {liquidity 1243732} {amount0 36790} {amount1 100000}]} + // event: {GNOSWAP gno.land/r/demo/position Mint [{p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 2} {liquidity 1243732} {amount0 36790} {amount1 100000}]} +} + +func TestDrySwapRouteBarQuxExactIn(t *testing.T) { + std.TestSetOrigCaller(gsa) + + 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(gsa) + + 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(gsa) + + 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 + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne true} {p_amountSpecified 500} {p_sqrtPriceLimitX96 4295128740} {p_payer g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 500} {amount1 -1355} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne true} {p_amountSpecified 1355} {p_sqrtPriceLimitX96 4295128740} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {amount0 1355} {amount1 -3673} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne true} {p_amountSpecified 500} {p_sqrtPriceLimitX96 4295128740} {p_payer g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 500} {amount1 -1353} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne true} {p_amountSpecified 1353} {p_sqrtPriceLimitX96 4295128740} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {amount0 1353} {amount1 -3655} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/bar} {p_output gno.land/r/demo/qux} {p_swapType EXACT_IN} {p_amountSpecified 1000} {p_route 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} {p_quote 50,50} {amountIn 1000} {amountOut -7318}]} + + shouldEQ(t, dryResult, "140") +} + +func TestSwapRouteBarQuxExactOut(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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 + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne true} {p_amountSpecified -187} {p_sqrtPriceLimitX96 4295128740} {p_payer g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 70} {amount1 -187} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne true} {p_amountSpecified 187} {p_sqrtPriceLimitX96 4295128740} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {amount0 187} {amount1 -501} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne true} {p_amountSpecified -187} {p_sqrtPriceLimitX96 4295128740} {p_payer g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 70} {amount1 -187} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne true} {p_amountSpecified 187} {p_sqrtPriceLimitX96 4295128740} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {amount0 187} {amount1 -501} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/bar} {p_output gno.land/r/demo/qux} {p_swapType EXACT_OUT} {p_amountSpecified 1000} {p_route 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} {p_quote 50,50} {amountIn 140} {amountOut -1001}]} + + shouldEQ(t, amountIn, "140") + shouldEQ(t, amountOut, "-1001") +} + +func TestDrySwapRouteQuxBarExactIn(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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(gsa) + + 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 + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne false} {p_amountSpecified 300} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 -110} {amount1 300} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne false} {p_amountSpecified 110} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {amount0 -40} {amount1 110} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne false} {p_amountSpecified 700} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 -259} {amount1 700} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne false} {p_amountSpecified 259} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {amount0 -95} {amount1 259} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/qux} {p_output gno.land/r/demo/bar} {p_swapType EXACT_IN} {p_amountSpecified 1000} {p_route 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} {p_quote 30,70} {amountIn 1000} {amountOut -135}]} + + shouldEQ(t, amountIn, "1000") + shouldEQ(t, amountOut, "-135") +} + +func TestDrySwapRouteQuxBarExactOut(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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, "7351") +} + +func TestSwapRouteQuxBarExactOut(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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 + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne false} {p_amountSpecified -815} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 -815} {amount1 2204} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne false} {p_amountSpecified 815} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {amount0 -300} {amount1 815} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne false} {p_amountSpecified -1902} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 -1902} {amount1 5161} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne false} {p_amountSpecified 1902} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5} {amount0 -700} {amount1 1902} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/qux} {p_output gno.land/r/demo/bar} {p_swapType EXACT_OUT} {p_amountSpecified 1000} {p_route 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} {p_quote 30,70} {amountIn 7365} {amountOut -999}]} + + shouldEQ(t, amountIn, "7365") + shouldEQ(t, amountOut, "-999") +} diff --git a/router/_TEST_/_TEST_router_swap_route_1route_1hop_native_in_out_test.gnoA b/router/_TEST_/_TEST_router_swap_route_1route_1hop_native_in_out_test.gnoA new file mode 100644 index 00000000..43b998af --- /dev/null +++ b/router/_TEST_/_TEST_router_swap_route_1route_1hop_native_in_out_test.gnoA @@ -0,0 +1,155 @@ +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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*3) + + pl.CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500}]} + + pl.CreatePool(bazPath, quxPath, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500}]} + + pl.CreatePool(quxPath, consts.WRAPPED_WUGNOT, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/qux:gno.land/r/demo/wugnot:500}]} + + // 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(gsa) + 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, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 1} {liquidity 1243732} {amount0 36790} {amount1 100000}]} + + shouldEQ(t, tokenId, uint64(1)) + shouldEQ(t, amount0, "36790") // bar + shouldEQ(t, amount1, "100000") // baz +} + +func TestPositionMintBazQux(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 2} {liquidity 1243732} {amount0 36790} {amount1 100000}]} + + shouldEQ(t, tokenId, uint64(2)) + shouldEQ(t, amount0, "36790") + shouldEQ(t, amount1, "100000") + +} + +func TestPositionMintQuxGnot(t *testing.T) { + std.TestSetPrevAddr(gsa) + + // send + std.TestIssueCoins(gsa, 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(gsa) + 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, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/qux:gno.land/r/demo/wugnot:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 3} {liquidity 1243732} {amount0 36790} {amount1 100000}]} + + shouldEQ(t, tokenId, uint64(3)) + shouldEQ(t, amount0, "36790") + shouldEQ(t, amount1, "100000") +} + +func TestDrySwapRouteBarGnotExactIn(t *testing.T) { + std.TestSetOrigCaller(gsa) + + 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(gsa) + + 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, "1014") +} + +func TestDrySwapRouteGnotBarExactIn(t *testing.T) { + std.TestSetOrigCaller(gsa) + + 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(gsa) + + 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, "2027") +} diff --git a/router/_TEST_/_TEST_router_swap_route_1route_1hop_out_range_test.gnoA b/router/_TEST_/_TEST_router_swap_route_1route_1hop_out_range_test.gnoA new file mode 100644 index 00000000..740583d1 --- /dev/null +++ b/router/_TEST_/_TEST_router_swap_route_1route_1hop_out_range_test.gnoA @@ -0,0 +1,88 @@ +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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + pl.CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500}]} + + 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 gsa + std.TestSetPrevAddr(gsa) + 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, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_tickLower 12000} {p_tickUpper 15000} {tokenId 1} {liquidity 1308149} {amount0 100000} {amount1 0}]} + + 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(gsa) + + 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(gsa) + + 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)) + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne false} {p_amountSpecified 1000} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 -300} {amount1 1000} {protocol_fee0 0} {protocol_fee1 0}]} + }) +} diff --git a/router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gnoA b/router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gnoA new file mode 100644 index 00000000..8088d8cb --- /dev/null +++ b/router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gnoA @@ -0,0 +1,189 @@ +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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + pl.CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500}]} + + // jsonOutput := pl.ApiGetPools() + // jsonStr := gjson.Parse(jsonOutput) + // shouldEQ(t, len(jsonStr.Get("response").Array()), 1) +} + +func TestPositionMint(t *testing.T) { + // bar_baz_500 by gsa + std.TestSetPrevAddr(gsa) + 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, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 1} {liquidity 1243732} {amount0 36790} {amount1 100000}]} + + shouldEQ(t, tokenId, uint64(1)) + shouldEQ(t, amount0, "36790") + shouldEQ(t, amount1, "100000") +} + +func TestDrySwapRouteBarBazExactIn(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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(gsa) + + 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 + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne true} {p_amountSpecified 1000} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 1000} {amount1 -2711} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/bar} {p_output gno.land/r/demo/baz} {p_swapType EXACT_IN} {p_amountSpecified 1000} {p_route gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_quote 100} {amountIn 1000} {amountOut -2707}]} + + shouldEQ(t, amountIn, "1000") + shouldEQ(t, amountOut, "-2707") +} + +func TestDrySwapRouteBarBazExactOut(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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, "371") +} + +func TestSwapRouteBarBazExactOut(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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 + "371", // tokenAmountLimit + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne true} {p_amountSpecified -1000} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 371} {amount1 -1000} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/bar} {p_output gno.land/r/demo/baz} {p_swapType EXACT_OUT} {p_amountSpecified 1000} {p_route gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_quote 100} {amountIn 371} {amountOut -999}]} + + shouldEQ(t, amountIn, "371") + shouldEQ(t, amountOut, "-999") +} + +func TestDrySwapRouteBazBarExactIn(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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(gsa) + + 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 + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne false} {p_amountSpecified 1000} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 -368} {amount1 1000} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/baz} {p_output gno.land/r/demo/bar} {p_swapType EXACT_IN} {p_amountSpecified 1000} {p_route gno.land/r/demo/baz:gno.land/r/demo/bar:500} {p_quote 100} {amountIn 1000} {amountOut -368}]} + + shouldEQ(t, amountIn, "1000") + shouldEQ(t, amountOut, "-368") +} + +func TestDrySwapRouteBazBarExactOut(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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, "8171") +} + +func TestSwapRouteBazBarExactOut(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne false} {p_amountSpecified -3000} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 -3000} {amount1 8171} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/baz} {p_output gno.land/r/demo/bar} {p_swapType EXACT_OUT} {p_amountSpecified 3000} {p_route gno.land/r/demo/baz:gno.land/r/demo/bar:500} {p_quote 100} {amountIn 8171} {amountOut -2996}]} + + shouldEQ(t, amountIn, "8171") + shouldEQ(t, amountOut, "-2996") +} diff --git a/router/_TEST_/_TEST_router_swap_route_1route_2hop_wrapped_native_in_out_test.gnoA b/router/_TEST_/_TEST_router_swap_route_1route_2hop_wrapped_native_in_out_test.gnoA new file mode 100644 index 00000000..e041af88 --- /dev/null +++ b/router/_TEST_/_TEST_router_swap_route_1route_2hop_wrapped_native_in_out_test.gnoA @@ -0,0 +1,144 @@ +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" + "gno.land/r/demo/qux" + "gno.land/r/demo/wugnot" +) + +func TestCreatePool(t *testing.T) { + std.TestSetPrevAddr(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*3) + + pl.CreatePool(barPath, bazPath, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500}] + + pl.CreatePool(bazPath, quxPath, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500}]} + + pl.CreatePool(quxPath, consts.WRAPPED_WUGNOT, fee500, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/qux:gno.land/r/demo/wugnot:500}] + + // 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(gsa) + 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, gsa.String()) + // vent: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 1} {liquidity 1243732} {amount0 36790} {amount1 100000}]} + + shouldEQ(t, tokenId, uint64(1)) + shouldEQ(t, amount0, "36790") // bar + shouldEQ(t, amount1, "100000") // baz +} + +func TestPositionMintBazQux(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 2} {liquidity 1243732} {amount0 36790} {amount1 100000}]} + + shouldEQ(t, tokenId, uint64(2)) + shouldEQ(t, amount0, "36790") + shouldEQ(t, amount1, "100000") + +} + +func TestPositionMintQuxGnot(t *testing.T) { + std.TestSetPrevAddr(gsa) + + // send + std.TestIssueCoins(gsa, 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(gsa) + 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, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/qux:gno.land/r/demo/wugnot:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 3} {liquidity 1243732} {amount0 36790} {amount1 100000}]} + + shouldEQ(t, tokenId, uint64(3)) + shouldEQ(t, amount0, "36790") + shouldEQ(t, amount1, "100000") +} + +func TestDrySwapRouteBarGnotExactIn(t *testing.T) { + std.TestSetOrigCaller(gsa) + + 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(gsa) + + 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, "1014") +} + +func TestDrySwapRouteGnotBarExactIn(t *testing.T) { + std.TestSetOrigCaller(gsa) + + 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(gsa) + + 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, "2027") +} diff --git a/router/_TEST_/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gnoA b/router/_TEST_/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gnoA new file mode 100644 index 00000000..89806eba --- /dev/null +++ b/router/_TEST_/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gnoA @@ -0,0 +1,118 @@ +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/gns" + "gno.land/r/demo/wugnot" +) + +func TestCreatePool(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/gns:gno.land/r/demo/wugnot:100}]} + + pl.CreatePool(consts.WRAPPED_WUGNOT, barPath, fee100, "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/wugnot:100}]} + + // 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(gsa) + + // send + std.TestIssueCoins(gsa, std.Coins{{"ugnot", 100000}}) + std.TestSetOrigSend(std.Coins{{"ugnot", 100000}}, nil) + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 100000) + + // Deposit(wrap) + std.TestSetPrevAddr(gsa) + wugnot.Deposit() + + 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, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/gns:gno.land/r/demo/wugnot:100} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 1} {liquidity 1243732} {amount0 36790} {amount1 100000}]} + + shouldEQ(t, tokenId, uint64(1)) + shouldEQ(t, amount0, "36790") + shouldEQ(t, amount1, "100000") +} + +func TestPositionMintGnotBar(t *testing.T) { + std.TestSetOrigCaller(gsa) + + // send + std.TestIssueCoins(gsa, std.Coins{{"ugnot", 100000}}) + std.TestSetOrigSend(std.Coins{{"ugnot", 100000}}, nil) + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 100000) + + // Deposit(wrap) + std.TestSetPrevAddr(gsa) + 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, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/wugnot:100} {p_tickLower -11000} {p_tickUpper -9000} {tokenId 2} {liquidity 1243732} {amount0 100000} {amount1 36790}]} + + shouldEQ(t, tokenId, uint64(2)) + shouldEQ(t, amount0, "100000") // 100000 + shouldEQ(t, amount1, "36790") // 36790 +} + +func TestDrySwapRouteGnsBarExactIn(t *testing.T) { + std.TestSetOrigCaller(gsa) + + 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(gsa) + + 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 + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/gns:gno.land/r/demo/wugnot:100} {p_zeroForOne true} {p_amountSpecified 1000} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 1000} {amount1 -2711} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/wugnot:100} {p_zeroForOne false} {p_amountSpecified 2711} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 -7339} {amount1 2711} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/gns} {p_output gno.land/r/demo/bar} {p_swapType EXACT_IN} {p_amountSpecified 1000} {p_route gno.land/r/demo/gns:gno.land/r/demo/wugnot:100*POOL*gno.land/r/demo/wugnot:gno.land/r/demo/bar:100} {p_quote 100} {amountIn 1000} {amountOut -7328}]} + + shouldEQ(t, amountIn, "1000") + shouldEQ(t, amountOut, "-7328") +} diff --git a/router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gnoA b/router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gnoA new file mode 100644 index 00000000..2f60d859 --- /dev/null +++ b/router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gnoA @@ -0,0 +1,201 @@ +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" + "gno.land/r/demo/qux" +) + +func TestCreatePool(t *testing.T) { + std.TestSetPrevAddr(gsa) + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*2) + + pl.CreatePool(barPath, bazPath, uint16(500), "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500}]} + + pl.CreatePool(bazPath, quxPath, uint16(500), "130621891405341611593710811006") // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500}]} + + // jsonOutput := pl.ApiGetPools() + // jsonStr := gjson.Parse(jsonOutput) + // shouldEQ(t, len(jsonStr.Get("response").Array()), 2) +} + +func TestPositionMint(t *testing.T) { + // bar_baz_500 by gsa + std.TestSetPrevAddr(gsa) + + 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, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 1} {liquidity 1243732} {amount0 36790} {amount1 100000}]} + + pn.Mint(bazPath, quxPath, uint16(500), int32(9000), int32(11000), "100000", "100000", "0", "0", max_timeout, gsa.String()) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 2} {liquidity 1243732} {amount0 36790} {amount1 100000}]} +} + +func TestDrySwapRouteBarQuxExactIn(t *testing.T) { + std.TestSetOrigCaller(gsa) + + 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(gsa) + + 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 + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne true} {p_amountSpecified 500} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 500} {amount1 -1355} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne true} {p_amountSpecified 1355} {p_sqrtPriceLimitX96 4295128740} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 1355} {amount1 -3673} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne true} {p_amountSpecified 500} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 500} {amount1 -1353} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne true} {p_amountSpecified 1353} {p_sqrtPriceLimitX96 4295128740} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 1353} {amount1 -3655} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/bar} {p_output gno.land/r/demo/qux} {p_swapType EXACT_IN} {p_amountSpecified 1000} {p_route 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} {p_quote 50,50} {amountIn 1000} {amountOut -7318}]} + + shouldEQ(t, amountIn, "1000") + shouldEQ(t, amountOut, "-7318") +} + +func TestDrySwapRouteBarQuxExactOut(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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, "140") +} + +func TestSwapRouteBarQuxExactOut(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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 + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne true} {p_amountSpecified -187} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 70} {amount1 -187} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne true} {p_amountSpecified 187} {p_sqrtPriceLimitX96 4295128740} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 187} {amount1 -501} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne true} {p_amountSpecified -187} {p_sqrtPriceLimitX96 4295128740} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 70} {amount1 -187} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne true} {p_amountSpecified 187} {p_sqrtPriceLimitX96 4295128740} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 187} {amount1 -501} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/bar} {p_output gno.land/r/demo/qux} {p_swapType EXACT_OUT} {p_amountSpecified 1000} {p_route 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} {p_quote 50,50} {amountIn 140} {amountOut -1001}]} + + shouldEQ(t, amountIn, "140") + shouldEQ(t, amountOut, "-1001") +} + +func TestDrySwapRouteQuxBarExactIn(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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(gsa) + + 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(gsa) + + 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 + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne false} {p_amountSpecified 300} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 -110} {amount1 300} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne false} {p_amountSpecified 110} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 -40} {amount1 110} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne false} {p_amountSpecified 700} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 -259} {amount1 700} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne false} {p_amountSpecified 259} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 -95} {amount1 259} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/qux} {p_output gno.land/r/demo/bar} {p_swapType EXACT_IN} {p_amountSpecified 1000} {p_route 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} {p_quote 30,70} {amountIn 1000} {amountOut -135}]} + + shouldEQ(t, dryResult, "7351") +} + +func TestwapRouteQuxBarExactOut(t *testing.T) { + std.TestSetPrevAddr(gsa) + + 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 + ) + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne false} {p_amountSpecified -815} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 -815} {amount1 2204} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne false} {p_amountSpecified 815} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 -300} {amount1 815} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/baz:gno.land/r/demo/qux:500} {p_zeroForOne false} {p_amountSpecified -1902} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {p_recipient g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {amount0 -1902} {amount1 5161} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/pool Swap [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/router} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:500} {p_zeroForOne false} {p_amountSpecified 1902} {p_sqrtPriceLimitX96 1461446703485210103287273052203988822378723970341} {p_payer g1pjtpgjpsn4hjfv2n4mpz8cczdn32jkpsqwxuav} {p_recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 -700} {amount1 1902} {protocol_fee0 0} {protocol_fee1 0}]} + // event: {GNOSWAP gno.land/r/demo/router SwapRoute [{p_input gno.land/r/demo/qux} {p_output gno.land/r/demo/bar} {p_swapType EXACT_OUT} {p_amountSpecified 1000} {p_route 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} {p_quote 30,70} {amountIn 7365} {amountOut -999}]} + + shouldEQ(t, amountIn, "7365") + shouldEQ(t, amountOut, "-999") +} diff --git a/router/_TEST_/z1_ratio_filetest.gno b/router/_TEST_/z1_ratio_filetest.gno deleted file mode 100644 index 7f7f96ef..00000000 --- a/router/_TEST_/z1_ratio_filetest.gno +++ /dev/null @@ -1,336 +0,0 @@ -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_/z1_swap_route_1route_1hop_filetest.gno similarity index 93% rename from router/_TEST_/z2_swap_route_1route_1hop_filetest.gno rename to router/_TEST_/z1_swap_route_1route_1hop_filetest.gno index fdbc0cba..f58ef90a 100644 --- a/router/_TEST_/z2_swap_route_1route_1hop_filetest.gno +++ b/router/_TEST_/z1_swap_route_1route_1hop_filetest.gno @@ -23,8 +23,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -170,17 +169,6 @@ func ugnotBalanceOf(addr std.Address) uint64 { } 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{}) @@ -215,12 +203,7 @@ func main() { } func createPool() { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 1); i++ { - gns.Faucet() - std.TestSkipHeights(1) - } + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) std.TestSkipHeights(1) @@ -231,12 +214,12 @@ func createPool() { } func mint() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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()) + _, _, amount0, amount1 := pn.Mint(barPath, bazPath, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", maxTimeout, gsa.String()) if amount0 != "36790" { "router_z2_mint() || amount0 should be 36790" } @@ -246,7 +229,7 @@ func mint() { } func drySwapBarBazExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( barPath, // inputToken @@ -262,7 +245,7 @@ func drySwapBarBazExactIn() { } func swapBarBazExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), uint64(1000)) baz.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee @@ -285,7 +268,7 @@ func swapBarBazExactIn() { } func drySwapBarBazExactOut() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( barPath, // inputToken @@ -301,7 +284,7 @@ func drySwapBarBazExactOut() { } func swapBarBazExactOut() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), uint64(1000)) baz.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee @@ -324,7 +307,7 @@ func swapBarBazExactOut() { } func drySwapBazBarExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( bazPath, // inputToken @@ -340,7 +323,7 @@ func drySwapBazBarExactIn() { } func swapBazBarExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) bar.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee @@ -363,7 +346,7 @@ func swapBazBarExactIn() { } func drySwapBazBarExactOut() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( bazPath, // inputToken @@ -379,7 +362,7 @@ func drySwapBazBarExactOut() { } func swapBazBarExactOut() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) amountIn, amountOut := rr.SwapRoute( diff --git a/router/_TEST_/z3_swap_route_1route_1hop_out_range_filetest.gno b/router/_TEST_/z2_swap_route_1route_1hop_out_range_filetest.gno similarity index 93% rename from router/_TEST_/z3_swap_route_1route_1hop_out_range_filetest.gno rename to router/_TEST_/z2_swap_route_1route_1hop_out_range_filetest.gno index 41640c5c..a2a87cb5 100644 --- a/router/_TEST_/z3_swap_route_1route_1hop_out_range_filetest.gno +++ b/router/_TEST_/z2_swap_route_1route_1hop_out_range_filetest.gno @@ -23,8 +23,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -170,17 +169,6 @@ func ugnotBalanceOf(addr std.Address) uint64 { } 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{}) @@ -209,12 +197,7 @@ func main() { } func createPool() { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 1); i++ { - gns.Faucet() - std.TestSkipHeights(1) - } + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) std.TestSkipHeights(1) @@ -225,12 +208,12 @@ func createPool() { } func mint() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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()) + _, _, amount0, amount1 := pn.Mint(barPath, bazPath, fee500, int32(12000), int32(15000), "100000", "100000", "0", "0", maxTimeout, gsa.String()) // only bar, no baz if amount0 != "36790" { "router_z3_mint() || amount0 should be 10000" @@ -241,7 +224,7 @@ func mint() { } func drySwapBarBazExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( barPath, // inputToken @@ -257,7 +240,7 @@ func drySwapBarBazExactIn() { } func swapBarBazExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), uint64(1000)) baz.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee diff --git a/router/_TEST_/z4_swap_route_1route_1hop_native_in_out_filetest.gno b/router/_TEST_/z3_swap_route_1route_1hop_native_in_out_filetest.gno similarity index 92% rename from router/_TEST_/z4_swap_route_1route_1hop_native_in_out_filetest.gno rename to router/_TEST_/z3_swap_route_1route_1hop_native_in_out_filetest.gno index c32d962f..bb9a95e1 100644 --- a/router/_TEST_/z4_swap_route_1route_1hop_native_in_out_filetest.gno +++ b/router/_TEST_/z3_swap_route_1route_1hop_native_in_out_filetest.gno @@ -23,8 +23,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -170,17 +169,6 @@ func ugnotBalanceOf(addr std.Address) uint64 { } 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{}) @@ -213,12 +201,7 @@ func main() { } func createPool() { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 3); i++ { - gns.Faucet() - std.TestSkipHeights(1) - } + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*3) std.TestSkipHeights(1) @@ -231,12 +214,12 @@ func createPool() { } func mintBarBaz() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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()) + _, _, amount0, amount1 := pn.Mint(barPath, bazPath, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", maxTimeout, gsa.String()) if amount0 != "36790" { "router_z4_mintBarBaz() || amount0 should be 36790" } @@ -246,12 +229,12 @@ func mintBarBaz() { } func mintBazQux() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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()) + _, _, amount0, amount1 := pn.Mint(bazPath, quxPath, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", maxTimeout, gsa.String()) if amount0 != "36790" { "router_z4_mintBazQux() || amount0 should be 36790" } @@ -261,23 +244,23 @@ func mintBazQux() { } func mintQuxGnot() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) // send - std.TestIssueCoins(test1, std.Coins{{"ugnot", 1000009}}) + std.TestIssueCoins(gsa, 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) + std.TestSetPrevAddr(gsa) 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()) + _, _, amount0, amount1 := pn.Mint(quxPath, consts.WRAPPED_WUGNOT, fee500, int32(9000), int32(11000), "100000", "100000", "0", "0", maxTimeout, gsa.String()) if amount0 != "36790" { "router_z4_mintQuxGnot() || amount0 should be 36790" } @@ -287,7 +270,7 @@ func mintQuxGnot() { } func drySwapBarGnotExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( barPath, // inputToken @@ -303,7 +286,7 @@ func drySwapBarGnotExactIn() { } func drySwapBarGnotExactOut() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( barPath, // inputToken @@ -319,7 +302,7 @@ func drySwapBarGnotExactOut() { } func drySwapGnotBarExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( consts.WRAPPED_WUGNOT, // intputToken @@ -335,7 +318,7 @@ func drySwapGnotBarExactIn() { } func drySwapGnotBarExactOut() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( consts.WRAPPED_WUGNOT, // intputToken diff --git a/router/_TEST_/z5_swap_route_1route_2hop_wrapped_middle_filetest.gno b/router/_TEST_/z4_swap_route_1route_2hop_wrapped_middle_filetest.gno similarity index 90% rename from router/_TEST_/z5_swap_route_1route_2hop_wrapped_middle_filetest.gno rename to router/_TEST_/z4_swap_route_1route_2hop_wrapped_middle_filetest.gno index d4fbac73..38430df5 100644 --- a/router/_TEST_/z5_swap_route_1route_2hop_wrapped_middle_filetest.gno +++ b/router/_TEST_/z4_swap_route_1route_2hop_wrapped_middle_filetest.gno @@ -23,8 +23,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -170,17 +169,6 @@ func ugnotBalanceOf(addr std.Address) uint64 { } 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{}) @@ -210,12 +198,7 @@ func main() { } func createPool() { - // call gns faucet to get creation fee - std.TestSetPrevAddr(test1) - for i := 0; i < (5 * 2); i++ { - gns.Faucet() - std.TestSkipHeights(1) - } + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*2) std.TestSkipHeights(1) @@ -227,25 +210,23 @@ func createPool() { } func mintGnsGnot() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) // send - std.TestIssueCoins(test1, std.Coins{{"ugnot", 999999}}) + std.TestIssueCoins(gsa, 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) + std.TestSetPrevAddr(gsa) 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()) + _, _, amount0, amount1 := pn.Mint(consts.GNS_PATH, consts.WRAPPED_WUGNOT, fee100, int32(9000), int32(11000), "100000", "100000", "0", "0", maxTimeout, gsa.String()) if amount0 != "36790" { "router_z5_mintGnsGnot() || amount0 should be 36790" } @@ -255,27 +236,27 @@ func mintGnsGnot() { } func mintGnotBar() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) // send - std.TestIssueCoins(test1, std.Coins{{"ugnot", 999999}}) + std.TestIssueCoins(gsa, 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) + std.TestSetPrevAddr(gsa) 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()) + tokenId, liquidity, amount0, amount1 := pn.Mint(consts.WRAPPED_WUGNOT, barPath, fee100, int32(9000), int32(11000), "100000", "100000", "0", "0", maxTimeout, gsa.String()) } func drySwapGnsBarExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( consts.GNS_PATH, // inputToken @@ -291,7 +272,7 @@ func drySwapGnsBarExactIn() { } func swapGnsBarExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) gns.Approve(a2u(consts.POOL_ADDR), 1000) // swap input amount bar.Approve(a2u(consts.ROUTER_ADDR), 7328) // 0.15% fee diff --git a/router/_TEST_/z6_dry_and_swap_filetest.gno b/router/_TEST_/z5_dry_and_swap_filetest.gno similarity index 93% rename from router/_TEST_/z6_dry_and_swap_filetest.gno rename to router/_TEST_/z5_dry_and_swap_filetest.gno index d3b7781e..ec0bac8d 100644 --- a/router/_TEST_/z6_dry_and_swap_filetest.gno +++ b/router/_TEST_/z5_dry_and_swap_filetest.gno @@ -23,8 +23,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -170,17 +169,6 @@ func ugnotBalanceOf(addr std.Address) uint64 { } 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{}) @@ -225,18 +213,7 @@ func main() { } func createPool() { - numPool := uint64(3) - - // 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) + std.TestSetPrevAddr(gsa) // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 @@ -258,12 +235,12 @@ func createPool() { } func mintBarBaz() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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, fee100, int32(9000), int32(11000), "100000000", "100000000", "0", "0", maxTimeout, test1.String()) + _, _, amount0, amount1 := pn.Mint(barPath, bazPath, fee100, int32(9000), int32(11000), "100000000", "100000000", "0", "0", maxTimeout, gsa.String()) if amount0 != "36789784" { panic("router_z6_mintBarBaz() || amount0 should be 36789784") } @@ -273,12 +250,12 @@ func mintBarBaz() { } func mintBazFoo() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) foo.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) std.TestSetPrevRealm(consts.STAKER_PATH) - _, _, amount0, amount1 := pn.Mint(bazPath, fooPath, fee100, int32(9000), int32(11000), "100000000", "100000000", "0", "0", maxTimeout, test1.String()) + _, _, amount0, amount1 := pn.Mint(bazPath, fooPath, fee100, int32(9000), int32(11000), "100000000", "100000000", "0", "0", maxTimeout, gsa.String()) if amount0 != "36789784" { panic("router_z6_mintBazFoo() || amount0 should be 36789784") } @@ -288,12 +265,12 @@ func mintBazFoo() { } func mintFooQux() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) foo.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(fooPath, quxPath, fee100, int32(9000), int32(11000), "100000000", "100000000", "0", "0", maxTimeout, test1.String()) + _, _, amount0, amount1 := pn.Mint(fooPath, quxPath, fee100, int32(9000), int32(11000), "100000000", "100000000", "0", "0", maxTimeout, gsa.String()) if amount0 != "36789784" { panic("router_z6_mintBazFoo() || amount0 should be 36789784") } @@ -304,7 +281,7 @@ func mintFooQux() { // BAR > BAZ > ExactIn || DRY func drySwapBarBazExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( barPath, // inputToken @@ -321,7 +298,7 @@ func drySwapBarBazExactIn() { // BAR > BAZ > ExactIn || SWAP func swapBarBazExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // swap input amount baz.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // 0.15% fee @@ -345,7 +322,7 @@ func swapBarBazExactIn() { // BAZ > BAR > ExactIn || DRY func drySwapBazBarExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( bazPath, // inputToken @@ -362,7 +339,7 @@ func drySwapBazBarExactIn() { // BAZ > BAR > ExactIn || SWAP func swapBazBarExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // swap input amount bar.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // 0.15% fee @@ -386,7 +363,7 @@ func swapBazBarExactIn() { // BAR > BAZ > ExactOut || DRY func drySwapBarBazExactOut() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( barPath, // inputToken @@ -403,7 +380,7 @@ func drySwapBarBazExactOut() { // BAR > BAZ > ExactOut || SWAP func swapBarBazExactOut() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // swap input amount baz.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // 0.15% fee @@ -427,7 +404,7 @@ func swapBarBazExactOut() { // BAR > QUX > ExactIn || DRY func drySwapBarQuxExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( barPath, // inputToken @@ -444,7 +421,7 @@ func drySwapBarQuxExactIn() { // BAR > QUX > ExactIn || SWAP func swapBarQuxExactIn() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // swap input amount qux.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // 0.15% fee @@ -469,7 +446,7 @@ func swapBarQuxExactIn() { // QUX > BAR > ExactOut || DRY func drySwapQuxBarExactOut() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) dryResult := rr.DrySwapRoute( quxPath, // inputToken @@ -486,7 +463,7 @@ func drySwapQuxBarExactOut() { // QUX > BAR > ExactOut || SWAP func swapQuxBarExactOut() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) // swap input amount bar.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // 0.15% fee diff --git a/router/router.gno b/router/router.gno index 8c7f4867..fcc1a432 100644 --- a/router/router.gno +++ b/router/router.gno @@ -148,9 +148,21 @@ func SwapRoute( panic(ufmt.Sprintf("[ROUTER] router.gno__SwapRoute() || too much spend for user (expected maximum spend:%s, actual spend:%s)", _tokenAmountLimit, resultAmountIn.ToString())) } } - //return resultAmountIn.ToString(), resultAmountOut.ToString() intAmountOut := i256.FromUint256(resultAmountOut) + + std.Emit( + "GNOSWAP", + "p_input", inputToken, + "p_output", outputToken, + "p_swapType", swapType, + "p_amountSpecified", _amountSpecified, + "p_route", strRouteArr, + "p_quote", quoteArr, + "amountIn", resultAmountIn.ToString(), + "amountOut", i256.Zero().Neg(intAmountOut).ToString(), + ) + return resultAmountIn.ToString(), i256.Zero().Neg(intAmountOut).ToString() } diff --git a/router/utils.gno b/router/utils.gno index 0a13cba9..db5fe4da 100644 --- a/router/utils.gno +++ b/router/utils.gno @@ -85,3 +85,11 @@ func min(a, b int) int { } return b } + +func origCaller() string { + return std.GetOrigCaller().String() +} + +func prevRealm() string { + return std.PrevRealm().PkgPath() +} From a26385bb1759c1bd084977d8a84ad436c33fce0c Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 15 May 2024 21:32:35 +0900 Subject: [PATCH 08/10] feat: apply emit & event in staker --- .../_TEST_0_INIT_TOKEN_REGISTER_test.gno | 155 +++++++++ .../_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno | 74 ++++ .../_TEST_staker_collect_reward_test.gnoA | 230 +++++++++++++ staker/_TEST_/_TEST_staker_full_test.gnoA | 278 +++++++++++++++ staker/_TEST_/_TEST_staker_get_test.gnoA | 147 ++++++++ .../_TEST_staker_internal_external_test.gnoA | 320 ++++++++++++++++++ .../_TEST_staker_manage_pool_tiers_test.gnoA | 59 ++++ .../_TEST_staker_mint_and_stake_test.gn | 92 +++++ ..._TEST_staker_one_external_native_test.gnoA | 214 ++++++++++++ .../_TEST_staker_one_external_test.gnoA | 204 +++++++++++ ...EST_staker_one_increase_external_test.gnoA | 248 ++++++++++++++ .../z1_one_increase_external_filetest.gno | 41 +-- staker/_TEST_/z2_one_external_filetest.gno | 61 ++-- .../_TEST_/z3_internal_external_filetest.gno | 101 +++--- staker/_TEST_/z4_mint_and_stake_filetest.gno | 33 +- .../_TEST_/z5_manage_pool_tier_filetest.gno | 25 +- staker/manage_pool_tiers.gno | 17 + staker/mint_stake.gno | 15 +- staker/reward_fee.gno | 14 + staker/staker.gno | 75 +++- staker/utils.gno | 24 ++ 21 files changed, 2252 insertions(+), 175 deletions(-) create mode 100644 staker/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno create mode 100644 staker/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno create mode 100644 staker/_TEST_/_TEST_staker_collect_reward_test.gnoA create mode 100644 staker/_TEST_/_TEST_staker_full_test.gnoA create mode 100644 staker/_TEST_/_TEST_staker_get_test.gnoA create mode 100644 staker/_TEST_/_TEST_staker_internal_external_test.gnoA create mode 100644 staker/_TEST_/_TEST_staker_manage_pool_tiers_test.gnoA create mode 100644 staker/_TEST_/_TEST_staker_mint_and_stake_test.gn create mode 100644 staker/_TEST_/_TEST_staker_one_external_native_test.gnoA create mode 100644 staker/_TEST_/_TEST_staker_one_external_test.gnoA create mode 100644 staker/_TEST_/_TEST_staker_one_increase_external_test.gnoA diff --git a/staker/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno b/staker/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno new file mode 100644 index 00000000..f181db81 --- /dev/null +++ b/staker/_TEST_/_TEST_0_INIT_TOKEN_REGISTER_test.gno @@ -0,0 +1,155 @@ +package staker + +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) + + // POOL + pl.RegisterGRC20Interface("gno.land/r/demo/bar", BarToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/foo", FooToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/baz", BazToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/qux", QuxToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) + pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) + + // STAKER + 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{}) +} diff --git a/staker/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno b/staker/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno new file mode 100644 index 00000000..9a28b234 --- /dev/null +++ b/staker/_TEST_/_TEST_0_INIT_VARIABLE_AND_HELPER_test.gno @@ -0,0 +1,74 @@ +package staker + +import ( + "std" + "testing" + + "gno.land/r/demo/gnoswap/consts" +) + +var ( + gsa std.Address = consts.GNOSWAP_ADMIN + + 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.gnoA b/staker/_TEST_/_TEST_staker_collect_reward_test.gnoA new file mode 100644 index 00000000..136809d4 --- /dev/null +++ b/staker/_TEST_/_TEST_staker_collect_reward_test.gnoA @@ -0,0 +1,230 @@ +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(gsa) + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + + pl.CreatePool(barPath, quxPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500}]} + + std.TestSkipHeights(1) +} + +func TestPositionMint01(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 1} {liquidity 12437} {amount0 368} {amount1 1000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, lpTokenId, 1) + shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "368") + shouldEQ(t, amount1, "1000") + + // approve nft to staker + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) + std.TestSkipHeights(1) +} + +func TestPositionMint02(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_tickLower 9100} {p_tickUpper 12000} {tokenId 2} {liquidity 68925} {amount0 3979} {amount1 5000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, lpTokenId, 2) + shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "3979") + shouldEQ(t, amount1, "5000") + + // approve nft to staker + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) + std.TestSkipHeights(1) +} + +func TestCreateExternalIncentive(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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) + // event: {GNOSWAP gno.land/r/demo/staker CreateExternalIncentive [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_rewardToken gno.land/r/demo/obl} {p_rewardAmount 1000000000} {p_startTimestamp 1234569600} {p_endTimestamp 1242345600}]} + + std.TestSkipHeights(1) +} + +func TestStakeToken01(t *testing.T) { + std.TestSetPrevAddr(gsa) + StakeToken(1) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {amount0 368} {amount1 1000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker + shouldEQ(t, len(deposits), 1) +} + +func TestStakeToken02(t *testing.T) { + std.TestSetPrevAddr(gsa) + StakeToken(2) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {amount0 3979} {amount1 5000}]} + + 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(gsa)) + std.TestSkipHeights(1) + shouldEQ(t, gnsOld, 500000000000000) + + std.TestSetPrevRealm(consts.STAKER_PATH) + std.TestSetOrigCaller(gsa) + CollectReward(1) // GNFT tokenId + // --- event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 247}]} + // --- event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 24502}]} + + std.TestSkipHeights(1) + + gnsNew := gns.BalanceOf(a2u(gsa)) + std.TestSkipHeights(1) + shouldEQ(t, gnsNew, 500000000024502) +} + +func TestUnstakeToken01(t *testing.T) { + std.TestSetPrevAddr(gsa) + std.TestSkipHeights(400) // spend more time + + UnstakeToken(1) // GNFT tokenId + // --- event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/obl} {p_internal false} {feeAmount 48}]} + // --- event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/obl} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 4851}]} + // --- event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 24873}]} + // --- event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 2462502}]} + // --- event: {GNOSWAP gno.land/r/demo/staker UnstakeToken [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {from g1puv9dz470prjshjm9qyg25dyfvrgph2kvjph68} {to g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 368} {amount1 1000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(1)), gsa) + + // check reward + shouldEQ(t, gns.BalanceOf(a2u(gsa)), 500000002487004) // internal + shouldEQ(t, obl.BalanceOf(a2u(gsa)), 99998000004851) // external +} + +func TestUnstakeToken02(t *testing.T) { + std.TestSetPrevAddr(gsa) + UnstakeToken(2) // GNFT tokenId + // --- event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/obl} {p_internal false} {feeAmount 330}]} + // --- event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/obl} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 32757}]} + // --- event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 167475}]} + // --- event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 16580025}]} + // --- event: {GNOSWAP gno.land/r/demo/staker UnstakeToken [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {from g1puv9dz470prjshjm9qyg25dyfvrgph2kvjph68} {to g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 3979} {amount1 5000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(2)), gsa) + + // check reward + shouldEQ(t, gns.BalanceOf(a2u(gsa)), 500000019067029) // internal + shouldEQ(t, obl.BalanceOf(a2u(gsa)), 99998000037608) // external +} + +func TestEndExternalIncentive(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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() + // event: {GNOSWAP gno.land/r/demo/staker EndExternalIncentive [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_rewardToken gno.land/r/demo/obl} {p_refundee g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {end_by g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {refund_amount 0}]} + + 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_full_test.gnoA b/staker/_TEST_/_TEST_staker_full_test.gnoA new file mode 100644 index 00000000..a43a4364 --- /dev/null +++ b/staker/_TEST_/_TEST_staker_full_test.gnoA @@ -0,0 +1,278 @@ +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/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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + pl.CreatePool(barPath, quxPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500}]} + + std.TestSkipHeights(1) +} + +func TestPositionMint01(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 1} {liquidity 12437} {amount0 368} {amount1 1000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, lpTokenId, 1) + shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "368") + shouldEQ(t, amount1, "1000") + + // approve nft to staker for staking + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) + std.TestSkipHeights(1) +} + +func TestPositionMint02(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_tickLower 9100} {p_tickUpper 12000} {tokenId 2} {liquidity 68925} {amount0 3979} {amount1 5000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, lpTokenId, 2) + shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "3979") + shouldEQ(t, amount1, "5000") + + // approve nft to staker + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) + std.TestSkipHeights(1) +} + +func TestCreateExternalIncentive(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + ) + // event: {GNOSWAP gno.land/r/demo/staker CreateExternalIncentive [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_rewardToken gno.land/r/demo/obl} {p_rewardAmount 1000000000} {p_startTimestamp 1234569600} {p_endTimestamp 1242345600}]} + + 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(gsa) + StakeToken(1) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {amount0 368} {amount1 1000}]} + + std.TestSkipHeights(500) + + shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker + shouldEQ(t, len(deposits), 1) +} + +func TestStakeToken02(t *testing.T) { + std.TestSetPrevAddr(gsa) + StakeToken(2) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {amount0 3979} {amount1 5000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(2)), GetOrigPkgAddr()) // staker + shouldEQ(t, len(deposits), 2) +} + +func TestMintAndStake(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm gno.land/r/demo/staker} {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 3} {liquidity 12437} {amount0 368} {amount1 1000}]} + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 3} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {amount0 368} {amount1 1000}]} + + std.TestSkipHeights(1) +} + +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(), 3) // lpTokenId 1, 2, 3 +} + +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(), 3) // lpTokenId 1, 2, 3 +} + +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(gsa)) + shouldEQ(t, gnsOld, 500000000000000) + + std.TestSetPrevAddr(gsa) + CollectReward(1) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/obl} {p_internal false} {feeAmount 97}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/obl} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 9663}]} + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 27080}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 2680982}]} + + std.TestSkipHeights(1) + + gnsNew := gns.BalanceOf(a2u(gsa)) + shouldEQ(t, gnsNew, 500000002680982) +} + +func TestUnstakeToken01(t *testing.T) { + std.TestSetPrevAddr(gsa) + UnstakeToken(1) // GNFT tokenId + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(1)), gsa) + + // check reward + shouldEQ(t, gns.BalanceOf(a2u(gsa)), 500000002686291) // internal + shouldEQ(t, obl.BalanceOf(a2u(gsa)), 99998000009718) // external +} + +func TestUnstakeToken02(t *testing.T) { + std.TestSetPrevAddr(gsa) + UnstakeToken(2) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/obl} {p_internal false} {feeAmount 24}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/obl} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 2470}]} + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 2425}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 240124}]} + // event: {GNOSWAP gno.land/r/demo/staker UnstakeToken [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {from g1puv9dz470prjshjm9qyg25dyfvrgph2kvjph68} {to g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 3979} {amount1 5000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(2)), gsa) + + // check reward + shouldEQ(t, gns.BalanceOf(a2u(gsa)), 500000002926415) + // internal + shouldEQ(t, obl.BalanceOf(a2u(gsa)), 99998000012188) // external +} + +func TestEndExternalIncentive(t *testing.T) { + std.TestSetOrigCaller(gsa) + 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() + // event: {GNOSWAP gno.land/r/demo/staker EndExternalIncentive [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_rewardToken gno.land/r/demo/obl} {p_refundee g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {end_by g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {refund_amount 0}]} + + 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.gnoA b/staker/_TEST_/_TEST_staker_get_test.gnoA new file mode 100644 index 00000000..a64d98d0 --- /dev/null +++ b/staker/_TEST_/_TEST_staker_get_test.gnoA @@ -0,0 +1,147 @@ +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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(2) + + pl.CreatePool(barPath, quxPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500}]} + + std.TestSkipHeights(1) +} + +func TestPositionMint01(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 1} {liquidity 12437} {amount0 368} {amount1 1000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, lpTokenId, 1) + shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "368") + shouldEQ(t, amount1, "1000") + + // approve nft to staker + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) + std.TestSkipHeights(1) +} + +func TestPositionMint02(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_tickLower 9100} {p_tickUpper 12000} {tokenId 2} {liquidity 68925} {amount0 3979} {amount1 5000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, lpTokenId, 2) + shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "3979") + shouldEQ(t, amount1, "5000") + + // approve nft to staker + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) + std.TestSkipHeights(1) +} + +func TestCreateExternalIncentive(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + ) + // event: {GNOSWAP gno.land/r/demo/staker CreateExternalIncentive [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_rewardToken gno.land/r/demo/obl} {p_rewardAmount 1000000000} {p_startTimestamp 1234569600} {p_endTimestamp 1242345600}]} + + 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(gsa) + StakeToken(1) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {amount0 368} {amount1 1000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker + shouldEQ(t, len(deposits), 1) +} + +// func TestStakeToken02(t *testing.T) { +// std.TestSetPrevAddr(gsa) +// 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.gnoA b/staker/_TEST_/_TEST_staker_internal_external_test.gnoA new file mode 100644 index 00000000..f82f53e9 --- /dev/null +++ b/staker/_TEST_/_TEST_staker_internal_external_test.gnoA @@ -0,0 +1,320 @@ +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(gsa) + + 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 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500}]} + + pl.CreatePool(barPath, bazPath, 100, "79228162514264337593543950337") // internal, tier 2 // tick 0 ≈ x1 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:100}]} + + pl.CreatePool(fooPath, quxPath, 500, "79228162514264337593543950337") // external // tick 0 ≈ x1 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/foo:gno.land/r/demo/qux:500}]} + + std.TestSkipHeights(3) +} + +func TestMintBarQux500(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 1} {liquidity 1243732} {amount0 36790} {amount1 100000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, tokenId, 1) + shouldEQ(t, gnft.OwnerOf(tid(tokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "36790") + shouldEQ(t, amount1, "100000") +} + +func TestMintBarBaz100(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, + gsa.String(), + ) + std.TestSkipHeights(1) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:100} {p_tickLower -1000} {p_tickUpper 1000} {tokenId 2} {liquidity 2050516} {amount0 100000} {amount1 100000}]} + + shouldEQ(t, tokenId, 2) + shouldEQ(t, gnft.OwnerOf(tid(tokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "100000") + shouldEQ(t, amount1, "100000") +} + +func TestMintFooQux500(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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, + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/foo:gno.land/r/demo/qux:500} {p_tickLower -1000} {p_tickUpper 1000} {tokenId 3} {liquidity 2050516} {amount0 100000} {amount1 100000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, tokenId, 3) + shouldEQ(t, gnft.OwnerOf(tid(tokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "100000") + shouldEQ(t, amount1, "100000") +} + +func TestCreateExternalIncentive(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + ) + // event: {GNOSWAP gno.land/r/demo/staker CreateExternalIncentive [{p_poolPath gno.land/r/demo/foo:gno.land/r/demo/qux:500} {p_rewardToken gno.land/r/demo/obl} {p_rewardAmount 100000000} {p_startTimestamp 1234569600} {p_endTimestamp 1242345600}]} + + std.TestSkipHeights(1) +} + +func TestStakeInternalTier1(t *testing.T) { + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(1)) + StakeToken(1) + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {amount0 36790} {amount1 100000}]} + + std.TestSkipHeights(2) + + shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) + shouldEQ(t, len(deposits), 1) +} + +func TestStakeInternalTier2(t *testing.T) { + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(2)) + StakeToken(2) + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:100} {amount0 100000} {amount1 100000}]} + + std.TestSkipHeights(2) + + shouldEQ(t, gnft.OwnerOf(tid(2)), GetOrigPkgAddr()) + shouldEQ(t, len(deposits), 2) +} + +func TestStakeExternal(t *testing.T) { + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(3)) + StakeToken(3) + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 3} {poolPath gno.land/r/demo/foo:gno.land/r/demo/qux:500} {amount0 100000} {amount1 100000}]} + + 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(gsa)) + shouldEQ(t, gnsOld, 500000000000000) + + std.TestSetPrevAddr(gsa) + CollectReward(1) + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 2887}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 285863}]} + + std.TestSkipHeights(1) + + gnsNew := gns.BalanceOf(a2u(gsa)) + shouldEQ(t, gnsNew, 500000000285863) +} + +func TestCollectRewardToken2(t *testing.T) { + std.TestSetPrevAddr(gsa) + + // before claim + gnsOld := gns.BalanceOf(a2u(gsa)) + shouldEQ(t, gnsOld, 500000000285863) + + CollectReward(2) + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 1485}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:100} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 147015}]} + + std.TestSkipHeights(1) + + gnsNew := gns.BalanceOf(a2u(gsa)) + shouldEQ(t, gnsNew, 500000000432878) +} + +func TestCollectRewardToken1Again(t *testing.T) { + std.TestSetPrevAddr(gsa) + + // before claim + gnsOld := gns.BalanceOf(a2u(gsa)) + shouldEQ(t, gnsOld, 500000000432878) + + // ACCELERATE TIME + std.TestSkipHeights(1000) + + CollectReward(1) + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 413325}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 40919175}]} + + std.TestSkipHeights(1) + + gnsNew := gns.BalanceOf(a2u(gsa)) + shouldEQ(t, gnsNew, 500000041352053) +} + +func TestCollectExternalReward(t *testing.T) { + std.TestSetPrevAddr(gsa) + + // before claim + oblOld := obl.BalanceOf(a2u(gsa)) + shouldEQ(t, oblOld, 99999900000000) + + CollectReward(3) + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/obl} {p_internal false} {feeAmount 143}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 3} {poolPath gno.land/r/demo/foo:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/obl} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 14200}]} + + std.TestSkipHeights(1) + + oblNew := obl.BalanceOf(a2u(gsa)) + shouldEQ(t, oblNew, 99999900014200) +} + +func TestCollectAll(t *testing.T) { + std.TestSetPrevAddr(gsa) + + CollectReward(1) + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 825}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 81675}]} + + CollectReward(2) + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 248242}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:100} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 24576008}]} + + CollectReward(3) + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/obl} {p_internal false} {feeAmount 0}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 3} {poolPath gno.land/r/demo/foo:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/obl} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 21}]} + + std.TestSkipHeights(3) + // claimed all remaining reward +} + +func TestUnstakeTokens(t *testing.T) { + std.TestSetPrevAddr(gsa) + + gnsOld := gns.BalanceOf(a2u(gsa)) + oblOld := obl.BalanceOf(a2u(gsa)) + shouldEQ(t, gnsOld, 500000066009736) + shouldEQ(t, oblOld, 99999900014221) + + std.TestSkipHeights(100000) + UnstakeToken(1) + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 69030087}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 6833978663}]} + // event: {GNOSWAP gno.land/r/demo/staker UnstakeToken [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/qux:500} {from g1puv9dz470prjshjm9qyg25dyfvrgph2kvjph68} {to g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 36790} {amount1 100000}]} + + gnsNew := gns.BalanceOf(a2u(gsa)) + shouldEQ(t, gnsNew, 500006899988399) + + UnstakeToken(2) + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 41417722}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:100} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 4100354528}]} + // event: {GNOSWAP gno.land/r/demo/staker UnstakeToken [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/baz:100} {from g1puv9dz470prjshjm9qyg25dyfvrgph2kvjph68} {to g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 100000} {amount1 100000}]} + + gnsNew = gns.BalanceOf(a2u(gsa)) + shouldEQ(t, gnsNew, 500011000342927) + + UnstakeToken(3) + oblNew := obl.BalanceOf(a2u(gsa)) + shouldEQ(t, oblNew, 99999903488806) + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/obl} {p_internal false} {feeAmount 35096}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 3} {poolPath gno.land/r/demo/foo:gno.land/r/demo/qux:500} {rewardToken gno.land/r/demo/obl} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 3474585}]} + // event: {GNOSWAP gno.land/r/demo/staker UnstakeToken [{p_tokenId 3} {poolPath gno.land/r/demo/foo:gno.land/r/demo/qux:500} {from g1puv9dz470prjshjm9qyg25dyfvrgph2kvjph68} {to g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 100000} {amount1 100000}]} + + std.TestSkipHeights(1) +} diff --git a/staker/_TEST_/_TEST_staker_manage_pool_tiers_test.gnoA b/staker/_TEST_/_TEST_staker_manage_pool_tiers_test.gnoA new file mode 100644 index 00000000..55d086be --- /dev/null +++ b/staker/_TEST_/_TEST_staker_manage_pool_tiers_test.gnoA @@ -0,0 +1,59 @@ +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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + pl.CreatePool(fooPath, barPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + 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) + // event: {GNOSWAP gno.land/r/demo/staker SetPoolTier [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tier 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) + // event: {GNOSWAP gno.land/r/demo/staker ChangePoolTier [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tier 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") + // event: {GNOSWAP gno.land/r/demo/staker RemovePoolTier [{p_poolPath 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 new file mode 100644 index 00000000..ed19a6e0 --- /dev/null +++ b/staker/_TEST_/_TEST_staker_mint_and_stake_test.gn @@ -0,0 +1,92 @@ +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.gnoA b/staker/_TEST_/_TEST_staker_one_external_native_test.gnoA new file mode 100644 index 00000000..7991b78a --- /dev/null +++ b/staker/_TEST_/_TEST_staker_one_external_native_test.gnoA @@ -0,0 +1,214 @@ +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(gsa) + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + pl.CreatePool(barPath, fooPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + std.TestSkipHeights(1) +} + +func TestPositionMint01(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower 9000} {p_tickUpper 11000} {tokenId 1} {liquidity 12437} {amount0 368} {amount1 1000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, lpTokenId, 1) + shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "368") + shouldEQ(t, amount1, "1000") + + // approve nft to staker for staking + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) + std.TestSkipHeights(1) +} + +func TestPositionMint02(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower 9100} {p_tickUpper 12000} {tokenId 2} {liquidity 68925} {amount0 3979} {amount1 5000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, lpTokenId, 2) + shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "3979") + shouldEQ(t, amount1, "5000") + + // approve nft to staker + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) + std.TestSkipHeights(1) +} + +func TestCreateExternalIncentive(t *testing.T) { + std.TestSetPrevAddr(gsa) + + // send + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.IssueCoin(gsa, "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 + ) + // event: {GNOSWAP gno.land/r/demo/staker CreateExternalIncentive [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_rewardToken gno.land/r/demo/wugnot} {p_rewardAmount 10000000000} {p_startTimestamp 1234569600} {p_endTimestamp 1242345600}]} + + std.TestSkipHeights(1) +} + +func TestStakeToken01(t *testing.T) { + std.TestSetPrevAddr(gsa) + StakeToken(1) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {amount0 368} {amount1 1000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker + shouldEQ(t, len(deposits), 1) +} + +func TestStakeToken02(t *testing.T) { + std.TestSetPrevAddr(gsa) + StakeToken(2) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {amount0 3979} {amount1 5000}]} + + 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(gsa) + UnstakeToken(1) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/wugnot} {p_internal false} {feeAmount 25}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {rewardToken gno.land/r/demo/wugnot} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 2521}]} + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 20851}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 2064336}]} + // event: {GNOSWAP gno.land/r/demo/staker UnstakeToken [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {from g1puv9dz470prjshjm9qyg25dyfvrgph2kvjph68} {to g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 368} {amount1 1000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(1)), gsa) + + // check reward + shouldEQ(t, gns.BalanceOf(a2u(gsa)), 500000002064336) // internal + shouldEQ(t, wugnot.BalanceOf(a2u(gsa)), 2521) // external +} + +func TestUnstakeToken02(t *testing.T) { + std.TestSetPrevAddr(gsa) + UnstakeToken(2) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/wugnot} {p_internal false} {feeAmount 190}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {rewardToken gno.land/r/demo/wugnot} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 18907}]} + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 139012}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 13762238}]} + // event: {GNOSWAP gno.land/r/demo/staker UnstakeToken [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {from g1puv9dz470prjshjm9qyg25dyfvrgph2kvjph68} {to g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 3979} {amount1 5000}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(2)), gsa) + + // check reward + shouldEQ(t, gns.BalanceOf(a2u(gsa)), 500000015826574) // internal + shouldEQ(t, wugnot.BalanceOf(a2u(gsa)), 21428) // external +} + +func TestEndExternalIncentive(t *testing.T) { + std.TestSetOrigCaller(gsa) + 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() + // event: {GNOSWAP gno.land/r/demo/staker EndExternalIncentive [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_rewardToken gno.land/r/demo/wugnot} {p_refundee g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {end_by g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {refund_amount 0}]} + + 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.gnoA b/staker/_TEST_/_TEST_staker_one_external_test.gnoA new file mode 100644 index 00000000..f3114aac --- /dev/null +++ b/staker/_TEST_/_TEST_staker_one_external_test.gnoA @@ -0,0 +1,204 @@ +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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + pl.CreatePool(fooPath, barPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 + // event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + std.TestSkipHeights(1) +} + +func TestPositionMint01(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower -11000} {p_tickUpper -9000} {tokenId 1} {liquidity 12437} {amount0 1000} {amount1 368}]} + + std.TestSkipHeights(1) + + shouldEQ(t, lpTokenId, 1) + shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "1000") + shouldEQ(t, amount1, "368") + + // approve nft to staker for staking + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) + std.TestSkipHeights(1) +} + +func TestPositionMint02(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + gsa.String(), + ) + // event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower -12000} {p_tickUpper -9100} {tokenId 2} {liquidity 68925} {amount0 5000} {amount1 3979}]} + + std.TestSkipHeights(1) + + shouldEQ(t, lpTokenId, 2) + shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "5000") + shouldEQ(t, amount1, "3979") + + // approve nft to staker + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) + std.TestSkipHeights(1) +} + +func TestCreateExternalIncentive(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + ) + // event: {GNOSWAP gno.land/r/demo/staker CreateExternalIncentive [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_rewardToken gno.land/r/demo/obl} {p_rewardAmount 10000000000} {p_startTimestamp 1234569600} {p_endTimestamp 1242345600}]} + + std.TestSkipHeights(1) +} + +func TestStakeToken01(t *testing.T) { + std.TestSetPrevAddr(gsa) + StakeToken(1) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {amount0 1000} {amount1 368}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker + shouldEQ(t, len(deposits), 1) +} + +func TestStakeToken02(t *testing.T) { + std.TestSetPrevAddr(gsa) + StakeToken(2) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {amount0 5000} {amount1 3979}]} + + 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(330) + + std.TestSetPrevAddr(gsa) + UnstakeToken(1) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/obl} {p_internal false} {feeAmount 6}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {rewardToken gno.land/r/demo/obl} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 630}]} + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 20542}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 2033707}]} + // event: {GNOSWAP gno.land/r/demo/staker UnstakeToken [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {from g1puv9dz470prjshjm9qyg25dyfvrgph2kvjph68} {to g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 1000} {amount1 368}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(1)), gsa) + + // check reward + shouldEQ(t, gns.BalanceOf(a2u(gsa)), 500000002033707) // internal + shouldEQ(t, obl.BalanceOf(a2u(gsa)), 99990000000630) // external +} + +func TestUnstakeToken02(t *testing.T) { + std.TestSetPrevAddr(gsa) + UnstakeToken(2) // GNFT tokenId + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/obl} {p_internal false} {feeAmount 63}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {rewardToken gno.land/r/demo/obl} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 6302}]} + // event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 136950}]} + // event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 13558050}]} + // event: {GNOSWAP gno.land/r/demo/staker UnstakeToken [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {from g1puv9dz470prjshjm9qyg25dyfvrgph2kvjph68} {to g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 5000} {amount1 3979}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(2)), gsa) + + // check reward + shouldEQ(t, gns.BalanceOf(a2u(gsa)), 500000015591757) // internal + shouldEQ(t, obl.BalanceOf(a2u(gsa)), 99990000006932) // external +} + +func TestEndExternalIncentive(t *testing.T) { + std.TestSetOrigCaller(gsa) + 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() + // event: {GNOSWAP gno.land/r/demo/staker EndExternalIncentive [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_rewardToken gno.land/r/demo/obl} {p_refundee g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {end_by g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {refund_amount 0}]} + + 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.gnoA b/staker/_TEST_/_TEST_staker_one_increase_external_test.gnoA new file mode 100644 index 00000000..d53601b0 --- /dev/null +++ b/staker/_TEST_/_TEST_staker_one_increase_external_test.gnoA @@ -0,0 +1,248 @@ +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(gsa) + + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE) + std.TestSkipHeights(1) + + pl.CreatePool(fooPath, barPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7 + // --- event: {GNOSWAP gno.land/r/demo/pool CreatePool [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500}]} + + std.TestSkipHeights(1) +} + +func TestPositionMint01(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + gsa.String(), + ) + // --- event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower -11000} {p_tickUpper -9000} {tokenId 1} {liquidity 12437} {amount0 1000} {amount1 368}]} + + std.TestSkipHeights(1) + + shouldEQ(t, lpTokenId, 1) + shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "1000") + shouldEQ(t, amount1, "368") + + // approve nft to staker for staking + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) + std.TestSkipHeights(1) +} + +func TestPositionMint02(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + gsa.String(), + ) + // --- event: {GNOSWAP gno.land/r/demo/position Mint [{m_origCaller g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {m_prevRealm } {p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_tickLower -12000} {p_tickUpper -9100} {tokenId 2} {liquidity 68925} {amount0 5000} {amount1 3979}]} + + std.TestSkipHeights(1) + + shouldEQ(t, lpTokenId, 2) + shouldEQ(t, gnft.OwnerOf(tid(lpTokenId)), std.GetOrigCaller()) // gsa + shouldEQ(t, amount0, "5000") + shouldEQ(t, amount1, "3979") + + // approve nft to staker + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(GetOrigPkgAddr()), tid(lpTokenId)) + std.TestSkipHeights(1) +} + +func TestCreateExternalIncentive(t *testing.T) { + std.TestSetPrevAddr(gsa) + 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 + ) + // --- event: {GNOSWAP gno.land/r/demo/staker CreateExternalIncentive [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_rewardToken gno.land/r/demo/obl} {p_rewardAmount 1000000000} {p_startTimestamp 1234569600} {p_endTimestamp 1242345600}]} + + 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(gsa) + StakeToken(1) // GNFT tokenId + // --- event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {amount0 1000} {amount1 368}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker + shouldEQ(t, len(deposits), 1) +} + +func TestStakeToken02(t *testing.T) { + std.TestSetPrevAddr(gsa) + StakeToken(2) // GNFT tokenId + // --- event: {GNOSWAP gno.land/r/demo/staker StakeToken [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {amount0 5000} {amount1 3979}]} + + 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(gsa) + UnstakeToken(1) // GNFT tokenId + // --- event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/obl} {p_internal false} {feeAmount 8}]} + // --- event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {rewardToken gno.land/r/demo/obl} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 831}]} + // --- event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 21779}]} + // --- event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 2156220}]} + // --- event: {GNOSWAP gno.land/r/demo/staker UnstakeToken [{p_tokenId 1} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {from g1puv9dz470prjshjm9qyg25dyfvrgph2kvjph68} {to g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 1000} {amount1 368}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(1)), gsa) + + // check reward + shouldEQ(t, gns.BalanceOf(a2u(gsa)), 500000002156220) // internal + shouldEQ(t, obl.BalanceOf(a2u(gsa)), 99998900000831) // external +} + +func TestUnstakeToken02(t *testing.T) { + std.TestSetPrevAddr(gsa) + UnstakeToken(2) // GNFT tokenId + // --- event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/obl} {p_internal false} {feeAmount 58}]} + // --- event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {rewardToken gno.land/r/demo/obl} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 5769}]} + // --- event: {GNOSWAP gno.land/r/demo/staker handleRewardFee [{p_tokenPath gno.land/r/demo/gns} {p_internal true} {feeAmount 145200}]} + // --- event: {GNOSWAP gno.land/r/demo/staker CollectReward [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {rewardToken gno.land/r/demo/gns} {recipient g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount 14374800}]} + // --- event: {GNOSWAP gno.land/r/demo/staker UnstakeToken [{p_tokenId 2} {poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {from g1puv9dz470prjshjm9qyg25dyfvrgph2kvjph68} {to g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {amount0 5000} {amount1 3979}]} + + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(2)), gsa) + + // check reward + shouldEQ(t, gns.BalanceOf(a2u(gsa)), 500000016531020) // internal + shouldEQ(t, obl.BalanceOf(a2u(gsa)), 99998900006600) // external +} + +func TestEndExternalIncentive(t *testing.T) { + shouldEQ(t, len(incentives), 1) + + std.TestSetOrigCaller(gsa) + + // incentive time isn't over yet + shouldPanicWithMsg(t, func() { + EndExternalIncentive(gsa.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(gsa.String(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() + // event: {GNOSWAP gno.land/r/demo/staker EndExternalIncentive [{p_poolPath gno.land/r/demo/bar:gno.land/r/demo/foo:500} {p_rewardToken gno.land/r/demo/obl} {p_refundee g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {end_by g13f63ua8uhmuf9mgc0x8zfz04yrsaqh7j78vcgq} {refund_amount 0}]} + + 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_/z1_one_increase_external_filetest.gno b/staker/_TEST_/z1_one_increase_external_filetest.gno index 04473b38..c05511ee 100644 --- a/staker/_TEST_/z1_one_increase_external_filetest.gno +++ b/staker/_TEST_/z1_one_increase_external_filetest.gno @@ -28,8 +28,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -192,17 +191,6 @@ func ugnotBalanceOf(addr std.Address) uint64 { } 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{}) @@ -243,43 +231,36 @@ func main() { } 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) - } + std.TestSetPrevAddr(gsa) - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*numPool) + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*0) std.TestSkipHeights(1) pl.CreatePool(barPath, fooPath, fee100, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) } func mint01() { - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) 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()) + pn.Mint(barPath, fooPath, fee100, 9000, 11000, "1000", "1000", "0", "0", max_timeout, gsa.String()) } func mint02() { - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) 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()) + pn.Mint(barPath, fooPath, fee100, 9100, 12000, "5000", "5000", "0", "0", max_timeout, gsa.String()) } func createExternalIncentiveInit() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) std.TestSkipHeights(1) @@ -294,7 +275,7 @@ func createExternalIncentiveInit() { } func createExternalIncentiveIncrease() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) std.TestSkipHeights(1) @@ -309,14 +290,14 @@ func createExternalIncentiveIncrease() { } func stakeToken01() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) gnft.Approve(a2u(consts.STAKER_ADDR), tid(1)) sr.StakeToken(1) std.TestSkipHeights(2) } func stakeToken02() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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 index bf771966..369ddbdd 100644 --- a/staker/_TEST_/z2_one_external_filetest.gno +++ b/staker/_TEST_/z2_one_external_filetest.gno @@ -28,8 +28,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -192,17 +191,6 @@ func ugnotBalanceOf(addr std.Address) uint64 { } 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{}) @@ -244,43 +232,36 @@ func main() { } 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) - } + std.TestSetPrevAddr(gsa) - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*numPool) + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*0) std.TestSkipHeights(1) pl.CreatePool(barPath, fooPath, fee100, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) } func mint01() { - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) 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()) + pn.Mint(barPath, fooPath, fee100, 9000, 11000, "1000", "1000", "0", "0", max_timeout, gsa.String()) } func mint02() { - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) 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()) + pn.Mint(barPath, fooPath, fee100, 9100, 12000, "5000", "5000", "0", "0", max_timeout, gsa.String()) } func createExternalIncentive() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) std.TestSkipHeights(1) @@ -295,14 +276,14 @@ func createExternalIncentive() { } func stakeToken01() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) gnft.Approve(a2u(consts.STAKER_ADDR), tid(1)) sr.StakeToken(1) std.TestSkipHeights(2) } func stakeToken02() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) gnft.Approve(a2u(consts.STAKER_ADDR), tid(2)) sr.StakeToken(2) std.TestSkipHeights(2) @@ -313,31 +294,31 @@ func unStakeToken01() { gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) // internal reward distribution std.TestSkipHeights(360) - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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") + gnsBal := gns.BalanceOf(a2u(gsa)) // 0 + oblBal := obl.BalanceOf(a2u(gsa)) + if oblBal != 99999000000788 { + panic("oblBal != 99999000000788") } } func unStakeToken02() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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") + gnsBal := gns.BalanceOf(a2u(gsa)) // 0 + oblBal := obl.BalanceOf(a2u(gsa)) + if oblBal != 99999000006247 { + panic("oblBal != 99999000006247") } } func endExternalIncentive() { - std.TestSetOrigCaller(test1) + std.TestSetOrigCaller(gsa) 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() diff --git a/staker/_TEST_/z3_internal_external_filetest.gno b/staker/_TEST_/z3_internal_external_filetest.gno index 13680ccb..b9ab02bc 100644 --- a/staker/_TEST_/z3_internal_external_filetest.gno +++ b/staker/_TEST_/z3_internal_external_filetest.gno @@ -28,8 +28,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -192,17 +191,6 @@ func ugnotBalanceOf(addr std.Address) uint64 { } 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{}) @@ -247,16 +235,9 @@ func main() { } 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) - } + std.TestSetPrevAddr(gsa) - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*numPool) + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*0) std.TestSkipHeights(1) pl.CreatePool(barPath, quxPath, fee500, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) @@ -274,37 +255,41 @@ func setPoolTier() { } func mintBarQux() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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()) + pn.Mint(barPath, quxPath, fee500, 9000, 11000, "100000", "100000", "0", "0", maxTimeout, gsa.String()) + } func mintBarBaz() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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()) + pn.Mint(barPath, bazPath, fee100, -1000, 1000, "100000", "100000", "0", "0", maxTimeout, gsa.String()) + } func mintFooQux() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) 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()) + pn.Mint(fooPath, quxPath, fee500, -1000, 11000, "100000", "100000", "0", "0", maxTimeout, gsa.String()) + } func createExternalIncentive() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) + obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) std.TestSkipHeights(1) @@ -319,7 +304,8 @@ func createExternalIncentive() { } func stakeTier01() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(consts.STAKER_ADDR), tid(1)) sr.StakeToken(1) @@ -327,7 +313,8 @@ func stakeTier01() { } func stakeTier02() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(consts.STAKER_ADDR), tid(2)) sr.StakeToken(2) @@ -335,7 +322,8 @@ func stakeTier02() { } func stakeExternal() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) + gnft.Approve(a2u(consts.STAKER_ADDR), tid(3)) sr.StakeToken(3) @@ -347,26 +335,30 @@ func collectReward01() { 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") + gnsBal := gns.BalanceOf(a2u(gsa)) + + if gnsBal != 500000000000000 { + panic("collectReward01() || gnsBal != 500000000000000") } - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) + sr.CollectReward(1) - gnsBal = gns.BalanceOf(a2u(test1)) - if gnsBal != 500266805 { - panic("collectReward01() || gnsBal != 500266805") + gnsBal = gns.BalanceOf(a2u(gsa)) + + if gnsBal != 500000000266805 { + panic("collectReward01() || gnsBal != 500000000266805") } } func collectReward01Again() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) - gnsBal := gns.BalanceOf(a2u(test1)) - if gnsBal != 500266805 { - panic("collectReward01Again() || gnsBal != 500266805") + gnsBal := gns.BalanceOf(a2u(gsa)) + + if gnsBal != 500000000266805 { + panic("collectReward01Again() || gnsBal != 500000000266805") } // ACCELERATE TIME @@ -375,24 +367,27 @@ func collectReward01Again() { sr.CollectReward(1) std.TestSkipHeights(1) - gnsBal = gns.BalanceOf(a2u(test1)) - if gnsBal != 538381805 { - panic("collectReward01() || gnsBal != 538381805") + gnsBal = gns.BalanceOf(a2u(gsa)) + + if gnsBal != 500000038381805 { + panic("collectReward01() || gnsBal != 500000038381805") } } func collectExternalReward() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) + + oblBal := obl.BalanceOf(a2u(gsa)) - oblBal := obl.BalanceOf(a2u(test1)) - if oblBal != 9000000000 { - panic("collectExternalReward() || oblBal != 9000000000") + if oblBal != 99999000000000 { + panic("collectExternalReward() || oblBal != 99999000000000") } sr.CollectReward(3) - oblBal = obl.BalanceOf(a2u(test1)) - if oblBal != 9000145306 { - panic("collectExternalReward() || oblBal != 9000145306") + oblBal = obl.BalanceOf(a2u(gsa)) + + if oblBal != 99999000141106 { + panic("collectExternalReward() || oblBal != 99999000141106") } } diff --git a/staker/_TEST_/z4_mint_and_stake_filetest.gno b/staker/_TEST_/z4_mint_and_stake_filetest.gno index 9cf0601f..a0bb8c4f 100644 --- a/staker/_TEST_/z4_mint_and_stake_filetest.gno +++ b/staker/_TEST_/z4_mint_and_stake_filetest.gno @@ -26,8 +26,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -190,17 +189,6 @@ func ugnotBalanceOf(addr std.Address) uint64 { } 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{}) @@ -238,16 +226,9 @@ func main() { } 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) - } + std.TestSetPrevAddr(gsa) - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*numPool) + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*0) std.TestSkipHeights(1) pl.CreatePool(barPath, quxPath, fee500, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) @@ -255,7 +236,7 @@ func createPool() { } func mintAndStake() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) qux.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) @@ -264,7 +245,7 @@ func mintAndStake() { } func collectFee() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) pn.CollectFee(1) } @@ -273,12 +254,12 @@ func collectReward() { std.TestSetPrevAddr(consts.INTERNAL_REWARD_ACCOUNT) gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) sr.CollectReward(1) } func unstakeToken() { - std.TestSetPrevAddr(test1) + std.TestSetPrevAddr(gsa) sr.UnstakeToken(1) } diff --git a/staker/_TEST_/z5_manage_pool_tier_filetest.gno b/staker/_TEST_/z5_manage_pool_tier_filetest.gno index 52117dca..d05a1a8d 100644 --- a/staker/_TEST_/z5_manage_pool_tier_filetest.gno +++ b/staker/_TEST_/z5_manage_pool_tier_filetest.gno @@ -25,8 +25,7 @@ import ( ) var ( - gsa std.Address = consts.GNOSWAP_ADMIN - test1 std.Address = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + gsa std.Address = consts.GNOSWAP_ADMIN fooPath string = "gno.land/r/demo/foo" barPath string = "gno.land/r/demo/bar" @@ -189,17 +188,6 @@ func ugnotBalanceOf(addr std.Address) uint64 { } 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{}) @@ -236,16 +224,9 @@ func main() { } 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) - } + std.TestSetPrevAddr(gsa) - gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*numPool) + gns.Approve(a2u(consts.POOL_ADDR), consts.POOL_CREATION_FEE*0) std.TestSkipHeights(1) pl.CreatePool(barPath, quxPath, fee500, common.TickMathGetSqrtRatioAtTick(10_000).ToString()) diff --git a/staker/manage_pool_tiers.gno b/staker/manage_pool_tiers.gno index 50302405..a220f790 100644 --- a/staker/manage_pool_tiers.gno +++ b/staker/manage_pool_tiers.gno @@ -40,6 +40,12 @@ func SetPoolTier(pool string, tier uint64) { validTier(tier) poolTiers[pool] = tier + + std.Emit( + "GNOSWAP", + "p_poolPath", pool, + "p_tier", uint64ToStr(tier), + ) } // ChangePoolTier changes the tier of the pool @@ -66,6 +72,12 @@ func ChangePoolTier(pool string, tier uint64) { panic(ufmt.Sprintf("[STAKER] manage_pool_tiers.gno__ChangePoolTier() || Cannot change tier of this pool(%s)")) } + std.Emit( + "GNOSWAP", + "p_poolPath", pool, + "p_tier", uint64ToStr(tier), + ) + poolTiers[pool] = tier } @@ -83,6 +95,11 @@ func RemovePoolTier(pool string) { } delete(poolTiers, pool) + + std.Emit( + "GNOSWAP", + "p_poolPath", pool, + ) } func isAdmin() { diff --git a/staker/mint_stake.gno b/staker/mint_stake.gno index 394c83ba..7645b4c1 100644 --- a/staker/mint_stake.gno +++ b/staker/mint_stake.gno @@ -34,7 +34,20 @@ func MintAndStake( consts.STAKER_ADDR.String(), ) - poolPath, _, _ := StakeToken(tokenId) + poolPath, _, _ := StakeToken(tokenId) // poolPath, stakedAmount0, stakedAmount1 + + /* + std.Emit( + "GNOSWAP", + "p_tickLower", int32ToStr(tickLower), + "p_tickUpper", int32ToStr(tickUpper), + "poolPath", poolPath, + "tokenId", uint64ToStr(tokenId), + "liquidity", liquidity, + "amount0", amount0, + "amount1", amount1, + ) + */ return tokenId, liquidity, amount0, amount1, poolPath } diff --git a/staker/reward_fee.gno b/staker/reward_fee.gno index 50625660..a16e86d8 100644 --- a/staker/reward_fee.gno +++ b/staker/reward_fee.gno @@ -1,6 +1,8 @@ package staker import ( + "std" + "gno.land/r/demo/gnoswap/common" "gno.land/r/demo/gnoswap/consts" @@ -37,6 +39,13 @@ func handleRewardFee( } } + std.Emit( + "GNOSWAP", + "p_tokenPath", tokenPath, + "p_internal", boolToStr(internal), + "feeAmount", feeAmount.ToString(), + ) + return new(u256.Uint).Sub(amount, feeAmount) } @@ -48,4 +57,9 @@ func SetRewardFee(fee uint64) { } rewardFee = fee + + std.Emit( + "GNOSWAP", + "p_fee", uint64ToStr(fee), + ) } diff --git a/staker/staker.gno b/staker/staker.gno index e085fef4..dc046e6e 100644 --- a/staker/staker.gno +++ b/staker/staker.gno @@ -117,6 +117,15 @@ func CreateExternalIncentive( } poolIncentives[targetPoolPath] = append(poolIncentives[targetPoolPath], incentiveId) + + std.Emit( + "GNOSWAP", + "p_poolPath", targetPoolPath, + "p_rewardToken", rewardToken, + "p_rewardAmount", _rewardAmount, + "p_startTimestamp", int64ToStr(startTimestamp), + "p_endTimestamp", int64ToStr(endTimestamp), + ) } // StakeToken stakes the lp token to the staker contract @@ -147,14 +156,14 @@ func StakeToken(tokenId uint64) (string, string, string) { // poolPath, token0Am } // check pool path from tokenid - poolKey := pn.PositionGetPositionPoolKey(tokenId) + poolPath := pn.PositionGetPositionPoolKey(tokenId) // staked status deposit := deposits[tokenId] deposit.owner = std.GetOrigCaller() // NOT ACTUAL OWNER, MUST BE USER ALWAYS deposit.numberOfStakes++ deposit.stakeTimestamp = time.Now().Unix() - deposit.targetPoolPath = poolKey + deposit.targetPoolPath = poolPath deposits[tokenId] = deposit if callerIsOwner { // if caller is owner, transfer NFT ownership to staker contract @@ -164,7 +173,16 @@ func StakeToken(tokenId uint64) (string, string, string) { // poolPath, token0Am } token0Amount, token1Amount := getTokenPairBalanceFromPosition(tokenId) - return poolKey, token0Amount, token1Amount + + std.Emit( + "GNOSWAP", + "p_tokenId", uint64ToStr(tokenId), + "poolPath", poolPath, + "amount0", token0Amount, + "amount1", token1Amount, + ) + + return poolPath, token0Amount, token1Amount } // CollectReward collects staked reward from tokenId @@ -197,6 +215,16 @@ func CollectReward(tokenId uint64) string { // poolPath // then transfer rest transferByRegisterCall(incentive.rewardToken, deposit.owner, toUser.Uint64()) + + // emit external reward tokens + std.Emit( + "GNOSWAP", + "p_tokenId", uint64ToStr(tokenId), + "poolPath", poolPath, + "rewardToken", incentive.rewardToken, + "recipient", deposit.owner.String(), + "amount", toUser.ToString(), + ) } incentive.rewardAmount = new(u256.Uint).Sub(incentive.rewardAmount, externalReward) @@ -213,6 +241,16 @@ func CollectReward(tokenId uint64) string { // poolPath // then transfer rest gns.TransferFrom(a2u(consts.INTERNAL_REWARD_ACCOUNT), a2u(deposit.owner), toUser.Uint64()) + + // emit internal reward tokens + std.Emit( + "GNOSWAP", + "p_tokenId", uint64ToStr(tokenId), + "poolPath", poolPath, + "rewardToken", consts.GNS_PATH, + "recipient", deposit.owner.String(), + "amount", toUser.ToString(), + ) } return poolPath @@ -238,6 +276,17 @@ func UnstakeToken(tokenId uint64) (string, string, string) { // poolPath, token0 poolPath := pn.PositionGetPositionPoolKey(tokenId) token0Amount, token1Amount := getTokenPairBalanceFromPosition(tokenId) + + std.Emit( + "GNOSWAP", + "p_tokenId", uint64ToStr(tokenId), + "poolPath", poolPath, + "from", GetOrigPkgAddr().String(), + "to", deposit.owner.String(), + "amount0", token0Amount, + "amount1", token1Amount, + ) + return poolPath, token0Amount, token1Amount } @@ -273,6 +322,16 @@ func EndExternalIncentive(_refundee, targetPoolPath, rewardToken string) { poolIncentives[targetPoolPath] = append(poolIncentives[targetPoolPath][:i], poolIncentives[targetPoolPath][i+1:]...) } } + + std.Emit( + "GNOSWAP", + "p_poolPath", targetPoolPath, + "p_rewardToken", rewardToken, + "p_refundee", _refundee, + "end_by", consts.GNOSWAP_ADMIN.String(), + "refund_amount", "0", + ) + case incentive.refundee: // refundee can end incentive with refund refund := incentive.rewardAmount @@ -291,6 +350,16 @@ func EndExternalIncentive(_refundee, targetPoolPath, rewardToken string) { poolIncentives[targetPoolPath] = append(poolIncentives[targetPoolPath][:i], poolIncentives[targetPoolPath][i+1:]...) } } + + std.Emit( + "GNOSWAP", + "p_poolPath", targetPoolPath, + "p_rewardToken", rewardToken, + "p_refundee", _refundee, + "end_by", incentive.refundee.String(), + "refund_amount", refund.ToString(), + ) + default: panic("[STAKER] staker.gno__EndExternalIncentive() || only refundee or admin can end incentive") } diff --git a/staker/utils.gno b/staker/utils.gno index 1ddbae7c..ce01a082 100644 --- a/staker/utils.gno +++ b/staker/utils.gno @@ -64,3 +64,27 @@ func min(x, y int64) int64 { } return y } + +func uint8ToStr(i uint8) string { + return strconv.FormatInt(int64(i), 10) +} + +func int32ToStr(i int32) string { + return strconv.FormatInt(int64(i), 10) +} + +func int64ToStr(i int64) string { + return strconv.FormatInt(i, 10) +} + +func uint64ToStr(i uint64) string { + return strconv.FormatInt(int64(i), 10) +} + +func boolToStr(b bool) string { + if b { + return "true" + } + + return "false" +} From cc0ce0d41e68c787c1f7157ca45c9280f03ff1fa Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 15 May 2024 21:33:18 +0900 Subject: [PATCH 09/10] feat: remove (test) faucet contract --- x/faucet/faucet.gno | 30 --------------- x/faucet/faucet_test.gno | 80 ---------------------------------------- 2 files changed, 110 deletions(-) delete mode 100644 x/faucet/faucet.gno delete mode 100644 x/faucet/faucet_test.gno diff --git a/x/faucet/faucet.gno b/x/faucet/faucet.gno deleted file mode 100644 index 4a0ef3f2..00000000 --- a/x/faucet/faucet.gno +++ /dev/null @@ -1,30 +0,0 @@ -package faucet - -import ( - "std" - - "gno.land/r/demo/gns" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/foo" - "gno.land/r/demo/qux" -) - -func Faucet(addr std.Address) { - faucetNative(addr) - faucetGRC20(addr) -} - -func faucetNative(addr std.Address) { - banker := std.GetBanker(std.BankerTypeRealmIssue) - banker.IssueCoin(addr, "ugnot", 100_000_000) -} - -func faucetGRC20(addr std.Address) { - gns.Faucet(addr) - bar.Faucet(addr) - baz.Faucet(addr) - foo.Faucet(addr) - qux.Faucet(addr) -} diff --git a/x/faucet/faucet_test.gno b/x/faucet/faucet_test.gno deleted file mode 100644 index 2c7d45f9..00000000 --- a/x/faucet/faucet_test.gno +++ /dev/null @@ -1,80 +0,0 @@ -package faucet - -import ( - "std" - "testing" - - "gno.land/p/demo/testutils" - - "gno.land/r/demo/users" - - "gno.land/r/demo/gns" - - "gno.land/r/demo/bar" - "gno.land/r/demo/baz" - "gno.land/r/demo/foo" - "gno.land/r/demo/qux" -) - -func TestFaucet(t *testing.T) { - user1 := testutils.TestAddress("user1") - - oldNative := ugnotBalanceOf(user1) - shouldEQ(t, oldNative, uint64(0)) - - oldGns := gns.BalanceOf(a2u(user1)) - shouldEQ(t, oldGns, uint64(0)) - - oldBar := bar.BalanceOf(a2u(user1)) - shouldEQ(t, oldBar, uint64(0)) - - oldBaz := baz.BalanceOf(a2u(user1)) - shouldEQ(t, oldBaz, uint64(0)) - - oldFoo := foo.BalanceOf(a2u(user1)) - shouldEQ(t, oldFoo, uint64(0)) - - oldQux := qux.BalanceOf(a2u(user1)) - shouldEQ(t, oldQux, uint64(0)) - - Faucet(user1) - - newNative := ugnotBalanceOf(user1) - shouldEQ(t, newNative-oldNative, uint64(100_000_000)) - - newGns := gns.BalanceOf(a2u(user1)) - shouldEQ(t, newGns-oldGns, uint64(100_000_000)) - - newBar := bar.BalanceOf(a2u(user1)) - shouldEQ(t, newBar-oldBar, uint64(100_000_000)) - - newBaz := baz.BalanceOf(a2u(user1)) - shouldEQ(t, newBaz-oldBaz, uint64(100_000_000)) - - newFoo := foo.BalanceOf(a2u(user1)) - shouldEQ(t, newFoo-oldFoo, uint64(100_000_000)) - - newQux := qux.BalanceOf(a2u(user1)) - shouldEQ(t, newQux-oldQux, uint64(100_000_000)) -} - -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 a2u(addr std.Address) users.AddressOrName { - return users.AddressOrName(addr) -} - -func shouldEQ(t *testing.T, got, expected interface{}) { - if got != expected { - t.Errorf("got %v, expected %v", got, expected) - } -} From 22ace36647dc099bb664e96449d91b56f015a8df Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 15 May 2024 21:45:54 +0900 Subject: [PATCH 10/10] fix: gov test --- gov/_TEST_/_TEST_gov_proposal_test.gno | 3 - gov/z_proposal_filetest.gno | 97 ++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 gov/z_proposal_filetest.gno diff --git a/gov/_TEST_/_TEST_gov_proposal_test.gno b/gov/_TEST_/_TEST_gov_proposal_test.gno index d0eed2d0..adb41552 100644 --- a/gov/_TEST_/_TEST_gov_proposal_test.gno +++ b/gov/_TEST_/_TEST_gov_proposal_test.gno @@ -5,8 +5,6 @@ import ( "std" "testing" "time" - - "gno.land/r/demo/gns" ) func TestSubmitProposal(t *testing.T) { @@ -145,7 +143,6 @@ func TestSubmitProposalRejected(t *testing.T) { func TestSubmitProposalCommunityPoolSpend(t *testing.T) { std.TestSetOrigCaller(std.CurrentRealm().Addr()) - gns.Faucet() initialBalance := balanceOf(std.CurrentRealm().Addr()) diff --git a/gov/z_proposal_filetest.gno b/gov/z_proposal_filetest.gno new file mode 100644 index 00000000..58b97991 --- /dev/null +++ b/gov/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: +//