From 68167bb526dea7cf0ef2058d532e8339c00024c3 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 9 Apr 2024 22:32:46 +0900 Subject: [PATCH 1/6] GSW-1012 feat: startTime of external incentive must be at least tomorrow midnight(and midnight of the day) --- staker/staker.gno | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/staker/staker.gno b/staker/staker.gno index 24b179f1..4560a870 100644 --- a/staker/staker.gno +++ b/staker/staker.gno @@ -29,7 +29,7 @@ var ( const ( TIMESTAMP_90DAYS = 7776000 TIMESTAMP_180DAYS = 15552000 - TIMESTAMP_360DAYS = 31104000 + TIMESTAMP_365DAYS = 31536000 MUST_EXISTS_IN_TIER_1 = "gno.land/r/demo/gns:gno.land/r/demo/wugnot:3000" ) @@ -44,7 +44,7 @@ func init() { // // Panics if any of the following conditions are met: // - startTimestamp is not in the future -// - externalDuration is not 90, 180, or 360 days +// - externalDuration is not 90, 180, or 365 days // - user doesn't have enough balance to provide as reward func CreateExternalIncentive( targetPoolPath string, @@ -59,13 +59,13 @@ func CreateExternalIncentive( rewardAmount := u256.MustFromDecimal(_rewardAmount) - if startTimestamp < time.Now().Unix() { - panic(ufmt.Sprintf("[STAKER] staker.gno__CreateExternalIncentive() || startTimestamp(%d) must be in the future [now:%d]", startTimestamp, time.Now().Unix())) - } + // must be at least +1 day midnight + // must be midnight of the day + checkStartTime(startTimestamp) externalDuration := uint64(endTimestamp - startTimestamp) - if !(externalDuration == TIMESTAMP_90DAYS || externalDuration == TIMESTAMP_180DAYS || externalDuration == TIMESTAMP_360DAYS) { - panic(ufmt.Sprintf("[STAKER] staker.gno__CreateExternalIncentive() || externalDuration(%d) must be 90, 180, 360 days)", externalDuration)) + if !(externalDuration == TIMESTAMP_90DAYS || externalDuration == TIMESTAMP_180DAYS || externalDuration == TIMESTAMP_365DAYS) { + panic(ufmt.Sprintf("[STAKER] staker.gno__CreateExternalIncentive() || externalDuration(%d) must be 90, 180, 365 days)", externalDuration)) } fromBalanceBefore := balanceOfByRegisterCall(rewardToken, std.GetOrigCaller()) @@ -283,6 +283,23 @@ func EndExternalIncentive(_refundee, targetPoolPath, rewardToken string) { } } +func checkStartTime(startTimestamp int64) { + // must be at least +1 day midnight + tomorrowMidnight := time.Now().AddDate(0, 0, 1).Truncate(24 * time.Hour).Unix() + if startTimestamp < tomorrowMidnight { + panic(ufmt.Sprintf("[STAKER] staker.gno__checkStarTime() || startTimestamp(%d) must be at least +1 day midnight(%d)", startTimestamp, tomorrowMidnight)) + } + + // must be midnight of the day + startTime := time.Unix(startTimestamp, 0) + hour, minute, second := startTime.Hour(), startTime.Minute(), startTime.Second() + + isMidnight := hour == 0 && minute == 0 && second == 0 + if !isMidnight { + panic(ufmt.Sprintf("[STAKER] staker.gno__checkStarTime() || startTimestamp(%s) must be midnight of the day", startTime.String())) + } +} + func transferDeposit(tokenId uint64, to std.Address) { owner := gnft.OwnerOf(tid(tokenId)) if std.GetOrigCaller() == to { From 93956b8101971e4a507b47f0162ffd0d951b7263 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 9 Apr 2024 22:42:50 +0900 Subject: [PATCH 2/6] fix: typo --- pool/_TEST_/z1_single_lp_filetest.gno | 1 + staker/staker.gno | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pool/_TEST_/z1_single_lp_filetest.gno b/pool/_TEST_/z1_single_lp_filetest.gno index 7fee5f7e..4abaeb10 100644 --- a/pool/_TEST_/z1_single_lp_filetest.gno +++ b/pool/_TEST_/z1_single_lp_filetest.gno @@ -221,6 +221,7 @@ func createBarFooPool() { std.TestSkipHeights(1) } func mint() { + panic("TEST") std.TestSetPrevAddr(test1) bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) diff --git a/staker/staker.gno b/staker/staker.gno index 4560a870..4e9172a5 100644 --- a/staker/staker.gno +++ b/staker/staker.gno @@ -287,7 +287,7 @@ func checkStartTime(startTimestamp int64) { // must be at least +1 day midnight tomorrowMidnight := time.Now().AddDate(0, 0, 1).Truncate(24 * time.Hour).Unix() if startTimestamp < tomorrowMidnight { - panic(ufmt.Sprintf("[STAKER] staker.gno__checkStarTime() || startTimestamp(%d) must be at least +1 day midnight(%d)", startTimestamp, tomorrowMidnight)) + panic(ufmt.Sprintf("[STAKER] staker.gno__checkStartTime() || startTimestamp(%d) must be at least +1 day midnight(%d)", startTimestamp, tomorrowMidnight)) } // must be midnight of the day @@ -296,7 +296,7 @@ func checkStartTime(startTimestamp int64) { isMidnight := hour == 0 && minute == 0 && second == 0 if !isMidnight { - panic(ufmt.Sprintf("[STAKER] staker.gno__checkStarTime() || startTimestamp(%s) must be midnight of the day", startTime.String())) + panic(ufmt.Sprintf("[STAKER] staker.gno__checkStartTime() || startTime(%s) must be midnight of the day", startTime.String())) } } From 0805dd3fd1001ef6abe8e43b8352802eed3fb5c2 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 9 Apr 2024 23:54:35 +0900 Subject: [PATCH 3/6] test: change external incentive start time - due to this, result of reward amount has been changed --- .../_TEST_staker_collect_reward_test.gn | 17 +++++++------- staker/_TEST_/_TEST_staker_get_test.gn | 7 +++--- .../_TEST_staker_internal_external_test.gn | 11 ++++----- .../_TEST_staker_one_external_native_test.gn | 15 ++++++------ .../_TEST_/_TEST_staker_one_external_test.gn | 19 ++++++++------- ..._TEST_staker_one_increase_external_test.gn | 23 +++++++++---------- 6 files changed, 44 insertions(+), 48 deletions(-) diff --git a/staker/_TEST_/_TEST_staker_collect_reward_test.gn b/staker/_TEST_/_TEST_staker_collect_reward_test.gn index 71f01060..e7be8c6b 100644 --- a/staker/_TEST_/_TEST_staker_collect_reward_test.gn +++ b/staker/_TEST_/_TEST_staker_collect_reward_test.gn @@ -3,7 +3,6 @@ package staker import ( "std" "testing" - "time" "gno.land/r/gnoswap/consts" @@ -118,15 +117,15 @@ func TestCreateExternalIncentive(t *testing.T) { "gno.land/r/demo/bar:gno.land/r/demo/qux:500", // targetPoolPath "gno.land/r/demo/obl", // rewardToken "1000000000", // rewardAmount 10_000_000_000 - time.Now().Unix(), // startTimestamp - time.Now().Unix()+TIMESTAMP_90DAYS, // endTimestamp + 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", time.Now().Unix(), time.Now().Unix()+TIMESTAMP_90DAYS) + 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) } @@ -170,14 +169,16 @@ func TestCollectReward(t *testing.T) { func TestUnstakeToken01(t *testing.T) { std.TestSetPrevAddr(test1) + std.TestSkipHeights(400) // spend more time + UnstakeToken(1) // GNFT tokenId std.TestSkipHeights(1) shouldEQ(t, gnft.OwnerOf(tid(1)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 36754) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 8000000378) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 2487004) // internal + shouldEQ(t, obl.BalanceOf(a2u(test1)), 8000004851) // external } func TestUnstakeToken02(t *testing.T) { @@ -188,8 +189,8 @@ func TestUnstakeToken02(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(2)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 281779) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 8000002898) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 19067029) // internal + shouldEQ(t, obl.BalanceOf(a2u(test1)), 8000037608) // external } func TestEndExternalIncentive(t *testing.T) { diff --git a/staker/_TEST_/_TEST_staker_get_test.gn b/staker/_TEST_/_TEST_staker_get_test.gn index d59630fb..4c8de4c3 100644 --- a/staker/_TEST_/_TEST_staker_get_test.gn +++ b/staker/_TEST_/_TEST_staker_get_test.gn @@ -3,7 +3,6 @@ package staker import ( "std" "testing" - "time" "gno.land/r/gnoswap/consts" @@ -104,15 +103,15 @@ func TestCreateExternalIncentive(t *testing.T) { "gno.land/r/demo/bar:gno.land/r/demo/qux:500", // targetPoolPath "gno.land/r/demo/obl", // rewardToken "1000000000", // rewardAmount - time.Now().Unix(), // startTimestamp - time.Now().Unix()+TIMESTAMP_90DAYS, // endTimestamp + 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", time.Now().Unix(), time.Now().Unix()+TIMESTAMP_90DAYS) + 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) } diff --git a/staker/_TEST_/_TEST_staker_internal_external_test.gn b/staker/_TEST_/_TEST_staker_internal_external_test.gn index 8156117b..0ea2650f 100644 --- a/staker/_TEST_/_TEST_staker_internal_external_test.gn +++ b/staker/_TEST_/_TEST_staker_internal_external_test.gn @@ -3,7 +3,6 @@ package staker import ( "std" "testing" - "time" "gno.land/r/gnoswap/consts" @@ -139,8 +138,8 @@ func TestCreateExternalIncentive(t *testing.T) { "gno.land/r/demo/foo:gno.land/r/demo/qux:500", // targetPoolPath "gno.land/r/demo/obl", // rewardToken "100000000", // rewardAmount - time.Now().Unix(), // startTimestamp - time.Now().Unix()+TIMESTAMP_90DAYS, // endTimestamp + 1234569600, // startTimestamp + 1234569600+TIMESTAMP_90DAYS, // endTimestamp ) std.TestSkipHeights(1) } @@ -234,7 +233,7 @@ func TestCollectExternalReward(t *testing.T) { std.TestSkipHeights(1) oblNew := obl.BalanceOf(a2u(test1)) - shouldEQ(t, oblNew, 9900020915) + shouldEQ(t, oblNew, 9900014200) } func TestCollectAll(t *testing.T) { @@ -252,7 +251,7 @@ func TestUnstakeToken1(t *testing.T) { gnsOld := gns.BalanceOf(a2u(test1)) oblOld := obl.BalanceOf(a2u(test1)) shouldEQ(t, gnsOld, 66009736) - shouldEQ(t, oblOld, 9900020936) + shouldEQ(t, oblOld, 9900014221) std.TestSkipHeights(100000) UnstakeToken(1) @@ -265,7 +264,7 @@ func TestUnstakeToken1(t *testing.T) { UnstakeToken(3) oblNew := obl.BalanceOf(a2u(test1)) - shouldEQ(t, oblNew, 9903499997) + shouldEQ(t, oblNew, 9903488806) std.TestSkipHeights(1) } diff --git a/staker/_TEST_/_TEST_staker_one_external_native_test.gn b/staker/_TEST_/_TEST_staker_one_external_native_test.gn index 635c1e70..777fb5a7 100644 --- a/staker/_TEST_/_TEST_staker_one_external_native_test.gn +++ b/staker/_TEST_/_TEST_staker_one_external_native_test.gn @@ -3,7 +3,6 @@ package staker import ( "std" "testing" - "time" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" @@ -129,8 +128,8 @@ func TestCreateExternalIncentive(t *testing.T) { "gno.land/r/demo/bar:gno.land/r/demo/foo:500", // targetPoolPath consts.WRAPPED_WUGNOT, // rewardToken "10000000000", // rewardAmount - time.Now().Unix(), // startTimestamp - time.Now().Unix()+TIMESTAMP_90DAYS, // endTimestamp + 1234569600, // startTimestamp + 1234569600+TIMESTAMP_90DAYS, // endTimestamp ) std.TestSkipHeights(1) } @@ -156,7 +155,7 @@ func TestStakeToken02(t *testing.T) { 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(1) + std.TestSkipHeights(335) // skip times std.TestSetPrevAddr(test1) UnstakeToken(1) // GNFT tokenId @@ -165,8 +164,8 @@ func TestUnstakeToken01(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(1)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 18377) // internal - shouldEQ(t, wugnot.BalanceOf(a2u(test1)), 945) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 2064336) // internal + shouldEQ(t, wugnot.BalanceOf(a2u(test1)), 2521) // external } func TestUnstakeToken02(t *testing.T) { @@ -177,8 +176,8 @@ func TestUnstakeToken02(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(2)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 140890) // internal - shouldEQ(t, wugnot.BalanceOf(a2u(test1)), 7247) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 15826574) // internal + shouldEQ(t, wugnot.BalanceOf(a2u(test1)), 21428) // external } func TestEndExternalIncentive(t *testing.T) { diff --git a/staker/_TEST_/_TEST_staker_one_external_test.gn b/staker/_TEST_/_TEST_staker_one_external_test.gn index 6e1563db..6b75f9b4 100644 --- a/staker/_TEST_/_TEST_staker_one_external_test.gn +++ b/staker/_TEST_/_TEST_staker_one_external_test.gn @@ -3,7 +3,6 @@ package staker import ( "std" "testing" - "time" // @notJoon JSON @@ -118,10 +117,10 @@ func TestCreateExternalIncentive(t *testing.T) { CreateExternalIncentive( "gno.land/r/demo/bar:gno.land/r/demo/foo:500", // targetPoolPath - oblPath, // rewardToken - "10000000000", // rewardAmount - time.Now().Unix(), // startTimestamp - time.Now().Unix()+TIMESTAMP_90DAYS, // endTimestamp + oblPath, // rewardToken + "10000000000", // rewardAmount + 1234569600, // startTimestamp + 1234569600+TIMESTAMP_90DAYS, // endTimestamp ) std.TestSkipHeights(1) } @@ -159,7 +158,7 @@ func TestStakeToken02(t *testing.T) { 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(1) + std.TestSkipHeights(330) std.TestSetPrevAddr(test1) UnstakeToken(1) // GNFT tokenId @@ -168,8 +167,8 @@ func TestUnstakeToken01(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(1)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 18377) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 945) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 2033707) // internal + shouldEQ(t, obl.BalanceOf(a2u(test1)), 630) // external } func TestUnstakeToken02(t *testing.T) { @@ -180,8 +179,8 @@ func TestUnstakeToken02(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(2)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 140890) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 7247) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 15591757) // internal + shouldEQ(t, obl.BalanceOf(a2u(test1)), 6932) // external } func TestEndExternalIncentive(t *testing.T) { diff --git a/staker/_TEST_/_TEST_staker_one_increase_external_test.gn b/staker/_TEST_/_TEST_staker_one_increase_external_test.gn index 71559547..ca219c8f 100644 --- a/staker/_TEST_/_TEST_staker_one_increase_external_test.gn +++ b/staker/_TEST_/_TEST_staker_one_increase_external_test.gn @@ -3,7 +3,6 @@ package staker import ( "std" "testing" - "time" "gno.land/p/demo/json" @@ -117,16 +116,16 @@ func TestCreateExternalIncentive(t *testing.T) { CreateExternalIncentive( "gno.land/r/demo/bar:gno.land/r/demo/foo:500", // targetPoolPath - oblPath, // rewardToken - "1000000000", // rewardAmount - time.Now().Unix(), // startTimestamp - time.Now().Unix()+TIMESTAMP_90DAYS, // endTimestamp + oblPath, // rewardToken + "1000000000", // rewardAmount + 1234569600, // startTimestamp + 1234569600+TIMESTAMP_90DAYS, // endTimestamp ) std.TestSkipHeights(1) obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) std.TestSkipHeights(1) - CreateExternalIncentive("gno.land/r/demo/bar:gno.land/r/demo/foo:500", oblPath, "100000000", time.Now().Unix(), time.Now().Unix()+TIMESTAMP_90DAYS) + CreateExternalIncentive("gno.land/r/demo/bar:gno.land/r/demo/foo:500", oblPath, "100000000", 1234569600, 1234569600+TIMESTAMP_90DAYS) std.TestSkipHeights(1) } @@ -183,7 +182,7 @@ func TestApiGetRewardTokens(t *testing.T) { 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(1) + std.TestSkipHeights(350) std.TestSetPrevAddr(test1) UnstakeToken(1) // GNFT tokenId @@ -192,8 +191,8 @@ func TestUnstakeToken01(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(1)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 18377) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 8900000103) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 2156220) // internal + shouldEQ(t, obl.BalanceOf(a2u(test1)), 8900000831) // external } func TestUnstakeToken02(t *testing.T) { @@ -204,8 +203,8 @@ func TestUnstakeToken02(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(2)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 140890) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 8900000796) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 16531020) // internal + shouldEQ(t, obl.BalanceOf(a2u(test1)), 8900006600) // external } func TestEndExternalIncentive(t *testing.T) { @@ -216,7 +215,7 @@ func TestEndExternalIncentive(t *testing.T) { // incentive time isn't over yet shouldPanicWithMsg(t, func() { EndExternalIncentive(test1.String(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", "gno.land/r/demo/obl") - }, "[STAKER] staker.gno__EndExternalIncentive() || cannot end incentive before endTimestamp(1242343945), current(1234567985)") + }, "[STAKER] staker.gno__EndExternalIncentive() || cannot end incentive before endTimestamp(1242345600), current(1234569730)") std.TestSkipHeights(9999999) EndExternalIncentive(test1.String(), "gno.land/r/demo/bar:gno.land/r/demo/foo:500", "gno.land/r/demo/obl") // use same parameter as CreateExternalIncentive() From 8bba9a317f4385448658055a393712b02de05c26 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 9 Apr 2024 23:57:49 +0900 Subject: [PATCH 4/6] test: update to current contract --- pool/_TEST_/z1_single_lp_filetest.gno | 2 - .../_TEST_position_increase_decrease_test.gn | 2 +- router/_TEST_/_TEST_router_ratio_test.gn | 6 +-- ...TEST_router_swap_route_1route_1hop_test.gn | 41 ++++++++++--------- ..._1route_3hop_wrapped_native_middle_test.gn | 5 ++- ...TEST_router_swap_route_2route_2hop_test.gn | 20 +++++---- 6 files changed, 41 insertions(+), 35 deletions(-) diff --git a/pool/_TEST_/z1_single_lp_filetest.gno b/pool/_TEST_/z1_single_lp_filetest.gno index 4abaeb10..5d7e0530 100644 --- a/pool/_TEST_/z1_single_lp_filetest.gno +++ b/pool/_TEST_/z1_single_lp_filetest.gno @@ -186,7 +186,6 @@ func init() { pl.RegisterGRC20Interface("gno.land/r/demo/obl", OBLToken{}) pl.RegisterGRC20Interface("gno.land/r/demo/wugnot", WugnotToken{}) pl.RegisterGRC20Interface("gno.land/r/demo/gns", GNSToken{}) - } /* START TEST */ @@ -221,7 +220,6 @@ func createBarFooPool() { std.TestSkipHeights(1) } func mint() { - panic("TEST") std.TestSetPrevAddr(test1) bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) diff --git a/position/_TEST_/_TEST_position_increase_decrease_test.gn b/position/_TEST_/_TEST_position_increase_decrease_test.gn index b65302b6..f3ee37db 100644 --- a/position/_TEST_/_TEST_position_increase_decrease_test.gn +++ b/position/_TEST_/_TEST_position_increase_decrease_test.gn @@ -59,7 +59,7 @@ func TestIncreaseLiquidity(t *testing.T) { pool := getPoolFromLpTokenId(uint64(1)) oldLiquidity := pool.PoolGetLiquidity() - _, _, m0, m1 := IncreaseLiquidity( + _, _, m0, m1, _ := IncreaseLiquidity( uint64(1), // tokenId "10000000", // amount0Desired "10000000", // amount1Desired diff --git a/router/_TEST_/_TEST_router_ratio_test.gn b/router/_TEST_/_TEST_router_ratio_test.gn index 6c3005f1..337e4f2e 100644 --- a/router/_TEST_/_TEST_router_ratio_test.gn +++ b/router/_TEST_/_TEST_router_ratio_test.gn @@ -116,8 +116,8 @@ func TestApiGetRatiosFromBase(t *testing.T) { shouldEQ(t, token0.String(), "{\"token\":\"gno.land/r/demo/wugnot\",\"ratio\":\"79228162514264337593543950336\"}") shouldEQ(t, token1.String(), "{\"token\":\"gno.land/r/demo/bar\",\"ratio\":\"19935653721785907863909200771\"}") shouldEQ(t, token2.String(), "{\"token\":\"gno.land/r/demo/qux\",\"ratio\":\"79228162514264337593543950338\"}") - shouldEQ(t, token3.String(), "{\"token\":\"gno.land/r/demo/foo\",\"ratio\":\"21827358772679825007021358318119\"}") + shouldEQ(t, token3.String(), "{\"token\":\"gno.land/r/demo/foo\",\"ratio\":\"10796452559328726350\"}") shouldEQ(t, token4.String(), "{\"token\":\"gno.land/r/demo/baz\",\"ratio\":\"158459202898910110285447649633\"}") - shouldEQ(t, token5.String(), "{\"token\":\"gno.land/r/demo/obl\",\"ratio\":\"316918405797820220570895299266\"}") - shouldEQ(t, token6.String(), "{\"token\":\"gno.land/r/demo/gns\",\"ratio\":\"633836811595640441141790598532\"}") + shouldEQ(t, token5.String(), "{\"token\":\"gno.land/r/demo/obl\",\"ratio\":\"6102283613564175414\"}") + shouldEQ(t, token6.String(), "{\"token\":\"gno.land/r/demo/gns\",\"ratio\":\"12204788885446722546\"}") } diff --git a/router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gn b/router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gn index 9a4e8816..8c2ea393 100644 --- a/router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gn +++ b/router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gn @@ -1,7 +1,6 @@ package router import ( - // @notJoon JSON "std" "testing" @@ -64,7 +63,7 @@ func TestSwapRouteBarBazExactIn(t *testing.T) { bar.Approve(a2u(consts.POOL_ADDR), uint64(1000)) baz.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee - swapResult := SwapRoute( + amountIn, amountOut := SwapRoute( barPath, // inputToken bazPath, // outputToken "1000", // amountSpecified @@ -73,7 +72,8 @@ func TestSwapRouteBarBazExactIn(t *testing.T) { "100", // quoteArr "2700", // tokenAmountLimit ) - shouldEQ(t, swapResult, "2707") + shouldEQ(t, amountIn, "1000") + shouldEQ(t, amountOut, "-2707") } func TestDrySwapRouteBarBazExactOut(t *testing.T) { @@ -97,7 +97,7 @@ func TestSwapRouteBarBazExactOut(t *testing.T) { bar.Approve(a2u(consts.POOL_ADDR), uint64(1000)) baz.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee - swapResult := SwapRoute( + amountIn, amountOut := SwapRoute( barPath, // inputToken bazPath, // outputToken "1000", // amountSpecified @@ -107,7 +107,8 @@ func TestSwapRouteBarBazExactOut(t *testing.T) { "370", // tokenAmountLimit ) - shouldEQ(t, swapResult, "370") + shouldEQ(t, amountIn, "370") + shouldEQ(t, amountOut, "-999") } func TestDrySwapRouteBazBarExactIn(t *testing.T) { @@ -131,7 +132,7 @@ func TestSwapRouteBazBarExactIn(t *testing.T) { baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX) bar.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) // ITS FOR 0.15% fee - swapResult := SwapRoute( + amountIn, amountOut := SwapRoute( bazPath, // inputToken barPath, // outputToken "1000", // amountSpecified @@ -141,37 +142,39 @@ func TestSwapRouteBazBarExactIn(t *testing.T) { "360", // tokenAmountLimit ) - shouldEQ(t, swapResult, "368") + shouldEQ(t, amountIn, "1000") + shouldEQ(t, amountOut, "-368") } func TestDrySwapRouteBazBarExactOut(t *testing.T) { std.TestSetPrevAddr(test1) dryResult := DrySwapRoute( - bazPath, // inputToken - barPath, // outputToken - "3000", // amountSpecified - "EXACT_IN", // swapType + 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, "1104") + shouldEQ(t, dryResult, "8169") } func TestSwapRouteBazBarExactOut(t *testing.T) { std.TestSetPrevAddr(test1) bar.Approve(a2u(consts.ROUTER_ADDR), consts.UINT64_MAX) - swapResult := SwapRoute( - bazPath, // inputToken - barPath, // outputToken - "3000", // amountSpecified - "EXACT_IN", // swapType + 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 - "1100", // tokenAmountLimit + "8200", // tokenAmountLimit ) - shouldEQ(t, swapResult, "1103") + shouldEQ(t, amountIn, "8169") + shouldEQ(t, amountOut, "-2996") } diff --git a/router/_TEST_/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gn b/router/_TEST_/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gn index 3cfc89f1..7c699fe5 100644 --- a/router/_TEST_/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gn +++ b/router/_TEST_/_TEST_router_swap_route_1route_3hop_wrapped_native_middle_test.gn @@ -98,7 +98,7 @@ func TestSwapRouteGnsBarExactIn(t *testing.T) { gns.Approve(a2u(consts.POOL_ADDR), 1000) // swap input amount bar.Approve(a2u(consts.ROUTER_ADDR), 7328) // 0.15% fee - swapResult := SwapRoute( + amountIn, amountOut := SwapRoute( consts.GNS_PATH, // inputToken barPath, // outputToken "1000", // amountSpecified @@ -107,5 +107,6 @@ func TestSwapRouteGnsBarExactIn(t *testing.T) { "100", // quoteArr "0", // tokenAmountLimit ) - shouldEQ(t, swapResult, "7328") + shouldEQ(t, amountIn, "1000") + shouldEQ(t, amountOut, "-7328") } diff --git a/router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gn b/router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gn index 49299bed..b5a1c044 100644 --- a/router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gn +++ b/router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gn @@ -65,7 +65,7 @@ func TestSwapRouteBarQuxExactIn(t *testing.T) { bar.Approve(a2u(consts.POOL_ADDR), 10000) qux.Approve(a2u(consts.ROUTER_ADDR), 10000) - swapResult := SwapRoute( + amountIn, amountOut := SwapRoute( barPath, // inputToken quxPath, // outputToken "1000", // amountSpecified @@ -75,7 +75,8 @@ func TestSwapRouteBarQuxExactIn(t *testing.T) { "1", // tokenAmountLimit ) - shouldEQ(t, swapResult, "7318") + shouldEQ(t, amountIn, "1000") + shouldEQ(t, amountOut, "-7318") } func TestDrySwapRouteBarQuxExactOut(t *testing.T) { @@ -96,7 +97,7 @@ func TestDrySwapRouteBarQuxExactOut(t *testing.T) { func TestSwapRouteBarQuxExactOut(t *testing.T) { std.TestSetPrevAddr(test1) - swapResult := SwapRoute( + amountIn, amountOut := SwapRoute( barPath, // inputToken quxPath, // outputToken "1000", // amountSpecified @@ -106,7 +107,8 @@ func TestSwapRouteBarQuxExactOut(t *testing.T) { "99999", // tokenAmountLimit ) - shouldEQ(t, swapResult, "138") + shouldEQ(t, amountIn, "138") + shouldEQ(t, amountOut, "-996") } func TestDrySwapRouteQuxBarExactIn(t *testing.T) { @@ -127,7 +129,7 @@ func TestDrySwapRouteQuxBarExactIn(t *testing.T) { func TestSwapRouteQuxBarExactIn(t *testing.T) { std.TestSetPrevAddr(test1) - swapResult := SwapRoute( + amountIn, amountOut := SwapRoute( quxPath, // inputToken barPath, // outputToken "1000", // amountSpecified @@ -137,7 +139,8 @@ func TestSwapRouteQuxBarExactIn(t *testing.T) { "1", // tokenAmountLimit ) - shouldEQ(t, swapResult, "135") + shouldEQ(t, amountIn, "1000") + shouldEQ(t, amountOut, "-135") } func TestDrySwapRouteQuxBarExactOut(t *testing.T) { @@ -161,7 +164,7 @@ func TestwapRouteQuxBarExactOut(t *testing.T) { qux.Approve(a2u(consts.POOL_ADDR), 10000) bar.Approve(a2u(consts.ROUTER_ADDR), 10000) - swapResult := SwapRoute( + amountIn, amountOut := SwapRoute( quxPath, // inputToken barPath, // outputToken "1000", // amountSpecified @@ -171,5 +174,6 @@ func TestwapRouteQuxBarExactOut(t *testing.T) { "99999", // tokenAmountLimit ) - shouldEQ(t, swapResult, "7350") + shouldEQ(t, amountIn, "7350") + shouldEQ(t, amountOut, "-997") } From 3448c40ec98bf42cc7405f4e27118e91ad34bad5 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 11 Apr 2024 11:10:37 +0900 Subject: [PATCH 5/6] feat: external incentive getter --- staker/_RPC_api_incentive.gno | 71 +++++++++++++++++----- staker/_TEST_/_TEST_staker_rpc_get_test.gn | 41 ++++++++----- 2 files changed, 83 insertions(+), 29 deletions(-) diff --git a/staker/_RPC_api_incentive.gno b/staker/_RPC_api_incentive.gno index 39edadc6..00f7ffd7 100644 --- a/staker/_RPC_api_incentive.gno +++ b/staker/_RPC_api_incentive.gno @@ -17,9 +17,13 @@ type RewardToken struct { RewardsTokenList []string `json:"rewardsTokenList"` } -type ResponseRewardTokens struct { - Stat ResponseQueryBase `json:"stat"` - Response []RewardToken `json:"response"` +type ApiIncentive struct { + PoolPath string `json:"poolPath"` + RewardToken string `json:"rewardToken"` + RewardAmount string `json:"rewardAmount"` + StartTimestamp int64 `json:"startTimestamp"` + EndTimestamp int64 `json:"endTimestamp"` + Refundee string `json:"refundee"` } func ApiGetRewardTokens() string { @@ -46,17 +50,6 @@ func ApiGetRewardTokens() string { }) } - // JSON - qb := ResponseQueryBase{ - Height: std.GetHeight(), - Timestamp: time.Now().Unix(), - } - - r := ResponseRewardTokens{ - Stat: qb, - Response: rewardTokens, - } - // STAT NODE _stat := json.ObjectNode("", map[string]*json.Node{ "height": json.NumberNode("height", float64(std.GetHeight())), @@ -65,7 +58,7 @@ func ApiGetRewardTokens() string { // RESPONSE (ARRAY) NODE responses := json.ArrayNode("", []*json.Node{}) - for i, rewardToken := range r.Response { + for i, rewardToken := range rewardTokens { _rewardTokenNode := json.ObjectNode("", map[string]*json.Node{ "poolPath": json.StringNode("poolPath", rewardToken.PoolPath), "tokens": json.ArrayNode("tokens", makeRewardTokensArray(rewardToken.RewardsTokenList)), @@ -86,6 +79,54 @@ func ApiGetRewardTokens() string { return string(b) } +func ApiGetIncentives() string { + apiIncentives := []ApiIncentive{} + + for _, incentive := range incentives { + apiIncentives = append(apiIncentives, ApiIncentive{ + PoolPath: incentive.targetPoolPath, + RewardToken: incentive.rewardToken, + RewardAmount: incentive.rewardAmount.ToString(), + StartTimestamp: incentive.startTimestamp, + EndTimestamp: incentive.endTimestamp, + Refundee: incentive.refundee.String(), + }) + } + + // 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 _, incentive := range apiIncentives { + _incentiveNode := json.ObjectNode("", map[string]*json.Node{ + "poolPath": json.StringNode("poolPath", incentive.PoolPath), + "rewardToken": json.StringNode("rewardToken", incentive.RewardToken), + "rewardAmount": json.StringNode("rewardAmount", incentive.RewardAmount), + "startTimestamp": json.NumberNode("startTimestamp", float64(incentive.StartTimestamp)), + "endTimestamp": json.NumberNode("endTimestamp", float64(incentive.EndTimestamp)), + "refundee": json.StringNode("refundee", incentive.Refundee), + }) + responses.AppendArray(_incentiveNode) + } + + // RETURN + node := json.ObjectNode("", map[string]*json.Node{ + "stat": _stat, + "response": responses, + }) + + b, err := json.Marshal(node) + if err != nil { + panic(ufmt.Sprintf("[STAKER] _RPC_api_stake.gno__ApiGetIncentives() || json marshal error: %s", err.Error())) + } + + return string(b) +} + func makeRewardTokensArray(rewardsTokenList []string) []*json.Node { rewardsTokenArray := make([]*json.Node, len(rewardsTokenList)) for i, rewardToken := range rewardsTokenList { diff --git a/staker/_TEST_/_TEST_staker_rpc_get_test.gn b/staker/_TEST_/_TEST_staker_rpc_get_test.gn index a2ce9226..c2e7f854 100644 --- a/staker/_TEST_/_TEST_staker_rpc_get_test.gn +++ b/staker/_TEST_/_TEST_staker_rpc_get_test.gn @@ -1,11 +1,9 @@ package staker import ( + "gno.land/p/demo/json" "std" "testing" - "time" - - "gno.land/p/demo/json" pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position" @@ -118,23 +116,23 @@ func TestCreateExternalIncentive(t *testing.T) { CreateExternalIncentive( "gno.land/r/demo/bar:gno.land/r/demo/qux:500", // targetPoolPath - oblPath, // rewardToken - "1000000000", // rewardAmount - time.Now().Unix(), // startTimestamp - time.Now().Unix()+TIMESTAMP_90DAYS, // endTimestamp + oblPath, // rewardToken + "1000000000", // rewardAmount + 1234569600, // startTimestamp + 1234569600+TIMESTAMP_90DAYS, // endTimestamp ) std.TestSkipHeights(1) obl.Approve(a2u(consts.STAKER_ADDR), 10_000_000_000) std.TestSkipHeights(1) - CreateExternalIncentive("gno.land/r/demo/bar:gno.land/r/demo/qux:500", oblPath, "1000000000", time.Now().Unix(), time.Now().Unix()+TIMESTAMP_90DAYS) + CreateExternalIncentive("gno.land/r/demo/bar:gno.land/r/demo/qux:500", oblPath, "1000000000", 1234569600, 1234569600+TIMESTAMP_90DAYS) std.TestSkipHeights(1) } func TestStakeToken01(t *testing.T) { std.TestSetPrevAddr(test1) StakeToken(1) // GNFT tokenId - std.TestSkipHeights(1) + std.TestSkipHeights(500) shouldEQ(t, gnft.OwnerOf(tid(1)), GetOrigPkgAddr()) // staker shouldEQ(t, len(deposits), 1) @@ -179,6 +177,21 @@ func TestApiGetStakes(t *testing.T) { shouldEQ(t, response.Size(), 2) // lpTokenId 1, 2 } +func TestApiGetIncentives(t *testing.T) { + agi := ApiGetIncentives() + root, err := json.Unmarshal([]byte(agi)) + if err != nil { + panic(err) + } + + response, err := root.GetKey("response") + if err != nil { + panic(err) + } + + shouldEQ(t, response.Size(), 1) // bar:qux:500, obl +} + func TestCollectReward(t *testing.T) { std.TestSetPrevAddr(consts.INTERNAL_REWARD_ACCOUNT) gns.Approve(a2u(consts.STAKER_ADDR), consts.UINT64_MAX) // internal reward distribution @@ -193,7 +206,7 @@ func TestCollectReward(t *testing.T) { std.TestSkipHeights(1) gnsNew := gns.BalanceOf(a2u(test1)) - shouldEQ(t, gnsNew, 18377) + shouldEQ(t, gnsNew, 3075063) } func TestUnstakeToken01(t *testing.T) { @@ -204,8 +217,8 @@ func TestUnstakeToken01(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(1)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 24503) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 8000000253) // external + shouldEQ(t, gns.BalanceOf(a2u(test1)), 3081190) // internal + shouldEQ(t, obl.BalanceOf(a2u(test1)), 8000011025) // external } func TestUnstakeToken02(t *testing.T) { @@ -216,9 +229,9 @@ func TestUnstakeToken02(t *testing.T) { shouldEQ(t, gnft.OwnerOf(tid(2)), test1) // check reward - shouldEQ(t, gns.BalanceOf(a2u(test1)), 187853) + shouldEQ(t, gns.BalanceOf(a2u(test1)), 3244540) // internal - shouldEQ(t, obl.BalanceOf(a2u(test1)), 8000001933) // external + shouldEQ(t, obl.BalanceOf(a2u(test1)), 8000012705) // external } func TestEndExternalIncentive(t *testing.T) { From 1a4131477e1e72698db29884fc8e1584209eee88 Mon Sep 17 00:00:00 2001 From: Lee ByeongJun Date: Thu, 11 Apr 2024 13:22:16 +0900 Subject: [PATCH 6/6] Update staker/_TEST_/_TEST_staker_one_external_test.gn --- staker/_TEST_/_TEST_staker_one_external_test.gn | 1 - 1 file changed, 1 deletion(-) diff --git a/staker/_TEST_/_TEST_staker_one_external_test.gn b/staker/_TEST_/_TEST_staker_one_external_test.gn index 6b75f9b4..92a6ab30 100644 --- a/staker/_TEST_/_TEST_staker_one_external_test.gn +++ b/staker/_TEST_/_TEST_staker_one_external_test.gn @@ -4,7 +4,6 @@ import ( "std" "testing" - // @notJoon JSON pl "gno.land/r/demo/pool" pn "gno.land/r/demo/position"