z5aa_transfer_filetest.gno
11.71 Kb · 364 lines
1// PKGPATH: gno.land/r/aib/main
2package main
3
4import (
5 "encoding/base64"
6 "encoding/hex"
7 "time"
8
9 tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
10 "gno.land/p/aib/ibc/types"
11
12 "gno.land/p/aib/ics23"
13 "gno.land/r/aib/ibc/apps/transfer"
14 "gno.land/r/aib/ibc/core"
15)
16
17// Transfer: success for IBC voucher coins.
18func main(cur realm) {
19 var (
20 chainID = "chain-id-2"
21 trustedHeight = types.NewHeight(2, 2)
22 clientState = tmtesting.NewClientState(chainID, trustedHeight)
23 apphash, _ = hex.DecodeString("0eadf50c7992d8e1366b8ea63ab7d1346de4762c3d449764ef7dc404ba48381f")
24 trustedValset = tmtesting.GenValset()
25 consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
26 counterpartyID = "07-tendermint-42"
27 )
28 clientID := core.CreateClient(cross(cur), clientState, consensusState)
29 core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
30
31 signer := cur.Previous().Address()
32
33 // Call recvPacket as a setup because we need the ibc denom to be in the
34 // store. recvPacket will mint 100ibc/abc tokens and transfer them to signer
35 // as vouchers.
36 recvPacket(cur, clientID, counterpartyID, trustedHeight, signer)
37
38 // ibc/CAEF9CA8CE6C302D73A831A49E34E59149D3A9AD96CCEBDFBF62F6D5710D92D8
39 voucherDenom := transfer.NewDenom("uphoton", transfer.NewHop(transfer.PortID, clientID)).IBCDenom()
40 println("\nreceiver voucher balance after RecvPacket:", transfer.VoucherBalanceOf(voucherDenom, signer))
41
42 transfer.Transfer(cross(cur), clientID, "atone1user", voucherDenom, 100, uint64(time.Now().Add(time.Hour).Unix()), "memo")
43
44 println("\nreceiver voucher balance after Transfer:", transfer.VoucherBalanceOf(voucherDenom, signer))
45}
46
47func recvPacket(cur realm, clientID, counterpartyID string, trustedHeight types.Height, receiver address) {
48 payload := transfer.NewFungibleTokenPacketData("uphoton", "100", "atone1user", receiver.String(), "")
49 payloadBz := payload.ProtoMarshal()
50 // NOTE this base64 value is used in payload.value in the gen-proof command below.
51 println("Payload proto:", base64.StdEncoding.EncodeToString(payloadBz))
52
53 specs := ics23.IavlSpec()
54 // NOTE code generated by:
55 // go run -C ./cmd/gen-proof . 'prefix2' '07-tendermint-42' 'packet' '{"sequence":1,"source_client":"07-tendermint-42","destination_client":"07-tendermint-1","timeout_timestamp":1234571490,"payloads":[{"source_port":"transfer","destination_port":"transfer","encoding":"application/x-protobuf", "value":"Cgd1cGhvdG9uEgMxMDAaCmF0b25lMXVzZXIiKGcxd3ltdTQ3ZHJocjBrdXEyMDk4bTc5Mmx5dGd0ajJueXg3N3lyc20=","version":"ics20-1"}]}'
56 proof := []ics23.CommitmentProof{
57
58 // iavl proof
59 ics23.CommitmentProof_Exist{
60 Exist: &ics23.ExistenceProof{
61 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"),
62 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"),
63 Leaf: &ics23.LeafOp{
64 Hash: specs.LeafSpec.Hash,
65 PrehashKey: specs.LeafSpec.PrehashKey,
66 PrehashValue: specs.LeafSpec.PrehashValue,
67 Length: specs.LeafSpec.Length,
68 Prefix: []byte("\x00\x02\x02"),
69 },
70 Path: []*ics23.InnerOp{
71 {
72 Hash: specs.InnerSpec.Hash,
73 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"),
74 Suffix: []byte(""),
75 },
76 {
77 Hash: specs.InnerSpec.Hash,
78 Prefix: []byte("\x04\x08\x02\x20"),
79 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"),
80 },
81 {
82 Hash: specs.InnerSpec.Hash,
83 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"),
84 Suffix: []byte(""),
85 },
86 },
87 },
88 },
89
90 // rootmulti proof
91 ics23.CommitmentProof_Exist{
92 Exist: &ics23.ExistenceProof{
93 Key: []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
94 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"),
95 Leaf: &ics23.LeafOp{
96 Hash: specs.LeafSpec.Hash,
97 PrehashKey: specs.LeafSpec.PrehashKey,
98 PrehashValue: specs.LeafSpec.PrehashValue,
99 Length: specs.LeafSpec.Length,
100 Prefix: []byte("\x00"),
101 },
102 Path: []*ics23.InnerOp{},
103 },
104 },
105 }
106 recvPacket := types.MsgRecvPacket{
107 Packet: types.Packet{
108 Sequence: 1,
109 SourceClient: counterpartyID,
110 DestinationClient: clientID,
111 TimeoutTimestamp: uint64(time.Now().Add(time.Hour).Unix()),
112 Payloads: []types.Payload{{
113 SourcePort: transfer.PortID,
114 DestinationPort: transfer.PortID,
115 Encoding: transfer.EncodingProtobuf,
116 Value: payloadBz,
117 Version: transfer.V1,
118 }},
119 },
120 ProofCommitment: proof,
121 ProofHeight: trustedHeight,
122 }
123
124 core.RecvPacket(cross(cur), recvPacket)
125}
126
127// Output:
128// Payload proto: Cgd1cGhvdG9uEgMxMDAaCmF0b25lMXVzZXIiKGcxd3ltdTQ3ZHJocjBrdXEyMDk4bTc5Mmx5dGd0ajJueXg3N3lyc20=
129//
130// receiver voucher balance after RecvPacket: 100
131//
132// receiver voucher balance after Transfer: 0
133
134// Events:
135// [
136// {
137// "type": "create_client",
138// "attrs": [
139// {
140// "key": "client_id",
141// "value": "07-tendermint-1"
142// },
143// {
144// "key": "client_type",
145// "value": "07-tendermint"
146// },
147// {
148// "key": "consensus_heights",
149// "value": "2/2"
150// }
151// ],
152// "pkg_path": "gno.land/r/aib/ibc/core"
153// },
154// {
155// "type": "recv_packet",
156// "attrs": [
157// {
158// "key": "packet_source_client",
159// "value": "07-tendermint-42"
160// },
161// {
162// "key": "packet_dest_client",
163// "value": "07-tendermint-1"
164// },
165// {
166// "key": "packet_sequence",
167// "value": "1"
168// },
169// {
170// "key": "packet_timeout_timestamp",
171// "value": "1234571490"
172// },
173// {
174// "key": "encoded_packet_hex",
175// "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a7b0a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a440a077570686f746f6e12033130301a0a61746f6e6531757365722228673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d"
176// }
177// ],
178// "pkg_path": "gno.land/r/aib/ibc/core"
179// },
180// {
181// "type": "denomination",
182// "attrs": [
183// {
184// "key": "denom_hash",
185// "value": "CAEF9CA8CE6C302D73A831A49E34E59149D3A9AD96CCEBDFBF62F6D5710D92D8"
186// },
187// {
188// "key": "denom",
189// "value": "{\"base\":\"uphoton\",\"path\":\"transfer/07-tendermint-1/uphoton\",\"denom\":\"ibc/CAEF9CA8CE6C302D73A831A49E34E59149D3A9AD96CCEBDFBF62F6D5710D92D8\"}"
190// }
191// ],
192// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
193// },
194// {
195// "type": "register",
196// "attrs": [
197// {
198// "key": "pkgpath",
199// "value": "gno.land/r/aib/ibc/apps/transfer"
200// },
201// {
202// "key": "slug",
203// "value": "CAEF9CA8CE6C302D73A831A49E34E59149D3A9AD96CCEBDFBF62F6D5710D92D8"
204// }
205// ],
206// "pkg_path": "gno.land/r/demo/defi/grc20reg"
207// },
208// {
209// "type": "Transfer",
210// "attrs": [
211// {
212// "key": "token",
213// "value": "gno.land/r/aib/ibc/apps/transfer.CAEF9CA8CE6"
214// },
215// {
216// "key": "from",
217// "value": ""
218// },
219// {
220// "key": "to",
221// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
222// },
223// {
224// "key": "value",
225// "value": "100"
226// }
227// ],
228// "pkg_path": "gno.land/p/demo/tokens/grc20"
229// },
230// {
231// "type": "fungible_token_packet",
232// "attrs": [
233// {
234// "key": "sender",
235// "value": "atone1user"
236// },
237// {
238// "key": "receiver",
239// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
240// },
241// {
242// "key": "denom",
243// "value": "transfer/07-tendermint-1/uphoton"
244// },
245// {
246// "key": "amount",
247// "value": "100"
248// },
249// {
250// "key": "memo",
251// "value": ""
252// },
253// {
254// "key": "success",
255// "value": "true"
256// }
257// ],
258// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
259// },
260// {
261// "type": "write_acknowledgement",
262// "attrs": [
263// {
264// "key": "packet_source_client",
265// "value": "07-tendermint-42"
266// },
267// {
268// "key": "packet_dest_client",
269// "value": "07-tendermint-1"
270// },
271// {
272// "key": "packet_sequence",
273// "value": "1"
274// },
275// {
276// "key": "packet_timeout_timestamp",
277// "value": "1234571490"
278// },
279// {
280// "key": "encoded_packet_hex",
281// "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a7b0a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a440a077570686f746f6e12033130301a0a61746f6e6531757365722228673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d"
282// },
283// {
284// "key": "encoded_acknowledgement_hex",
285// "value": "0a117b22726573756c74223a2241513d3d227d"
286// }
287// ],
288// "pkg_path": "gno.land/r/aib/ibc/core"
289// },
290// {
291// "type": "send_packet",
292// "attrs": [
293// {
294// "key": "packet_source_client",
295// "value": "07-tendermint-1"
296// },
297// {
298// "key": "packet_dest_client",
299// "value": "07-tendermint-42"
300// },
301// {
302// "key": "packet_sequence",
303// "value": "1"
304// },
305// {
306// "key": "packet_timeout_timestamp",
307// "value": "1234571490"
308// },
309// {
310// "key": "encoded_packet_hex",
311// "value": "0801120f30372d74656e6465726d696e742d311a1030372d74656e6465726d696e742d343220e2a1d8cc042a9a010a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a630a207472616e736665722f30372d74656e6465726d696e742d312f7570686f746f6e12033130301a28673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d220a61746f6e6531757365722a046d656d6f"
312// }
313// ],
314// "pkg_path": "gno.land/r/aib/ibc/core"
315// },
316// {
317// "type": "Transfer",
318// "attrs": [
319// {
320// "key": "token",
321// "value": "gno.land/r/aib/ibc/apps/transfer.CAEF9CA8CE6"
322// },
323// {
324// "key": "from",
325// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
326// },
327// {
328// "key": "to",
329// "value": ""
330// },
331// {
332// "key": "value",
333// "value": "100"
334// }
335// ],
336// "pkg_path": "gno.land/p/demo/tokens/grc20"
337// },
338// {
339// "type": "ibc_transfer",
340// "attrs": [
341// {
342// "key": "sender",
343// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
344// },
345// {
346// "key": "receiver",
347// "value": "atone1user"
348// },
349// {
350// "key": "denom",
351// "value": "transfer/07-tendermint-1/uphoton"
352// },
353// {
354// "key": "amount",
355// "value": "100"
356// },
357// {
358// "key": "memo",
359// "value": "memo"
360// }
361// ],
362// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
363// }
364// ]