Skip to content

Commit

Permalink
test: staker test code
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyhyde committed Jan 3, 2025
1 parent 372cb22 commit 158f563
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 14 deletions.
5 changes: 2 additions & 3 deletions gov/staker/delegate_undelegate_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"testing"

"gno.land/p/demo/avl"
"gno.land/p/demo/testutils"
)

func TestDelegate(t *testing.T) {
func TestDelegateInternal(t *testing.T) {
t.Skip("Must running separately. because this test depends on TestUndelegate's state")
std.TestSetOrigCaller(testAddr1)
resetState()
Expand Down Expand Up @@ -54,7 +53,7 @@ func TestDelegate(t *testing.T) {
})
}

func TestUndelegate(t *testing.T) {
func TestUndelegateInternal(t *testing.T) {
t.Skip("Must running separately. because this test depends on TestUndelegate's state")
addr1 := testAddr1
std.TestSetOrigCaller(addr1)
Expand Down
1 change: 0 additions & 1 deletion gov/staker/staker.gno
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ func collectEmissionReward(addr std.Address) uint64 {
if value, exists := userEmissionReward.Get(addr.String()); exists {
emissionReward = value.(uint64)
}
println("emissionReward", emissionReward)

if emissionReward <= 0 {
return 0
Expand Down
95 changes: 85 additions & 10 deletions gov/staker/staker_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,85 @@ import (
"gno.land/p/demo/avl"
"gno.land/p/demo/testutils"

"gno.land/r/demo/wugnot"
"gno.land/r/gnoswap/v1/consts"
"gno.land/r/gnoswap/v1/gns"
"gno.land/r/gnoswap/v1/gov/xgns"
)

// Mock or define test realms/addresses if needed
var adminRealm = std.NewUserRealm(consts.ADMIN)
var userRealm = std.NewUserRealm(testutils.TestAddress("alice"))
var invalidAddr = testutils.TestAddress("invalid")

var (
adminRealm = std.NewUserRealm(consts.ADMIN)
userRealm = std.NewUserRealm(testutils.TestAddress("alice"))
user2Realm = std.NewUserRealm(testutils.TestAddress("bob"))
invalidAddr = testutils.TestAddress("invalid")

ugnotDenom string = "ugnot"
ugnotPath string = "ugnot"
wugnotPath string = "gno.land/r/demo/wugnot"
)

func makeFakeAddress(name string) std.Address {
return testutils.TestAddress(name)
}

func ugnotTransfer(t *testing.T, from, to std.Address, amount uint64) {
t.Helper()

std.TestSetRealm(std.NewUserRealm(from))
std.TestSetOrigSend(std.Coins{{ugnotDenom, int64(amount)}}, nil)
banker := std.GetBanker(std.BankerTypeRealmSend)
banker.SendCoins(from, to, std.Coins{{ugnotDenom, int64(amount)}})
}

func ugnotBalanceOf(t *testing.T, addr std.Address) uint64 {
t.Helper()

banker := std.GetBanker(std.BankerTypeRealmIssue)
coins := banker.GetCoins(addr)
if len(coins) == 0 {
return 0
}

return uint64(coins.AmountOf(ugnotDenom))
}

func ugnotMint(t *testing.T, addr std.Address, denom string, amount int64) {
t.Helper()
banker := std.GetBanker(std.BankerTypeRealmIssue)
banker.IssueCoin(addr, denom, amount)
std.TestIssueCoins(addr, std.Coins{{denom, int64(amount)}})
}

func ugnotBurn(t *testing.T, addr std.Address, denom string, amount int64) {
t.Helper()
banker := std.GetBanker(std.BankerTypeRealmIssue)
banker.RemoveCoin(addr, denom, amount)
}

func ugnotFaucet(t *testing.T, to std.Address, amount uint64) {
t.Helper()
faucetAddress := consts.ADMIN
std.TestSetOrigCaller(faucetAddress)

if ugnotBalanceOf(t, faucetAddress) < amount {
newCoins := std.Coins{{ugnotDenom, int64(amount)}}
ugnotMint(t, faucetAddress, newCoins[0].Denom, newCoins[0].Amount)
std.TestSetOrigSend(newCoins, nil)
}
ugnotTransfer(t, faucetAddress, to, amount)
}

func ugnotDeposit(t *testing.T, addr std.Address, amount uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(addr))
wugnotAddr := consts.WUGNOT_ADDR
banker := std.GetBanker(std.BankerTypeRealmSend)
banker.SendCoins(addr, wugnotAddr, std.Coins{{ugnotDenom, int64(amount)}})
wugnot.Deposit()
}

func TestDelegate(t *testing.T) {
std.TestSetOrigCaller(consts.ADMIN)
SetRunning(true)
Expand Down Expand Up @@ -223,12 +288,21 @@ func TestCollectUndelegatedGns(t *testing.T) {
}

func TestCollectReward(t *testing.T) {
// We can test that common.IsHalted(), en.MintAndDistributeGns() are called
// and that emission/protocol fee are distributed if user has a reward recorded.
// This generally requires mocking userEmissionReward, userProtocolFeeReward, etc.
std.TestSetRealm(user2Realm)
{
std.TestSetRealm(std.NewCodeRealm(consts.EMISSION_PATH))
std.TestSkipHeights(100)
gns.MintGns(a2u(consts.GOV_STAKER_ADDR))

std.TestSetRealm(userRealm)
user := userRealm.Addr().String()
std.TestSetRealm(std.NewUserRealm(consts.ADMIN))
ugnotFaucet(t, consts.GOV_STAKER_ADDR, 1_000_000)
ugnotDeposit(t, consts.GOV_STAKER_ADDR, 1_000_000)
ugnotFaucet(t, derivePkgAddr(wugnotPath), 1_000_000)
ugnotFaucet(t, user2Realm.Addr(), 1_000_000)
}

std.TestSetRealm(user2Realm)
user := user2Realm.Addr().String()

// set a fake emission reward
userEmissionReward.Set(user, uint64(50_000))
Expand All @@ -239,12 +313,13 @@ func TestCollectReward(t *testing.T) {

std.TestSetRealm(std.NewCodeRealm(consts.EMISSION_PATH))
std.TestSkipHeights(100)
gns.MintGns(a2u(userRealm.Addr())) // 2M gns
gns.MintGns(a2u(user2Realm.Addr()))

std.TestSetRealm(adminRealm)
std.TestSetRealm(user2Realm)
std.TestSkipHeights(100)

// call CollectReward
std.TestSetOrigCaller(user2Realm.Addr())
CollectReward()

// expect user emissionReward = 0
Expand Down
5 changes: 5 additions & 0 deletions gov/staker/util.gno
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func getPrev() (string, string) {
return prev.Addr().String(), prev.PkgPath()
}

// derivePkgAddr derives the Realm address from it's pkgPath parameter
func derivePkgAddr(pkgPath string) std.Address {
return std.DerivePkgAddr(pkgPath)
}

// a2u converts std.Address to pusers.AddressOrName.
// pusers is a package that contains the user-related functions.
//
Expand Down

0 comments on commit 158f563

Please sign in to comment.