-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add grc20reg that works... today (#3135)
- [x] Switch to storing a `type XXX func() grc20.Token` instead of a `grc20.Token` directly. - [x] Implement `grc20reg`. - [x] Add new tests in `gnovm/tests` to demonstrate the current VM's management of the cross-realm feature and support potential changes in #2743. - [x] Create a demo in `atomicswap` or a similar application. (#2510 (comment)) - [x] Try using a `Token.Getter()` helper. (Works! f99654e) - [ ] Demonstrate how to manage "disappearing" functions during garbage collection by checking if the function pointer is nil or non-resolvable. Alternative to #2516 NOT(!) depending on #2743 --------- Signed-off-by: moul <94029+moul@users.noreply.github.com>
- Loading branch information
Showing
29 changed files
with
3,495 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module gno.land/r/demo/grc20reg | ||
|
||
require ( | ||
gno.land/p/demo/avl v0.0.0-latest | ||
gno.land/p/demo/fqname v0.0.0-latest | ||
gno.land/p/demo/grc/grc20 v0.0.0-latest | ||
gno.land/p/demo/ufmt v0.0.0-latest | ||
gno.land/p/demo/urequire v0.0.0-latest | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package grc20reg | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/fqname" | ||
"gno.land/p/demo/grc/grc20" | ||
"gno.land/p/demo/ufmt" | ||
) | ||
|
||
var registry = avl.NewTree() // rlmPath[.slug] -> TokenGetter (slug is optional) | ||
|
||
func Register(tokenGetter grc20.TokenGetter, slug string) { | ||
rlmPath := std.PrevRealm().PkgPath() | ||
key := fqname.Construct(rlmPath, slug) | ||
registry.Set(key, tokenGetter) | ||
std.Emit( | ||
registerEvent, | ||
"pkgpath", rlmPath, | ||
"slug", slug, | ||
) | ||
} | ||
|
||
func Get(key string) grc20.TokenGetter { | ||
tokenGetter, ok := registry.Get(key) | ||
if !ok { | ||
return nil | ||
} | ||
return tokenGetter.(grc20.TokenGetter) | ||
} | ||
|
||
func MustGet(key string) grc20.TokenGetter { | ||
tokenGetter := Get(key) | ||
if tokenGetter == nil { | ||
panic("unknown token: " + key) | ||
} | ||
return tokenGetter | ||
} | ||
|
||
func Render(path string) string { | ||
switch { | ||
case path == "": // home | ||
// TODO: add pagination | ||
s := "" | ||
count := 0 | ||
registry.Iterate("", "", func(key string, tokenI interface{}) bool { | ||
count++ | ||
tokenGetter := tokenI.(grc20.TokenGetter) | ||
token := tokenGetter() | ||
rlmPath, slug := fqname.Parse(key) | ||
rlmLink := fqname.RenderLink(rlmPath, slug) | ||
infoLink := "/r/demo/grc20reg:" + key | ||
s += ufmt.Sprintf("- **%s** - %s - [info](%s)\n", token.GetName(), rlmLink, infoLink) | ||
return false | ||
}) | ||
if count == 0 { | ||
return "No registered token." | ||
} | ||
return s | ||
default: // specific token | ||
key := path | ||
tokenGetter := MustGet(key) | ||
token := tokenGetter() | ||
rlmPath, slug := fqname.Parse(key) | ||
rlmLink := fqname.RenderLink(rlmPath, slug) | ||
s := ufmt.Sprintf("# %s\n", token.GetName()) | ||
s += ufmt.Sprintf("- symbol: **%s**\n", token.GetSymbol()) | ||
s += ufmt.Sprintf("- realm: %s\n", rlmLink) | ||
s += ufmt.Sprintf("- decimals: %d\n", token.GetDecimals()) | ||
s += ufmt.Sprintf("- total supply: %d\n", token.TotalSupply()) | ||
return s | ||
} | ||
} | ||
|
||
const registerEvent = "register" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package grc20reg | ||
|
||
import ( | ||
"std" | ||
"strings" | ||
"testing" | ||
|
||
"gno.land/p/demo/grc/grc20" | ||
"gno.land/p/demo/urequire" | ||
) | ||
|
||
func TestRegistry(t *testing.T) { | ||
std.TestSetRealm(std.NewCodeRealm("gno.land/r/demo/foo")) | ||
realmAddr := std.CurrentRealm().PkgPath() | ||
token, ledger := grc20.NewToken("TestToken", "TST", 4) | ||
ledger.Mint(std.CurrentRealm().Addr(), 1234567) | ||
tokenGetter := func() *grc20.Token { return token } | ||
// register | ||
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") | ||
|
||
expected := `- **TestToken** - [gno.land/r/demo/foo](/r/demo/foo) - [info](/r/demo/grc20reg:gno.land/r/demo/foo) | ||
` | ||
got := Render("") | ||
urequire.True(t, strings.Contains(got, expected)) | ||
// 404 | ||
invalidToken := Get("0xdeadbeef") | ||
urequire.True(t, invalidToken == nil) | ||
|
||
// register with a slug | ||
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(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") | ||
|
||
got = Render("") | ||
urequire.True(t, strings.Contains(got, `- **TestToken** - [gno.land/r/demo/foo](/r/demo/foo) - [info](/r/demo/grc20reg:gno.land/r/demo/foo)`)) | ||
urequire.True(t, strings.Contains(got, `- **TestToken** - [gno.land/r/demo/foo](/r/demo/foo).mySlug - [info](/r/demo/grc20reg:gno.land/r/demo/foo.mySlug)`)) | ||
|
||
expected = `# TestToken | ||
- symbol: **TST** | ||
- realm: [gno.land/r/demo/foo](/r/demo/foo).mySlug | ||
- decimals: 4 | ||
- total supply: 1234567 | ||
` | ||
got = Render("gno.land/r/demo/foo.mySlug") | ||
urequire.Equal(t, expected, got) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
examples/gno.land/r/demo/tests/crossrealm_b/crossrealm.gno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package crossrealm_b | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/r/demo/tests/crossrealm" | ||
) | ||
|
||
type fooer struct { | ||
s string | ||
} | ||
|
||
func (f *fooer) SetS(newVal string) { | ||
f.s = newVal | ||
} | ||
|
||
func (f *fooer) Foo() { | ||
println("hello " + f.s + " cur=" + std.CurrentRealm().PkgPath() + " prev=" + std.PrevRealm().PkgPath()) | ||
} | ||
|
||
var ( | ||
Fooer = &fooer{s: "A"} | ||
FooerGetter = func() crossrealm.Fooer { return Fooer } | ||
FooerGetterBuilder = func() crossrealm.FooerGetter { return func() crossrealm.Fooer { return Fooer } } | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module gno.land/r/demo/tests/crossrealm_b | ||
|
||
require gno.land/r/demo/tests/crossrealm v0.0.0-latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.