Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

veto_no_event_filetest.gno

1.83 Kb · 85 lines
 1// PKGPATH: gno.land/r/demo/grc721veto
 2
 3// Extension hooks run before the Transfer event is emitted, so a hook that vetoes
 4// a movement leaves nothing on the event stream. Only the second mint, which no
 5// hook refuses, is announced.
 6package grc721veto
 7
 8import (
 9	"gno.land/p/nt/grc721/v0"
10)
11
12type vetoExtension struct {
13	refuse grc721.TokenID
14}
15
16func (e *vetoExtension) ExtensionKind() string { return "veto" }
17
18func (e *vetoExtension) OnMint(to address, tid grc721.TokenID) {
19	if tid == e.refuse {
20		panic("veto: mint refused")
21	}
22}
23
24func (e *vetoExtension) OnTransfer(from, to address, tid grc721.TokenID) {}
25
26func (e *vetoExtension) OnBurn(tid grc721.TokenID) {}
27
28func main(cur realm) {
29	_, ledger := grc721.NewToken("Veto", "VETO", 0, cur)
30	ledger.RegisterExtension(&vetoExtension{refuse: "1"})
31
32	func() {
33		defer func() { println("recovered:", recover()) }()
34		ledger.Mint(cur.Address(), "1")
35	}()
36
37	ledger.Mint(cur.Address(), "2")
38}
39
40// Output:
41// recovered: veto: mint refused
42
43// Events:
44// [
45//   {
46//     "type": "NewToken",
47//     "attrs": [
48//       {
49//         "key": "token",
50//         "value": "gno.land/r/demo/grc721veto.VETO.0000000"
51//       },
52//       {
53//         "key": "name",
54//         "value": "Veto"
55//       },
56//       {
57//         "key": "symbol",
58//         "value": "VETO"
59//       }
60//     ],
61//     "pkg_path": "gno.land/p/nt/grc721/v0"
62//   },
63//   {
64//     "type": "Transfer",
65//     "attrs": [
66//       {
67//         "key": "token",
68//         "value": "gno.land/r/demo/grc721veto.VETO.0000000"
69//       },
70//       {
71//         "key": "from",
72//         "value": ""
73//       },
74//       {
75//         "key": "to",
76//         "value": "g1dywcn8v2jdpl6fuks8tw3l8gnzer0hv8mfkraa"
77//       },
78//       {
79//         "key": "tokenId",
80//         "value": "2"
81//       }
82//     ],
83//     "pkg_path": "gno.land/p/nt/grc721/v0"
84//   }
85// ]