Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSW-1144 feat: emission contract that calls gns mint #248

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions _deploy/r/demo/emission/_TEST_emission_test.gno
Original file line number Diff line number Diff line change
@@ -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()
}
19 changes: 19 additions & 0 deletions _deploy/r/demo/emission/emission.gno
Original file line number Diff line number Diff line change
@@ -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"
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// emissionAddr is the address from which GNS tokens will be emitted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var emissionAddr std.Address = consts.EMISSION_ADDR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// EmitGns mints new GNS tokens to the emission address.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func EmitGns() {
gns.Mint(a2u(emissionAddr))
}

func a2u(addr std.Address) pusers.AddressOrName {
return pusers.AddressOrName(addr)
}
9 changes: 9 additions & 0 deletions _deploy/r/demo/gns_halving/gns.gno
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,12 @@ func Mint(address pusers.AddressOrName) {
// println("height:", i, "minted:", amount)
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XXX: Remove this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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
}