Skip to content

Commit

Permalink
feat: caller can burn token only they owned
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Sep 15, 2023
1 parent 71d85a1 commit 040fdd1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/gno.land/r/demo/realm_banker/gno.mod
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module gno.land/r/dmeo/realm_banker
module gno.land/r/demo/realm_banker
26 changes: 18 additions & 8 deletions examples/gno.land/r/demo/realm_banker/realm_banke_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestMint(t *testing.T) {
}

c0 := coins[0]
if c0.Denom != "realm/gno.land/r/dmeo/realm_banker/ugnot" {
if c0.Denom != "realm/gno.land/r/demo/realm_banker/ugnot" {
t.Errorf("expected denom ugnot, got %s", c0.Denom)
}

Expand All @@ -31,29 +31,39 @@ func TestMint(t *testing.T) {
}
}

func TestBurnOtherAddress(t *testing.T) {
// burn other address's coin => go panic will occur
// Burn(target, "ugnot", 1234) // how to catch go panic in gno ??
}

func TestBurn(t *testing.T) {
// burn 1 coin
Burn(target, "ugnot", 1)
caller := std.GetOrigCaller()
Mint(caller, "ugnot", 1234)

banker := std.GetBanker(std.BankerTypeRealmIssue)
coins := banker.GetCoins(target)
coins := banker.GetCoins(caller)

// burn 1 coin
Burn(caller, "ugnot", 1)

coins = banker.GetCoins(caller)

if len(coins) != 1 {
t.Errorf("expected 1 type coin(s), got %d", len(coins))
}

c0 := coins[0]
if c0.Denom != "realm/gno.land/r/dmeo/realm_banker/ugnot" {
t.Errorf("expected denom realm/gno.land/r/dmeo/realm_banker/ugnot, got %s", c0.Denom)
if c0.Denom != "realm/gno.land/r/demo/realm_banker/ugnot" {
t.Errorf("expected denom realm/gno.land/r/demo/realm_banker/ugnot, got %s", c0.Denom)
}

if c0.Amount != 1233 {
t.Errorf("expected amount 1233, got %d", c0.Amount)
}

// burn rest
Burn(target, "ugnot", 1233)
coins = banker.GetCoins(target)
Burn(caller, "ugnot", 1233)
coins = banker.GetCoins(caller)

if len(coins) != 0 {
t.Errorf("expected 0 type coin(s), got %d", len(coins))
Expand Down
12 changes: 9 additions & 3 deletions gnovm/stdlibs/banker.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ func (rsb RealmSendBanker) RemoveCoin(addr crypto.Bech32Address, denom string, a
type RealmIssueBanker struct {
banker Banker
pkgAddr crypto.Bech32Address
caller crypto.Bech32Address
realmPath string
}

func NewRealmIssueBanker(banker Banker, pkgAddr crypto.Bech32Address, realmPath string) RealmIssueBanker {
func NewRealmIssueBanker(banker Banker, pkgAddr, caller crypto.Bech32Address, realmPath string) RealmIssueBanker {
return RealmIssueBanker{
banker: banker,
pkgAddr: pkgAddr,
caller: caller,
realmPath: realmPath,
}
}
Expand All @@ -196,7 +198,7 @@ func (rib RealmIssueBanker) IssueCoin(addr crypto.Bech32Address, denom string, a
panic("no realm")
}

if !strings.HasPrefix(rib.realmPath, "gno.land/r") {
if !strings.HasPrefix(rib.realmPath, "gno.land/r/") {
panic("invalid realm path")
}

Expand All @@ -213,10 +215,14 @@ func (rib RealmIssueBanker) RemoveCoin(addr crypto.Bech32Address, denom string,
panic("no realm")
}

if !strings.HasPrefix(rib.realmPath, "gno.land/r") {
if !strings.HasPrefix(rib.realmPath, "gno.land/r/") {
panic("invalid realm path")
}

if rib.caller.String() != addr.String() {
panic("can't burn another address's coin")
}

denom = "realm/" + rib.realmPath + "/" + denom
rib.banker.RemoveCoin(addr, denom, amount)
}
2 changes: 1 addition & 1 deletion gnovm/stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
case BankerTypeRealmSend:
banker = NewRealmSendBanker(banker, ctx.OrigPkgAddr)
case BankerTypeRealmIssue:
banker = NewRealmIssueBanker(banker, ctx.OrigPkgAddr, m.Realm.Path)
banker = NewRealmIssueBanker(banker, ctx.OrigPkgAddr, ctx.OrigCaller, m.Realm.Path)
default:
panic("should not happen") // defensive
}
Expand Down

0 comments on commit 040fdd1

Please sign in to comment.