Skip to content

Commit

Permalink
feat: add r/demo/bar20 example (#2388)
Browse files Browse the repository at this point in the history
```go
// Package bar20 is similar to foo20 but exposes a safe-object that can be used
// by `maketx run`, another contract importing foo20, and in the future when
// we'll support `maketx call Token.XXX`.
```

This package currently has limited functionality, but it should become
more useful in the future.

I'm using it to demonstrate the rationale for having two implementations
in the `grc20` package - one with a banker, and one with a safe object.

Related with #2314

---------

Signed-off-by: moul <[email protected]>
  • Loading branch information
moul authored Jun 19, 2024
1 parent 028a4ed commit f0bc611
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
46 changes: 46 additions & 0 deletions examples/gno.land/r/demo/bar20/bar20.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Package bar20 is similar to foo20 but exposes a safe-object that can be used
// by `maketx run`, another contract importing foo20, and in the future when
// we'll support `maketx call Token.XXX`.
package bar20

import (
"std"
"strings"

"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ufmt"
)

var (
banker *grc20.AdminToken // private banker.
Token grc20.IGRC20 // public safe-object.
)

func init() {
banker = grc20.NewAdminToken("Bar", "BAR", 4)
Token = banker.GRC20()
}

func Faucet() string {
caller := std.PrevRealm().Addr()
if err := banker.Mint(caller, 1_000_000); err != nil {
return "error: " + err.Error()
}
return "OK"
}

func Render(path string) string {
parts := strings.Split(path, "/")
c := len(parts)

switch {
case path == "":
return banker.RenderHome() // XXX: should be Token.RenderHome()
case c == 2 && parts[0] == "balance":
owner := std.Address(parts[1])
balance, _ := Token.BalanceOf(owner)
return ufmt.Sprintf("%d\n", balance)
default:
return "404\n"
}
}
31 changes: 31 additions & 0 deletions examples/gno.land/r/demo/bar20/bar20_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package bar20

import (
"std"
"testing"

"gno.land/p/demo/testutils"
)

func TestPackage(t *testing.T) {
alice := testutils.TestAddress("alice")
std.TestSetRealm(std.NewUserRealm(alice))
std.TestSetOrigCaller(alice) // XXX: should not need this

balance, _ := Token.BalanceOf(alice)
expected := uint64(0)
if balance != expected {
t.Errorf("balance should be %d, got %d", expected, balance)
}

ret := Faucet()
if ret != "OK" {
t.Errorf("faucet should be OK, got %s", ret)
}

balance, _ = Token.BalanceOf(alice)
expected = uint64(1_000_000)
if balance != expected {
t.Errorf("balance should be %d, got %d", expected, balance)
}
}
7 changes: 7 additions & 0 deletions examples/gno.land/r/demo/bar20/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module bar20

require (
gno.land/p/demo/grc/grc20 v0.0.0-latest
gno.land/p/demo/testutils v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
)

0 comments on commit f0bc611

Please sign in to comment.