Skip to content

Commit

Permalink
chore: add r/demo/tests/test20 + add grc20.TokenGetter to reduce boil…
Browse files Browse the repository at this point in the history
…erplate when using grc20reg (#3397)

- add `r/demo/tests/test20`
- add `grc20.TokenGetter` to reduce boilerplate when using
`grc20reg.Register(...)`

Replaces #2549
Continues #3135

Signed-off-by: moul <[email protected]>
  • Loading branch information
moul authored Dec 28, 2024
1 parent b6a4aab commit 597f3f5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
8 changes: 8 additions & 0 deletions examples/gno.land/p/demo/grc/grc20/token.gno
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ func (tok *Token) RenderHome() string {
return str
}

// Getter returns a TokenGetter function that returns this token. This allows
// storing indirect pointers to a token in a remote realm.
func (tok *Token) Getter() TokenGetter {
return func() *Token {
return tok
}
}

// SpendAllowance decreases the allowance of the specified owner and spender.
func (led *PrivateLedger) SpendAllowance(owner, spender std.Address, amount uint64) error {
if !owner.IsValid() {
Expand Down
3 changes: 1 addition & 2 deletions examples/gno.land/r/demo/bar20/bar20.gno
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ var (
)

func init() {
getter := func() *grc20.Token { return Token }
grc20reg.Register(getter, "")
grc20reg.Register(Token.Getter(), "")
}

func Faucet() string {
Expand Down
3 changes: 1 addition & 2 deletions examples/gno.land/r/demo/foo20/foo20.gno
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ var (

func init() {
privateLedger.Mint(Ownable.Owner(), 1_000_000*10_000) // @privateLedgeristrator (1M)
getter := func() *grc20.Token { return Token }
grc20reg.Register(getter, "")
grc20reg.Register(Token.Getter(), "")
}

func TotalSupply() uint64 {
Expand Down
3 changes: 1 addition & 2 deletions examples/gno.land/r/demo/grc20factory/grc20factory.gno
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ func NewWithAdmin(name, symbol string, decimals uint, initialMint, faucet uint64
faucet: faucet,
}
instances.Set(symbol, &inst)
getter := func() *grc20.Token { return token }
grc20reg.Register(getter, symbol)
grc20reg.Register(token.Getter(), symbol)
}

func (inst instance) Token() *grc20.Token {
Expand Down
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/tests/test20/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/demo/tests/test20
20 changes: 20 additions & 0 deletions examples/gno.land/r/demo/tests/test20/test20.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Package test20 implements a deliberately insecure ERC20 token for testing purposes.
// The Test20 token allows anyone to mint any amount of tokens to any address, making
// it unsuitable for production use. The primary goal of this package is to facilitate
// testing and experimentation without any security measures or restrictions.
//
// WARNING: This token is highly insecure and should not be used in any
// production environment. It is intended solely for testing and
// educational purposes.
package test20

import (
"gno.land/p/demo/grc/grc20"
"gno.land/r/demo/grc20reg"
)

var Token, PrivateLedger = grc20.NewToken("Test20", "TST", 4)

func init() {
grc20reg.Register(Token.Getter(), "")
}
3 changes: 1 addition & 2 deletions examples/gno.land/r/demo/wugnot/wugnot.gno
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const (
)

func init() {
getter := func() *grc20.Token { return Token }
grc20reg.Register(getter, "")
grc20reg.Register(Token.Getter(), "")
}

func Deposit() {
Expand Down

0 comments on commit 597f3f5

Please sign in to comment.