Skip to content

Commit

Permalink
Merge pull request #207 from gnoswap-labs/GSW-1012-feat-start-time-of…
Browse files Browse the repository at this point in the history
…-external-incentive-must-be-at-least-tomorrow-midnight

GSW-1012 feat: conditions for external incentive start time
  • Loading branch information
notJoon authored Apr 11, 2024
2 parents a1ab898 + 1a41314 commit 5ca23ff
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 119 deletions.
1 change: 0 additions & 1 deletion pool/_TEST_/z1_single_lp_filetest.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion position/_TEST_/_TEST_position_increase_decrease_test.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions router/_TEST_/_TEST_router_ratio_test.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}")
}
41 changes: 22 additions & 19 deletions router/_TEST_/_TEST_router_swap_route_1route_1hop_test.gn
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package router

import (
// @notJoon JSON
"std"
"testing"

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
20 changes: 12 additions & 8 deletions router/_TEST_/_TEST_router_swap_route_2route_2hop_test.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -171,5 +174,6 @@ func TestwapRouteQuxBarExactOut(t *testing.T) {
"99999", // tokenAmountLimit
)

shouldEQ(t, swapResult, "7350")
shouldEQ(t, amountIn, "7350")
shouldEQ(t, amountOut, "-997")
}
71 changes: 56 additions & 15 deletions staker/_RPC_api_incentive.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())),
Expand All @@ -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)),
Expand All @@ -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 {
Expand Down
17 changes: 9 additions & 8 deletions staker/_TEST_/_TEST_staker_collect_reward_test.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package staker
import (
"std"
"testing"
"time"

"gno.land/r/gnoswap/consts"

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 5ca23ff

Please sign in to comment.