Skip to content

Commit

Permalink
use cosmos's format regexp for denom
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Jun 7, 2023
1 parent 6e1e324 commit 844c616
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
12 changes: 10 additions & 2 deletions gnovm/stdlibs/std/banker.gno
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package std

import (
"crypto/sha256"
"encoding/hex"
istd "internal/std"
)

Expand Down Expand Up @@ -67,12 +69,18 @@ func (ba bankAdapter) TotalCoin(denom string) int64 {

func (ba bankAdapter) IssueCoin(addr Address, denom string, amount int64) {
// XXX apply PrevRealm.PkgPath, https://github.com/gnolang/gno/pull/667
// denom shoulde `pkg_path@denom`

// Similar to ibc ==> "ibc/" + sha256('path' + 'base_denom')
// "realm/" + sha256('pkg_path' + 'base_denom')

// from := std.PrevRealm().PkgPath
from := istd.GetOrigCaller().String()
to_hash := from + "/" + denom

hash := sha256.Sum256([]byte(to_hash))[:]
hex_hash := hex.EncodeToString(hash)

denom = from + "@" + denom
denom = "realm/" + hex_hash
ba.nativeBanker.IssueCoin(addr, denom, amount)
}

Expand Down
47 changes: 47 additions & 0 deletions gnovm/stdlibs/std/banker_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package std

import (
"std"
"testing"

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

var (
denoms avl.Tree // id -> *minter
)

func Mint(addr std.Address, denom string, amount int64) {
caller := std.GetOrigCaller()
if denoms.Has(denom) {
data, _ := denoms.Get(denom)
minter := data.(std.Address)
if minter != caller {
panic("not minter")
}
} else {
denoms.Set(denom, caller)
}

issuer := std.GetBanker(std.BankerTypeRealmIssue)
issuer.IssueCoin(addr, denom, amount)
}

func TestMint(t *testing.T) {
who := testutils.TestAddress("who")
issuer := std.GetBanker(std.BankerTypeRealmIssue)

shouldEQ(t, len(issuer.GetCoins(who)), 0)

issuer.IssueCoin(who, "ugnot", 123)
shouldEQ(t, len(issuer.GetCoins(who)), 1)
shouldEQ(t, issuer.GetCoins(who)[0].Amount, 123)
shouldEQ(t, issuer.GetCoins(who)[0].Denom, "realm/dde02d16adbf1a4ff70e273c871d6de322b30075a0a8a0e7b6cd5a27f5189922")
}

func shouldEQ(t *testing.T, got, wanted interface{}) {
if got != wanted {
t.Errorf("got %v(%T), wanted %v(%T)", got, got, wanted, wanted)
}
}
3 changes: 2 additions & 1 deletion tm2/pkg/std/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ func (coins Coins) Sort() Coins {

var (
// Denominations can be 3 ~ 16 characters long.
reDnmString = `[a-z][a-z0-9]{2,15}`
// reDnmString = `[a-z][a-z0-9]{2,15}` // Gno
reDnmString = `[a-zA-Z][a-zA-Z0-9@/:._-]{2,127}` // Cosmos with '@'
reAmt = `[[:digit:]]+`
reDecAmt = `[[:digit:]]*\.[[:digit:]]+`
reSpc = `[[:space:]]*`
Expand Down

0 comments on commit 844c616

Please sign in to comment.