Skip to content

Commit

Permalink
feat: foo20 check an error from grc20 package (#977)
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s authored Jul 20, 2023
1 parent 973be98 commit 346359e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
30 changes: 24 additions & 6 deletions examples/gno.land/r/demo/foo20/foo20.gno
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,26 @@ func Allowance(owner, spender users.AddressOrName) uint64 {

func Transfer(to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
foo.Transfer(caller, to.Resolve(), amount)
err := foo.Transfer(caller, to.Resolve(), amount)
if err != nil {
panic(err)
}
}

func Approve(spender users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
foo.Approve(caller, spender.Resolve(), amount)
err := foo.Approve(caller, spender.Resolve(), amount)
if err != nil {
panic(err)
}
}

func TransferFrom(from, to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
foo.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
err := foo.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
if err != nil {
panic(err)
}
}

// faucet.
Expand All @@ -68,21 +77,30 @@ func Faucet() {
// FIXME: add limits?
// FIXME: add payment in gnot?
caller := std.PrevRealm().Addr()
foo.Mint(caller, 1000*10000) // 1k
err := foo.Mint(caller, 1000*10000) // 1k
if err != nil {
panic(err)
}
}

// administration.

func Mint(address users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
foo.Mint(address.Resolve(), amount)
err := foo.Mint(address.Resolve(), amount)
if err != nil {
panic(err)
}
}

func Burn(address users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
foo.Burn(address.Resolve(), amount)
err := foo.Burn(address.Resolve(), amount)
if err != nil {
panic(err)
}
}

// render.
Expand Down
36 changes: 36 additions & 0 deletions examples/gno.land/r/demo/foo20/foo20_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,39 @@ func TestReadOnlyPublicMethods(t *testing.T) {
}
}
}

func TestErrConditions(t *testing.T) {
admin := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
empty := std.Address("")

type test struct {
name string
msg string
fn func()
}

std.TestSetOrigCaller(admin)
{
tests := []test{
{"Transfer(admin, 1)", "cannot send transfer to self", func() { Transfer(users.AddressOrName(admin), 1) }},
{"Approve(empty, 1))", "invalid address", func() { Approve(users.AddressOrName(empty), 1) }},
}
for _, tc := range tests {
shouldPanicWithMsg(t, tc.fn, tc.msg)
}
}
}

func shouldPanicWithMsg(t *testing.T, f func(), msg string) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
} else {
errMsg := error(r).Error()
if errMsg != msg {
t.Errorf("excepted panic(%v), got(%v)", msg, errMsg)
}
}
}()
f()
}

0 comments on commit 346359e

Please sign in to comment.