diff --git a/_deploy/r/gnoswap/gns/gns.gno b/_deploy/r/gnoswap/gns/gns.gno index f647460a8..99836ae85 100644 --- a/_deploy/r/gnoswap/gns/gns.gno +++ b/_deploy/r/gnoswap/gns/gns.gno @@ -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()) @@ -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() } diff --git a/_deploy/r/gnoswap/gns/gns_test.gno b/_deploy/r/gnoswap/gns/gns_test.gno index 758d94b03..33ae2200c 100644 --- a/_deploy/r/gnoswap/gns/gns_test.gno +++ b/_deploy/r/gnoswap/gns/gns_test.gno @@ -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 diff --git a/_deploy/r/gnoswap/gns/tests/z1_filetest.gno b/_deploy/r/gnoswap/gns/tests/z1_filetest.gno index c88626cf6..d98424197 100644 --- a/_deploy/r/gnoswap/gns/tests/z1_filetest.gno +++ b/_deploy/r/gnoswap/gns/tests/z1_filetest.gno @@ -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()) } @@ -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 } diff --git a/_deploy/r/gnoswap/gns/tests/z2_filetest.gno b/_deploy/r/gnoswap/gns/tests/z2_filetest.gno index 9a018bb77..c5ec13db1 100644 --- a/_deploy/r/gnoswap/gns/tests/z2_filetest.gno +++ b/_deploy/r/gnoswap/gns/tests/z2_filetest.gno @@ -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 diff --git a/_deploy/r/gnoswap/gns/tests/z3_filetest.gno b/_deploy/r/gnoswap/gns/tests/z3_filetest.gno index 532030f89..1f81f34f7 100644 --- a/_deploy/r/gnoswap/gns/tests/z3_filetest.gno +++ b/_deploy/r/gnoswap/gns/tests/z3_filetest.gno @@ -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)) @@ -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))