Skip to content
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: add r/grc20reg #2516

Draft
wants to merge 71 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
8a5ecbc
feat: grc20 registry
n0izn0iz Aug 25, 2023
9c5fcde
initial commit of receiver implied realm for pkg methods
jaekwon Oct 19, 2023
a9bee16
deref receiver before checking if object
jaekwon Oct 19, 2023
bc47c25
...
jaekwon Oct 19, 2023
ec4d717
...
jaekwon Oct 19, 2023
75b1d10
add realmtest
jaekwon Oct 19, 2023
86235c7
Merge remote-tracking branch 'origin/master' into grc20_registry
moul Jul 5, 2024
60162b9
feat: add p/demo/fqname
moul Jul 5, 2024
621a877
feat: add fqname.RenderLink
moul Jul 5, 2024
e545bfb
chore: refactor grc20_registry
moul Jul 5, 2024
cdbe6da
chore: rename grc20reg
moul Jul 5, 2024
a65537b
chore: emit event on registration
moul Jul 5, 2024
e8668f0
chore: fixup
moul Jul 5, 2024
e1a79fe
Merge remote-tracking branch 'origin/dev/jae/boundmethodrealm' into d…
moul Jul 5, 2024
3f83d01
chore: fixup
moul Jul 5, 2024
10719e7
Revert "Merge remote-tracking branch 'origin/dev/jae/boundmethodrealm…
moul Jul 5, 2024
b8a8282
override pv and rlm if method receiver has one
jaekwon Jul 5, 2024
1a86dbb
chore: fixup
moul Jul 5, 2024
23f7173
add tests
jaekwon Jul 5, 2024
b0397ed
Merge branch 'master' into dev/jae/boundmethodrealm
jaekwon Jul 5, 2024
d0cacdd
Merge remote-tracking branch 'origin/dev/jae/boundmethodrealm' into d…
moul Jul 5, 2024
b1254a7
chore: add missing gno.mod files
moul Jul 5, 2024
03c520f
Merge remote-tracking branch 'origin/dev/jae/boundmethodrealm' into d…
moul Jul 5, 2024
d7508a0
chore: fixup
moul Jul 5, 2024
752d9e7
chore: fixup
moul Jul 5, 2024
0bf2937
chore: fixup
moul Jul 5, 2024
9536cae
chore: fixup
moul Jul 5, 2024
61a6e39
chore: fixup
moul Jul 5, 2024
10a5456
chore: fixup
moul Jul 6, 2024
6a41a93
resave after init
jaekwon Jul 6, 2024
dbe4655
Merge remote-tracking branch 'origin/dev/jae/boundmethodrealm' into d…
moul Jul 6, 2024
cfef0ee
Merge branch 'master' into dev/moul/grc20reg
moul Jul 6, 2024
b1dfa2d
Merge branch 'master' into dev/moul/grc20reg
moul Jul 6, 2024
abb8b1e
Merge branch 'master' into dev/moul/grc20reg
moul Jul 6, 2024
a4a6ea0
Merge branch 'master' into dev/moul/grc20reg
moul Jul 6, 2024
4b3dabe
chore(examples): make lint -> verbose
moul Jul 6, 2024
cc07f46
Merge branch 'master' into dev/moul/grc20reg
moul Jul 6, 2024
bdb44bd
test(gnovm): add test for bounding issue
moul Jul 6, 2024
7cba9ca
chore: fixup
moul Jul 6, 2024
4ba7490
chore: fixup
moul Jul 6, 2024
621d599
chore: fixup
moul Jul 6, 2024
04bcd67
Merge branch 'master' into dev/moul/boundbug
moul Jul 6, 2024
b0a1e1f
allow passing in an object about to be persisted in the previous realm
jaekwon Jul 6, 2024
7445ae0
Merge remote-tracking branch 'origin/dev/jae/crossrealm' into dev/mou…
moul Jul 7, 2024
48f564e
Merge remote-tracking branch 'origin/master' into dev/moul/grc20reg
moul Jul 7, 2024
795f12f
Merge remote-tracking branch 'origin/master' into dev/moul/boundbug
moul Jul 7, 2024
4a2d67d
Merge remote-tracking branch 'origin/dev/jae/crossrealm' into dev/mou…
moul Jul 7, 2024
cbf2753
remove spurious lines
jaekwon Jul 7, 2024
5f647fb
fix for all cases
jaekwon Jul 7, 2024
bd889f7
reset new escape for both cases
jaekwon Jul 7, 2024
2a90a3d
Merge remote-tracking branch 'origin/dev/jae/crossrealm' into dev/mou…
moul Jul 7, 2024
00a2779
optimized fix
jaekwon Jul 7, 2024
ff894be
Merge remote-tracking branch 'origin/dev/jae/crossrealm' into dev/mou…
moul Jul 7, 2024
9aa33e4
Merge remote-tracking branch 'origin/dev/jae/crossrealm' into dev/mou…
moul Jul 7, 2024
3a498f6
optimize
jaekwon Jul 7, 2024
5ebf68f
Merge remote-tracking branch 'origin/dev/jae/crossrealm' into dev/mou…
moul Jul 7, 2024
60b99cc
add test for crossrealm new escaped internal objects
jaekwon Jul 7, 2024
ebd2f44
Merge remote-tracking branch 'origin/dev/jae/crossrealm' into dev/mou…
moul Jul 7, 2024
5eb4447
Merge remote-tracking branch 'origin/dev/jae/crossrealm' into dev/mou…
moul Jul 7, 2024
03e838b
chore: fixup
moul Jul 7, 2024
71b74f4
chore: fixup
moul Jul 7, 2024
4ca57af
chore: fixup
moul Jul 7, 2024
df5df54
Merge branch 'dev/moul/boundbug' into dev/moul/grc20reg
moul Jul 7, 2024
a91c84a
change realm to receiver's
jaekwon Jul 7, 2024
90034b9
Merge remote-tracking branch 'origin/dev/jae/crossrealm' into dev/mou…
moul Jul 7, 2024
90bee8b
Merge branch 'master' into dev/moul/grc20reg
moul Jul 8, 2024
f48048b
chore: fixup
moul Jul 8, 2024
9073056
chore: fixup
moul Jul 8, 2024
67e5353
chore: fixup
moul Jul 8, 2024
3b906ad
Merge branch 'master' into dev/moul/grc20reg
moul Jul 9, 2024
aaa5922
Merge branch 'master' into dev/moul/grc20reg
moul Sep 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test:

.PHONY: lint
lint:
go run ../gnovm/cmd/gno lint $(OFFICIAL_PACKAGES)
go run ../gnovm/cmd/gno lint -v $(OFFICIAL_PACKAGES)

.PHONY: test.sync
test.sync:
Expand Down
2 changes: 2 additions & 0 deletions examples/gno.land/r/demo/bar20/bar20.gno
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ufmt"
"gno.land/r/demo/grc20reg"
)

var (
Expand All @@ -19,6 +20,7 @@ var (
func init() {
banker = grc20.NewBanker("Bar", "BAR", 4)
Token = banker.Token()
grc20reg.Register(Token, "")
}

func Faucet() string {
Expand Down
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/bar20/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ require (
gno.land/p/demo/testutils v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/urequire v0.0.0-latest
gno.land/r/demo/grc20reg v0.0.0-latest
)
2 changes: 2 additions & 0 deletions examples/gno.land/r/demo/foo20/foo20.gno
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"gno.land/p/demo/ownable"
"gno.land/p/demo/ufmt"
pusers "gno.land/p/demo/users"
"gno.land/r/demo/grc20reg"
"gno.land/r/demo/users"
)

Expand All @@ -24,6 +25,7 @@ func init() {
banker = grc20.NewBanker("Foo", "FOO", 4)
banker.Mint(admin.Owner(), 1000000*10000) // @administrator (1M)
token = banker.Token()
grc20reg.Register(token, "")
}

func TotalSupply() uint64 { return token.TotalSupply() }
Expand Down
15 changes: 13 additions & 2 deletions examples/gno.land/r/demo/foo20/foo20_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,21 @@ func TestErrConditions(t *testing.T) {
fn func()
}

std.TestSetOrigCaller(users.Resolve(admin))
banker.Mint(std.Address(admin), 10000)
{
tests := []test{
{"Transfer(admin, 1)", "cannot send transfer to self", func() { Transfer(admin, 1) }},
{"Transfer(admin, 1)", "cannot send transfer to self", func() {
// XXX: should replace with:
// Transfer(admin, 1)
// but there is currently a limitation in
// manipulating the frame stack and simulate
// calling this package from an outside point of
// view.
adminAddr := std.Address(admin)
if err := banker.Transfer(adminAddr, adminAddr, 1); err != nil {
panic(err)
}
}},
{"Approve(empty, 1))", "invalid address", func() { Approve(empty, 1) }},
}
for _, tc := range tests {
Expand Down
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/foo20/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ require (
gno.land/p/demo/uassert v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/users v0.0.0-latest
gno.land/r/demo/grc20reg v0.0.0-latest
gno.land/r/demo/users v0.0.0-latest
)
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/grc20factory/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ require (
gno.land/p/demo/ownable v0.0.0-latest
gno.land/p/demo/uassert v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/r/demo/grc20reg v0.0.0-latest
)
3 changes: 2 additions & 1 deletion examples/gno.land/r/demo/grc20factory/grc20factory.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ownable"
"gno.land/p/demo/ufmt"
"gno.land/r/demo/grc20reg"
)

var instances avl.Tree // symbol -> instance
Expand All @@ -33,8 +34,8 @@ func NewWithAdmin(name, symbol string, decimals uint, initialMint, faucet uint64
admin: ownable.NewWithAddress(admin),
faucet: faucet,
}

instances.Set(symbol, &inst)
grc20reg.Register(banker.Token(), symbol)
}

type instance struct {
Expand Down
9 changes: 9 additions & 0 deletions examples/gno.land/r/demo/grc20reg/gno.mod
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
)
74 changes: 74 additions & 0 deletions examples/gno.land/r/demo/grc20reg/grc20reg.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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] -> Token

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

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

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

func Render(path string) string {
switch {
case path == "": // home
// TODO: add pagination
s := ""
count := 0
registry.Iterate("", "", func(key string, tokenI interface{}) bool {
count++
token := tokenI.(grc20.Token)
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
token := MustGet(key)
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"
74 changes: 74 additions & 0 deletions examples/gno.land/r/demo/grc20reg/grc20reg_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package grc20reg

import (
"std"
"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()
tokenImpl := &dummyImpl{}

// register
Register(tokenImpl, "")
regToken := Get(realmAddr)
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.Equal(t, expected, got)

// 404
invalidToken := Get("0xdeadbeef")
urequire.True(t, invalidToken == nil)

// register with a slug
Register(tokenImpl, "mySlug")
regToken = Get(realmAddr + ".mySlug")
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 + "")
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)
- **TestToken** - [gno.land/r/demo/foo](/r/demo/foo).mySlug - [info](/r/demo/grc20reg:gno.land/r/demo/foo.mySlug)
`
got = Render("")
urequire.Equal(t, expected, got)

expected = `# TestToken
- symbol: **TST**
- realm: [gno.land/r/demo/foo](/r/demo/foo).mySlug
- decimals: 1337
- 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")
}
10 changes: 10 additions & 0 deletions examples/gno.land/r/demo/tests/crossrealm/crossrealm.gno
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ func Make1() *p_crossrealm.Container {
B: local,
}
}

type Fooer interface{ Foo() }

var fooer Fooer

func SetFooer(f Fooer) Fooer {
fooer = f
return fooer
}
func CallFoo() { fooer.Foo() }
2 changes: 2 additions & 0 deletions examples/gno.land/r/demo/tests/tests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type TestRealmObject struct {
Field string
}

var TestRealmObjectValue TestRealmObject

func ModifyTestRealmObject(t *TestRealmObject) {
t.Field += "_modified"
}
Expand Down
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/wugnot/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ require (
gno.land/p/demo/grc/grc20 v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/users v0.0.0-latest
gno.land/r/demo/grc20reg v0.0.0-latest
gno.land/r/demo/users v0.0.0-latest
)
5 changes: 5 additions & 0 deletions examples/gno.land/r/demo/wugnot/wugnot.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gno.land/p/demo/grc/grc20"
"gno.land/p/demo/ufmt"
pusers "gno.land/p/demo/users"
"gno.land/r/demo/grc20reg"
"gno.land/r/demo/users"
)

Expand All @@ -20,6 +21,10 @@ const (
wugnotMinDeposit uint64 = 1
)

func init() {
grc20reg.Register(Token, "")
}

func Deposit() {
caller := std.PrevRealm().Addr()
sent := std.GetOrigSend()
Expand Down
Loading
Loading