Skip to content

Commit

Permalink
chore: fixup
Browse files Browse the repository at this point in the history
Signed-off-by: moul <[email protected]>
  • Loading branch information
moul committed Nov 16, 2024
1 parent 5c152d2 commit b22cd05
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
24 changes: 13 additions & 11 deletions examples/gno.land/r/demo/grc20reg/grc20reg.gno
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ import (

var registry = avl.NewTree() // rlmPath[.slug] -> TokenGetter

func Register(token grc20.Token, slug string) {
func Register(tokenGetter grc20.TokenGetter, slug string) {
rlmPath := std.PrevRealm().PkgPath()
key := fqname.Construct(rlmPath, slug)
registry.Set(key, token)
registry.Set(key, tokenGetter)
std.Emit(
registerEvent,
"pkgpath", rlmPath,
"slug", slug,
)
}

func Get(key string) grc20.Token {
token, ok := registry.Get(key)
func Get(key string) grc20.TokenGetter {
tokenGetter, ok := registry.Get(key)
if !ok {
return nil
}
return token.(grc20.Token)
return tokenGetter.(grc20.TokenGetter)
}

func MustGet(key string) grc20.Token {
token := Get(key)
if token == nil {
func MustGet(key string) grc20.TokenGetter {
tokenGetter := Get(key)
if tokenGetter == nil {
panic("unknown token: " + key)
}
return token
return tokenGetter
}

func Render(path string) string {
Expand All @@ -46,7 +46,8 @@ func Render(path string) string {
count := 0
registry.Iterate("", "", func(key string, tokenI interface{}) bool {
count++
token := tokenI.(grc20.Token)
tokenGetter := tokenI.(grc20.TokenGetter)
token := tokenGetter()
rlmPath, slug := fqname.Parse(key)
rlmLink := fqname.RenderLink(rlmPath, slug)
infoLink := "/r/demo/grc20reg:" + key
Expand All @@ -59,7 +60,8 @@ func Render(path string) string {
return s
default: // specific token
key := path
token := MustGet(key)
tokenGetter := MustGet(key)
token := tokenGetter()
rlmPath, slug := fqname.Parse(key)
rlmLink := fqname.RenderLink(rlmPath, slug)
s := ufmt.Sprintf("# %s\n", token.GetName())
Expand Down
39 changes: 13 additions & 26 deletions examples/gno.land/r/demo/grc20reg/grc20reg_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
func TestRegistry(t *testing.T) {
std.TestSetRealm(std.NewCodeRealm("gno.land/r/demo/foo"))
realmAddr := std.CurrentRealm().PkgPath()
tokenImpl := &dummyImpl{}

token, ledger := grc20.NewToken("TestToken", "TST", 4)
ledger.Mint(std.CurrentRealm().Addr(), 1234567)
tokenGetter := func() *grc20.Token { return token }
// register
Register(tokenImpl, "")
regToken := Get(realmAddr)
Register(tokenGetter, "")
regTokenGetter := Get(realmAddr)
regToken := regTokenGetter()
urequire.True(t, regToken != nil, "expected to find a token") // fixme: use urequire.NotNil
urequire.Equal(t, regToken.GetSymbol(), "TST")

Expand All @@ -29,14 +31,16 @@ func TestRegistry(t *testing.T) {
urequire.True(t, invalidToken == nil)

// register with a slug
Register(tokenImpl, "mySlug")
regToken = Get(realmAddr + ".mySlug")
Register(tokenGetter, "mySlug")
regTokenGetter = Get(realmAddr + ".mySlug")
regToken = regTokenGetter()
urequire.True(t, regToken != nil, "expected to find a token") // fixme: use urequire.NotNil
urequire.Equal(t, regToken.GetSymbol(), "TST")

// override
Register(tokenImpl, "")
regToken = Get(realmAddr + "")
Register(tokenGetter, "")
regTokenGetter = Get(realmAddr + "")
regToken = regTokenGetter()
urequire.True(t, regToken != nil, "expected to find a token") // fixme: use urequire.NotNil
urequire.Equal(t, regToken.GetSymbol(), "TST")

Expand All @@ -49,26 +53,9 @@ func TestRegistry(t *testing.T) {
expected = `# TestToken
- symbol: **TST**
- realm: [gno.land/r/demo/foo](/r/demo/foo).mySlug
- decimals: 1337
- decimals: 4
- total supply: 1234567
`
got = Render("gno.land/r/demo/foo.mySlug")
urequire.Equal(t, expected, got)
}

type dummyImpl struct{}

// FIXME: this should fail.
var _ grc20.Token = (*dummyImpl)(nil)

func (impl *dummyImpl) GetName() string { return "TestToken" }
func (impl *dummyImpl) GetSymbol() string { return "TST" }
func (impl *dummyImpl) GetDecimals() uint { return 1337 }
func (impl *dummyImpl) TotalSupply() uint64 { return 1234567 }
func (impl *dummyImpl) BalanceOf(account std.Address) uint64 { panic("not implemented") }
func (impl *dummyImpl) Transfer(to std.Address, amount uint64) error { panic("not implemented") }
func (impl *dummyImpl) Allowance(owner, spender std.Address) uint64 { panic("not implemented") }
func (impl *dummyImpl) Approve(spender std.Address, amount uint64) error { panic("not implemented") }
func (impl *dummyImpl) TransferFrom(from, to std.Address, amount uint64) error {
panic("not implemented")
}

0 comments on commit b22cd05

Please sign in to comment.