-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |