z0d_voucher_approve_filetest.gno
9.71 Kb · 313 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 // grc20reg keys tokens by rlmpath.symbol; the voucher symbol is the truncated
39 // IBC hash (VoucherSymbol), not the full hash.
40 token := grc20reg.MustGet("gno.land/r/aib/ibc/apps/transfer." + transfer.VoucherSymbol(voucherDenom))
41
42 println("allowance before VoucherApprove:", token.Allowance(owner, spender))
43
44 testing.SetRealm(testing.NewUserRealm(owner))
45 transfer.VoucherApprove(cross(cur), voucherDenom, spender, 40)
46
47 println("allowance after VoucherApprove:", token.Allowance(owner, spender))
48}
49
50func mintVoucher(cur realm, clientID, counterpartyID string, trustedHeight types.Height, receiver address) {
51 payload := transfer.NewFungibleTokenPacketData("uphoton", "100", "atone1user", receiver.String(), "")
52 payloadBz := payload.ProtoMarshal()
53
54 specs := ics23.IavlSpec()
55 proof := []ics23.CommitmentProof{
56 ics23.CommitmentProof_Exist{
57 Exist: &ics23.ExistenceProof{
58 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"),
59 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"),
60 Leaf: &ics23.LeafOp{
61 Hash: specs.LeafSpec.Hash,
62 PrehashKey: specs.LeafSpec.PrehashKey,
63 PrehashValue: specs.LeafSpec.PrehashValue,
64 Length: specs.LeafSpec.Length,
65 Prefix: []byte("\x00\x02\x02"),
66 },
67 Path: []*ics23.InnerOp{
68 {
69 Hash: specs.InnerSpec.Hash,
70 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"),
71 Suffix: []byte(""),
72 },
73 {
74 Hash: specs.InnerSpec.Hash,
75 Prefix: []byte("\x04\x08\x02\x20"),
76 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"),
77 },
78 {
79 Hash: specs.InnerSpec.Hash,
80 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"),
81 Suffix: []byte(""),
82 },
83 },
84 },
85 },
86 ics23.CommitmentProof_Exist{
87 Exist: &ics23.ExistenceProof{
88 Key: []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
89 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"),
90 Leaf: &ics23.LeafOp{
91 Hash: specs.LeafSpec.Hash,
92 PrehashKey: specs.LeafSpec.PrehashKey,
93 PrehashValue: specs.LeafSpec.PrehashValue,
94 Length: specs.LeafSpec.Length,
95 Prefix: []byte("\x00"),
96 },
97 Path: []*ics23.InnerOp{},
98 },
99 },
100 }
101
102 recvPacket := types.MsgRecvPacket{
103 Packet: types.Packet{
104 Sequence: 1,
105 SourceClient: counterpartyID,
106 DestinationClient: clientID,
107 TimeoutTimestamp: uint64(time.Now().Add(time.Hour).Unix()),
108 Payloads: []types.Payload{{
109 SourcePort: transfer.PortID,
110 DestinationPort: transfer.PortID,
111 Encoding: transfer.EncodingProtobuf,
112 Value: payloadBz,
113 Version: transfer.V1,
114 }},
115 },
116 ProofCommitment: proof,
117 ProofHeight: trustedHeight,
118 }
119
120 core.RecvPacket(cross(cur), recvPacket)
121}
122
123// Output:
124// allowance before VoucherApprove: 0
125// allowance after VoucherApprove: 40
126
127// Events:
128// [
129// {
130// "type": "create_client",
131// "attrs": [
132// {
133// "key": "client_id",
134// "value": "07-tendermint-1"
135// },
136// {
137// "key": "client_type",
138// "value": "07-tendermint"
139// },
140// {
141// "key": "consensus_heights",
142// "value": "2/2"
143// }
144// ],
145// "pkg_path": "gno.land/r/aib/ibc/core"
146// },
147// {
148// "type": "recv_packet",
149// "attrs": [
150// {
151// "key": "packet_source_client",
152// "value": "07-tendermint-42"
153// },
154// {
155// "key": "packet_dest_client",
156// "value": "07-tendermint-1"
157// },
158// {
159// "key": "packet_sequence",
160// "value": "1"
161// },
162// {
163// "key": "packet_timeout_timestamp",
164// "value": "1234571490"
165// },
166// {
167// "key": "encoded_packet_hex",
168// "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a7b0a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a440a077570686f746f6e12033130301a0a61746f6e6531757365722228673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d"
169// }
170// ],
171// "pkg_path": "gno.land/r/aib/ibc/core"
172// },
173// {
174// "type": "denomination",
175// "attrs": [
176// {
177// "key": "denom_hash",
178// "value": "CAEF9CA8CE6C302D73A831A49E34E59149D3A9AD96CCEBDFBF62F6D5710D92D8"
179// },
180// {
181// "key": "denom",
182// "value": "{\"base\":\"uphoton\",\"path\":\"transfer/07-tendermint-1/uphoton\",\"denom\":\"ibc/CAEF9CA8CE6C302D73A831A49E34E59149D3A9AD96CCEBDFBF62F6D5710D92D8\"}"
183// }
184// ],
185// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
186// },
187// {
188// "type": "register",
189// "attrs": [
190// {
191// "key": "token_path",
192// "value": "gno.land/r/aib/ibc/apps/transfer.CAEF9CA8CE6"
193// },
194// {
195// "key": "pkgpath",
196// "value": "gno.land/r/aib/ibc/apps/transfer"
197// },
198// {
199// "key": "slug",
200// "value": "CAEF9CA8CE6C302D73A831A49E34E59149D3A9AD96CCEBDFBF62F6D5710D92D8"
201// },
202// {
203// "key": "symbol",
204// "value": "CAEF9CA8CE6"
205// }
206// ],
207// "pkg_path": "gno.land/r/demo/defi/grc20reg"
208// },
209// {
210// "type": "Transfer",
211// "attrs": [
212// {
213// "key": "token",
214// "value": "gno.land/r/aib/ibc/apps/transfer.CAEF9CA8CE6.0000001"
215// },
216// {
217// "key": "from",
218// "value": ""
219// },
220// {
221// "key": "to",
222// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
223// },
224// {
225// "key": "value",
226// "value": "100"
227// }
228// ],
229// "pkg_path": "gno.land/p/demo/tokens/grc20"
230// },
231// {
232// "type": "fungible_token_packet",
233// "attrs": [
234// {
235// "key": "sender",
236// "value": "atone1user"
237// },
238// {
239// "key": "receiver",
240// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
241// },
242// {
243// "key": "denom",
244// "value": "transfer/07-tendermint-1/uphoton"
245// },
246// {
247// "key": "amount",
248// "value": "100"
249// },
250// {
251// "key": "memo",
252// "value": ""
253// },
254// {
255// "key": "success",
256// "value": "true"
257// }
258// ],
259// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
260// },
261// {
262// "type": "write_acknowledgement",
263// "attrs": [
264// {
265// "key": "packet_source_client",
266// "value": "07-tendermint-42"
267// },
268// {
269// "key": "packet_dest_client",
270// "value": "07-tendermint-1"
271// },
272// {
273// "key": "packet_sequence",
274// "value": "1"
275// },
276// {
277// "key": "packet_timeout_timestamp",
278// "value": "1234571490"
279// },
280// {
281// "key": "encoded_packet_hex",
282// "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a7b0a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a440a077570686f746f6e12033130301a0a61746f6e6531757365722228673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d"
283// },
284// {
285// "key": "encoded_acknowledgement_hex",
286// "value": "0a117b22726573756c74223a2241513d3d227d"
287// }
288// ],
289// "pkg_path": "gno.land/r/aib/ibc/core"
290// },
291// {
292// "type": "Approval",
293// "attrs": [
294// {
295// "key": "token",
296// "value": "gno.land/r/aib/ibc/apps/transfer.CAEF9CA8CE6.0000001"
297// },
298// {
299// "key": "owner",
300// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
301// },
302// {
303// "key": "spender",
304// "value": "g1wdcx2mnyv4e97h6lta047h6lta047h6l735n06"
305// },
306// {
307// "key": "value",
308// "value": "40"
309// }
310// ],
311// "pkg_path": "gno.land/p/demo/tokens/grc20"
312// }
313// ]