Skip to content

Commit

Permalink
GSW-1845 test: fix and add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Dec 20, 2024
1 parent b98a424 commit 62faf25
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
2 changes: 2 additions & 0 deletions _deploy/r/gnoswap/gns/gns.gno
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func MintGns(address pusers.AddressOrName) uint64 {
setMintedEmissionAmount(GetMintedEmissionAmount() + amountToMint)
setLeftEmissionAmount(GetLeftEmissionAmount() - amountToMint)

// mint calculated amount to address
err := privateLedger.Mint(users.Resolve(address), amountToMint)
if err != nil {
panic(err.Error())
Expand Down Expand Up @@ -150,6 +151,7 @@ func checkErr(err error) {
func calculateAmountToMint(fromHeight, toHeight int64) uint64 {
fromYear := GetHalvingYearByHeight(fromHeight)

// if toHeight is greater than emission end height, set toHeight to emission end height
if toHeight > GetEndHeight() {
toHeight = GetEndHeight()
}
Expand Down
32 changes: 32 additions & 0 deletions _deploy/r/gnoswap/gns/gns_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@ var (
bob = testutils.TestAddress("bob")
)

func TestAssertTooManyEmission(t *testing.T) {
tests := []struct {
name string
amount uint64
shouldPanic bool
panicMsg string
}{
{
name: "should panic if emission amount is too large",
amount: MAXIMUM_SUPPLY,
shouldPanic: true,
panicMsg: "[GNOSWAP-GNS-002] too many emission reward || amount: 1000000000000000",
},
{
name: "should not panic if emission amount is not too large",
amount: 123,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.shouldPanic {
uassert.PanicsWithMessage(t, tt.panicMsg, func() {
assertTooManyEmission(tt.amount)
})
} else {
uassert.NotPanics(t, func() { assertTooManyEmission(tt.amount) })
}
})
}
}

func TestIsLastBlockOfHalvingYear(t *testing.T) {
tests := make([]struct {
name string
Expand Down
6 changes: 4 additions & 2 deletions _deploy/r/gnoswap/gns/tests/z1_filetest.gno
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func skip50Blocks() {
uassert.Equal(t, std.GetHeight(), int64(173))

std.TestSetRealm(std.NewCodeRealm(consts.EMISSION_PATH))
mintedAmount := gns.MintGns(a2u(user01Addr)) // 14269406 * 50 = 713470300)
mintedAmount := gns.MintGns(a2u(user01Addr))
uassert.Equal(t, uint64(713470300), mintedAmount) // 14269406 * 50
uassert.Equal(t, uint64(713470300), gns.GetMintedEmissionAmount())
}

Expand All @@ -62,7 +63,8 @@ func blockTime2500ms() {

std.TestSetRealm(std.NewCodeRealm(consts.EMISSION_PATH))
std.TestSkipHeights(1)
mintedAmount := gns.MintGns(a2u(user01Addr)) // 17836757
mintedAmount := gns.MintGns(a2u(user01Addr))
uassert.Equal(t, uint64(17836757), mintedAmount)
uassert.Equal(t, uint64(713470300+17836757), gns.GetMintedEmissionAmount()) // 731307057
}

Expand Down
16 changes: 16 additions & 0 deletions _deploy/r/gnoswap/gns/tests/z2_filetest.gno
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ func blockTime4000ms() {
gns.SetAvgBlockTimeInMsByAdmin(4000)
// for block time 4s, amount per block is 28538812

// first halving year end block = 15768122
// first halving year end timestamp = 1234567990

// 50 block minted from L#38
// > current height = 173
// > current timestamp = 1234568140

// 1266103890 - 1234567990 = 31535900 // timestamp left for current halving year
// 31535900000 / 4000(new block avg time/ms) = 7883975 // number of block left for current halving year

// 713470300 // already minted amount

// first halving year should mint 225000000000000
// 225000000000000 - 713470300 = 224999286529700
// 224999286529700 / 7883975 ≈ 28538812

std.TestSetRealm(std.NewCodeRealm(consts.EMISSION_PATH))
std.TestSkipHeights(1)
mintedAmount := gns.MintGns(a2u(user01Addr)) // 28538812
Expand Down
5 changes: 2 additions & 3 deletions _deploy/r/gnoswap/gns/tests/z3_filetest.gno
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func skip50Blocks() {
func blockTime4000ms() {
std.TestSetRealm(std.NewUserRealm(consts.ADMIN))
gns.SetAvgBlockTimeInMsByAdmin(4000)
// for block time 4s, amount per block is 28538812

std.TestSkipHeights(1)
std.TestSetRealm(std.NewCodeRealm(consts.EMISSION_PATH))
Expand All @@ -57,10 +56,10 @@ func blockTime4000ms() {

func reachAboutHalfOfFirstHalving() {
// current block = 174
// next halving year start block = 7884148
// end block of first halving year = 7884148
// 7884148 - 174 = 7883974 // block left to next halving

std.TestSkipHeights(3941987) // 7883974 / 2
std.TestSkipHeights(3941987)
std.TestSetRealm(std.NewCodeRealm(consts.EMISSION_PATH))
gns.MintGns(a2u(user01Addr))

Expand Down

0 comments on commit 62faf25

Please sign in to comment.