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