z0d_voucher_approve_filetest.gno
9.35 Kb · 303 lines
1// PKGPATH: gno.land/r/aib/main
2package main
3
4import (
5 "encoding/hex"
6 "testing"
7 "time"
8
9 tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
10 "gno.land/p/aib/ibc/types"
11 "gno.land/p/aib/ics23"
12 "gno.land/p/nt/testutils/v0"
13 "gno.land/r/aib/ibc/apps/transfer"
14 "gno.land/r/aib/ibc/core"
15 "gno.land/r/demo/defi/grc20reg"
16)
17
18// VoucherApprove: success for IBC voucher token via MsgCall helper.
19func main(cur realm) {
20 var (
21 chainID = "chain-id-2"
22 trustedHeight = types.NewHeight(2, 2)
23 clientState = tmtesting.NewClientState(chainID, trustedHeight)
24 apphash, _ = hex.DecodeString("0eadf50c7992d8e1366b8ea63ab7d1346de4762c3d449764ef7dc404ba48381f")
25 trustedValset = tmtesting.GenValset()
26 consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
27 counterpartyID = "07-tendermint-42"
28 )
29 clientID := core.CreateClient(cross(cur), clientState, consensusState)
30 core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
31
32 owner := cur.Previous().Address()
33 spender := testutils.TestAddress("spender")
34
35 mintVoucher(cur, clientID, counterpartyID, trustedHeight, owner)
36
37 voucherDenom := transfer.NewDenom("uphoton", transfer.NewHop(transfer.PortID, clientID)).IBCDenom()
38 token := grc20reg.MustGet("gno.land/r/aib/ibc/apps/transfer." + voucherDenom[len("ibc/"):])
39
40 println("allowance before VoucherApprove:", token.Allowance(owner, spender))
41
42 testing.SetRealm(testing.NewUserRealm(owner))
43 transfer.VoucherApprove(cross(cur), voucherDenom, spender, 40)
44
45 println("allowance after VoucherApprove:", token.Allowance(owner, spender))
46}
47
48func mintVoucher(cur realm, clientID, counterpartyID string, trustedHeight types.Height, receiver address) {
49 payload := transfer.NewFungibleTokenPacketData("uphoton", "100", "atone1user", receiver.String(), "")
50 payloadBz := payload.ProtoMarshal()
51
52 specs := ics23.IavlSpec()
53 proof := []ics23.CommitmentProof{
54 ics23.CommitmentProof_Exist{
55 Exist: &ics23.ExistenceProof{
56 Key: []byte("\x70\x72\x65\x66\x69\x78\x32\x30\x37\x2d\x74\x65\x6e\x64\x65\x72\x6d\x69\x6e\x74\x2d\x34\x32\x01\x00\x00\x00\x00\x00\x00\x00\x01"),
57 Value: []byte("\x12\x6c\xcc\xff\x00\xd6\xb9\x73\x09\xb3\x8b\x1b\xec\x0f\xe2\xef\xc9\x54\xe5\xac\x1d\x90\x47\x36\xd0\x32\xbc\xc0\xb1\x66\xc4\x8f"),
58 Leaf: &ics23.LeafOp{
59 Hash: specs.LeafSpec.Hash,
60 PrehashKey: specs.LeafSpec.PrehashKey,
61 PrehashValue: specs.LeafSpec.PrehashValue,
62 Length: specs.LeafSpec.Length,
63 Prefix: []byte("\x00\x02\x02"),
64 },
65 Path: []*ics23.InnerOp{
66 {
67 Hash: specs.InnerSpec.Hash,
68 Prefix: []byte("\x02\x04\x02\x20\x35\xf8\xea\x80\x53\x90\xe0\x84\x85\x4f\x39\x9b\x42\xcc\xde\xae\xa3\x3a\x1d\xed\xc1\x15\x63\x8a\xc4\x8d\x06\x00\x63\x7d\xba\x1f\x20"),
69 Suffix: []byte(""),
70 },
71 {
72 Hash: specs.InnerSpec.Hash,
73 Prefix: []byte("\x04\x08\x02\x20"),
74 Suffix: []byte("\x20\x79\x8e\x2c\xaa\x96\xfd\xfb\xa3\x76\xdd\xeb\x47\x99\x99\x54\xd2\xf4\x7e\x65\x16\x22\x64\xb0\x53\x6a\xb5\xdf\xf7\xfc\x0a\x2e\x07"),
75 },
76 {
77 Hash: specs.InnerSpec.Hash,
78 Prefix: []byte("\x06\x0c\x02\x20\x9a\xf3\x7d\xd5\x95\xa0\x19\x08\x03\xb5\xe0\x5a\xae\xf4\x2a\xe3\xfa\xd4\x99\xe4\xfb\xe3\x7f\x7c\xd3\x1c\xad\xff\x22\xa9\xee\x74\x20"),
79 Suffix: []byte(""),
80 },
81 },
82 },
83 },
84 ics23.CommitmentProof_Exist{
85 Exist: &ics23.ExistenceProof{
86 Key: []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
87 Value: []byte("\x4f\xa3\xdf\x13\x8a\x86\x32\xc8\x3f\xd8\x84\x92\x00\xe0\xf1\xd8\x8a\x05\x36\x85\x32\x15\x1b\x45\x67\xf6\x86\xec\xf1\xfb\x9f\x45"),
88 Leaf: &ics23.LeafOp{
89 Hash: specs.LeafSpec.Hash,
90 PrehashKey: specs.LeafSpec.PrehashKey,
91 PrehashValue: specs.LeafSpec.PrehashValue,
92 Length: specs.LeafSpec.Length,
93 Prefix: []byte("\x00"),
94 },
95 Path: []*ics23.InnerOp{},
96 },
97 },
98 }
99
100 recvPacket := types.MsgRecvPacket{
101 Packet: types.Packet{
102 Sequence: 1,
103 SourceClient: counterpartyID,
104 DestinationClient: clientID,
105 TimeoutTimestamp: uint64(time.Now().Add(time.Hour).Unix()),
106 Payloads: []types.Payload{{
107 SourcePort: transfer.PortID,
108 DestinationPort: transfer.PortID,
109 Encoding: transfer.EncodingProtobuf,
110 Value: payloadBz,
111 Version: transfer.V1,
112 }},
113 },
114 ProofCommitment: proof,
115 ProofHeight: trustedHeight,
116 }
117
118 core.RecvPacket(cross(cur), recvPacket)
119}
120
121// Output:
122// allowance before VoucherApprove: 0
123// allowance after VoucherApprove: 40
124
125// Events:
126// [
127// {
128// "type": "create_client",
129// "attrs": [
130// {
131// "key": "client_id",
132// "value": "07-tendermint-1"
133// },
134// {
135// "key": "client_type",
136// "value": "07-tendermint"
137// },
138// {
139// "key": "consensus_heights",
140// "value": "2/2"
141// }
142// ],
143// "pkg_path": "gno.land/r/aib/ibc/core"
144// },
145// {
146// "type": "recv_packet",
147// "attrs": [
148// {
149// "key": "packet_source_client",
150// "value": "07-tendermint-42"
151// },
152// {
153// "key": "packet_dest_client",
154// "value": "07-tendermint-1"
155// },
156// {
157// "key": "packet_sequence",
158// "value": "1"
159// },
160// {
161// "key": "packet_timeout_timestamp",
162// "value": "1234571490"
163// },
164// {
165// "key": "encoded_packet_hex",
166// "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a7b0a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a440a077570686f746f6e12033130301a0a61746f6e6531757365722228673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d"
167// }
168// ],
169// "pkg_path": "gno.land/r/aib/ibc/core"
170// },
171// {
172// "type": "denomination",
173// "attrs": [
174// {
175// "key": "denom_hash",
176// "value": "CAEF9CA8CE6C302D73A831A49E34E59149D3A9AD96CCEBDFBF62F6D5710D92D8"
177// },
178// {
179// "key": "denom",
180// "value": "{\"base\":\"uphoton\",\"path\":\"transfer/07-tendermint-1/uphoton\",\"denom\":\"ibc/CAEF9CA8CE6C302D73A831A49E34E59149D3A9AD96CCEBDFBF62F6D5710D92D8\"}"
181// }
182// ],
183// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
184// },
185// {
186// "type": "register",
187// "attrs": [
188// {
189// "key": "pkgpath",
190// "value": "gno.land/r/aib/ibc/apps/transfer"
191// },
192// {
193// "key": "slug",
194// "value": "CAEF9CA8CE6C302D73A831A49E34E59149D3A9AD96CCEBDFBF62F6D5710D92D8"
195// }
196// ],
197// "pkg_path": "gno.land/r/demo/defi/grc20reg"
198// },
199// {
200// "type": "Transfer",
201// "attrs": [
202// {
203// "key": "token",
204// "value": "gno.land/r/aib/ibc/apps/transfer.CAEF9CA8CE6"
205// },
206// {
207// "key": "from",
208// "value": ""
209// },
210// {
211// "key": "to",
212// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
213// },
214// {
215// "key": "value",
216// "value": "100"
217// }
218// ],
219// "pkg_path": "gno.land/p/demo/tokens/grc20"
220// },
221// {
222// "type": "fungible_token_packet",
223// "attrs": [
224// {
225// "key": "sender",
226// "value": "atone1user"
227// },
228// {
229// "key": "receiver",
230// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
231// },
232// {
233// "key": "denom",
234// "value": "transfer/07-tendermint-1/uphoton"
235// },
236// {
237// "key": "amount",
238// "value": "100"
239// },
240// {
241// "key": "memo",
242// "value": ""
243// },
244// {
245// "key": "success",
246// "value": "true"
247// }
248// ],
249// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
250// },
251// {
252// "type": "write_acknowledgement",
253// "attrs": [
254// {
255// "key": "packet_source_client",
256// "value": "07-tendermint-42"
257// },
258// {
259// "key": "packet_dest_client",
260// "value": "07-tendermint-1"
261// },
262// {
263// "key": "packet_sequence",
264// "value": "1"
265// },
266// {
267// "key": "packet_timeout_timestamp",
268// "value": "1234571490"
269// },
270// {
271// "key": "encoded_packet_hex",
272// "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a7b0a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a440a077570686f746f6e12033130301a0a61746f6e6531757365722228673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d"
273// },
274// {
275// "key": "encoded_acknowledgement_hex",
276// "value": "0a117b22726573756c74223a2241513d3d227d"
277// }
278// ],
279// "pkg_path": "gno.land/r/aib/ibc/core"
280// },
281// {
282// "type": "Approval",
283// "attrs": [
284// {
285// "key": "token",
286// "value": "gno.land/r/aib/ibc/apps/transfer.CAEF9CA8CE6"
287// },
288// {
289// "key": "owner",
290// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
291// },
292// {
293// "key": "spender",
294// "value": "g1wdcx2mnyv4e97h6lta047h6lta047h6l735n06"
295// },
296// {
297// "key": "value",
298// "value": "40"
299// }
300// ],
301// "pkg_path": "gno.land/p/demo/tokens/grc20"
302// }
303// ]