-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: grc20 registry #1072
feat: grc20 registry #1072
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module gno.land/r/demo/grc20_registry | ||
|
||
require ( | ||
"gno.land/p/demo/avl" v0.0.0-latest | ||
"gno.land/p/demo/grc/grc20" v0.0.0-latest | ||
) |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,44 @@ | ||||||||||
package grc20_registry | ||||||||||
|
||||||||||
import ( | ||||||||||
"std" | ||||||||||
"strings" | ||||||||||
|
||||||||||
"gno.land/p/demo/avl" | ||||||||||
"gno.land/p/demo/grc/grc20" | ||||||||||
) | ||||||||||
|
||||||||||
var registry = avl.NewTree() // pkg path -> IGRC20 | ||||||||||
|
||||||||||
func Register(token grc20.IGRC20) { | ||||||||||
caller := std.PrevRealm().PkgPath() | ||||||||||
registry.Set(caller, token) | ||||||||||
} | ||||||||||
|
||||||||||
func Get(pkgPath string) (grc20.IGRC20, bool) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
bool is not needed, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
coinI, ok := registry.Get(pkgPath) | ||||||||||
if !ok { | ||||||||||
return nil, false | ||||||||||
} | ||||||||||
coin, ok := coinI.(grc20.IGRC20) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this check is unnecessary, protected by |
||||||||||
if !ok { | ||||||||||
panic("internal error: registered object is not a GRC20 token") | ||||||||||
} | ||||||||||
return coin, true | ||||||||||
} | ||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a |
||||||||||
func Render(path string) string { | ||||||||||
s := "# GRC20 Registry\n\n" + | ||||||||||
"## Registered Tokens\n\n" | ||||||||||
Comment on lines
+31
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
concise output. |
||||||||||
registry.Iterate("", "", func(pkgPath string, tokenI interface{}) bool { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a todo comment to add pagination. |
||||||||||
token, ok := tokenI.(grc20.IGRC20) | ||||||||||
pkgWebPath := strings.TrimPrefix(pkgPath, "gno.land") | ||||||||||
if ok { | ||||||||||
s += "- [" + token.GetName() + " (" + pkgPath + ")](" + pkgWebPath + ")\n" | ||||||||||
} else { | ||||||||||
s += "- [internal error: registered object is not a GRC20 token (" + pkgPath + ")](" + pkgWebPath + ")\n" | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this check. |
||||||||||
} | ||||||||||
return false | ||||||||||
}) | ||||||||||
return s | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package grc20_registry | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
|
||
"gno.land/p/demo/grc/grc20" | ||
) | ||
|
||
func TestRegistry(t *testing.T) { | ||
coin := &dummyImpl{} | ||
realmAddr := std.CurrentRealm().PkgPath() | ||
Register(coin) | ||
regCoin, ok := Get(realmAddr) | ||
if !ok { | ||
t.Fatal("expected to find coin") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can now use |
||
} | ||
if coin.GetSymbol() != "TST" { | ||
t.Fatal("expected coin to have symbol TST") | ||
} | ||
expected := `# GRC20 Registry | ||
|
||
## Registered Tokens | ||
|
||
* [TestToken ()]() | ||
` | ||
got := Render("") | ||
if got != expected { | ||
t.Fatalf("expected `%s`, got `%s`", expected, got) | ||
} | ||
|
||
// we test this here because there is more chance to find a bug after a token has been registered | ||
if _, ok := Get("0xdeadbeef"); ok { | ||
t.Fatal("expected not to find coin") | ||
} | ||
} | ||
|
||
type dummyImpl struct{} | ||
|
||
// FIXME: this should fail. | ||
var _ grc20.IGRC20 = (*dummyImpl)(nil) | ||
|
||
func (impl *dummyImpl) GetName() string { return "TestToken" } | ||
func (impl *dummyImpl) GetSymbol() string { return "TST" } | ||
func (impl *dummyImpl) GetDecimals() uint { panic("not implemented") } | ||
func (impl *dummyImpl) TotalSupply() uint64 { panic("not implemented") } | ||
func (impl *dummyImpl) BalanceOf(account std.Address) (uint64, error) { 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, error) { | ||
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") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To support registering several tokens from the same contract.