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

chore: add contract-contract interation with ownership example #1262

Merged
merged 9 commits into from
Dec 7, 2023
61 changes: 61 additions & 0 deletions gno.land/cmd/gnoland/testdata/grc20-registry.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# test for add package
moul marked this conversation as resolved.
Show resolved Hide resolved

## start a new node
gnoland start

# add registry
gnokey maketx addpkg -pkgdir $WORK/registry -pkgpath gno.land/r/registry -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1

# we call Transfer with foo20, before it's registered
gnokey maketx call -pkgpath gno.land/r/registry -func TransferByName -args 'foo20' -args 'g123456789' -args '42' -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout 'not found'

# add foo20, and foo20wrapper
gnokey maketx addpkg -pkgdir $WORK/foo20 -pkgpath gno.land/r/foo20 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
gnokey maketx addpkg -pkgdir $WORK/foo20wrapper -pkgpath gno.land/r/foo20wrapper -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1

# we call Transfer with foo20, after it's registered
gnokey maketx call -pkgpath gno.land/r/registry -func TransferByName -args 'foo20' -args 'g123456789' -args '42' -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout 'same address, success!'

-- registry/registry.gno --
package registry

import "std"

type transferCb func(to std.Address, amount uint64) string
type pair struct {name string; cb transferCb}
moul marked this conversation as resolved.
Show resolved Hide resolved
var registry = []pair{}
func Register(name string, cb transferCb) { registry = append(registry, pair{name: name, cb: cb}) }
func TransferByName(name string, to string, amount uint64) string {
for _, pair := range registry {
if pair.name == name {
if std.CurrentRealm().Addr().String() == pair.cb(std.Address(to), amount) {
return "same address, success!"
} else {
return "invalid address, ownership issue :("
}
}
}
moul marked this conversation as resolved.
Show resolved Hide resolved
return "not found"
}

-- foo20wrapper/foo20wrapper.gno --
package foo20wrapper

import "gno.land/r/registry"
import "gno.land/r/foo20"

func init() {
registry.Register("foo20", foo20.Transfer)
}

-- foo20/foo20.gno --
package foo20

import "std"

func Transfer(to std.Address, amount uint64) string {
println("transfer from=" + std.PrevRealm().Addr().String() + " to=" + to.String() + " some-amount")
return std.PrevRealm().Addr().String()
}
Loading