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(p/grc721): Distinct Event Types for GRC721 Functions #3102

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 29 additions & 10 deletions examples/gno.land/p/demo/grc/grc721/basic_nft.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grc721

import (
"std"
"strconv"

"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
Expand Down Expand Up @@ -120,8 +121,12 @@ func (s *basicNFT) Approve(to std.Address, tid TokenID) error {
}

s.tokenApprovals.Set(string(tid), to.String())
event := ApprovalEvent{owner, to, tid}
emit(&event)
std.Emit(
ApprovalEvent,
"owner", string(owner),
"to", string(to),
"tokenId", string(tid),
)

return nil
}
Expand Down Expand Up @@ -219,8 +224,11 @@ func (s *basicNFT) Burn(tid TokenID) error {
s.balances.Set(owner.String(), balance)
s.owners.Remove(string(tid))

event := TransferEvent{owner, zeroAddress, tid}
emit(&event)
std.Emit(
BurnEvent,
"from", string(owner),
"tokenId", string(tid),
)

s.afterTokenTransfer(owner, zeroAddress, tid, 1)

Expand All @@ -238,8 +246,12 @@ func (s *basicNFT) setApprovalForAll(owner, operator std.Address, approved bool)
key := owner.String() + ":" + operator.String()
s.operatorApprovals.Set(key, approved)

event := ApprovalForAllEvent{owner, operator, approved}
emit(&event)
std.Emit(
ApprovalForAllEvent,
"owner", string(owner),
"to", string(operator),
"approved", strconv.FormatBool(approved),
)

return nil
}
Expand Down Expand Up @@ -291,8 +303,12 @@ func (s *basicNFT) transfer(from, to std.Address, tid TokenID) error {
s.balances.Set(to.String(), toBalance)
s.owners.Set(string(tid), to)

event := TransferEvent{from, to, tid}
emit(&event)
std.Emit(
TransferEvent,
"from", string(from),
"to", string(to),
"tokenId", string(tid),
)

s.afterTokenTransfer(from, to, tid, 1)

Expand Down Expand Up @@ -324,8 +340,11 @@ func (s *basicNFT) mint(to std.Address, tid TokenID) error {
s.balances.Set(to.String(), toBalance)
s.owners.Set(string(tid), to)

event := TransferEvent{zeroAddress, to, tid}
emit(&event)
std.Emit(
MintEvent,
"to", string(to),
"tokenId", string(tid),
)

s.afterTokenTransfer(zeroAddress, to, tid, 1)

Expand Down
9 changes: 6 additions & 3 deletions examples/gno.land/p/demo/grc/grc721/grc721_metadata.gno
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ func (s *metadataNFT) mint(to std.Address, tid TokenID) error {
// Set owner of the token ID to the recipient address
s.basicNFT.owners.Set(string(tid), to)

// Emit transfer event
event := TransferEvent{zeroAddress, to, tid}
emit(&event)
std.Emit(
TransferEvent,
"from", string(zeroAddress),
"to", string(to),
"tokenId", string(tid),
)

s.basicNFT.afterTokenTransfer(zeroAddress, to, tid, 1)

Expand Down
24 changes: 7 additions & 17 deletions examples/gno.land/p/demo/grc/grc721/igrc721.gno
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,10 @@ type (
TokenURI string
)

type TransferEvent struct {
From std.Address
To std.Address
TokenID TokenID
}

type ApprovalEvent struct {
Owner std.Address
Approved std.Address
TokenID TokenID
}

type ApprovalForAllEvent struct {
Owner std.Address
Operator std.Address
Approved bool
}
const (
MintEvent = "Mint"
BurnEvent = "Burn"
TransferEvent = "Transfer"
ApprovalEvent = "Approval"
ApprovalForAllEvent = "ApprovalForAll"
)
95 changes: 95 additions & 0 deletions gno.land/cmd/gnoland/testdata/grc721_emit.txtar
Copy link
Member

@thehowl thehowl Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note; we'll need to refactor these tests eventually so we test for the events emitted inside a _test.gno, but it kind of depends on the discussions in #1475

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Test for https://github.com/gnolang/gno/pull/3102
loadpkg gno.land/p/demo/grc/grc721
loadpkg gno.land/r/demo/users
loadpkg gno.land/r/foo721 $WORK/foo721

gnoland start

# Mint
gnokey maketx call -pkgpath gno.land/r/foo721 -func Mint -args g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -args 1 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout '\[{\"type\":\"Mint\",\"attrs\":\[{\"key\":\"to\",\"value\":\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"},{\"key\":\"tokenId\",\"value\":\"1\"}],\"pkg_path\":\"gno.land\/r\/foo721\",\"func\":\"mint\"}\]'

# Approve
gnokey maketx call -pkgpath gno.land/r/foo721 -func Approve -args g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj -args 1 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout '\[{\"type\":\"Approval\",\"attrs\":\[{\"key\":\"owner\",\"value\":\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"},{\"key\":\"to\",\"value\":\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"},{\"key\":\"tokenId\",\"value\":\"1\"}],\"pkg_path\":\"gno.land\/r\/foo721\",\"func\":\"Approve\"}\]'

# SetApprovalForAll
gnokey maketx call -pkgpath gno.land/r/foo721 -func SetApprovalForAll -args g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj -args false -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout '\[{\"type\":\"ApprovalForAll\",\"attrs\":\[{\"key\":\"owner\",\"value\":\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"},{\"key\":\"to\",\"value\":\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"},{\"key\":\"approved\",\"value\":\"false\"}],\"pkg_path\":\"gno\.land/r/foo721\",\"func\":\"setApprovalForAll\"}\]'

# TransferFrom
gnokey maketx call -pkgpath gno.land/r/foo721 -func TransferFrom -args g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -args g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj -args 1 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout '\[{\"type\":\"Transfer\",\"attrs\":\[{\"key\":\"from\",\"value\":\"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5\"},{\"key\":\"to\",\"value\":\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"},{\"key\":\"tokenId\",\"value\":\"1\"}],\"pkg_path\":\"gno\.land/r/foo721\",\"func\":\"transfer\"}\]'

# Burn
gnokey maketx call -pkgpath gno.land/r/foo721 -func Burn -args 1 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout '\[{\"type\":\"Burn\",\"attrs\":\[{\"key\":\"from\",\"value\":\"g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj\"},{\"key\":\"tokenId\",\"value\":\"1\"}],\"pkg_path\":\"gno\.land/r/foo721\",\"func\":\"Burn\"}\]'


-- foo721/foo721.gno --
package foo721

import (
"std"

"gno.land/p/demo/grc/grc721"
"gno.land/r/demo/users"

pusers "gno.land/p/demo/users"
)

var (
admin std.Address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
foo = grc721.NewBasicNFT("FooNFT", "FNFT")
)

// Setters

func Approve(user pusers.AddressOrName, tid grc721.TokenID) {
err := foo.Approve(users.Resolve(user), tid)
if err != nil {
panic(err)
}
}

func SetApprovalForAll(user pusers.AddressOrName, approved bool) {
err := foo.SetApprovalForAll(users.Resolve(user), approved)
if err != nil {
panic(err)
}
}

func TransferFrom(from, to pusers.AddressOrName, tid grc721.TokenID) {
err := foo.TransferFrom(users.Resolve(from), users.Resolve(to), tid)
if err != nil {
panic(err)
}
}

// Admin

func Mint(to pusers.AddressOrName, tid grc721.TokenID) {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
err := foo.Mint(users.Resolve(to), tid)
if err != nil {
panic(err)
}
}

func Burn(tid grc721.TokenID) {
caller := std.PrevRealm().Addr()
assertIsAdmin(caller)
err := foo.Burn(tid)
if err != nil {
panic(err)
}
}

// Util

func assertIsAdmin(address std.Address) {
if address != admin {
panic("restricted access")
}
}
Loading