Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: staker test code and external incentive leftAmount management #463

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,15 +10,15 @@ 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"
)

var (
poolPath string = "gno.land/r/onbloc/bar:gno.land/r/onbloc/baz:100"
)
var poolPath string = "gno.land/r/onbloc/bar:gno.land/r/onbloc/baz:100"

func TestMore04(t *testing.T) {
testInit(t)
Expand All @@ -39,13 +39,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 +47,29 @@ 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) {
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)
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
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 @@ -23,11 +24,9 @@ import (
"gno.land/r/onbloc/obl"
)

var (
anoAdmin = users.Resolve(admin)
)
var anoAdmin = users.Resolve(admin)

func TestCollectReward(t *testing.T) {
func TestCollectReward_Full(t *testing.T) {
testInit(t)
testPoolCreatePool(t)
testPositionMint01(t)
Expand Down Expand Up @@ -259,7 +258,17 @@ 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)
now := uint64(time.Now().Unix())
gap := now - 1234569600

// 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)

std.TestSkipHeights(1)

Expand All @@ -269,7 +278,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
Loading