Skip to content

Commit

Permalink
test(fix): external gnoA
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Jan 19, 2025
1 parent 27445d3 commit b9a7dcb
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 141 deletions.
63 changes: 31 additions & 32 deletions staker/__TEST_staker_short_warmup_period_external_10_test.gnoA
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ func TestShortWarmUpExternal(t *testing.T) {
testCollectReward(t)
testMintBarQux100_2(t)
testStakeToken_2(t)
testSingleBlock_TwoPosition(t)
testCollectRewardAll(t)
}

func testInit(t *testing.T) {
t.Run("initialize", func(t *testing.T) {
std.TestSetRealm(adminRealm)

// override warm-up period for testing
changeWarmup(t, 0, 150)
changeWarmup(t, 1, 300)
Expand Down Expand Up @@ -133,18 +134,12 @@ func testAfterActive(t *testing.T) {
std.TestSkipHeights(849) // in active
std.TestSkipHeights(1) // active // but no block passed since active
std.TestSkipHeights(50) // skip 50 more block

// pei := GetPrintExternalInfo()
// uassert.Equal(t, pei, `{"height":1028,"time":1234569700,"position":[{"lpTokenId":1,"stakedHeight":126,"stakedTimestamp":1234567896,"incentive":[{"poolPath":"gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:100","rewardToken":"gno.land/r/onbloc/bar","rewardAmount":"20000000","startTimestamp":1234569600,"endTimestamp":1242345600,"rewardPerBlockX96":"407552276307944123423579991440","stakedOrExternalDuration":50,"rewardPerBlock":"5","refundee":"g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d","tokenAmountFull":257,"tokenAmountToGive":77,"full30":257,"give30":77,"full50":0,"give50":0,"full70":0,"give70":0,"full100":0}]}]}`)
})
}

func testDuratino200(t *testing.T) {
t.Run("duration 200", func(t *testing.T) {
std.TestSkipHeights(199) // skip 1 + 199 = 200 more block

// pei := GetPrintExternalInfo()
// uassert.Equal(t, pei, `{"height":1227,"time":1234570098,"position":[{"lpTokenId":1,"stakedHeight":126,"stakedTimestamp":1234567896,"incentive":[{"poolPath":"gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:100","rewardToken":"gno.land/r/onbloc/bar","rewardAmount":"20000000","startTimestamp":1234569600,"endTimestamp":1242345600,"rewardPerBlockX96":"407552276307944123423579991440","stakedOrExternalDuration":249,"rewardPerBlock":"5","refundee":"g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d","tokenAmountFull":1280,"tokenAmountToGive":485,"full30":771,"give30":231,"full50":509,"give50":254,"full70":0,"give70":0,"full100":0}]}]}`)
t.Run("skip duration 200", func(t *testing.T) {
std.TestSkipHeights(200)
})
}

Expand All @@ -154,14 +149,22 @@ func testCollectReward(t *testing.T) {

oldBar := bar.BalanceOf(admin)
CollectReward(1, false)
std.TestSkipHeights(1)
newBar := bar.BalanceOf(admin)
println("collected bar", newBar-oldBar)

// uassert.Equal(t, newBar-oldBar, uint64(481))
uassert.True(t, isInErrorRange(4025, newBar-oldBar))
// 4025 received
// 1725 penalty
// 5750 (total)

// > block per reward is 23
// > position staked 250 duration
// > 23 * 250 = 5750

// position warm up is 70% ( 5750 * 0.7 = 4025 )
// penalty is 30% ( 5750 * 0.3 = 1725 )

std.TestSkipHeights(1)

// pei := GetPrintExternalInfo()
// uassert.Equal(t, pei, `{"height":1228,"time":1234570100,"position":[{"lpTokenId":1,"stakedHeight":126,"stakedTimestamp":1234567896,"incentive":[{"poolPath":"gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:100","rewardToken":"gno.land/r/onbloc/bar","rewardAmount":"20000000","startTimestamp":1234569600,"endTimestamp":1242345600,"rewardPerBlockX96":"407552276307944123423579991440","stakedOrExternalDuration":250,"rewardPerBlock":"5","refundee":"g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d","tokenAmountFull":5,"tokenAmountToGive":2,"full30":0,"give30":0,"full50":5,"give50":2,"full70":0,"give70":0,"full100":0}]}]}`)
})
}

Expand Down Expand Up @@ -200,39 +203,35 @@ func testStakeToken_2(t *testing.T) {

gnft.Approve(consts.STAKER_ADDR, tid(2))
StakeToken(2)

std.TestSkipHeights(1)
})
}

func testSingleBlock_TwoPosition(t *testing.T) {
t.Run("single block, two position", func(t *testing.T) {
// skipped 1 block from previous test

// pei := GetPrintExternalInfo()
// uassert.Equal(t, pei, `{"height":1230,"time":1234570104,"position":[{"lpTokenId":1,"stakedHeight":126,"stakedTimestamp":1234567896,"incentive":[{"poolPath":"gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:100","rewardToken":"gno.land/r/onbloc/bar","rewardAmount":"20000000","startTimestamp":1234569600,"endTimestamp":1242345600,"rewardPerBlockX96":"407552276307944123423579991440","stakedOrExternalDuration":252,"rewardPerBlock":"5","refundee":"g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d","tokenAmountFull":12,"tokenAmountToGive":5,"full30":0,"give30":0,"full50":12,"give50":5,"full70":0,"give70":0,"full100":0}]},{"lpTokenId":2,"stakedHeight":1229,"stakedTimestamp":1234570102,"incentive":[{"poolPath":"gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:100","rewardToken":"gno.land/r/onbloc/bar","rewardAmount":"20000000","startTimestamp":1234569600,"endTimestamp":1242345600,"rewardPerBlockX96":"407552276307944123423579991440","stakedOrExternalDuration":1,"rewardPerBlock":"5","refundee":"g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d","tokenAmountFull":2,"tokenAmountToGive":0,"full30":2,"give30":0,"full50":0,"give50":0,"full70":0,"give70":0,"full100":0}]}]}`)

std.TestSkipHeights(1)
})
}

func testCollectRewardAll(t *testing.T) {
t.Run("collect reward all", func(t *testing.T) {
std.TestSkipHeights(10)
std.TestSetRealm(adminRealm)

// pei := GetPrintExternalInfo()
// uassert.Equal(t, pei, `{"height":1241,"time":1234570126,"position":[{"lpTokenId":1,"stakedHeight":126,"stakedTimestamp":1234567896,"incentive":[{"poolPath":"gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:100","rewardToken":"gno.land/r/onbloc/bar","rewardAmount":"20000000","startTimestamp":1234569600,"endTimestamp":1242345600,"rewardPerBlockX96":"407552276307944123423579991440","stakedOrExternalDuration":263,"rewardPerBlock":"5","refundee":"g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d","tokenAmountFull":40,"tokenAmountToGive":19,"full30":0,"give30":0,"full50":40,"give50":19,"full70":0,"give70":0,"full100":0}]},{"lpTokenId":2,"stakedHeight":1229,"stakedTimestamp":1234570102,"incentive":[{"poolPath":"gno.land/r/onbloc/bar:gno.land/r/onbloc/qux:100","rewardToken":"gno.land/r/onbloc/bar","rewardAmount":"20000000","startTimestamp":1234569600,"endTimestamp":1242345600,"rewardPerBlockX96":"407552276307944123423579991440","stakedOrExternalDuration":12,"rewardPerBlock":"5","refundee":"g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d","tokenAmountFull":30,"tokenAmountToGive":8,"full30":30,"give30":8,"full50":0,"give50":0,"full70":0,"give70":0,"full100":0}]}]}`)
// clear rewards
CollectReward(1, false)
CollectReward(2, false)

std.TestSetRealm(adminRealm)
std.TestSkipHeights(10)
// > block per reward is 23
// > skip 10 block => reward 230

// position 01 and 02 has same liquidity
// each position's 100% reward = (230 / 2) = 115
// position 01 warmup 70% = 115 * 0.7 = 80.5
// position 02 warmup 30% = 115 * 0.3 = 34.5

oldBar := bar.BalanceOf(admin)
CollectReward(1, false)
newBar := bar.BalanceOf(admin)
uassert.Equal(t, newBar-oldBar, uint64(19))
uassert.True(t, isInErrorRange(80, newBar-oldBar))

oldBar = newBar
CollectReward(2, false)
newBar = bar.BalanceOf(admin)
uassert.Equal(t, newBar-oldBar, uint64(8))
uassert.True(t, isInErrorRange(34, newBar-oldBar))
})
}
15 changes: 9 additions & 6 deletions staker/__TEST_staker_short_warmup_period_external_12_test.gnoA
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
package staker

import (
"math"
"std"
"testing"

"gno.land/p/demo/uassert"
"gno.land/r/demo/users"
pusers "gno.land/p/demo/users"

"gno.land/r/gnoswap/v1/consts"

Expand Down Expand Up @@ -40,10 +40,13 @@ func TestShortWarmUpTWoExternalIncentive(t *testing.T) {

func testInit(t *testing.T) {
t.Run("initialize", func(t *testing.T) {
changeWarmup(t, 100, 901)
changeWarmup(t, 70, 301)
changeWarmup(t, 50, 151)
changeWarmup(t, 30, 1)
std.TestSetRealm(adminRealm)

// override warm-up period for testing
changeWarmup(t, 0, 150)
changeWarmup(t, 1, 300)
changeWarmup(t, 2, 900)
changeWarmup(t, 3, math.MaxInt64)

// set unstaking fee to 0
SetUnstakingFeeByAdmin(0)
Expand Down Expand Up @@ -86,7 +89,7 @@ func testMintBarQux100_1(t *testing.T) {
)

uassert.Equal(t, tokenId, uint64(1))
uassert.Equal(t, gnft.OwnerOf(tid(tokenId)), admin)
uassert.Equal(t, gnft.MustOwnerOf(tid(tokenId)), adminAddr)

std.TestSkipHeights(1)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func TestShortWarmUpTwoExternalIncentive_OneOfThemIsGNS(t *testing.T) {

func testInit(t *testing.T) {
t.Run("initialize", func(t *testing.T) {
std.TestSetRealm(adminRealm)

// override warm-up period for testing
changeWarmup(t, 0, 150)
changeWarmup(t, 1, 300)
Expand Down Expand Up @@ -87,7 +89,7 @@ func testMintBarQux100_1(t *testing.T) {
)

uassert.Equal(t, tokenId, uint64(1))
uassert.Equal(t, gnft.OwnerOf(tid(tokenId)), admin)
uassert.Equal(t, gnft.MustOwnerOf(tid(tokenId)), adminAddr)

std.TestSkipHeights(1)
})
Expand Down Expand Up @@ -178,8 +180,8 @@ func testCollectReward(t *testing.T) {
newBar := bar.BalanceOf(admin)
newGns := gns.BalanceOf(admin)

uassert.Equal(t, newBar-oldBar, uint64(485))
uassert.Equal(t, newGns-oldGns, uint64(485))
uassert.Equal(t, newBar-oldBar, uint64(878))
uassert.Equal(t, newGns-oldGns, uint64(878))

uassert.True(t, newBar > oldBar)
uassert.True(t, newGns > oldGns)
Expand Down
Loading

0 comments on commit b9a7dcb

Please sign in to comment.