From 224125556583ce5ca45c27083b91639dc968e399 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Mon, 4 Dec 2023 20:51:18 +0900 Subject: [PATCH 1/3] feat: makefile until mint --- _test/_TEST_gs_test.gno | 544 ------------------ _test/init_only.mk | 321 +++++++++++ router/_TEST_router_api_getter_test.gnoa | 88 --- ...ter_swap_route_1route_3hop_native_test.gno | 161 ------ 4 files changed, 321 insertions(+), 793 deletions(-) delete mode 100644 _test/_TEST_gs_test.gno create mode 100644 _test/init_only.mk delete mode 100644 router/_TEST_router_api_getter_test.gnoa delete mode 100644 router/_TEST_router_swap_route_1route_3hop_native_test.gno diff --git a/_test/_TEST_gs_test.gno b/_test/_TEST_gs_test.gno deleted file mode 100644 index c1dcb68a..00000000 --- a/_test/_TEST_gs_test.gno +++ /dev/null @@ -1,544 +0,0 @@ -package tc - -import ( - "std" - "strconv" - "testing" - - "gno.land/p/demo/grc/grc20" - "gno.land/p/demo/grc/grc721" - "gno.land/p/demo/testutils" - - "gno.land/r/demo/users" - - // swap contract - gov "gno.land/r/gov" - pl "gno.land/r/pool" - pos "gno.land/r/position" - rou "gno.land/r/router" - stk "gno.land/r/staker" - - // token - _ "gno.land/r/grc20_wrapper" - - bar "gno.land/r/bar" - baz "gno.land/r/baz" - foo "gno.land/r/foo" - gns "gno.land/r/gns" - obl "gno.land/r/obl" // external reward - qux "gno.land/r/qux" - - wugnot "gno.land/r/wugnot" - - gnft "gno.land/r/gnft" // GRC721 LP Token -) - -const ( - TIMESTAMP_90DAYS int64 = 7776000 -) - -var ( - // test address - gsa = testutils.TestAddress("gsa") // Gnswap Admin - pc01 = testutils.TestAddress("pc01") // Pool Creator 01 - ci01 = testutils.TestAddress("ci01") // Create Incentive Caller 01 - lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 - lp02 = testutils.TestAddress("lp02") // Liquidity Provider 02 - tr01 = testutils.TestAddress("tr01") // Trader 01 - - // contract address - poolAddr = pl.GetOrigPkgAddr() // Pool Contract - posAddr = pos.GetOrigPkgAddr() // Position Contract - stakerAddr = stk.GetOrigPkgAddr() // Staker Contract - govAddr = gov.GetOrigPkgAddr() // Gov Contract - rouAddr = rou.GetOrigPkgAddr() // Router Contract - - // token path - fooPath = "gno.land/r/foo" - barPath = "gno.land/r/bar" - bazPath = "gno.land/r/baz" - quxPath = "gno.land/r/qux" - gnsPath = "gno.land/r/gns" - oblPath = "gno.land/r/obl" - - gnotPath = "gnot" - - MIN_TICK bigint = -887272 - MAX_TICK bigint = 887272 - - MIN_SQRT_RATIO bigint = 4295128739 // same as TickMathGetSqrtRatioAtTick(MIN_TICK) - MAX_SQRT_RATIO bigint = 1461446703485210103287273052203988822378723970342 // same as TickMathGetSqrtRatioAtTick(MAX_TICK) - - MAX_TIMEOUT bigint = 9999999999 -) - -// func init() { -// // debug addr -// println(gsa, "// gsa") -// println(pc01, "// pc01") -// println(ci01, "// ci01") -// println(lp01, "// lp01") -// println(lp02, "// lp02") -// println(tr01, "// tr01") -// println(poolAddr, "// pool") -// println(posAddr, "// pos") -// println(rouAddr, "// router") -// println(stakerAddr, "// staker") -// println(govAddr, "// gov") -// } - -// 1. [TC - POOL] Init -func TestPoolInitManual(t *testing.T) { - std.TestSetOrigCaller(pc01) - - pl.InitManual() - std.TestSkipHeights(1) - - shouldPanicWithMsg(t, func() { pl.InitManual() }, "[POOl] pool_manager.gno__InitManual() || contract must not be initialized") -} - -// 2. [TC - POOL] Create(Multi)Pool -func TestPoolCreatePool(t *testing.T) { - std.TestSetOrigCaller(pc01) - - gsaOldGnsBalance := gns.BalanceOf(a2u(gsa)) - - pl.CreatePool(gnotPath, barPath, uint16(100), 101729702841318637793976746270) // tick = 5_000, ratio = 1.648680055931176 - pl.CreatePool(barPath, bazPath, uint16(100), 101729702841318637793976746270) // tick = 5_000, ratio = 1.648680055931176 - pl.CreatePool(bazPath, quxPath, uint16(100), 101729702841318637793976746270) // tick = 5_000, ratio = 1.648680055931176 - - pl.CreatePool(gnotPath, barPath, uint16(500), 101729702841318637793976746270) // tick = 5_000, ratio = 1.648680055931176 - pl.CreatePool(barPath, bazPath, uint16(500), 101729702841318637793976746270) // tick = 5_000, ratio = 1.648680055931176 - pl.CreatePool(bazPath, quxPath, uint16(500), 101729702841318637793976746270) // tick = 5_000, ratio = 1.648680055931176 - std.TestSkipHeights(7) - - gsaNewGnsBalance := gns.BalanceOf(a2u(gsa)) - - shouldEQ(t, len(pl.PoolGetPoolList()), 6) - shouldEQ(t, gsaNewGnsBalance-gsaOldGnsBalance, 3000) -} - -// 3. [TC - POSITION] Mint LP -func TestPositionMint(t *testing.T) { - // gnot_bar_100 by lp01 - { - std.TestSetOrigCaller(lp01) - - // prepare ugnot - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 500_000_000) - - poolOldBarBalance := bar.BalanceOf(a2u(poolAddr)) - poolOldWugnotBalance := wugnot.BalanceOf(a2u(poolAddr)) - shouldEQ(t, poolOldBarBalance, 0) - shouldEQ(t, poolOldWugnotBalance, 0) - - lp01OldBarBalance := bar.BalanceOf(a2u(lp01)) - lp01OldWugnotBalance := wugnot.BalanceOf(a2u(lp01)) - - // simulate transfer & decrase - std.TestSetOrigSend(std.Coins{{"ugnot", 10000000}}, nil) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 10000000) - - // Mint - tokenId, liquidity, amount0, amount1 := pos.Mint(gnotPath, barPath, uint16(100), int32(4000), int32(6000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(1)), lp01) - shouldEQ(t, tokenId, 1) - shouldEQ(t, amount0 > 0, true) - shouldEQ(t, amount1 > 0, true) - - poolNewBarBalance := bar.BalanceOf(a2u(poolAddr)) - poolNewWugnotBalance := wugnot.BalanceOf(a2u(poolAddr)) - lp01NewBarBalance := bar.BalanceOf(a2u(lp01)) - lp01NewWugnotBalance := wugnot.BalanceOf(a2u(lp01)) - - shouldEQ(t, poolOldBarBalance+uint64(amount0), poolNewBarBalance) - shouldEQ(t, poolOldWugnotBalance+uint64(amount1), poolNewWugnotBalance) - shouldEQ(t, lp01OldBarBalance-uint64(amount0), lp01NewBarBalance) - } - - // bar_baz_100 by lp02 - { - std.TestSetOrigCaller(lp02) - - poolOldBarBalance := bar.BalanceOf(a2u(poolAddr)) - poolOldBazBalance := baz.BalanceOf(a2u(poolAddr)) - shouldEQ(t, poolOldBarBalance, 9999999) - shouldEQ(t, poolOldBazBalance, 0) - - lp02OldBarBalance := bar.BalanceOf(a2u(lp02)) - lp02OldBazBalance := baz.BalanceOf(a2u(lp02)) - - // Mint - tokenId, liquidity, amount0, amount1 := pos.Mint(barPath, bazPath, uint16(100), int32(4000), int32(6000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(2)), lp02) - shouldEQ(t, tokenId, 2) - shouldEQ(t, amount0 > 0, true) - shouldEQ(t, amount1 > 0, true) - - poolNewBarBalance := bar.BalanceOf(a2u(poolAddr)) - poolNewBazBalance := baz.BalanceOf(a2u(poolAddr)) - lp02NewBarBalance := bar.BalanceOf(a2u(lp02)) - lp02NewBazBalance := baz.BalanceOf(a2u(lp02)) - - shouldEQ(t, poolOldBarBalance+uint64(amount0), poolNewBarBalance) - shouldEQ(t, poolOldBazBalance+uint64(amount1), poolNewBazBalance) - shouldEQ(t, lp02OldBarBalance-uint64(amount0), lp02NewBarBalance) - shouldEQ(t, lp02OldBazBalance-uint64(amount1), lp02NewBazBalance) - } - - // baz_qux_100 by lp02 - { - std.TestSetOrigCaller(lp02) - - poolOldBazBalance := baz.BalanceOf(a2u(poolAddr)) - poolOldQuxBalance := qux.BalanceOf(a2u(poolAddr)) - shouldEQ(t, poolOldBazBalance, 9999999) - shouldEQ(t, poolOldQuxBalance, 0) - - lp02OldBazBalance := baz.BalanceOf(a2u(lp02)) - lp02OldQuxBalance := qux.BalanceOf(a2u(lp02)) - - // Mint - tokenId, liquidity, amount0, amount1 := pos.Mint(bazPath, quxPath, uint16(100), int32(4000), int32(6000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(tid(3)), lp02) - shouldEQ(t, tokenId, 3) - shouldEQ(t, amount0 > 0, true) - shouldEQ(t, amount1 > 0, true) - - poolNewBazBalance := baz.BalanceOf(a2u(poolAddr)) - poolNewQuxBalance := qux.BalanceOf(a2u(poolAddr)) - lp02NewBazBalance := baz.BalanceOf(a2u(lp02)) - lp02NewQuxBalance := qux.BalanceOf(a2u(lp02)) - - shouldEQ(t, poolOldBazBalance+uint64(amount0), poolNewBazBalance) - shouldEQ(t, poolOldQuxBalance+uint64(amount1), poolNewQuxBalance) - shouldEQ(t, lp02OldBazBalance-uint64(amount0), lp02NewBazBalance) - shouldEQ(t, lp02OldQuxBalance-uint64(amount1), lp02NewQuxBalance) - } - - { - // bar_gnot_500 by lp01 - // simulate transfer & decrase - std.TestSetOrigCaller(lp01) - std.TestSetOrigSend(std.Coins{{"ugnot", 10000000}}, nil) - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 10000000) - // pos.Mint(gnotPath, barPath, uint16(500), int32(4000), int32(6000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) - pos.Mint(barPath, gnotPath, uint16(500), int32(-6000), int32(-4000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) - - // bar_baz_500 by lp02 - std.TestSetOrigCaller(lp02) - pos.Mint(barPath, bazPath, uint16(500), int32(4000), int32(6000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) - - // baz_qux_500 by lp02 - std.TestSetOrigCaller(lp02) - pos.Mint(bazPath, quxPath, uint16(500), int32(4000), int32(6000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) - - std.TestSkipHeights(3) - } -} - -// 4. [TC - STAKER] CreateExternalIncentive -// Internal incentive will automatcially created via init() -func TestStakerCreateExternalIncentive(t *testing.T) { - std.TestSetOrigCaller(ci01) - - stk.CreateExternalIncentive( - "gno.land/r/bar:gnot:100", // targetPoolPath - "gno.land/r/obl", // rewardToken - 10_000_000_000, // rewardAmount - GetTimestamp(), // startTimestamp - GetTimestamp()+TIMESTAMP_90DAYS, // endTimestamp - ) - std.TestSkipHeights(1) -} - -// 5. [TC - STAKER] StakeToken -func TestStakerStakeToken(t *testing.T) { - // lp01 stakes tokenId '1' - std.TestSetPrevAddr(lp01) // r3v4_xxx: only test code, we need to call gnft.Approve directly from user - gnft.Approve(a2u(stakerAddr), tid("1")) - - stk.StakeToken(1) - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(bigint(1)), stakerAddr) - - // lp02 stakes tokenId '2' - std.TestSetPrevAddr(lp02) // r3v4_xxx: only test code, we need to call gnft.Approve directly from user - gnft.Approve(a2u(stakerAddr), tid("2")) - - stk.StakeToken(2) - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(bigint(2)), stakerAddr) -} - -// 6. [TC - POOL] SetFeeProtocol -func TestPoolSetFeeProtocol(t *testing.T) { - std.TestSetOrigCaller(pc01) - - pl.SetFeeProtocol(6, 8) - std.TestSkipHeights(1) - - tmpPool := pl.GetPool(barPath, gnotPath, uint16(100)) - shouldEQ(t, tmpPool.PoolGetSlot0FeeProtocol(), bigint(134)) -} - -// 7. [TC - ROUTER] SwapRoute ExactIn Single ( bar -> baz ) -func TestRotuerSwapRouteExactInSinglePath(t *testing.T) { - std.TestSetPrevRealm("gno.land/r/router") - std.TestSetOrigCaller(tr01) - - tr01OldBarBalance := bar.BalanceOf(a2u(tr01)) - tr01OldBazBalance := baz.BalanceOf(a2u(tr01)) - poolOldBarBalance := bar.BalanceOf(a2u(poolAddr)) - poolOldBazBalance := baz.BalanceOf(a2u(poolAddr)) - - swapAmount := 123_456 - rou.SwapRoute( - barPath, - bazPath, - bigint(swapAmount), - "EXACT_IN", - "gno.land/r/bar:gno.land/r/baz:100", - "100", - 200_000, - ) - - tr01NewBarBalance := bar.BalanceOf(a2u(tr01)) - tr01NewBazBalance := baz.BalanceOf(a2u(tr01)) - poolNewBarBalance := bar.BalanceOf(a2u(poolAddr)) - poolNewBazBalance := baz.BalanceOf(a2u(poolAddr)) - - shouldEQ(t, tr01OldBarBalance-tr01NewBarBalance, bigint(123400)) - shouldEQ(t, tr01NewBazBalance > tr01OldBazBalance, true) - shouldEQ(t, poolNewBarBalance-poolOldBarBalance, bigint(123400)) - shouldEQ(t, poolNewBazBalance < poolOldBazBalance, true) -} - -// 8. [TC - ROUTER] SwapRoute ExactOut Multi ( wugnot -> qux ) -func TestRotuerSwapRouteExactOutputMultiPath(t *testing.T) { - std.TestSetPrevRealm("gno.land/r/router") - std.TestSetOrigCaller(tr01) - - // prepare ugnot - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 500_000_000) - - tr01OldWugnotBalance := wugnot.BalanceOf(a2u(tr01)) - tr01OldQuxBalance := qux.BalanceOf(a2u(tr01)) - poolOldWugnotBalance := wugnot.BalanceOf(a2u(poolAddr)) - poolOldQuxBalance := qux.BalanceOf(a2u(poolAddr)) - - std.TestSetOrigSend(std.Coins{{"ugnot", 1_000_000}}, nil) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 1_000_000) - - swapAmount := 987_654 - rou.SwapRoute( - gnotPath, - quxPath, - bigint(swapAmount), - "EXACT_OUT", - "gnot:gno.land/r/bar:100*POOL*gno.land/r/bar:gno.land/r/baz:100*POOL*gno.land/r/baz:gno.land/r/qux:100,gnot:gno.land/r/bar:500*POOL*gno.land/r/bar:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/qux:500", - "40,60", - 123456789, - ) - - tr01NewWugnotBalance := wugnot.BalanceOf(a2u(tr01)) - tr01NewQuxBalance := qux.BalanceOf(a2u(tr01)) - poolNewWugnotBalance := wugnot.BalanceOf(a2u(poolAddr)) - poolNewQuxBalance := qux.BalanceOf(a2u(poolAddr)) - - shouldEQ(t, poolOldQuxBalance-poolNewQuxBalance < uint64(swapAmount), true) - shouldEQ(t, poolNewWugnotBalance > poolOldWugnotBalance, true) -} - -// 9. [TC - POSITION] Collect -func TestPositionCollect01(t *testing.T) { - { - // lp01 collects fee from tokenId '1' - std.TestSetPrevRealm("gno.land/r/position") - std.TestSetOrigCaller(lp01) - cAmount0, cAmount1 := pos.Collect( - 1, // tokenId - lp01, // recipient - 100000, // amount0Max - 100000, // amount1Max - ) - std.TestSkipHeights(1) - - shouldEQ(t, cAmount0, bigint(0)) // bar - shouldEQ(t, cAmount1, bigint(6)) // ugnot - } - - { - // lp02 collects fee from tokenId '2' - std.TestSetOrigCaller(lp02) - - lp02OldBarBalance := bar.BalanceOf(a2u(lp02)) - lp02OldBazBalance := baz.BalanceOf(a2u(lp02)) - - cAmount0, cAmount1 := pos.Collect( - 2, // tokenId - lp02, // recipient - 100000, // amount0Max - 100000, // amount1Max - ) - std.TestSkipHeights(1) - - lp02NewBarBalance := bar.BalanceOf(a2u(lp02)) - lp02NewBazBalance := baz.BalanceOf(a2u(lp02)) - - shouldEQ(t, cAmount0 > 0, true) - shouldEQ(t, cAmount1 == 0, true) - - shouldEQ(t, lp02NewBarBalance-uint64(cAmount0), lp02OldBarBalance) - shouldEQ(t, lp02OldBazBalance, lp02OldBazBalance) - } -} - -// 10. [TC - STAKER] UnstakeToken -func TestStakerUnstakeToken(t *testing.T) { - { - // lp01 unstakes tokenId '1' - std.TestSetPrevAddr(lp01) // r3v4_xxx: only test code, we need to call stk.UnstakeToken directly from user - - // skip 15 days - std.TestSkipHeights(259200) - stk.UnstakeToken(1) - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(bigint(1)), lp01) - shouldEQ(t, gns.BalanceOf(a2u(lp01)), bigint(3850)) - shouldEQ(t, obl.BalanceOf(a2u(lp01)), bigint(4951)) - } - - { - // lp02 unstakes tokenId '2' - std.TestSetPrevAddr(lp02) // r3v4_xxx: only test code, we need to stk gnft.UnstakeToken directly from user - - // skip 20 days more - std.TestSkipHeights(345600) - stk.UnstakeToken(2) - std.TestSkipHeights(1) - - shouldEQ(t, gnft.OwnerOf(bigint(2)), lp02) - shouldEQ(t, gns.BalanceOf(a2u(lp02)), bigint(0)) // pool for tokenId '2' isn't reward target - shouldEQ(t, obl.BalanceOf(a2u(lp02)), bigint(0)) // pool for tokenId '2' isn't reward target - } -} - -// 11. [TC - STAKER] EndExternalIncentive -func TestStakerEndExternalIncentive(t *testing.T) { - std.TestSetOrigCaller(ci01) - std.TestSkipHeights(1036800) - - ci01OldRewardBal := obl.BalanceOf(a2u(ci01)) - - stk.EndExternalIncentive(ci01.String(), "gno.land/r/bar:gnot:100", "gno.land/r/obl") // use same parameter as CreateExternalIncentive - std.TestSkipHeights(1) - - ci01NewRewardBal := obl.BalanceOf(a2u(ci01)) - shouldGT(t, ci01NewRewardBal, ci01OldRewardBal) - - // shouldPanicWithMsg( - // t, - // func() { - // stk.EndExternalIncentive(ci01.String(), "gno.land/r/bar:gnot:100", "gno.land/r/obl") - // }, - // "[STAKER] staker.gno__EndExternalIncentive() || cannot end non existent incentive(ZzF2ZDVucXYybHRhMDQ3aDZsdGEwNDdoNmx0YTA0N2g2bGswd2hjZDpnbm8ubGFuZC9yL2Jhcjp1Z25vdDoxMDA6Z25vLmxhbmQvci9vYmw=)", - // ) -} - -/* UTILS */ -func token0Balance(addr std.Address) bigint { - return bigint(foo.BalanceOf(a2u(addr))) -} - -func token1Balance(addr std.Address) bigint { - return bigint(bar.BalanceOf(a2u(addr))) -} - -func a2u(addr std.Address) users.AddressOrName { - return users.AddressOrName(addr) -} - -func tid(tokenId interface{}) grc721.TokenID { - if tokenId == nil { - panic("[TC - POSITION] test_helper.gno__tid() || tokenId is nil") - } - - switch tokenId.(type) { - case bigint: - return grc721.TokenID(string(tokenId.(bigint))) - case string: - return grc721.TokenID(tokenId.(string)) - case int: - return grc721.TokenID(strconv.Itoa(tokenId.(int))) - case uint64: - return grc721.TokenID(strconv.Itoa(int(tokenId.(uint64)))) - case grc721.TokenID: - return tokenId.(grc721.TokenID) - default: - panic("[TC - STAKER] utils.gno__tid() || unsupported tokenId type") - } -} - -func BalanceOf(token *grc20.AdminToken, addr std.Address) bigint { - balance, err := token.BalanceOf(addr) - if err != nil { - panic(err) - } - return bigint(balance) -} - -/* HELPERS */ -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, 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 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() -} - -func ugnotBalance(addr std.Address) uint64 { - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - return uint64(testBanker.GetCoins(addr)[0].Amount) -} diff --git a/_test/init_only.mk b/_test/init_only.mk new file mode 100644 index 00000000..fbccc92a --- /dev/null +++ b/_test/init_only.mk @@ -0,0 +1,321 @@ +# Test Mnemonic +# source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast + +# Test Accounts +# gnokey add -recover=true -index 10 gsa +# gnokey add -recover=true -index 11 lp01 +# gnokey add -recover=true -index 12 lp02 +# gnokey add -recover=true -index 13 tr01 + +ADDR_GSA := g12l9splsyngcgefrwa52x5a7scc29e9v086m6p4 +ADDR_LP01 := g1jqpr8r5akez83kp7ers0sfjyv2kgx45qa9qygd +ADDR_LP02 := g126yz2f34qdxaqxelmky40dym379q0vw3yzhyrq +ADDR_TR01 := g1wgdjecn5lylgvujzyspfzvhjm6qn4z8xqyyxdn + +ADDR_POOL := g1ee305k8yk0pjz443xpwtqdyep522f9g5r7d63w +ADDR_POS := g1htpxzv2dkplvzg50nd8fswrneaxmdpwn459thx +ADDR_STAKER := g13h5s9utqcwg3a655njen0p89txusjpfrs3vxp8 +ADDR_ROUTER := g1ernz3lj85hnn3ucug73ymgkhqdv2lg8e4yd48e +ADDR_GOV := g1wj5lwwmkru3ky6dh2zztanrcj2ups8g0pfe8cu + +TX_EXPIRE := 9999999999 + +NOW := $(shell date +%s) +INCENTIVE_START := $(shell expr $(NOW) + 120) +INCENTIVE_END := $(shell expr $(NOW) + 7776000) # 90 DAY + +MAKEFILE := $(shell realpath $(firstword $(MAKEFILE_LIST))) +GNOLAND_RPC_URL ?= localhost:26657 +CHAINID ?= dev +ROOT_DIR:=$(shell dirname $(MAKEFILE))/.. + +.PHONY: help +help: + @echo "Available make commands:" + @cat $(MAKEFILE) | grep '^[a-z][^:]*:' | cut -d: -f1 | sort | sed 's/^/ /' + +.PHONY: all +all: deploy faucet approve pool-setup position-mint + +.PHONY: deploy +deploy: deploy-foo deploy-bar deploy-baz deploy-qux deploy-wugnot deploy-gns deploy-obl deploy-gnft deploy-gov deploy-pool deploy-position deploy-staker deploy-router deploy-grc20_wrapper + +.PHONY: faucet +faucet: faucet-lp01 faucet-lp02 faucet-tr01 faucet-gsa + +.PHONY: approve +approve: approve-lp01 approve-lp02 approve-tr01 approve-gsa + +.PHONY: pool-setup +pool-setup: pool-init pool-create + +.PHONY: position-mint +position-mint: mint-01 mint-02 mint-03 mint-rest + +# Deploy Tokens +# [GRC20] FOO, BAR, BAZ, QUX: Token Pair for Pool +# [GRC20] WUGNOT: Wrapped GRC20 for native ugnot +# [GRC20] GNS: Default Staking Reward +# [GRC20] OBL: External Staking Reward +# [GRC721] GNFT: LP Token +deploy-foo: + $(info ************ [FOO] deploy foo ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/_setup/foo -pkgpath gno.land/r/foo -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + +deploy-bar: + $(info ************ [BAR] deploy bar ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/_setup/bar -pkgpath gno.land/r/bar -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + +deploy-baz: + $(info ************ [BAZ] deploy baz ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/_setup/baz -pkgpath gno.land/r/baz -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + +deploy-qux: + $(info ************ [QUX] deploy qux ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/_setup/qux -pkgpath gno.land/r/qux -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + +deploy-wugnot: + $(info ************ [WUGNOT] deploy wugnot ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/_setup/wugnot -pkgpath gno.land/r/wugnot -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + +deploy-gns: + $(info ************ [GNS] deploy staking reward ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/_setup/gns -pkgpath gno.land/r/gns -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + +deploy-obl: + $(info ************ [OBL] deploy external staking reward ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/_setup/obl -pkgpath gno.land/r/obl -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + +deploy-gnft: + $(info ************ [GNFT] deploy lp token ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/_setup/gnft -pkgpath gno.land/r/gnft -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + + +# Deploy Contracts +deploy-gov: + $(info ************ [GOV] deploy governance ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/gov -pkgpath gno.land/r/gov -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + +deploy-pool: + $(info ************ [POOL] deploy pool ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/pool -pkgpath gno.land/r/pool -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + +deploy-position: + $(info ************ [POSITION] deploy position ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/position -pkgpath gno.land/r/position -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + +deploy-staker: + $(info ************ [STAKER] deploy staker ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/staker -pkgpath gno.land/r/staker -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + +deploy-router: + $(info ************ [ROUTER] deploy router ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/router -pkgpath gno.land/r/router -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + +deploy-grc20_wrapper: + $(info ************ [GRC20 Wrapper] deploy grc20_wrapper ************) + @echo "" | gnokey maketx addpkg -pkgdir $(ROOT_DIR)/_setup/grc20_wrapper -pkgpath gno.land/r/grc20_wrapper -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo + + +# Facuet Tokens +faucet-lp01: + $(info ************ [FAUCET] foo & bar & baz & qux & wugnot to lp01 ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/foo -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/bar -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/baz -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/qux -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo + +faucet-lp02: + $(info ************ [FAUCET] foo & bar & baz & qux & wugnot to lp02 ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/foo -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/bar -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/baz -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/qux -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo + +faucet-tr01: + $(info ************ [FAUCET] foo & bar & baz & qux & wugnot to tr01 ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/foo -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" tr01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/bar -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" tr01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/baz -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" tr01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/qux -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" tr01 > /dev/null + @echo + +faucet-gsa: + $(info ************ [FAUCET] foo & bar & baz & qux & wugnot & gns to gsa ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/foo -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/bar -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/baz -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/qux -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/gns -func FaucetL -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo + + +# Approve Tokens +approve-lp01: + $(info ************ [APPROVE] foo & bar & baz & qux & wugnot from lp01 to pool ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/foo -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/bar -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/baz -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/qux -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/wugnot -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo + +approve-lp02: + $(info ************ [APPROVE] foo & bar & baz & qux & wugnot from lp02 to pool ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/foo -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/bar -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/baz -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/qux -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/wugnot -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo + +approve-tr01: + $(info ************ [APPROVE] foo & bar & baz & qux & wugnot from tr01 to pool ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/foo -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" tr01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/bar -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" tr01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/baz -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" tr01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/qux -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" tr01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/wugnot -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" tr01 > /dev/null + @echo + +approve-gsa: + $(info ************ [APPROVE] from gsa (gns to pool, wugnot to staker) ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/gns -func Approve -args $(ADDR_POOL) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/wugnot -func Approve -args $(ADDR_STAKER) -args 50000000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo + + +# Pool +pool-init: + $(info ************ [POOL] init pool ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/pool -func InitManual -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo + +pool-create: + $(info ************ [POOL] create pools ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/pool -func CreatePool -args "gnot" -args "gno.land/r/bar" -args 100 -args 101729702841318637793976746270 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/pool -func CreatePool -args "gno.land/r/bar" -args "gno.land/r/baz" -args 100 -args 101729702841318637793976746270 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/pool -func CreatePool -args "gno.land/r/baz" -args "gno.land/r/qux" -args 100 -args 101729702841318637793976746270 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + + @echo "" | gnokey maketx call -pkgpath gno.land/r/pool -func CreatePool -args "gnot" -args "gno.land/r/bar" -args 500 -args 101729702841318637793976746270 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/pool -func CreatePool -args "gno.land/r/bar" -args "gno.land/r/baz" -args 500 -args 101729702841318637793976746270 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/pool -func CreatePool -args "gno.land/r/baz" -args "gno.land/r/qux" -args 500 -args 101729702841318637793976746270 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo + + +# Position +mint-01: + $(info ************ [POSITION - 1] mint gnot & bar // tick range 4000 ~ 6000 // by lp01 ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/position -func Mint -args "gnot" -args "gno.land/r/bar" -args 100 -args 4000 -args 6000 -args 10000000 -args 10000000 -args 0 -args 0 -args $(TX_EXPIRE) -send "10000000ugnot" -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo + +mint-02: + $(info ************ [POSITION - 2] mint bar & baz // tick range 4000 ~ 6000 // by lp02 ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/position -func Mint -args "gno.land/r/bar" -args "gno.land/r/baz" -args 100 -args 4000 -args 6000 -args 10000000 -args 10000000 -args 0 -args 0 -args $(TX_EXPIRE) -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo + +mint-03: + $(info ************ [POSITION - 3] mint baz & qux // tick range 4000 ~ 6000 // by lp02 ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/position -func Mint -args "gno.land/r/baz" -args "gno.land/r/qux" -args 100 -args 4000 -args 6000 -args 10000000 -args 10000000 -args 0 -args 0 -args $(TX_EXPIRE) -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo + +mint-rest: + $(info ************ [POSITION - 4,5,6] ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/position -func Mint -args "gno.land/r/bar" -args "gnot" -args 500 -args -6000 -args -4000 -args 10000000 -args 10000000 -args 0 -args 0 -args $(TX_EXPIRE) -send "10000000ugnot" -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/position -func Mint -args "gno.land/r/bar" -args "gno.land/r/baz" -args 500 -args 4000 -args 6000 -args 10000000 -args 10000000 -args 0 -args 0 -args $(TX_EXPIRE) -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/position -func Mint -args "gno.land/r/baz" -args "gno.land/r/qux" -args 500 -args 4000 -args 6000 -args 10000000 -args 10000000 -args 0 -args 0 -args $(TX_EXPIRE) -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo + + +# Staker +create-external-incentive: + $(info ************ [STAKER] create external incentive ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/staker -func CreateExternalIncentive -args "gno.land/r/bar:gnot:100" -args "gno.land/r/obl" -args 10000000000 -args $(INCENTIVE_START) -args $(INCENTIVE_END)-insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo + +stake-token-1: + $(info ************ [STAKER] stake gnft tokenId 1) + @$(MAKE) -f $(MAKEFILE) skip-time + @echo "" | gnokey maketx call -pkgpath gno.land/r/gnft -func Approve -args $(ADDR_STAKER) -args "1" -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/staker -func StakeToken -args 1 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo + +stake-token-2: + $(info ************ [STAKER] stake gnft tokenId 2) + @$(MAKE) -f $(MAKEFILE) skip-time + @echo "" | gnokey maketx call -pkgpath gno.land/r/gnft -func Approve -args $(ADDR_STAKER) -args "2" -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo "" | gnokey maketx call -pkgpath gno.land/r/staker -func StakeToken -args 2 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo + + +# Swap +set-protocol-fee: + $(info ************ [POOL] Set Protocol Fee ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/pool -func SetFeeProtocol -args 6 -args 8 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" gsa > /dev/null + @echo + +swap-exact-in-single: + $(info ************ [ROUTER] Swap 123_456 BAR to BAZ // singlePath ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/router -func SwapRoute -args "gno.land/r/bar" -args "gno.land/r/baz" -args 123456 -args "EXACT_IN" -args "gno.land/r/bar:gno.land/r/baz:100" -args "100" -args 200000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" tr01 > /dev/null + @echo + +swap-exact-out-multi: + $(info ************ [ROUTER] Swap NATIVE ugnot to 987_654 QUX // multiPath ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/router -func SwapRoute -args "gnot" -args "gno.land/r/qux" -args 987654 -args "EXACT_OUT" -args "gnot:gno.land/r/bar:100*POOL*gno.land/r/bar:gno.land/r/baz:100*POOL*gno.land/r/baz:gno.land/r/qux:100,gnot:gno.land/r/bar:500*POOL*gno.land/r/bar:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/qux:500" -args "40,60" -args 654321 -send 100000000ugnot -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" tr01 > /dev/null + @echo + +collect-lp01: + $(info ************ [POSITION] Collect swap fee at position of tokenId 1 ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/position -func Collect -args 1 -args $(ADDR_LP01) -args 1000000 -args 1000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo + +collect-lp02: + $(info ************ [POSITION] Collect swap fee at position of tokenId 2 ************) + @echo "" | gnokey maketx call -pkgpath gno.land/r/position -func Collect -args 2 -args $(ADDR_LP02) -args 1000000 -args 1000000 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo + + +## Staker // Unstake +unstake-token-1: + $(info ************ [STAKER] unstake gnft tokenId 1 ************) + @$(MAKE) -f $(MAKEFILE) skip-time + @echo "" | gnokey maketx call -pkgpath gno.land/r/staker -func UnstakeToken -args 1 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp01 > /dev/null + @echo + +unstake-token-2: + $(info ************ [STAKER] unstake gnft tokenId 2 ************) + @$(MAKE) -f $(MAKEFILE) skip-time + @echo "" | gnokey maketx call -pkgpath gno.land/r/staker -func UnstakeToken -args 2 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" lp02 > /dev/null + @echo + +### can not test staker EndExternalIncentive +### it needs to wait for 90 days ( which we can't skip it in makefiles ) + +done: + @echo "" | gnokey maketx send -send 1ugnot -to $(ADDR_GOV) -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + +## ETC +# gno time.Now returns last block time, not actual time +# so to skip time, we need new block +skip-time: + $(info > SKIP 3 BLOCKS) + @echo "" | gnokey maketx send -send 1ugnot -to g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo "" | gnokey maketx send -send 1ugnot -to g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo "" | gnokey maketx send -send 1ugnot -to g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -insecure-password-stdin=true -remote $(GNOLAND_RPC_URL) -broadcast=true -chainid $(CHAINID) -gas-fee 1ugnot -gas-wanted 9000000 -memo "" test1 > /dev/null + @echo \ No newline at end of file diff --git a/router/_TEST_router_api_getter_test.gnoa b/router/_TEST_router_api_getter_test.gnoa deleted file mode 100644 index e516abb0..00000000 --- a/router/_TEST_router_api_getter_test.gnoa +++ /dev/null @@ -1,88 +0,0 @@ -package router - -import ( - "encoding/gjson" - "std" - "testing" - - "gno.land/p/demo/testutils" - - _ "gno.land/r/grc20_wrapper" - p "gno.land/r/pool" - pos "gno.land/r/position" -) - -var ( - pc01 = testutils.TestAddress("pc01") // Pool Creator 01 - lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 - tr01 = testutils.TestAddress("tr01") // Trader 01 - - poolAddr = std.DerivePkgAddr("gno.land/r/pool") - posAddr = std.DerivePkgAddr("gno.land/r/position") - routerAddr = std.DerivePkgAddr("gno.land/r/router") -) - -var ( - // Common - wugnotPath = "gno.land/r/wugnot" // token1 - barPath = "gno.land/r/bar" // token2 - bazPath = "gno.land/r/baz" // token3 - quxPath = "gno.land/r/qux" // token4 - - test_fee100 = uint16(100) - - max_timeout = bigint(9999999999) -) - -func TestInitManual(t *testing.T) { - std.TestSetOrigCaller(pc01) - p.InitManual() - std.TestSkipHeights(1) -} - -func TestCreatePool(t *testing.T) { - std.TestSetOrigCaller(pc01) - - p.CreatePool(wugnotPath, barPath, test_fee100, 130621891405341611593710811006) // tick > -9999, ratio = x0.3679346241605614 ( 1 bar = 0.37 wugnot ) - p.CreatePool(barPath, quxPath, test_fee100, 130621891405341611593710811006) // tick = 10_000, ratio = x2.7181459268252253 ( 1 bar = 2.7 qux ) - p.CreatePool(quxPath, bazPath, test_fee100, 215353707227994575755767921544) // tick > -20000, ratio = x0.13534881653937755 ( 1 qux = 0.13 baz ) - - jsonOutput := p.ApiGetPools() - jsonStr := gjson.Parse(jsonOutput) - shouldEQ(t, len(jsonStr.Get("response.data").Array()), 3) -} - -func TestPositionMint(t *testing.T) { - std.TestSetOrigCaller(lp01) - - // prepare ugnot - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 50000000) - - // simulate transfer & decrase - std.TestSetOrigSend(std.Coins{{"ugnot", 10000000}}, nil) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 10000000) - pos.Mint(wugnotPath, barPath, test_fee100, int32(9000), int32(11000), bigint(10000000), bigint(10000000), 0, 0, max_timeout) - - pos.Mint(barPath, quxPath, test_fee100, int32(9000), int32(11000), bigint(10000000), bigint(10000000), 0, 0, max_timeout) - pos.Mint(quxPath, bazPath, test_fee100, int32(18000), int32(22000), bigint(10000000), bigint(10000000), 0, 0, max_timeout) -} - -func TestApiGetRatiosFromBase(t *testing.T) { - jsonOutput := ApiGetRatiosFromBase() - jsonStr := gjson.Parse(jsonOutput) - jsonArr := jsonStr.Get("response.data").Array() - shouldEQ(t, len(jsonArr), 4) - - shouldEQ(t, jsonArr[0].String(), "{\"gno.land/r/wugnot\":79228162514264337593543950336}") // 1 - shouldEQ(t, jsonArr[1].String(), "{\"gno.land/r/bar\":215332174010593516404127508794}") // 2.7178741394 - shouldEQ(t, jsonArr[2].String(), "{\"gno.land/r/qux\":585304271701315311476720143083}") // 7.3875785217 - shouldEQ(t, jsonArr[3].String(), "{\"gno.land/r/baz\":4323981266259057910270272221253}") // 54.5763164138 -} - -/* HELPER */ -func shouldEQ(t *testing.T, got, expected interface{}) { - if got != expected { - t.Errorf("got %v, expected %v", got, expected) - } -} diff --git a/router/_TEST_router_swap_route_1route_3hop_native_test.gno b/router/_TEST_router_swap_route_1route_3hop_native_test.gno deleted file mode 100644 index 04d32fd8..00000000 --- a/router/_TEST_router_swap_route_1route_3hop_native_test.gno +++ /dev/null @@ -1,161 +0,0 @@ -package router - -import ( - "encoding/gjson" - "std" - "testing" - - "gno.land/p/demo/testutils" - - _ "gno.land/r/grc20_wrapper" - pl "gno.land/r/pool" - pos "gno.land/r/position" -) - -var ( - pc01 = testutils.TestAddress("pc01") // Pool Creator 01 - lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 - tr01 = testutils.TestAddress("tr01") // Trader 01 - - poolAddr = std.DerivePkgAddr("gno.land/r/pool") - posAddr = std.DerivePkgAddr("gno.land/r/position") - routerAddr = std.DerivePkgAddr("gno.land/r/router") -) - -var ( - // Common - barPath = "gno.land/r/bar" - bazPath = "gno.land/r/baz" - fooPath = "gno.land/r/foo" - quxPath = "gno.land/r/qux" - gnotPath = "gnot" - - MAX_TIMEOUT bigint = 9999999999 -) - -// debug addr -func init() { - println(pc01, "// pc01") - println(lp01, "// lp01") - println(tr01, "// tr01") - println(poolAddr, "// pool") - println(posAddr, "// pos") - println(routerAddr, "// router") -} - -func TestInitManual(t *testing.T) { - std.TestSetOrigCaller(pc01) - pl.InitManual() - std.TestSkipHeights(1) -} - -func TestCreatePool(t *testing.T) { - std.TestSetOrigCaller(pc01) - - 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 - pl.CreatePool(quxPath, gnotPath, uint16(500), 130621891405341611593710811006) // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 - // 1 bar ≈ 19.683 gnot - - jsonOutput := pl.ApiGetPools() - jsonStr := gjson.Parse(jsonOutput) - shouldEQ(t, len(jsonStr.Get("response.data").Array()), 3) -} - -func TestPositionMintBarBaz(t *testing.T) { - std.TestSetOrigCaller(lp01) - - tokenId, liquidity, amount0, amount1 := pos.Mint(barPath, bazPath, uint16(500), int32(9000), int32(11000), bigint(100_000), bigint(100_000), 0, 0, MAX_TIMEOUT) - shouldEQ(t, tokenId, uint64(1)) - shouldEQ(t, amount0 > 0, true) // 36789 bar - shouldEQ(t, amount1 > 0, true) // 99999 baz -} - -func TestPositionMintBazQux(t *testing.T) { - std.TestSetOrigCaller(lp01) - - tokenId, liquidity, amount0, amount1 := pos.Mint(bazPath, quxPath, uint16(500), int32(9000), int32(11000), bigint(100_000), bigint(100_000), 0, 0, MAX_TIMEOUT) - shouldEQ(t, tokenId, uint64(2)) - shouldEQ(t, amount0 > 0, true) // 36789 baz - shouldEQ(t, amount1 > 0, true) // 99999 qux -} - -func TestPositionMintQuxGnot(t *testing.T) { - std.TestSetOrigCaller(lp01) - - // prepare ugnot - testBanker := std.GetBanker(std.BankerTypeRealmIssue) - testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 99999) - - // send - std.TestSetOrigSend(std.Coins{{"ugnot", 99999}}, nil) - testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 99999) - - tokenId, liquidity, amount0, amount1 := pos.Mint(quxPath, gnotPath, uint16(500), int32(9000), int32(11000), bigint(100_000), bigint(100_000), 0, 0, MAX_TIMEOUT) - shouldEQ(t, tokenId, uint64(3)) - shouldEQ(t, amount0 > 0, true) // 36789 qux - shouldEQ(t, amount1 > 0, true) // 99999 gnot -} - -func TestDrySwapRouteBarGnotExactIn(t *testing.T) { - std.TestSetOrigCaller(lp01) - - dryResult := DrySwapRoute( - barPath, // inputToken - gnotPath, // outputToken - bigint(1000), // amountSpecified - "EXACT_IN", // swapType - "gno.land/r/bar:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/qux:500*POOL*gno.land/r/qux:gnot:500", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, bigint(19740)) -} - -func TestDrySwapRouteBarGnotExactOut(t *testing.T) { - std.TestSetOrigCaller(lp01) - - dryResult := DrySwapRoute( - barPath, // inputToken - gnotPath, // outputToken - bigint(20000), // amountSpecified - "EXACT_OUT", // swapType - "gno.land/r/bar:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/qux:500*POOL*gno.land/r/qux:gnot:500", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, bigint(1011)) -} - -func TestDrySwapRouteGnotBarExactIn(t *testing.T) { - std.TestSetOrigCaller(lp01) - - dryResult := DrySwapRoute( - gnotPath, // intputToken - barPath, // outputToken - bigint(5000), // amountSpecified - "EXACT_IN", // swapType - "gnot:gno.land/r/qux:500*POOL*gno.land/r/qux:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/bar:500", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, bigint(247)) -} - -func TestDrySwapRouteGnotBarExactOut(t *testing.T) { - std.TestSetOrigCaller(lp01) - - dryResult := DrySwapRoute( - gnotPath, // intputToken - barPath, // outputToken - bigint(100), // amountSpecified - "EXACT_OUT", // swapType - "gnot:gno.land/r/qux:500*POOL*gno.land/r/qux:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/bar:500", // strRouteArr - "100", // quoteArr - ) - shouldEQ(t, dryResult, bigint(2003)) -} - -/* HELPER */ -func shouldEQ(t *testing.T, got, expected interface{}) { - if got != expected { - t.Errorf("got %v, expected %v", got, expected) - } -} From ff49d0d4c5e85d236e8ea5f16b7992a3242b091a Mon Sep 17 00:00:00 2001 From: n3wbie Date: Mon, 4 Dec 2023 20:51:37 +0900 Subject: [PATCH 2/3] fix: price calculation --- router/_RPC_api.gno | 6 +- router/_TEST_router_api_getter_test.gno | 88 ++++++++++ ...er_swap_route_1route_3hop_native_test.gnoa | 161 ++++++++++++++++++ ...ST_router_swap_route_2route_2hop_test.gnoa | 18 +- router/consts.gno | 2 - 5 files changed, 261 insertions(+), 14 deletions(-) create mode 100644 router/_TEST_router_api_getter_test.gno create mode 100644 router/_TEST_router_swap_route_1route_3hop_native_test.gnoa diff --git a/router/_RPC_api.gno b/router/_RPC_api.gno index 23dc6d39..372447ef 100644 --- a/router/_RPC_api.gno +++ b/router/_RPC_api.gno @@ -51,13 +51,13 @@ func getRatiosFromBase(maxHops int) []map[string]bigint { tokenPrice := make(map[string]bigint, 0) // BASE - tokenPrice[BASE_TOKEN] = Q96 // ~= 1 + tokenPrice[GNOT] = Q96 // ~= 1 // ELSE tokenList := getTokenList() for _, token := range tokenList { - if token != BASE_TOKEN { - _swapPaths := findSwapPaths(token, BASE_TOKEN, maxHops) + if token != GNOT { + _swapPaths := findSwapPaths(token, GNOT, maxHops) swapPaths := strings.Split(_swapPaths, "_FIN_") swapPaths = swapPaths[:len(swapPaths)-1] numSwapPaths := len(swapPaths) diff --git a/router/_TEST_router_api_getter_test.gno b/router/_TEST_router_api_getter_test.gno new file mode 100644 index 00000000..102f3772 --- /dev/null +++ b/router/_TEST_router_api_getter_test.gno @@ -0,0 +1,88 @@ +package router + +import ( + "encoding/gjson" + "std" + "testing" + + "gno.land/p/demo/testutils" + + _ "gno.land/r/grc20_wrapper" + p "gno.land/r/pool" + pos "gno.land/r/position" +) + +var ( + pc01 = testutils.TestAddress("pc01") // Pool Creator 01 + lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 + tr01 = testutils.TestAddress("tr01") // Trader 01 + + poolAddr = std.DerivePkgAddr("gno.land/r/pool") + posAddr = std.DerivePkgAddr("gno.land/r/position") + routerAddr = std.DerivePkgAddr("gno.land/r/router") +) + +var ( + // Common + gnotPath = "gnot" // token1 + barPath = "gno.land/r/bar" // token2 + bazPath = "gno.land/r/baz" // token3 + quxPath = "gno.land/r/qux" // token4 + + test_fee100 = uint16(100) + + max_timeout = bigint(9999999999) +) + +func TestInitManual(t *testing.T) { + std.TestSetOrigCaller(pc01) + p.InitManual() + std.TestSkipHeights(1) +} + +func TestCreatePool(t *testing.T) { + std.TestSetOrigCaller(pc01) + + p.CreatePool(gnotPath, barPath, test_fee100, 130621891405341611593710811006) // tick > -9999, ratio = x0.3679346241605614 ( 1 bar = 0.37 wugnot ) + p.CreatePool(barPath, quxPath, test_fee100, 130621891405341611593710811006) // tick = 10_000, ratio = x2.7181459268252253 ( 1 bar = 2.7 qux ) + p.CreatePool(quxPath, bazPath, test_fee100, 215353707227994575755767921544) // tick > -20000, ratio = x0.13534881653937755 ( 1 qux = 0.13 baz ) + + jsonOutput := p.ApiGetPools() + jsonStr := gjson.Parse(jsonOutput) + shouldEQ(t, len(jsonStr.Get("response.data").Array()), 3) +} + +func TestPositionMint(t *testing.T) { + std.TestSetOrigCaller(lp01) + + // prepare ugnot + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 50000000) + + // simulate transfer & decrase + std.TestSetOrigSend(std.Coins{{"ugnot", 10000000}}, nil) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 10000000) + pos.Mint(gnotPath, barPath, test_fee100, int32(9000), int32(11000), bigint(10000000), bigint(10000000), 0, 0, max_timeout) + + pos.Mint(barPath, quxPath, test_fee100, int32(9000), int32(11000), bigint(10000000), bigint(10000000), 0, 0, max_timeout) + pos.Mint(quxPath, bazPath, test_fee100, int32(18000), int32(22000), bigint(10000000), bigint(10000000), 0, 0, max_timeout) +} + +func TestApiGetRatiosFromBase(t *testing.T) { + jsonOutput := ApiGetRatiosFromBase() + jsonStr := gjson.Parse(jsonOutput) + jsonArr := jsonStr.Get("response.data").Array() + shouldEQ(t, len(jsonArr), 4) + + shouldEQ(t, jsonArr[0].String(), "{\"gnot\":79228162514264337593543950336}") // 1 + shouldEQ(t, jsonArr[1].String(), "{\"gno.land/r/bar\":215332174010593516404127508794}") // 2.7178741394 + shouldEQ(t, jsonArr[2].String(), "{\"gno.land/r/qux\":585304271701315311476720143083}") // 7.3875785217 + shouldEQ(t, jsonArr[3].String(), "{\"gno.land/r/baz\":4323981266259057910270272221253}") // 54.5763164138 +} + +/* HELPER */ +func shouldEQ(t *testing.T, got, expected interface{}) { + if got != expected { + t.Errorf("got %v, expected %v", got, expected) + } +} diff --git a/router/_TEST_router_swap_route_1route_3hop_native_test.gnoa b/router/_TEST_router_swap_route_1route_3hop_native_test.gnoa new file mode 100644 index 00000000..4389b5d3 --- /dev/null +++ b/router/_TEST_router_swap_route_1route_3hop_native_test.gnoa @@ -0,0 +1,161 @@ +package router + +import ( + "encoding/gjson" + "std" + "testing" + + "gno.land/p/demo/testutils" + + _ "gno.land/r/grc20_wrapper" + pl "gno.land/r/pool" + pos "gno.land/r/position" +) + +var ( + pc01 = testutils.TestAddress("pc01") // Pool Creator 01 + lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 + tr01 = testutils.TestAddress("tr01") // Trader 01 + + poolAddr = std.DerivePkgAddr("gno.land/r/pool") + posAddr = std.DerivePkgAddr("gno.land/r/position") + routerAddr = std.DerivePkgAddr("gno.land/r/router") +) + +var ( + // Common + barPath = "gno.land/r/bar" + bazPath = "gno.land/r/baz" + fooPath = "gno.land/r/foo" + quxPath = "gno.land/r/qux" + gnotPath = "gnot" + + MAX_TIMEOUT bigint = 9999999999 +) + +// // debug addr +// func init() { +// println(pc01, "// pc01") +// println(lp01, "// lp01") +// println(tr01, "// tr01") +// println(poolAddr, "// pool") +// println(posAddr, "// pos") +// println(routerAddr, "// router") +// } + +func TestInitManual(t *testing.T) { + std.TestSetOrigCaller(pc01) + pl.InitManual() + std.TestSkipHeights(1) +} + +func TestCreatePool(t *testing.T) { + std.TestSetOrigCaller(pc01) + + 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 + pl.CreatePool(quxPath, gnotPath, uint16(500), 130621891405341611593710811006) // tick = 10_000, ratio = 2.71814592682522526700950038502924144268035888671875 + // 1 bar ≈ 19.683 gnot + + jsonOutput := pl.ApiGetPools() + jsonStr := gjson.Parse(jsonOutput) + shouldEQ(t, len(jsonStr.Get("response.data").Array()), 3) +} + +func TestPositionMintBarBaz(t *testing.T) { + std.TestSetOrigCaller(lp01) + + tokenId, liquidity, amount0, amount1 := pos.Mint(barPath, bazPath, uint16(500), int32(9000), int32(11000), bigint(100_000), bigint(100_000), 0, 0, MAX_TIMEOUT) + shouldEQ(t, tokenId, uint64(1)) + shouldEQ(t, amount0 > 0, true) // 36789 bar + shouldEQ(t, amount1 > 0, true) // 99999 baz +} + +func TestPositionMintBazQux(t *testing.T) { + std.TestSetOrigCaller(lp01) + + tokenId, liquidity, amount0, amount1 := pos.Mint(bazPath, quxPath, uint16(500), int32(9000), int32(11000), bigint(100_000), bigint(100_000), 0, 0, MAX_TIMEOUT) + shouldEQ(t, tokenId, uint64(2)) + shouldEQ(t, amount0 > 0, true) // 36789 baz + shouldEQ(t, amount1 > 0, true) // 99999 qux +} + +func TestPositionMintQuxGnot(t *testing.T) { + std.TestSetOrigCaller(lp01) + + // prepare ugnot + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 99999) + + // send + std.TestSetOrigSend(std.Coins{{"ugnot", 99999}}, nil) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 99999) + + tokenId, liquidity, amount0, amount1 := pos.Mint(quxPath, gnotPath, uint16(500), int32(9000), int32(11000), bigint(100_000), bigint(100_000), 0, 0, MAX_TIMEOUT) + shouldEQ(t, tokenId, uint64(3)) + shouldEQ(t, amount0 > 0, true) // 36789 qux + shouldEQ(t, amount1 > 0, true) // 99999 gnot +} + +func TestDrySwapRouteBarGnotExactIn(t *testing.T) { + std.TestSetOrigCaller(lp01) + + dryResult := DrySwapRoute( + barPath, // inputToken + gnotPath, // outputToken + bigint(1000), // amountSpecified + "EXACT_IN", // swapType + "gno.land/r/bar:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/qux:500*POOL*gno.land/r/qux:gnot:500", // strRouteArr + "100", // quoteArr + ) + shouldEQ(t, dryResult, bigint(19740)) +} + +func TestDrySwapRouteBarGnotExactOut(t *testing.T) { + std.TestSetOrigCaller(lp01) + + dryResult := DrySwapRoute( + barPath, // inputToken + gnotPath, // outputToken + bigint(20000), // amountSpecified + "EXACT_OUT", // swapType + "gno.land/r/bar:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/qux:500*POOL*gno.land/r/qux:gnot:500", // strRouteArr + "100", // quoteArr + ) + shouldEQ(t, dryResult, bigint(1011)) +} + +func TestDrySwapRouteGnotBarExactIn(t *testing.T) { + std.TestSetOrigCaller(lp01) + + dryResult := DrySwapRoute( + gnotPath, // intputToken + barPath, // outputToken + bigint(5000), // amountSpecified + "EXACT_IN", // swapType + "gnot:gno.land/r/qux:500*POOL*gno.land/r/qux:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/bar:500", // strRouteArr + "100", // quoteArr + ) + shouldEQ(t, dryResult, bigint(247)) +} + +func TestDrySwapRouteGnotBarExactOut(t *testing.T) { + std.TestSetOrigCaller(lp01) + + dryResult := DrySwapRoute( + gnotPath, // intputToken + barPath, // outputToken + bigint(100), // amountSpecified + "EXACT_OUT", // swapType + "gnot:gno.land/r/qux:500*POOL*gno.land/r/qux:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/bar:500", // strRouteArr + "100", // quoteArr + ) + shouldEQ(t, dryResult, bigint(2003)) +} + +/* HELPER */ +func shouldEQ(t *testing.T, got, expected interface{}) { + if got != expected { + t.Errorf("got %v, expected %v", got, expected) + } +} diff --git a/router/_TEST_router_swap_route_2route_2hop_test.gnoa b/router/_TEST_router_swap_route_2route_2hop_test.gnoa index 42b81990..8908e001 100644 --- a/router/_TEST_router_swap_route_2route_2hop_test.gnoa +++ b/router/_TEST_router_swap_route_2route_2hop_test.gnoa @@ -32,15 +32,15 @@ var ( MAX_TIMEOUT bigint = 9999999999 ) -// debug addr -func init() { - println(pc01, "// pc01") - println(lp01, "// lp01") - println(tr01, "// tr01") - println(poolAddr, "// pool") - println(posAddr, "// pos") - println(routerAddr, "// router") -} +// // debug addr +// func init() { +// println(pc01, "// pc01") +// println(lp01, "// lp01") +// println(tr01, "// tr01") +// println(poolAddr, "// pool") +// println(posAddr, "// pos") +// println(routerAddr, "// router") +// } func TestInitManual(t *testing.T) { std.TestSetOrigCaller(pc01) diff --git a/router/consts.gno b/router/consts.gno index ead1b831..6763dc99 100644 --- a/router/consts.gno +++ b/router/consts.gno @@ -3,8 +3,6 @@ package router import "std" const ( - BASE_TOKEN = "gno.land/r/wugnot" // find_path.gno - MIN_PRICE bigint = 4295128740 // MIN_SQRT_RATIO + 1 MAX_PRICE bigint = 1461446703485210103287273052203988822378723970341 // MAX_SQRT_RATIO - 1 From caeb10d0d372025f79bdcd98e18f2929431fd288 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Mon, 4 Dec 2023 20:52:00 +0900 Subject: [PATCH 3/3] chore: similar testcase for init.mk --- _test/_TEST_init_only_test.gno | 340 +++++++++++++++++++++ _test/_TEST_live_test.gnoa | 544 +++++++++++++++++++++++++++++++++ 2 files changed, 884 insertions(+) create mode 100644 _test/_TEST_init_only_test.gno create mode 100644 _test/_TEST_live_test.gnoa diff --git a/_test/_TEST_init_only_test.gno b/_test/_TEST_init_only_test.gno new file mode 100644 index 00000000..6860117b --- /dev/null +++ b/_test/_TEST_init_only_test.gno @@ -0,0 +1,340 @@ +package tc + +import ( + "encoding/gjson" + "std" + "strconv" + "testing" + + "gno.land/p/demo/grc/grc20" + "gno.land/p/demo/grc/grc721" + "gno.land/p/demo/testutils" + + "gno.land/r/demo/users" + + // swap contract + gov "gno.land/r/gov" + pl "gno.land/r/pool" + pos "gno.land/r/position" + rou "gno.land/r/router" + stk "gno.land/r/staker" + + // token + _ "gno.land/r/grc20_wrapper" + + bar "gno.land/r/bar" + foo "gno.land/r/foo" + gns "gno.land/r/gns" // external reward + // GRC721 LP Token +) + +const ( + TIMESTAMP_90DAYS int64 = 7776000 +) + +var ( + // test address + gsa = testutils.TestAddress("gsa") // Gnswap Admin + pc01 = testutils.TestAddress("pc01") // Pool Creator 01 + ci01 = testutils.TestAddress("ci01") // Create Incentive Caller 01 + lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 + lp02 = testutils.TestAddress("lp02") // Liquidity Provider 02 + tr01 = testutils.TestAddress("tr01") // Trader 01 + + // contract address + poolAddr = pl.GetOrigPkgAddr() // Pool Contract + posAddr = pos.GetOrigPkgAddr() // Position Contract + stakerAddr = stk.GetOrigPkgAddr() // Staker Contract + govAddr = gov.GetOrigPkgAddr() // Gov Contract + rouAddr = rou.GetOrigPkgAddr() // Router Contract + + // token path + fooPath = "gno.land/r/foo" + barPath = "gno.land/r/bar" + bazPath = "gno.land/r/baz" + quxPath = "gno.land/r/qux" + gnsPath = "gno.land/r/gns" + oblPath = "gno.land/r/obl" + + gnotPath = "gnot" + + MIN_TICK bigint = -887272 + MAX_TICK bigint = 887272 + + MIN_SQRT_RATIO bigint = 4295128739 // same as TickMathGetSqrtRatioAtTick(MIN_TICK) + MAX_SQRT_RATIO bigint = 1461446703485210103287273052203988822378723970342 // same as TickMathGetSqrtRatioAtTick(MAX_TICK) + + MAX_TIMEOUT bigint = 9999999999 +) + +// func init() { +// // debug addr +// println(gsa, "// gsa") +// println(pc01, "// pc01") +// println(ci01, "// ci01") +// println(lp01, "// lp01") +// println(lp02, "// lp02") +// println(tr01, "// tr01") +// println(poolAddr, "// pool") +// println(posAddr, "// pos") +// println(rouAddr, "// router") +// println(stakerAddr, "// staker") +// println(govAddr, "// gov") +// } + +func TestPoolInitManual(t *testing.T) { + std.TestSetOrigCaller(pc01) + + pl.InitManual() + std.TestSkipHeights(1) + + shouldPanicWithMsg(t, func() { pl.InitManual() }, "[POOl] pool_manager.gno__InitManual() || contract must not be initialized") +} + +func TestPoolCreatePool(t *testing.T) { + std.TestSetOrigCaller(pc01) + + gsaOldGnsBalance := gns.BalanceOf(a2u(gsa)) + + pl.CreatePool(gnotPath, barPath, uint16(100), 112040957517951813098925484553) // tick = 6_931, ratio = x1.99983634019692790850797337043331936001777648925781 + pl.CreatePool(barPath, bazPath, uint16(100), 112040957517951813098925484553) // tick = 6_931, ratio = x1.99983634019692790850797337043331936001777648925781 + pl.CreatePool(bazPath, fooPath, uint16(100), 177157928842132501967358423881) // tick = 16_095, ratio = x4.99990809049723150536692628520540893077850341796875 + pl.CreatePool(gnsPath, gnotPath, uint16(100), 79228162514264337593543950337) // tick = 0, ratio = x1.00000000000000000000000000000000000000000000000000 + pl.CreatePool(gnsPath, quxPath, uint16(100), 250541420775534450580036817218) // tick = 23_027, ratio = x9.99999779681069611569910193793475627899169921875000 + + gsaNewGnsBalance := gns.BalanceOf(a2u(gsa)) + + shouldEQ(t, len(pl.PoolGetPoolList()), 5) + shouldEQ(t, gsaNewGnsBalance-gsaOldGnsBalance, 2500) +} + +func TestPositionMintGnotBar(t *testing.T) { + std.TestSetOrigCaller(lp01) + + // prepare ugnot + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 1000) + + // simulate transfer & decrase + std.TestSetOrigSend(std.Coins{{"ugnot", 1000}}, nil) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 1000) + + tokenId, liquidity, amount0, amount1 := pos.Mint( + gnotPath, // token0 ( will be token1 ) + barPath, // token1 ( will be token0 ) + uint16(100), // fee + int32(5932), // tickLower + int32(7932), // tickUpper + bigint(1000), // amount0Desired + bigint(999999), // amount1Desired + 0, // amount0Min + 0, // amount1Min + MAX_TIMEOUT, // deadline + ) + std.TestSetOrigSend(std.Coins{}, nil) + std.TestSkipHeights(1) + + shouldEQ(t, tokenId, 1) + println("liquidity\t", liquidity) + println("amount0(bar)\t", amount0) + println("amount1(gnot)\t", amount1) +} + +func TestPositionMintBarBaz(t *testing.T) { + std.TestSetOrigCaller(lp01) + + tokenId, liquidity, amount0, amount1 := pos.Mint( + barPath, // token0 + bazPath, // token1 + uint16(100), // fee + int32(5932), // tickLower + int32(7932), // tickUpper + bigint(1000), // amount0Desired + bigint(999999), // amount1Desired + 0, // amount0Min + 0, // amount1Min + MAX_TIMEOUT, // deadline + ) + std.TestSkipHeights(1) + + shouldEQ(t, tokenId, 2) + println("liquidity\t", liquidity) + println("amount0(bar)\t", amount0) + println("amount1(baz)\t", amount1) +} + +func TestPositionMintBazFoo(t *testing.T) { + std.TestSetOrigCaller(lp01) + + tokenId, liquidity, amount0, amount1 := pos.Mint( + bazPath, // token0 + fooPath, // token1 + uint16(100), // fee + int32(15096), // tickLower + int32(17096), // tickUpper + bigint(1000), // amount0Desired + bigint(999999), // amount1Desired + 0, // amount0Min + 0, // amount1Min + MAX_TIMEOUT, // deadline + ) + std.TestSkipHeights(1) + + shouldEQ(t, tokenId, 3) + println("liquidity\t", liquidity) + println("amount0(baz)\t", amount0) + println("amount1(foo)\t", amount1) +} + +func TestPositionMintGnsGnot(t *testing.T) { + std.TestSetOrigCaller(lp01) + + // prepare ugnot + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 1000) + + // simulate transfer & decrase + std.TestSetOrigSend(std.Coins{{"ugnot", 1000}}, nil) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 1000) + + tokenId, liquidity, amount0, amount1 := pos.Mint( + gnsPath, // token0 + gnotPath, // token1 + uint16(100), // fee + int32(-1000), // tickLower + int32(1000), // tickUpper + bigint(1000), // amount0Desired + bigint(1000), // amount1Desired + 0, // amount0Min + 0, // amount1Min + MAX_TIMEOUT, // deadline + ) + std.TestSetOrigSend(std.Coins{}, nil) + std.TestSkipHeights(1) + + shouldEQ(t, tokenId, 4) + println("liquidity\t", liquidity) + println("amount0(gns)\t", amount0) + println("amount1(gnot)\t", amount1) +} + +func TestPositionMintGnsQux(t *testing.T) { + std.TestSetOrigCaller(lp01) + + tokenId, liquidity, amount0, amount1 := pos.Mint( + gnsPath, // token0 + quxPath, // token1 + uint16(100), // fee + int32(22028), // tickLower + int32(24028), // tickUpper + bigint(1000), // amount0Desired + bigint(999999), // amount1Desired + 0, // amount0Min + 0, // amount1Min + MAX_TIMEOUT, // deadline + ) + std.TestSkipHeights(1) + + shouldEQ(t, tokenId, 5) + println("liquidity\t", liquidity) + println("amount0(gns)\t", amount0) + println("amount1(qux)\t", amount1) +} + +// TEST RATIOS +func TestRouterApiGetRatiosFromBase(t *testing.T) { + jsonStr := gjson.Parse(rou.ApiGetRatiosFromBase()) + jsonArr := jsonStr.Get("response.data").Array() + + shouldEQ(t, jsonArr[0].String(), "{\"gnot\":79228162514264337593543950336}") // 1 + shouldEQ(t, jsonArr[1].String(), "{\"gno.land/r/bar\":158427515811472657639193234591}") // 1.9996363766 + shouldEQ(t, jsonArr[2].String(), "{\"gno.land/r/baz\":316829103406906364771139440526}") // 3.998945493 + shouldEQ(t, jsonArr[3].String(), "{\"gno.land/r/foo\":1584116397429175401616934621999}") // 19.994359924 + shouldEQ(t, jsonArr[4].String(), "{\"gno.land/r/gns\":79228162514264337593543950334}") // 1 + shouldEQ(t, jsonArr[5].String(), "{\"gno.land/r/qux\":792281450588003167884250659067}") // 9.9999977968 +} + +/* UTILS */ +func token0Balance(addr std.Address) bigint { + return bigint(foo.BalanceOf(a2u(addr))) +} + +func token1Balance(addr std.Address) bigint { + return bigint(bar.BalanceOf(a2u(addr))) +} + +func a2u(addr std.Address) users.AddressOrName { + return users.AddressOrName(addr) +} + +func tid(tokenId interface{}) grc721.TokenID { + if tokenId == nil { + panic("[TC - POSITION] test_helper.gno__tid() || tokenId is nil") + } + + switch tokenId.(type) { + case bigint: + return grc721.TokenID(string(tokenId.(bigint))) + case string: + return grc721.TokenID(tokenId.(string)) + case int: + return grc721.TokenID(strconv.Itoa(tokenId.(int))) + case uint64: + return grc721.TokenID(strconv.Itoa(int(tokenId.(uint64)))) + case grc721.TokenID: + return tokenId.(grc721.TokenID) + default: + panic("[TC - STAKER] utils.gno__tid() || unsupported tokenId type") + } +} + +func BalanceOf(token *grc20.AdminToken, addr std.Address) bigint { + balance, err := token.BalanceOf(addr) + if err != nil { + panic(err) + } + return bigint(balance) +} + +/* HELPERS */ +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, 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 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() +} + +func ugnotBalance(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + return uint64(testBanker.GetCoins(addr)[0].Amount) +} diff --git a/_test/_TEST_live_test.gnoa b/_test/_TEST_live_test.gnoa new file mode 100644 index 00000000..c1dcb68a --- /dev/null +++ b/_test/_TEST_live_test.gnoa @@ -0,0 +1,544 @@ +package tc + +import ( + "std" + "strconv" + "testing" + + "gno.land/p/demo/grc/grc20" + "gno.land/p/demo/grc/grc721" + "gno.land/p/demo/testutils" + + "gno.land/r/demo/users" + + // swap contract + gov "gno.land/r/gov" + pl "gno.land/r/pool" + pos "gno.land/r/position" + rou "gno.land/r/router" + stk "gno.land/r/staker" + + // token + _ "gno.land/r/grc20_wrapper" + + bar "gno.land/r/bar" + baz "gno.land/r/baz" + foo "gno.land/r/foo" + gns "gno.land/r/gns" + obl "gno.land/r/obl" // external reward + qux "gno.land/r/qux" + + wugnot "gno.land/r/wugnot" + + gnft "gno.land/r/gnft" // GRC721 LP Token +) + +const ( + TIMESTAMP_90DAYS int64 = 7776000 +) + +var ( + // test address + gsa = testutils.TestAddress("gsa") // Gnswap Admin + pc01 = testutils.TestAddress("pc01") // Pool Creator 01 + ci01 = testutils.TestAddress("ci01") // Create Incentive Caller 01 + lp01 = testutils.TestAddress("lp01") // Liquidity Provider 01 + lp02 = testutils.TestAddress("lp02") // Liquidity Provider 02 + tr01 = testutils.TestAddress("tr01") // Trader 01 + + // contract address + poolAddr = pl.GetOrigPkgAddr() // Pool Contract + posAddr = pos.GetOrigPkgAddr() // Position Contract + stakerAddr = stk.GetOrigPkgAddr() // Staker Contract + govAddr = gov.GetOrigPkgAddr() // Gov Contract + rouAddr = rou.GetOrigPkgAddr() // Router Contract + + // token path + fooPath = "gno.land/r/foo" + barPath = "gno.land/r/bar" + bazPath = "gno.land/r/baz" + quxPath = "gno.land/r/qux" + gnsPath = "gno.land/r/gns" + oblPath = "gno.land/r/obl" + + gnotPath = "gnot" + + MIN_TICK bigint = -887272 + MAX_TICK bigint = 887272 + + MIN_SQRT_RATIO bigint = 4295128739 // same as TickMathGetSqrtRatioAtTick(MIN_TICK) + MAX_SQRT_RATIO bigint = 1461446703485210103287273052203988822378723970342 // same as TickMathGetSqrtRatioAtTick(MAX_TICK) + + MAX_TIMEOUT bigint = 9999999999 +) + +// func init() { +// // debug addr +// println(gsa, "// gsa") +// println(pc01, "// pc01") +// println(ci01, "// ci01") +// println(lp01, "// lp01") +// println(lp02, "// lp02") +// println(tr01, "// tr01") +// println(poolAddr, "// pool") +// println(posAddr, "// pos") +// println(rouAddr, "// router") +// println(stakerAddr, "// staker") +// println(govAddr, "// gov") +// } + +// 1. [TC - POOL] Init +func TestPoolInitManual(t *testing.T) { + std.TestSetOrigCaller(pc01) + + pl.InitManual() + std.TestSkipHeights(1) + + shouldPanicWithMsg(t, func() { pl.InitManual() }, "[POOl] pool_manager.gno__InitManual() || contract must not be initialized") +} + +// 2. [TC - POOL] Create(Multi)Pool +func TestPoolCreatePool(t *testing.T) { + std.TestSetOrigCaller(pc01) + + gsaOldGnsBalance := gns.BalanceOf(a2u(gsa)) + + pl.CreatePool(gnotPath, barPath, uint16(100), 101729702841318637793976746270) // tick = 5_000, ratio = 1.648680055931176 + pl.CreatePool(barPath, bazPath, uint16(100), 101729702841318637793976746270) // tick = 5_000, ratio = 1.648680055931176 + pl.CreatePool(bazPath, quxPath, uint16(100), 101729702841318637793976746270) // tick = 5_000, ratio = 1.648680055931176 + + pl.CreatePool(gnotPath, barPath, uint16(500), 101729702841318637793976746270) // tick = 5_000, ratio = 1.648680055931176 + pl.CreatePool(barPath, bazPath, uint16(500), 101729702841318637793976746270) // tick = 5_000, ratio = 1.648680055931176 + pl.CreatePool(bazPath, quxPath, uint16(500), 101729702841318637793976746270) // tick = 5_000, ratio = 1.648680055931176 + std.TestSkipHeights(7) + + gsaNewGnsBalance := gns.BalanceOf(a2u(gsa)) + + shouldEQ(t, len(pl.PoolGetPoolList()), 6) + shouldEQ(t, gsaNewGnsBalance-gsaOldGnsBalance, 3000) +} + +// 3. [TC - POSITION] Mint LP +func TestPositionMint(t *testing.T) { + // gnot_bar_100 by lp01 + { + std.TestSetOrigCaller(lp01) + + // prepare ugnot + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 500_000_000) + + poolOldBarBalance := bar.BalanceOf(a2u(poolAddr)) + poolOldWugnotBalance := wugnot.BalanceOf(a2u(poolAddr)) + shouldEQ(t, poolOldBarBalance, 0) + shouldEQ(t, poolOldWugnotBalance, 0) + + lp01OldBarBalance := bar.BalanceOf(a2u(lp01)) + lp01OldWugnotBalance := wugnot.BalanceOf(a2u(lp01)) + + // simulate transfer & decrase + std.TestSetOrigSend(std.Coins{{"ugnot", 10000000}}, nil) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 10000000) + + // Mint + tokenId, liquidity, amount0, amount1 := pos.Mint(gnotPath, barPath, uint16(100), int32(4000), int32(6000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(1)), lp01) + shouldEQ(t, tokenId, 1) + shouldEQ(t, amount0 > 0, true) + shouldEQ(t, amount1 > 0, true) + + poolNewBarBalance := bar.BalanceOf(a2u(poolAddr)) + poolNewWugnotBalance := wugnot.BalanceOf(a2u(poolAddr)) + lp01NewBarBalance := bar.BalanceOf(a2u(lp01)) + lp01NewWugnotBalance := wugnot.BalanceOf(a2u(lp01)) + + shouldEQ(t, poolOldBarBalance+uint64(amount0), poolNewBarBalance) + shouldEQ(t, poolOldWugnotBalance+uint64(amount1), poolNewWugnotBalance) + shouldEQ(t, lp01OldBarBalance-uint64(amount0), lp01NewBarBalance) + } + + // bar_baz_100 by lp02 + { + std.TestSetOrigCaller(lp02) + + poolOldBarBalance := bar.BalanceOf(a2u(poolAddr)) + poolOldBazBalance := baz.BalanceOf(a2u(poolAddr)) + shouldEQ(t, poolOldBarBalance, 9999999) + shouldEQ(t, poolOldBazBalance, 0) + + lp02OldBarBalance := bar.BalanceOf(a2u(lp02)) + lp02OldBazBalance := baz.BalanceOf(a2u(lp02)) + + // Mint + tokenId, liquidity, amount0, amount1 := pos.Mint(barPath, bazPath, uint16(100), int32(4000), int32(6000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(2)), lp02) + shouldEQ(t, tokenId, 2) + shouldEQ(t, amount0 > 0, true) + shouldEQ(t, amount1 > 0, true) + + poolNewBarBalance := bar.BalanceOf(a2u(poolAddr)) + poolNewBazBalance := baz.BalanceOf(a2u(poolAddr)) + lp02NewBarBalance := bar.BalanceOf(a2u(lp02)) + lp02NewBazBalance := baz.BalanceOf(a2u(lp02)) + + shouldEQ(t, poolOldBarBalance+uint64(amount0), poolNewBarBalance) + shouldEQ(t, poolOldBazBalance+uint64(amount1), poolNewBazBalance) + shouldEQ(t, lp02OldBarBalance-uint64(amount0), lp02NewBarBalance) + shouldEQ(t, lp02OldBazBalance-uint64(amount1), lp02NewBazBalance) + } + + // baz_qux_100 by lp02 + { + std.TestSetOrigCaller(lp02) + + poolOldBazBalance := baz.BalanceOf(a2u(poolAddr)) + poolOldQuxBalance := qux.BalanceOf(a2u(poolAddr)) + shouldEQ(t, poolOldBazBalance, 9999999) + shouldEQ(t, poolOldQuxBalance, 0) + + lp02OldBazBalance := baz.BalanceOf(a2u(lp02)) + lp02OldQuxBalance := qux.BalanceOf(a2u(lp02)) + + // Mint + tokenId, liquidity, amount0, amount1 := pos.Mint(bazPath, quxPath, uint16(100), int32(4000), int32(6000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(tid(3)), lp02) + shouldEQ(t, tokenId, 3) + shouldEQ(t, amount0 > 0, true) + shouldEQ(t, amount1 > 0, true) + + poolNewBazBalance := baz.BalanceOf(a2u(poolAddr)) + poolNewQuxBalance := qux.BalanceOf(a2u(poolAddr)) + lp02NewBazBalance := baz.BalanceOf(a2u(lp02)) + lp02NewQuxBalance := qux.BalanceOf(a2u(lp02)) + + shouldEQ(t, poolOldBazBalance+uint64(amount0), poolNewBazBalance) + shouldEQ(t, poolOldQuxBalance+uint64(amount1), poolNewQuxBalance) + shouldEQ(t, lp02OldBazBalance-uint64(amount0), lp02NewBazBalance) + shouldEQ(t, lp02OldQuxBalance-uint64(amount1), lp02NewQuxBalance) + } + + { + // bar_gnot_500 by lp01 + // simulate transfer & decrase + std.TestSetOrigCaller(lp01) + std.TestSetOrigSend(std.Coins{{"ugnot", 10000000}}, nil) + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 10000000) + // pos.Mint(gnotPath, barPath, uint16(500), int32(4000), int32(6000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) + pos.Mint(barPath, gnotPath, uint16(500), int32(-6000), int32(-4000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) + + // bar_baz_500 by lp02 + std.TestSetOrigCaller(lp02) + pos.Mint(barPath, bazPath, uint16(500), int32(4000), int32(6000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) + + // baz_qux_500 by lp02 + std.TestSetOrigCaller(lp02) + pos.Mint(bazPath, quxPath, uint16(500), int32(4000), int32(6000), bigint(10000000), bigint(10000000), 0, 0, MAX_TIMEOUT) + + std.TestSkipHeights(3) + } +} + +// 4. [TC - STAKER] CreateExternalIncentive +// Internal incentive will automatcially created via init() +func TestStakerCreateExternalIncentive(t *testing.T) { + std.TestSetOrigCaller(ci01) + + stk.CreateExternalIncentive( + "gno.land/r/bar:gnot:100", // targetPoolPath + "gno.land/r/obl", // rewardToken + 10_000_000_000, // rewardAmount + GetTimestamp(), // startTimestamp + GetTimestamp()+TIMESTAMP_90DAYS, // endTimestamp + ) + std.TestSkipHeights(1) +} + +// 5. [TC - STAKER] StakeToken +func TestStakerStakeToken(t *testing.T) { + // lp01 stakes tokenId '1' + std.TestSetPrevAddr(lp01) // r3v4_xxx: only test code, we need to call gnft.Approve directly from user + gnft.Approve(a2u(stakerAddr), tid("1")) + + stk.StakeToken(1) + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(bigint(1)), stakerAddr) + + // lp02 stakes tokenId '2' + std.TestSetPrevAddr(lp02) // r3v4_xxx: only test code, we need to call gnft.Approve directly from user + gnft.Approve(a2u(stakerAddr), tid("2")) + + stk.StakeToken(2) + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(bigint(2)), stakerAddr) +} + +// 6. [TC - POOL] SetFeeProtocol +func TestPoolSetFeeProtocol(t *testing.T) { + std.TestSetOrigCaller(pc01) + + pl.SetFeeProtocol(6, 8) + std.TestSkipHeights(1) + + tmpPool := pl.GetPool(barPath, gnotPath, uint16(100)) + shouldEQ(t, tmpPool.PoolGetSlot0FeeProtocol(), bigint(134)) +} + +// 7. [TC - ROUTER] SwapRoute ExactIn Single ( bar -> baz ) +func TestRotuerSwapRouteExactInSinglePath(t *testing.T) { + std.TestSetPrevRealm("gno.land/r/router") + std.TestSetOrigCaller(tr01) + + tr01OldBarBalance := bar.BalanceOf(a2u(tr01)) + tr01OldBazBalance := baz.BalanceOf(a2u(tr01)) + poolOldBarBalance := bar.BalanceOf(a2u(poolAddr)) + poolOldBazBalance := baz.BalanceOf(a2u(poolAddr)) + + swapAmount := 123_456 + rou.SwapRoute( + barPath, + bazPath, + bigint(swapAmount), + "EXACT_IN", + "gno.land/r/bar:gno.land/r/baz:100", + "100", + 200_000, + ) + + tr01NewBarBalance := bar.BalanceOf(a2u(tr01)) + tr01NewBazBalance := baz.BalanceOf(a2u(tr01)) + poolNewBarBalance := bar.BalanceOf(a2u(poolAddr)) + poolNewBazBalance := baz.BalanceOf(a2u(poolAddr)) + + shouldEQ(t, tr01OldBarBalance-tr01NewBarBalance, bigint(123400)) + shouldEQ(t, tr01NewBazBalance > tr01OldBazBalance, true) + shouldEQ(t, poolNewBarBalance-poolOldBarBalance, bigint(123400)) + shouldEQ(t, poolNewBazBalance < poolOldBazBalance, true) +} + +// 8. [TC - ROUTER] SwapRoute ExactOut Multi ( wugnot -> qux ) +func TestRotuerSwapRouteExactOutputMultiPath(t *testing.T) { + std.TestSetPrevRealm("gno.land/r/router") + std.TestSetOrigCaller(tr01) + + // prepare ugnot + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + testBanker.IssueCoin(std.GetOrigCaller(), "ugnot", 500_000_000) + + tr01OldWugnotBalance := wugnot.BalanceOf(a2u(tr01)) + tr01OldQuxBalance := qux.BalanceOf(a2u(tr01)) + poolOldWugnotBalance := wugnot.BalanceOf(a2u(poolAddr)) + poolOldQuxBalance := qux.BalanceOf(a2u(poolAddr)) + + std.TestSetOrigSend(std.Coins{{"ugnot", 1_000_000}}, nil) + testBanker.RemoveCoin(std.GetOrigCaller(), "ugnot", 1_000_000) + + swapAmount := 987_654 + rou.SwapRoute( + gnotPath, + quxPath, + bigint(swapAmount), + "EXACT_OUT", + "gnot:gno.land/r/bar:100*POOL*gno.land/r/bar:gno.land/r/baz:100*POOL*gno.land/r/baz:gno.land/r/qux:100,gnot:gno.land/r/bar:500*POOL*gno.land/r/bar:gno.land/r/baz:500*POOL*gno.land/r/baz:gno.land/r/qux:500", + "40,60", + 123456789, + ) + + tr01NewWugnotBalance := wugnot.BalanceOf(a2u(tr01)) + tr01NewQuxBalance := qux.BalanceOf(a2u(tr01)) + poolNewWugnotBalance := wugnot.BalanceOf(a2u(poolAddr)) + poolNewQuxBalance := qux.BalanceOf(a2u(poolAddr)) + + shouldEQ(t, poolOldQuxBalance-poolNewQuxBalance < uint64(swapAmount), true) + shouldEQ(t, poolNewWugnotBalance > poolOldWugnotBalance, true) +} + +// 9. [TC - POSITION] Collect +func TestPositionCollect01(t *testing.T) { + { + // lp01 collects fee from tokenId '1' + std.TestSetPrevRealm("gno.land/r/position") + std.TestSetOrigCaller(lp01) + cAmount0, cAmount1 := pos.Collect( + 1, // tokenId + lp01, // recipient + 100000, // amount0Max + 100000, // amount1Max + ) + std.TestSkipHeights(1) + + shouldEQ(t, cAmount0, bigint(0)) // bar + shouldEQ(t, cAmount1, bigint(6)) // ugnot + } + + { + // lp02 collects fee from tokenId '2' + std.TestSetOrigCaller(lp02) + + lp02OldBarBalance := bar.BalanceOf(a2u(lp02)) + lp02OldBazBalance := baz.BalanceOf(a2u(lp02)) + + cAmount0, cAmount1 := pos.Collect( + 2, // tokenId + lp02, // recipient + 100000, // amount0Max + 100000, // amount1Max + ) + std.TestSkipHeights(1) + + lp02NewBarBalance := bar.BalanceOf(a2u(lp02)) + lp02NewBazBalance := baz.BalanceOf(a2u(lp02)) + + shouldEQ(t, cAmount0 > 0, true) + shouldEQ(t, cAmount1 == 0, true) + + shouldEQ(t, lp02NewBarBalance-uint64(cAmount0), lp02OldBarBalance) + shouldEQ(t, lp02OldBazBalance, lp02OldBazBalance) + } +} + +// 10. [TC - STAKER] UnstakeToken +func TestStakerUnstakeToken(t *testing.T) { + { + // lp01 unstakes tokenId '1' + std.TestSetPrevAddr(lp01) // r3v4_xxx: only test code, we need to call stk.UnstakeToken directly from user + + // skip 15 days + std.TestSkipHeights(259200) + stk.UnstakeToken(1) + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(bigint(1)), lp01) + shouldEQ(t, gns.BalanceOf(a2u(lp01)), bigint(3850)) + shouldEQ(t, obl.BalanceOf(a2u(lp01)), bigint(4951)) + } + + { + // lp02 unstakes tokenId '2' + std.TestSetPrevAddr(lp02) // r3v4_xxx: only test code, we need to stk gnft.UnstakeToken directly from user + + // skip 20 days more + std.TestSkipHeights(345600) + stk.UnstakeToken(2) + std.TestSkipHeights(1) + + shouldEQ(t, gnft.OwnerOf(bigint(2)), lp02) + shouldEQ(t, gns.BalanceOf(a2u(lp02)), bigint(0)) // pool for tokenId '2' isn't reward target + shouldEQ(t, obl.BalanceOf(a2u(lp02)), bigint(0)) // pool for tokenId '2' isn't reward target + } +} + +// 11. [TC - STAKER] EndExternalIncentive +func TestStakerEndExternalIncentive(t *testing.T) { + std.TestSetOrigCaller(ci01) + std.TestSkipHeights(1036800) + + ci01OldRewardBal := obl.BalanceOf(a2u(ci01)) + + stk.EndExternalIncentive(ci01.String(), "gno.land/r/bar:gnot:100", "gno.land/r/obl") // use same parameter as CreateExternalIncentive + std.TestSkipHeights(1) + + ci01NewRewardBal := obl.BalanceOf(a2u(ci01)) + shouldGT(t, ci01NewRewardBal, ci01OldRewardBal) + + // shouldPanicWithMsg( + // t, + // func() { + // stk.EndExternalIncentive(ci01.String(), "gno.land/r/bar:gnot:100", "gno.land/r/obl") + // }, + // "[STAKER] staker.gno__EndExternalIncentive() || cannot end non existent incentive(ZzF2ZDVucXYybHRhMDQ3aDZsdGEwNDdoNmx0YTA0N2g2bGswd2hjZDpnbm8ubGFuZC9yL2Jhcjp1Z25vdDoxMDA6Z25vLmxhbmQvci9vYmw=)", + // ) +} + +/* UTILS */ +func token0Balance(addr std.Address) bigint { + return bigint(foo.BalanceOf(a2u(addr))) +} + +func token1Balance(addr std.Address) bigint { + return bigint(bar.BalanceOf(a2u(addr))) +} + +func a2u(addr std.Address) users.AddressOrName { + return users.AddressOrName(addr) +} + +func tid(tokenId interface{}) grc721.TokenID { + if tokenId == nil { + panic("[TC - POSITION] test_helper.gno__tid() || tokenId is nil") + } + + switch tokenId.(type) { + case bigint: + return grc721.TokenID(string(tokenId.(bigint))) + case string: + return grc721.TokenID(tokenId.(string)) + case int: + return grc721.TokenID(strconv.Itoa(tokenId.(int))) + case uint64: + return grc721.TokenID(strconv.Itoa(int(tokenId.(uint64)))) + case grc721.TokenID: + return tokenId.(grc721.TokenID) + default: + panic("[TC - STAKER] utils.gno__tid() || unsupported tokenId type") + } +} + +func BalanceOf(token *grc20.AdminToken, addr std.Address) bigint { + balance, err := token.BalanceOf(addr) + if err != nil { + panic(err) + } + return bigint(balance) +} + +/* HELPERS */ +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, 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 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() +} + +func ugnotBalance(addr std.Address) uint64 { + testBanker := std.GetBanker(std.BankerTypeRealmIssue) + return uint64(testBanker.GetCoins(addr)[0].Amount) +}