From ae3c8d68b0dac3836ae26e70a5c9c62f505640a9 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 3 Jul 2024 16:42:50 +0900 Subject: [PATCH 1/2] GSW-1144 feat: emission contract that calls gns mint --- .../r/demo/emission/_TEST_emission_test.gno | 69 +++++++++++++++++++ _deploy/r/demo/emission/emission.gno | 19 +++++ _deploy/r/demo/gns_halving/gns.gno | 9 +++ 3 files changed, 97 insertions(+) create mode 100644 _deploy/r/demo/emission/_TEST_emission_test.gno create mode 100644 _deploy/r/demo/emission/emission.gno diff --git a/_deploy/r/demo/emission/_TEST_emission_test.gno b/_deploy/r/demo/emission/_TEST_emission_test.gno new file mode 100644 index 00000000..3849f95f --- /dev/null +++ b/_deploy/r/demo/emission/_TEST_emission_test.gno @@ -0,0 +1,69 @@ +package emission + +import ( + "std" + "testing" + + pusers "gno.land/p/demo/users" + + "gno.land/r/demo/gns" +) + +func TestEmitGns(t *testing.T) { + shouldEQ(t, gns.TotalSupply(), 100000000000000) // GSA has + shouldEQ(t, gnsBalance(emissionAddr), 0) + + EmitGns() // 1 ~ 123 height + + shouldEQ(t, gnsBalance(emissionAddr), 4387842345) + shouldEQ(t, gns.TotalSupply(), 100000000000000+4387842345) + + shouldEQ(t, std.GetHeight(), 123) +} + +func TestEmitGnsSameBlock(t *testing.T) { + // request mint again in same block => do not mint again + // it may happen because single block can have multiple txs & msgs + EmitGns() + shouldEQ(t, gns.TotalSupply(), 100000000000000+4387842345) +} + +func TestEmitGnsAllAmount(t *testing.T) { + std.TestSkipHeights(75686400 - 1) + gns.TestSetLastMintedHeight(std.GetHeight()) + std.TestSkipHeights(1) + + EmitGns() + shouldEQ(t, gns.TotalSupply(), 100000000000000+4387842345+2229594) + // all emission duratino has been passed + + std.TestSkipHeights(1) + EmitGns() // since all emission has been done, no more minting + shouldEQ(t, gns.TotalSupply(), 100000000000000+4387842345+2229594) +} + +// UTILs +func gnsBalance(addr std.Address) uint64 { + a2u := pusers.AddressOrName(addr) + + return gns.BalanceOf(a2u) +} + +func shouldEQ(t *testing.T, got, expected interface{}) { + if got != expected { + t.Errorf("got %v, expected %v", got, expected) + } +} + +func shouldPanicWithMsg(t *testing.T, f func(), msg string) { + defer func() { + if r := recover(); r == nil { + t.Errorf("The code did not panic") + } else { + if r != msg { + t.Errorf("excepted panic(%v), got(%v)", msg, r) + } + } + }() + f() +} diff --git a/_deploy/r/demo/emission/emission.gno b/_deploy/r/demo/emission/emission.gno new file mode 100644 index 00000000..ac612431 --- /dev/null +++ b/_deploy/r/demo/emission/emission.gno @@ -0,0 +1,19 @@ +package emission + +import ( + "std" + + pusers "gno.land/p/demo/users" + "gno.land/r/demo/gnoswap/consts" + "gno.land/r/demo/gns" +) + +var emissionAddr std.Address = consts.EMISSION_ADDR + +func EmitGns() { + gns.Mint(a2u(emissionAddr)) +} + +func a2u(addr std.Address) pusers.AddressOrName { + return pusers.AddressOrName(addr) +} diff --git a/_deploy/r/demo/gns_halving/gns.gno b/_deploy/r/demo/gns_halving/gns.gno index 3753c96d..fecf52c0 100644 --- a/_deploy/r/demo/gns_halving/gns.gno +++ b/_deploy/r/demo/gns_halving/gns.gno @@ -120,3 +120,12 @@ func Mint(address pusers.AddressOrName) { // println("height:", i, "minted:", amount) } } + +// ONLY FOR EMISSION TESTING, REMOVE THIS FUNCTION IN PRODUCTION +func TestSetLastMintedHeight(height int64) { + caller := std.PrevRealm().Addr() + if caller != consts.EMISSION_ADDR { + panic("only emission contract can call TestSetLastMintedHeight") + } + lastMintedHeight = height +} From 92eedc4d4a3245e6a9376a75bac78d54e9a5f389 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 3 Jul 2024 20:28:30 +0900 Subject: [PATCH 2/2] docs: comments --- _deploy/r/demo/emission/emission.gno | 2 ++ _deploy/r/demo/gns_halving/gns.gno | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/_deploy/r/demo/emission/emission.gno b/_deploy/r/demo/emission/emission.gno index ac612431..9b843b70 100644 --- a/_deploy/r/demo/emission/emission.gno +++ b/_deploy/r/demo/emission/emission.gno @@ -8,8 +8,10 @@ import ( "gno.land/r/demo/gns" ) +// emissionAddr is the address from which GNS tokens will be emitted. var emissionAddr std.Address = consts.EMISSION_ADDR +// EmitGns mints new GNS tokens to the emission address. func EmitGns() { gns.Mint(a2u(emissionAddr)) } diff --git a/_deploy/r/demo/gns_halving/gns.gno b/_deploy/r/demo/gns_halving/gns.gno index fecf52c0..ddd9ec1a 100644 --- a/_deploy/r/demo/gns_halving/gns.gno +++ b/_deploy/r/demo/gns_halving/gns.gno @@ -121,7 +121,8 @@ func Mint(address pusers.AddressOrName) { } } -// ONLY FOR EMISSION TESTING, REMOVE THIS FUNCTION IN PRODUCTION +// XXX: Remove this +// ONLY FOR EMISSION TESTING func TestSetLastMintedHeight(height int64) { caller := std.PrevRealm().Addr() if caller != consts.EMISSION_ADDR {