Skip to content

Commit

Permalink
fix: testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Feb 13, 2024
1 parent 86c267f commit fdbf927
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 24 deletions.
21 changes: 11 additions & 10 deletions examples/gno.land/r/x/grc20_dynamic_call/bar/bar.gno
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func init() {
bar = grc20.NewAdminToken("Bar", "BAR", 4)
bar.Mint(admin, 1000000*10000) // @administrator (1M)
bar.Mint("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq", 10000*10000) // @manfred (10k)

bar.Mint("g1tkmrcu9m0xjddxh0c29r3zac8m2yjwaytlvh6u", 1000*10000) // REALM_gno.land/r/x/grc20_dynamic_call/registry
}

// method proxies as public functions.
Expand All @@ -32,15 +34,15 @@ func TotalSupply() uint64 {
func BalanceOf(owner users.AddressOrName) uint64 {
balance, err := bar.BalanceOf(owner.Resolve())
if err != nil {
panic(err)
panic(err.Error())
}
return balance
}

func Allowance(owner, spender users.AddressOrName) uint64 {
allowance, err := bar.Allowance(owner.Resolve(), spender.Resolve())
if err != nil {
panic(err)
panic(err.Error())
}
return allowance
}
Expand All @@ -51,24 +53,23 @@ func Transfer(to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := bar.Transfer(caller, to.Resolve(), amount)
if err != nil {
panic(err)
panic(err.Error())
}
}

func Approve(spender users.AddressOrName, amount uint64) {
// caller := std.PrevRealm().Addr()
caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
err := bar.Approve(caller, spender.Resolve(), amount)
if err != nil {
panic(err)
panic(err.Error())
}
}

func TransferFrom(from, to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := bar.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
if err != nil {
panic(err)
panic(err.Error())
}
}

Expand All @@ -80,7 +81,7 @@ func Faucet() {
caller := std.PrevRealm().Addr()
err := bar.Mint(caller, 1000*10000) // 1k
if err != nil {
panic(err)
panic(err.Error())
}
}

Expand All @@ -91,7 +92,7 @@ func Mint(address users.AddressOrName, amount uint64) {
assertIsAdmin(caller)
err := bar.Mint(address.Resolve(), amount)
if err != nil {
panic(err)
panic(err.Error())
}
}

Expand All @@ -100,7 +101,7 @@ func Burn(address users.AddressOrName, amount uint64) {
assertIsAdmin(caller)
err := bar.Burn(address.Resolve(), amount)
if err != nil {
panic(err)
panic(err.Error())
}
}

Expand Down
21 changes: 11 additions & 10 deletions examples/gno.land/r/x/grc20_dynamic_call/foo/foo.gno
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func init() {
foo = grc20.NewAdminToken("Foo", "FOO", 4)
foo.Mint(admin, 1000000*10000) // @administrator (1M)
foo.Mint("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq", 10000*10000) // @manfred (10k)

foo.Mint("g1tkmrcu9m0xjddxh0c29r3zac8m2yjwaytlvh6u", 1000*10000) // REALM_gno.land/r/x/grc20_dynamic_call/registry
}

// method proxies as public functions.
Expand All @@ -32,15 +34,15 @@ func TotalSupply() uint64 {
func BalanceOf(owner users.AddressOrName) uint64 {
balance, err := foo.BalanceOf(owner.Resolve())
if err != nil {
panic(err)
panic(err.Error())
}
return balance
}

func Allowance(owner, spender users.AddressOrName) uint64 {
allowance, err := foo.Allowance(owner.Resolve(), spender.Resolve())
if err != nil {
panic(err)
panic(err.Error())
}
return allowance
}
Expand All @@ -51,24 +53,23 @@ func Transfer(to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := foo.Transfer(caller, to.Resolve(), amount)
if err != nil {
panic(err)
panic(err.Error())
}
}

func Approve(spender users.AddressOrName, amount uint64) {
// caller := std.PrevRealm().Addr()
caller := std.GetOrigCaller()
caller := std.PrevRealm().Addr()
err := foo.Approve(caller, spender.Resolve(), amount)
if err != nil {
panic(err)
panic(err.Error())
}
}

func TransferFrom(from, to users.AddressOrName, amount uint64) {
caller := std.PrevRealm().Addr()
err := foo.TransferFrom(caller, from.Resolve(), to.Resolve(), amount)
if err != nil {
panic(err)
panic(err.Error())
}
}

Expand All @@ -80,7 +81,7 @@ func Faucet() {
caller := std.PrevRealm().Addr()
err := foo.Mint(caller, 1000*10000) // 1k
if err != nil {
panic(err)
panic(err.Error())
}
}

Expand All @@ -91,7 +92,7 @@ func Mint(address users.AddressOrName, amount uint64) {
assertIsAdmin(caller)
err := foo.Mint(address.Resolve(), amount)
if err != nil {
panic(err)
panic(err.Error())
}
}

Expand All @@ -100,7 +101,7 @@ func Burn(address users.AddressOrName, amount uint64) {
assertIsAdmin(caller)
err := foo.Burn(address.Resolve(), amount)
if err != nil {
panic(err)
panic(err.Error())
}
}

Expand Down
5 changes: 1 addition & 4 deletions examples/gno.land/r/x/grc20_dynamic_call/registry/gno.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
module gno.land/r/x/grc20_dynamic_call/registry

require (
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/r/demo/users v0.0.0-latest
gno.land/r/x/grc20_dynamic_call/bar v0.0.0-latest
gno.land/r/x/grc20_dynamic_call/baz v0.0.0-latest
gno.land/r/x/grc20_dynamic_call/foo v0.0.0-latest
gno.land/r/x/grc20_dynamic_call/wrapper v0.0.0-latest
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// PKGPATH: gno.land/r/x/grc20_dynamic_call/registries_test
package registries_test

import (
"std"

"gno.land/p/demo/testutils"

"gno.land/r/x/grc20_dynamic_call/registry"

"gno.land/r/x/grc20_dynamic_call/bar"
"gno.land/r/x/grc20_dynamic_call/foo"

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

var (
actualRealmAddr std.Address
transferTo std.Address
)

// FOO
type FooStruct struct{}

func (FooStruct) Transfer() func(to users.AddressOrName, amount uint64) {
return foo.Transfer
}
func (FooStruct) TransferFrom() func(from, to users.AddressOrName, amount uint64) {
return foo.TransferFrom
}
func (FooStruct) BalanceOf() func(owner users.AddressOrName) uint64 {
return foo.BalanceOf
}

// BAR
type BarStruct struct{}

func (BarStruct) Transfer() func(to users.AddressOrName, amount uint64) {
return bar.Transfer
}
func (BarStruct) TransferFrom() func(from, to users.AddressOrName, amount uint64) {
return bar.TransferFrom
}
func (BarStruct) BalanceOf() func(owner users.AddressOrName) uint64 {
return bar.BalanceOf
}

func init() {
registry.RegisterGRC20Interface("gno.land/r/x/grc20_dynamic_call/foo", FooStruct{})
registry.RegisterGRC20Interface("gno.land/r/x/grc20_dynamic_call/bar", BarStruct{})

actualRealmAddr = std.DerivePkgAddr("gno.land/r/x/grc20_dynamic_call/registry")

transferTo = testutils.TestAddress("transferTo")
}

func main() {
// FOO
fooBalanceRealm := registry.BalanceOfByInterface("gno.land/r/x/grc20_dynamic_call/foo", actualRealmAddr)
println(fooBalanceRealm)

fooBalanceTo := registry.BalanceOfByInterface("gno.land/r/x/grc20_dynamic_call/foo", transferTo)
println(fooBalanceTo)

registry.TransferByInterface("gno.land/r/x/grc20_dynamic_call/foo", transferTo, 12345)
fooBalanceTo = registry.BalanceOfByInterface("gno.land/r/x/grc20_dynamic_call/foo", transferTo)
println(fooBalanceTo)

// BAR
barBalanceRealm := registry.BalanceOfByInterface("gno.land/r/x/grc20_dynamic_call/bar", actualRealmAddr)
println(barBalanceRealm)

barBalanceTo := registry.BalanceOfByInterface("gno.land/r/x/grc20_dynamic_call/bar", transferTo)
println(barBalanceTo)

registry.TransferByInterface("gno.land/r/x/grc20_dynamic_call/bar", transferTo, 54321)
barBalanceTo = registry.BalanceOfByInterface("gno.land/r/x/grc20_dynamic_call/bar", transferTo)
println(barBalanceTo)
}

// Output:
// 10000000
// 0
// 12345
// 10000000
// 0
// 54321
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"std"

"gno.land/p/demo/testutils"

"gno.land/r/x/grc20_dynamic_call/registry"

"gno.land/r/x/grc20_dynamic_call/bar"
"gno.land/r/x/grc20_dynamic_call/foo"

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

var (
actualRealmAddr std.Address

transferFromFrom std.Address = testutils.TestAddress("transferFromFrom")
transferFromTo std.Address = testutils.TestAddress("transferFromTo")
)

// FOO
type FooStruct struct{}

func (FooStruct) Transfer() func(to users.AddressOrName, amount uint64) {
return foo.Transfer
}
func (FooStruct) TransferFrom() func(from, to users.AddressOrName, amount uint64) {
return foo.TransferFrom
}
func (FooStruct) BalanceOf() func(owner users.AddressOrName) uint64 {
return foo.BalanceOf
}

// BAR
type BarStruct struct{}

func (BarStruct) Transfer() func(to users.AddressOrName, amount uint64) {
return bar.Transfer
}
func (BarStruct) TransferFrom() func(from, to users.AddressOrName, amount uint64) {
return bar.TransferFrom
}
func (BarStruct) BalanceOf() func(owner users.AddressOrName) uint64 {
return bar.BalanceOf
}

func init() {
registry.RegisterGRC20Interface("gno.land/r/x/grc20_dynamic_call/foo", FooStruct{})
registry.RegisterGRC20Interface("gno.land/r/x/grc20_dynamic_call/bar", BarStruct{})

actualRealmAddr = std.DerivePkgAddr("gno.land/r/x/grc20_dynamic_call/registry")
}

func main() {
foo.Faucet()
foo.Approve(users.AddressOrName(std.DerivePkgAddr("gno.land/r/x/grc20_dynamic_call/registry")), 1000*10000)

fooBalanceOrigCaller := registry.BalanceOfByInterface("gno.land/r/x/grc20_dynamic_call/foo", std.GetOrigCaller())
println(fooBalanceOrigCaller)

fooBalanceTo := registry.BalanceOfByInterface("gno.land/r/x/grc20_dynamic_call/foo", transferFromTo)
println(fooBalanceTo)

registry.TransferFromByInterface("gno.land/r/x/grc20_dynamic_call/foo", std.GetOrigCaller(), transferFromTo, 54321)

fooBalanceOrigCaller = registry.BalanceOfByInterface("gno.land/r/x/grc20_dynamic_call/foo", std.GetOrigCaller())
println(fooBalanceOrigCaller)

fooBalanceTo = registry.BalanceOfByInterface("gno.land/r/x/grc20_dynamic_call/foo", transferFromTo)
println(fooBalanceTo)
}

// Output:
// 10000000
// 0
// 9945679
// 54321

0 comments on commit fdbf927

Please sign in to comment.