forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use callback to simulate dynamic call
- Loading branch information
Showing
5 changed files
with
207 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/r/registry |
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,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 | ||
} |
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,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)) | ||
} | ||
} |
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 @@ | ||
module gno.land/r/wrapper |
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,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) | ||
} |