From f0bc61176e9193df8115ef27d46c6d91e57d713d Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Wed, 19 Jun 2024 19:20:06 +0200 Subject: [PATCH] feat: add r/demo/bar20 example (#2388) ```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 <94029+moul@users.noreply.github.com> --- examples/gno.land/r/demo/bar20/bar20.gno | 46 +++++++++++++++++++ examples/gno.land/r/demo/bar20/bar20_test.gno | 31 +++++++++++++ examples/gno.land/r/demo/bar20/gno.mod | 7 +++ 3 files changed, 84 insertions(+) create mode 100644 examples/gno.land/r/demo/bar20/bar20.gno create mode 100644 examples/gno.land/r/demo/bar20/bar20_test.gno create mode 100644 examples/gno.land/r/demo/bar20/gno.mod diff --git a/examples/gno.land/r/demo/bar20/bar20.gno b/examples/gno.land/r/demo/bar20/bar20.gno new file mode 100644 index 00000000000..7388d87d24d --- /dev/null +++ b/examples/gno.land/r/demo/bar20/bar20.gno @@ -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" + } +} diff --git a/examples/gno.land/r/demo/bar20/bar20_test.gno b/examples/gno.land/r/demo/bar20/bar20_test.gno new file mode 100644 index 00000000000..b2a49ebd864 --- /dev/null +++ b/examples/gno.land/r/demo/bar20/bar20_test.gno @@ -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) + } +} diff --git a/examples/gno.land/r/demo/bar20/gno.mod b/examples/gno.land/r/demo/bar20/gno.mod new file mode 100644 index 00000000000..d104f0b9987 --- /dev/null +++ b/examples/gno.land/r/demo/bar20/gno.mod @@ -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 +)