diff --git a/gnovm/stdlibs/std/banker.gno b/gnovm/stdlibs/std/banker.gno index 2b564288cdd..569e3a7c81b 100644 --- a/gnovm/stdlibs/std/banker.gno +++ b/gnovm/stdlibs/std/banker.gno @@ -1,6 +1,8 @@ package std import ( + "crypto/sha256" + "encoding/hex" istd "internal/std" ) @@ -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) } diff --git a/gnovm/stdlibs/std/banker_test.gno b/gnovm/stdlibs/std/banker_test.gno new file mode 100644 index 00000000000..3402c5d9b37 --- /dev/null +++ b/gnovm/stdlibs/std/banker_test.gno @@ -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) + } +} diff --git a/tm2/pkg/std/coin.go b/tm2/pkg/std/coin.go index d5eb9bb1da0..bb1d65deb75 100644 --- a/tm2/pkg/std/coin.go +++ b/tm2/pkg/std/coin.go @@ -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:]]*`