Skip to content

Commit

Permalink
fix: staker test code and external incentive leftAmount management
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyhyde committed Jan 15, 2025
1 parent b1ce569 commit c9fb880
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 26 deletions.
2 changes: 1 addition & 1 deletion position/native_token_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestTransferUGNOT(t *testing.T) {
}
}

func TestWrap(t *testing.T) {
func TestWrapInPosition(t *testing.T) {
tests := []struct {
name string
action func(t *testing.T, from, to std.Address) error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,10 @@ func testInit(t *testing.T) {

// bar:baz:100 is only pool for internal emission reward (tier1)
deletePoolTier(t, MUST_EXISTS_IN_TIER_1)
addPoolTier(t, poolPath, 1)

// set unstaking fee to 0
SetUnstakingFeeByAdmin(0)

// set pool creation fee to 0
pl.SetPoolCreationFeeByAdmin(0)

// set community pool distribution to 0% (give it to devOps)
en.ChangeDistributionPctByAdmin(
1, 7500,
Expand All @@ -65,8 +61,11 @@ func testInit(t *testing.T) {
func testCreatePool(t *testing.T) {
t.Run("create pool", func(t *testing.T) {
std.TestSetRealm(adminRealm)
// set pool creation fee to 0
pl.SetPoolCreationFeeByAdmin(0)

pl.CreatePool(barPath, bazPath, 100, "79228162514264337593543950337")
addPoolTier(t, poolPath, 1)
std.TestSkipHeights(1)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
pl "gno.land/r/gnoswap/v1/pool"
pn "gno.land/r/gnoswap/v1/position"

"gno.land/r/gnoswap/v1/common"
"gno.land/r/gnoswap/v1/gnft"

"gno.land/r/gnoswap/v1/gns"
"gno.land/r/onbloc/bar"
"gno.land/r/onbloc/baz"
)
Expand Down Expand Up @@ -39,13 +41,6 @@ func testInit(t *testing.T) {

// bar:baz:100 is only pool for internal emission reward (tier1)
deletePoolTier(t, MUST_EXISTS_IN_TIER_1)
addPoolTier(t, poolPath, 1)

// set unstaking fee to 0
SetUnstakingFeeByAdmin(0)

// set pool creation fee to 0
pl.SetPoolCreationFeeByAdmin(0)

// set community pool distribution to 0% (give it to devOps)
en.ChangeDistributionPctByAdmin(
Expand All @@ -54,14 +49,34 @@ func testInit(t *testing.T) {
3, 0,
4, 0,
)

// set unstaking fee to 0
SetUnstakingFeeByAdmin(0)

std.TestSkipHeights(1)
})
}

func testCreatePool(t *testing.T) {
t.Run("create pool", func(t *testing.T) {
println("[", std.GetHeight(), "]", "create pool bar-baz-100")
std.TestSetRealm(adminRealm)

// set pool creation fee to 0
pl.SetPoolCreationFeeByAdmin(uint64(0))

barToken := common.GetTokenTeller(barPath)
bazToken := common.GetTokenTeller(bazPath)
gnsToken := common.GetTokenTeller(consts.GNS_PATH)
println(barToken.BalanceOf(adminAddr))
println(bazToken.BalanceOf(adminAddr))
println(gnsToken.BalanceOf(adminAddr))
gns.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)
bar.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)
baz.Approve(a2u(consts.POOL_ADDR), consts.UINT64_MAX)
pl.CreatePool(barPath, bazPath, 100, "79228162514264337593543950337") // price ratio 1:1
println(gnsToken.BalanceOf(adminAddr))
addPoolTier(t, poolPath, 1)
std.TestSkipHeights(1)
})
}
Expand Down
13 changes: 4 additions & 9 deletions staker/__TEST_staker_full_with_emission_test.gnoA
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ func testInit(t *testing.T) {
// set pool create fee to 0 for testing
std.TestSetRealm(adminRealm)
pl.SetPoolCreationFeeByAdmin(0)

// init pool tiers
// tier 1
addPoolTier(t, `gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:500`, 1)
})
}

Expand All @@ -63,6 +59,7 @@ func testCreatePool(t *testing.T) {

pl.CreatePool(consts.WUGNOT_PATH, consts.GNS_PATH, 3000, "79228162514264337593543950336") // tick 0 ≈ x1
pl.CreatePool(barPath, quxPath, 500, "130621891405341611593710811006") // tick 10_000 ≈ x2.7
addPoolTier(t, `gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:500`, 1)
std.TestSkipHeights(2)

uassert.Equal(t, gns.TotalSupply(), uint64(100000014269406)) // + 14269406
Expand Down Expand Up @@ -249,7 +246,6 @@ func testStakeToken03(t *testing.T) {
}

func testSameHeightCalculation(t *testing.T) {

}

func testCollectReward01(t *testing.T) {
Expand All @@ -275,24 +271,23 @@ func testUnstakeToken01(t *testing.T) {

func testUnstakeToken02(t *testing.T) {
t.Run("unstake token 02", func(t *testing.T) {

std.TestSetRealm(adminRealm)
UnstakeToken(2, false)
std.TestSkipHeights(1)

// 1 block passed
uassert.Equal(t, gns.TotalSupply(), uint64(100000299657526)) // + 14269406
uassert.Equal(t, gnsBalance(consts.EMISSION_ADDR), uint64(1))
uassert.Equal(t, gnsBalance(consts.STAKER_ADDR), uint64(1024301275))
uassert.Equal(t, gnsBalance(consts.DEV_OPS), uint64(1481164342))
uassert.Equal(t, uint64(1029652302), gnsBalance(consts.STAKER_ADDR))
uassert.Equal(t, gnsBalance(consts.DEV_OPS), uint64(59931504)) // + 2853881
})
}

func testCollectReward02(t *testing.T) {
t.Run("collect reward from unstaked position should panic", func(t *testing.T) {
uassert.PanicsWithMessage(
t,
"[GNOSWAP-STAKER-022] requested data not found || staker.gno__Deposits.Get() || tokenId(2) not found",
"[GNOSWAP-STAKER-022] requested data not found || tokenId(2) not found",
func() {
CollectReward(2, false)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math"
"std"
"testing"
"time"

"gno.land/p/demo/uassert"
pusers "gno.land/p/demo/users"
Expand All @@ -27,7 +28,7 @@ var (
anoAdmin = users.Resolve(admin)
)

func TestCollectReward(t *testing.T) {
func TestCollectReward_Full(t *testing.T) {
testInit(t)
testPoolCreatePool(t)
testPositionMint01(t)
Expand All @@ -53,7 +54,7 @@ func testInit(t *testing.T) {
changeWarmup(t, 3, math.MaxInt64)

// set unstaking fee to 0
// SetUnstakingFeeByAdmin(0)
//SetUnstakingFeeByAdmin(0)
})
}

Expand Down Expand Up @@ -198,6 +199,7 @@ func testStakeToken01(t *testing.T) {

func testStakeToken02(t *testing.T) {
t.Run("stake position 02, bar:qux:500", func(t *testing.T) {
println("========>[", std.GetHeight(), "]", "Stake 2")
std.TestSetRealm(adminRealm)
StakeToken(2) // GNFT tokenId

Expand Down Expand Up @@ -259,7 +261,21 @@ func testUnstakeToken01(t *testing.T) {
func testUnstakeToken02(t *testing.T) {
t.Run("unstake position 02, bar:qux:500", func(t *testing.T) {
std.TestSetRealm(adminRealm)
println("========>[", std.GetHeight(), "]", "UnStake 2")
now := uint64(time.Now().Unix())
gap := now - 1234569600
println("========>[", now, "], [", gap, "], [", gap/uint64(consts.BLOCK_GENERATION_INTERVAL), "] UnStake 2")

// 0.4586225023
// 514
// 235.9169250514
beforeBalance := obl.BalanceOf(a2u(consts.STAKER_ADDR))
beforeObl := obl.BalanceOf(admin)
UnstakeToken(2, true) // GNFT tokenId
afterBalance := obl.BalanceOf(a2u(consts.STAKER_ADDR))
afterObl := obl.BalanceOf(admin)
println("before : ", beforeBalance, ", after : ", afterBalance, ", diff : ", beforeBalance-afterBalance)
println("before : ", beforeObl, ", after : ", afterObl, ", diff : ", afterObl-beforeObl)

std.TestSkipHeights(1)

Expand All @@ -269,7 +285,7 @@ func testUnstakeToken02(t *testing.T) {

// check reward
uassert.Equal(t, gns.BalanceOf(admin), uint64(99999000000000))
uassert.Equal(t, obl.BalanceOf(admin), uint64(99999000011371)) // external
uassert.Equal(t, obl.BalanceOf(admin), uint64(99999000011399)) // external + 9684
})
}

Expand Down
2 changes: 1 addition & 1 deletion staker/errors.gno
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var (
errNoPermission = errors.New("[GNOSWAP-STAKER-001] caller has no permission")
errPoolNotFound = errors.New("[GNOSWAP-STAKER-002] pool not found")
errAlreadyRegistered = errors.New("[GNOSWAP-STAKER-003] already registered token")
errLocked = errors.New("[GNOSWAP-STAKER-004] can't transfer token while locked")
errInsufficientReward = errors.New("[GNOSWAP-STAKER-004] insufficient reward")
errWrapUnwrap = errors.New("[GNOSWAP-STAKER-005] wrap, unwrap failed")
errWugnotMinimum = errors.New("[GNOSWAP-STAKER-006] can not wrapless than minimum amount")
errInvalidInput = errors.New("[GNOSWAP-STAKER-007] invalid input data")
Expand Down
13 changes: 12 additions & 1 deletion staker/staker.gno
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,15 @@ func CollectReward(tokenId uint64, unwrapResult bool) (string, string) {
for incentiveId, amount := range externalReward {
incentive := externalIncentives.Get(incentiveId)
rewardToken := incentive.rewardToken
if incentive.rewardAmount < amount {
panic(addDetailToError(
errInsufficientReward,
ufmt.Sprintf("incentiveId(%s) has insufficient reward(%d)", incentiveId, amount),
))
}
incentive.rewardAmount -= amount
incentive.rewardLeft = incentive.rewardAmount
externalIncentives.Set(incentiveId, incentive)

toUser := handleUnstakingFee(rewardToken, amount, false, tokenId, incentive.targetPoolPath)

Expand All @@ -391,7 +400,9 @@ func CollectReward(tokenId uint64, unwrapResult bool) (string, string) {

// do nothing for external penalty
// it will be stored in staker, and when external ends will be refunded
externalPenalty := reward.ExternalPenalty
externalIncentivePenalty := reward.ExternalPenalty
externalIncentiveReward := reward.External

// TODO:
// externalPenalty should be stored in staker
// And update reward ledger
Expand Down

0 comments on commit c9fb880

Please sign in to comment.