Skip to content

Commit

Permalink
feat: use callback to simulate dynamic call
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Oct 20, 2023
1 parent 0b07727 commit 947fe39
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/gno.land/r/registry/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/registry
66 changes: 66 additions & 0 deletions examples/gno.land/r/registry/registry.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package registry

import (
"gno.land/r/demo/users"
)

var registered = []grc20Pair{}

type grc20TransferCb func(to users.AddressOrName, amount uint64)
type grc20TransferFromCb func(from, to users.AddressOrName, amount uint64)
type grc20Pair struct { /// xxx: map
name string // xxx: realm_path
transferCb grc20TransferCb
transferFromCb grc20TransferFromCb
}

func RegisterTransfer(name string, cb grc20TransferCb) {
if len(registered) == 0 {
registered = append(registered, grc20Pair{name: name, transferCb: cb})
} else {
for i, pair := range registered {
if pair.name == name {
registered[i].transferCb = cb
} else { // new found
registered = append(registered, grc20Pair{name: name, transferCb: cb})
}
}
}
}

func RegisterTransferFrom(name string, cb grc20TransferFromCb) {
if len(registered) == 0 {
registered = append(registered, grc20Pair{name: name, transferFromCb: cb})
} else {
for i, pair := range registered {
if pair.name == name {
registered[i].transferFromCb = cb
} else { // new found
registered = append(registered, grc20Pair{name: name, transferFromCb: cb})
}
}
}

}

func TransferByName(name string, to string, amount uint64) bool {
for i, pair := range registered {
if pair.name == name { // if token is registered
pair.transferCb(users.AddressOrName(to), amount)
return true
}
}

return false
}

func TransferFromByName(name string, from, to string, amount uint64) bool {
for _, pair := range registered {
if pair.name == name { // if token is registered
pair.transferFromCb(users.AddressOrName(from), users.AddressOrName(to), amount)
return true
}
}

return false
}
119 changes: 119 additions & 0 deletions examples/gno.land/r/registry/registry_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package registry

import (
"std"
"testing"

"gno.land/r/bar"
"gno.land/r/baz"
"gno.land/r/foo"

"gno.land/r/demo/users"

_ "gno.land/r/wrapper"
)

var cRealmAddr std.Address

const target string = "g12345"
const fromTo string = "g54321"

func init() {
cRealmAddr = std.CurrentRealm().Addr()
// println(cRealmAddr) // g1sqt92sa06ugh8nlt98kyghw83qy84paf4csyh6

std.TestSetOrigCaller(cRealmAddr)
foo.Faucet()
bar.Faucet()
baz.Faucet()
}

func TestTransferByNameFOO(t *testing.T) {
if foo.BalanceOf(target) != 0 {
t.Fatal("target should have 0 FOO but", foo.BalanceOf(target))
}

TransferByName("foo", target, 12345)

if foo.BalanceOf(target) != 12345 {
t.Fatal("target should have 12345 FOO, but", foo.BalanceOf(target))
}
}

func TestTransferByNameBAR(t *testing.T) {
if bar.BalanceOf(target) != 0 {
t.Fatal("target should have 0 BAR, but", bar.BalanceOf(target))
}

TransferByName("bar", target, 12345)

if bar.BalanceOf(target) != 12345 {
t.Fatal("target should have 12345 BAR, but", bar.BalanceOf(target))
}
}

func TestTransferByNameBAZ(t *testing.T) {
if baz.BalanceOf(target) != 0 {
t.Fatal("target should have 0 BAZ, but", baz.BalanceOf(target))
}

TransferByName("baz", target, 12345)

if baz.BalanceOf(target) != 12345 {
t.Fatal("target should have 12345 BAZ, but", baz.BalanceOf(target))
}
}

func TestTransferFromByNameFOO(t *testing.T) {
if foo.BalanceOf(fromTo) != 0 {
t.Fatal("fromTo should have 0 FOO, but", foo.BalanceOf(fromTo))
}

std.TestSetOrigCaller(target)
foo.Approve(users.AddressOrName(cRealmAddr), 5)
TransferFromByName("foo", target, fromTo, 3)

if foo.BalanceOf(fromTo) != 3 {
t.Fatal("fromTo should have 3 FOO, but", foo.BalanceOf(fromTo))
}

if foo.BalanceOf(target) != 12342 {
t.Fatal("target should have 12342 FOO, but", foo.BalanceOf(target))
}
}

func TestTransferFromByNameBAR(t *testing.T) {
if bar.BalanceOf(fromTo) != 0 {
t.Fatal("fromTo should have 0 BAR, but", bar.BalanceOf(fromTo))
}

std.TestSetOrigCaller(target)
bar.Approve(users.AddressOrName(cRealmAddr), 5)
TransferFromByName("bar", target, fromTo, 3)

if bar.BalanceOf(fromTo) != 3 {
t.Fatal("fromTo should have 3 BAR, but", bar.BalanceOf(fromTo))
}

if bar.BalanceOf(target) != 12342 {
t.Fatal("target should have 12342 BAR, but", bar.BalanceOf(target))
}
}

func TestTransferFromByNameBAZ(t *testing.T) {
if baz.BalanceOf(fromTo) != 0 {
t.Fatal("fromTo should have 0 BAZ, but", baz.BalanceOf(fromTo))
}

std.TestSetOrigCaller(target)
baz.Approve(users.AddressOrName(cRealmAddr), 5)
TransferFromByName("baz", target, fromTo, 3)

if baz.BalanceOf(fromTo) != 3 {
t.Fatal("fromTo should have 3 BAZ, but", baz.BalanceOf(fromTo))
}

if baz.BalanceOf(target) != 12342 {
t.Fatal("target should have 12342 BAZ, but", baz.BalanceOf(target))
}
}
1 change: 1 addition & 0 deletions examples/gno.land/r/wrapper/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/wrapper
20 changes: 20 additions & 0 deletions examples/gno.land/r/wrapper/wrapper.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package wrapper

import (
"gno.land/r/bar"
"gno.land/r/baz"
"gno.land/r/foo"
"gno.land/r/registry"
)

func init() {
// Transfer
registry.RegisterTransfer("foo", foo.Transfer)
registry.RegisterTransfer("bar", bar.Transfer)
registry.RegisterTransfer("baz", baz.Transfer)

// TransferFrom
registry.RegisterTransferFrom("foo", foo.TransferFrom)
registry.RegisterTransferFrom("bar", bar.TransferFrom)
registry.RegisterTransferFrom("baz", baz.TransferFrom)
}

0 comments on commit 947fe39

Please sign in to comment.