Search Apps Documentation Source Content File Folder Download Copy Actions Download

z4aaa_on_recv_packet_filetest.gno

13.46 Kb · 403 lines
  1// PKGPATH: gno.land/r/aib/main
  2package main
  3
  4import (
  5	"chain"
  6	"encoding/base64"
  7	"encoding/hex"
  8	"testing"
  9	"time"
 10
 11	tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
 12	"gno.land/p/aib/ibc/types"
 13	"gno.land/p/aib/ics23"
 14	"gno.land/p/nt/testutils/v0"
 15	"gno.land/r/aib/ibc/apps/testing/grc20test"
 16	"gno.land/r/aib/ibc/apps/transfer"
 17	"gno.land/r/aib/ibc/core"
 18)
 19
 20// OnRecvPacket: success w/ non-IBC GRC20 token
 21//
 22// Scenario: chain A sent a GRC20 token to chain B (SendPacket escrows),
 23// then chain B sends it back (RecvPacket unescrows via isGRC20Alias).
 24func main(cur realm) {
 25	var (
 26		chainID       = "chain-id-2"
 27		trustedHeight = types.NewHeight(2, 2)
 28		clientState   = tmtesting.NewClientState(chainID, trustedHeight)
 29		// NOTE this apphash was provided by the gen-proof command below.
 30		apphash, _     = hex.DecodeString("b945cf87b8d1cfb958cf90c83fc5d9404ef9d8cd7cc2cec622e7386507a61d2e")
 31		trustedValset  = tmtesting.GenValset()
 32		consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
 33		counterpartyID = "07-tendermint-2"
 34	)
 35	clientID := core.CreateClient(cross(cur), clientState, consensusState)
 36	core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
 37	relayer := cur.Previous().Address()
 38
 39	signer := cur.Address()
 40
 41	// Mint GRC20 tokens to signer and approve transfer app
 42	grc20test.Mint(cross(cur), signer, 1000)
 43	transferAppAddr := chain.PackageAddress("gno.land/r/aib/ibc/apps/transfer")
 44	grc20test.Approve(cross(cur), signer, transferAppAddr, 100)
 45
 46	// Step 1: Transfer to escrow GRC20 tokens (chain A → chain B)
 47	grc20regKey := "gno.land/r/aib/ibc/apps/testing/grc20test." + grc20test.Slug
 48	testing.SetOriginCaller(signer)
 49	println("signer grc20 balance before Transfer:", grc20test.BalanceOf(signer))
 50	transfer.Transfer(cross(cur), clientID, "atone1user", grc20regKey, 100, uint64(time.Now().Add(time.Hour).Unix()), "")
 51	println("signer grc20 balance after Transfer:", grc20test.BalanceOf(signer))
 52
 53	// Step 2: RecvPacket returning the GRC20 tokens (chain B → chain A)
 54	// The counterparty sends back with denom prefixed: transfer/{counterpartyID}/{alias}
 55	receiver := testutils.TestAddress("receiver")
 56	recvPayload := transfer.NewFungibleTokenPacketData("transfer/" + counterpartyID + "/" + transfer.GRC20Alias(grc20regKey), "100", "atone1user", receiver.String(), "")
 57	recvPayloadBz := recvPayload.ProtoMarshal()
 58	println("\nRecv payload proto:", base64.StdEncoding.EncodeToString(recvPayloadBz))
 59
 60	specs := ics23.IavlSpec()
 61	// NOTE code generated by:
 62	// go run -C ./cmd/gen-proof . 'prefix2' '07-tendermint-2' 'packet' '{"sequence":1,"source_client":"07-tendermint-2","destination_client":"07-tendermint-1","timeout_timestamp":1234571490,"payloads":[{"source_port":"transfer","destination_port":"transfer","version":"ics20-1","encoding":"application/x-protobuf","value":"Ckd0cmFuc2Zlci8wNy10ZW5kZXJtaW50LTIvZ25vLmxhbmQ6cjphaWI6aWJjOmFwcHM6dGVzdGluZzpncmMyMHRlc3QuVEVTVBIDMTAwGgphdG9uZTF1c2VyIihnMXdmamt4ZXRmd2VqaHloNmx0YTA0N2g2bHRhMDQ3aDZsbG04Mnls"}]}'
 63	proof := []ics23.CommitmentProof{
 64
 65		// iavl proof
 66		ics23.CommitmentProof_Exist{
 67			Exist: &ics23.ExistenceProof{
 68				Key:   []byte("\x70\x72\x65\x66\x69\x78\x32\x30\x37\x2d\x74\x65\x6e\x64\x65\x72\x6d\x69\x6e\x74\x2d\x32\x01\x00\x00\x00\x00\x00\x00\x00\x01"),
 69				Value: []byte("\x3e\x39\x2e\x85\xf9\xfd\xa4\xfe\xe3\x53\xeb\xf6\xc0\xe3\xfb\x7c\x93\x58\xf5\x67\x79\x7a\x90\xf7\xd9\x8d\x2d\xac\xb3\xdc\xd6\x50"),
 70				Leaf: &ics23.LeafOp{
 71					Hash:         specs.LeafSpec.Hash,
 72					PrehashKey:   specs.LeafSpec.PrehashKey,
 73					PrehashValue: specs.LeafSpec.PrehashValue,
 74					Length:       specs.LeafSpec.Length,
 75					Prefix:       []byte("\x00\x02\x02"),
 76				},
 77				Path: []*ics23.InnerOp{
 78					{
 79						Hash:   specs.InnerSpec.Hash,
 80						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"),
 81						Suffix: []byte(""),
 82					},
 83					{
 84						Hash:   specs.InnerSpec.Hash,
 85						Prefix: []byte("\x04\x08\x02\x20"),
 86						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"),
 87					},
 88					{
 89						Hash:   specs.InnerSpec.Hash,
 90						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"),
 91						Suffix: []byte(""),
 92					},
 93				},
 94			},
 95		},
 96
 97		// rootmulti proof
 98		ics23.CommitmentProof_Exist{
 99			Exist: &ics23.ExistenceProof{
100				Key:   []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
101				Value: []byte("\x32\xa2\x15\x7f\xfd\xa4\xb0\x9b\xec\xc3\x4f\xb9\x9f\x6a\x90\xfd\x1c\x81\x99\xba\x53\x41\xd9\xf5\x89\x20\x90\xbe\x92\x59\xd7\x7f"),
102				Leaf: &ics23.LeafOp{
103					Hash:         specs.LeafSpec.Hash,
104					PrehashKey:   specs.LeafSpec.PrehashKey,
105					PrehashValue: specs.LeafSpec.PrehashValue,
106					Length:       specs.LeafSpec.Length,
107					Prefix:       []byte("\x00"),
108				},
109				Path: []*ics23.InnerOp{},
110			},
111		},
112	}
113
114	recvPacket := types.MsgRecvPacket{
115		Packet: types.Packet{
116			Sequence:          1,
117			SourceClient:      counterpartyID,
118			DestinationClient: clientID,
119			TimeoutTimestamp:  uint64(time.Now().Add(time.Hour).Unix()),
120			Payloads: []types.Payload{{
121				SourcePort:      transfer.PortID,
122				DestinationPort: transfer.PortID,
123				Encoding:        transfer.EncodingProtobuf,
124				Value:           recvPayloadBz,
125				Version:         transfer.V1,
126			}},
127		},
128		ProofCommitment: proof,
129		ProofHeight:     trustedHeight,
130	}
131
132	println("\nreceiver grc20 balance before RecvPacket:", grc20test.BalanceOf(receiver))
133	println("transfer app grc20 balance before RecvPacket:", grc20test.BalanceOf(transferAppAddr))
134
135	testing.SetOriginCaller(relayer)
136	res := core.RecvPacket(cross(cur), recvPacket)
137
138	println("\nack res:", res)
139	println("\nreceiver grc20 balance after RecvPacket:", grc20test.BalanceOf(receiver))
140	println("transfer app grc20 balance after RecvPacket:", grc20test.BalanceOf(transferAppAddr))
141}
142
143// Output:
144// signer grc20 balance before Transfer: 1000
145// signer grc20 balance after Transfer: 900
146//
147// Recv payload proto: Ckd0cmFuc2Zlci8wNy10ZW5kZXJtaW50LTIvZ25vLmxhbmQ6cjphaWI6aWJjOmFwcHM6dGVzdGluZzpncmMyMHRlc3QuVEVTVBIDMTAwGgphdG9uZTF1c2VyIihnMXdmamt4ZXRmd2VqaHloNmx0YTA0N2g2bHRhMDQ3aDZsbG04Mnls
148//
149// receiver grc20 balance before RecvPacket: 0
150// transfer app grc20 balance before RecvPacket: 100
151//
152// ack res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
153//
154// receiver grc20 balance after RecvPacket: 100
155// transfer app grc20 balance after RecvPacket: 0
156
157// Events:
158// [
159//   {
160//     "type": "create_client",
161//     "attrs": [
162//       {
163//         "key": "client_id",
164//         "value": "07-tendermint-1"
165//       },
166//       {
167//         "key": "client_type",
168//         "value": "07-tendermint"
169//       },
170//       {
171//         "key": "consensus_heights",
172//         "value": "2/2"
173//       }
174//     ],
175//     "pkg_path": "gno.land/r/aib/ibc/core"
176//   },
177//   {
178//     "type": "Transfer",
179//     "attrs": [
180//       {
181//         "key": "token",
182//         "value": "gno.land/r/aib/ibc/apps/testing/grc20test.TEST"
183//       },
184//       {
185//         "key": "from",
186//         "value": ""
187//       },
188//       {
189//         "key": "to",
190//         "value": "g1gjs8nfdrd55z3xu9gjcatnj29vkwuyx4ffqxd9"
191//       },
192//       {
193//         "key": "value",
194//         "value": "1000"
195//       }
196//     ],
197//     "pkg_path": "gno.land/p/demo/tokens/grc20"
198//   },
199//   {
200//     "type": "Approval",
201//     "attrs": [
202//       {
203//         "key": "token",
204//         "value": "gno.land/r/aib/ibc/apps/testing/grc20test.TEST"
205//       },
206//       {
207//         "key": "owner",
208//         "value": "g1gjs8nfdrd55z3xu9gjcatnj29vkwuyx4ffqxd9"
209//       },
210//       {
211//         "key": "spender",
212//         "value": "g1tp3gk4quumurav4858hjfdy6hxtyffwmnxyr00"
213//       },
214//       {
215//         "key": "value",
216//         "value": "100"
217//       }
218//     ],
219//     "pkg_path": "gno.land/p/demo/tokens/grc20"
220//   },
221//   {
222//     "type": "send_packet",
223//     "attrs": [
224//       {
225//         "key": "packet_source_client",
226//         "value": "07-tendermint-1"
227//       },
228//       {
229//         "key": "packet_dest_client",
230//         "value": "07-tendermint-2"
231//       },
232//       {
233//         "key": "packet_sequence",
234//         "value": "1"
235//       },
236//       {
237//         "key": "packet_timeout_timestamp",
238//         "value": "1234571490"
239//       },
240//       {
241//         "key": "encoded_packet_hex",
242//         "value": "0801120f30372d74656e6465726d696e742d311a0f30372d74656e6465726d696e742d3220e2a1d8cc042aa2010a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a6b0a2e676e6f2e6c616e643a723a6169623a6962633a617070733a74657374696e673a6772633230746573742e5445535412033130301a286731676a73386e6664726435357a33787539676a6361746e6a3239766b7775797834666671786439220a61746f6e653175736572"
243//       }
244//     ],
245//     "pkg_path": "gno.land/r/aib/ibc/core"
246//   },
247//   {
248//     "type": "Transfer",
249//     "attrs": [
250//       {
251//         "key": "token",
252//         "value": "gno.land/r/aib/ibc/apps/testing/grc20test.TEST"
253//       },
254//       {
255//         "key": "from",
256//         "value": "g1gjs8nfdrd55z3xu9gjcatnj29vkwuyx4ffqxd9"
257//       },
258//       {
259//         "key": "to",
260//         "value": "g1tp3gk4quumurav4858hjfdy6hxtyffwmnxyr00"
261//       },
262//       {
263//         "key": "value",
264//         "value": "100"
265//       }
266//     ],
267//     "pkg_path": "gno.land/p/demo/tokens/grc20"
268//   },
269//   {
270//     "type": "ibc_transfer",
271//     "attrs": [
272//       {
273//         "key": "sender",
274//         "value": "g1gjs8nfdrd55z3xu9gjcatnj29vkwuyx4ffqxd9"
275//       },
276//       {
277//         "key": "receiver",
278//         "value": "atone1user"
279//       },
280//       {
281//         "key": "denom",
282//         "value": "gno.land:r:aib:ibc:apps:testing:grc20test.TEST"
283//       },
284//       {
285//         "key": "amount",
286//         "value": "100"
287//       },
288//       {
289//         "key": "memo",
290//         "value": ""
291//       }
292//     ],
293//     "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
294//   },
295//   {
296//     "type": "recv_packet",
297//     "attrs": [
298//       {
299//         "key": "packet_source_client",
300//         "value": "07-tendermint-2"
301//       },
302//       {
303//         "key": "packet_dest_client",
304//         "value": "07-tendermint-1"
305//       },
306//       {
307//         "key": "packet_sequence",
308//         "value": "1"
309//       },
310//       {
311//         "key": "packet_timeout_timestamp",
312//         "value": "1234571490"
313//       },
314//       {
315//         "key": "encoded_packet_hex",
316//         "value": "0801120f30372d74656e6465726d696e742d321a0f30372d74656e6465726d696e742d3120e2a1d8cc042abc010a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a84010a477472616e736665722f30372d74656e6465726d696e742d322f676e6f2e6c616e643a723a6169623a6962633a617070733a74657374696e673a6772633230746573742e5445535412033130301a0a61746f6e6531757365722228673177666a6b7865746677656a687968366c746130343768366c746130343768366c6c6d3832796c"
317//       }
318//     ],
319//     "pkg_path": "gno.land/r/aib/ibc/core"
320//   },
321//   {
322//     "type": "Transfer",
323//     "attrs": [
324//       {
325//         "key": "token",
326//         "value": "gno.land/r/aib/ibc/apps/testing/grc20test.TEST"
327//       },
328//       {
329//         "key": "from",
330//         "value": "g1tp3gk4quumurav4858hjfdy6hxtyffwmnxyr00"
331//       },
332//       {
333//         "key": "to",
334//         "value": "g1wfjkxetfwejhyh6lta047h6lta047h6llm82yl"
335//       },
336//       {
337//         "key": "value",
338//         "value": "100"
339//       }
340//     ],
341//     "pkg_path": "gno.land/p/demo/tokens/grc20"
342//   },
343//   {
344//     "type": "fungible_token_packet",
345//     "attrs": [
346//       {
347//         "key": "sender",
348//         "value": "atone1user"
349//       },
350//       {
351//         "key": "receiver",
352//         "value": "g1wfjkxetfwejhyh6lta047h6lta047h6llm82yl"
353//       },
354//       {
355//         "key": "denom",
356//         "value": "gno.land:r:aib:ibc:apps:testing:grc20test.TEST"
357//       },
358//       {
359//         "key": "amount",
360//         "value": "100"
361//       },
362//       {
363//         "key": "memo",
364//         "value": ""
365//       },
366//       {
367//         "key": "success",
368//         "value": "true"
369//       }
370//     ],
371//     "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
372//   },
373//   {
374//     "type": "write_acknowledgement",
375//     "attrs": [
376//       {
377//         "key": "packet_source_client",
378//         "value": "07-tendermint-2"
379//       },
380//       {
381//         "key": "packet_dest_client",
382//         "value": "07-tendermint-1"
383//       },
384//       {
385//         "key": "packet_sequence",
386//         "value": "1"
387//       },
388//       {
389//         "key": "packet_timeout_timestamp",
390//         "value": "1234571490"
391//       },
392//       {
393//         "key": "encoded_packet_hex",
394//         "value": "0801120f30372d74656e6465726d696e742d321a0f30372d74656e6465726d696e742d3120e2a1d8cc042abc010a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a84010a477472616e736665722f30372d74656e6465726d696e742d322f676e6f2e6c616e643a723a6169623a6962633a617070733a74657374696e673a6772633230746573742e5445535412033130301a0a61746f6e6531757365722228673177666a6b7865746677656a687968366c746130343768366c746130343768366c6c6d3832796c"
395//       },
396//       {
397//         "key": "encoded_acknowledgement_hex",
398//         "value": "0a117b22726573756c74223a2241513d3d227d"
399//       }
400//     ],
401//     "pkg_path": "gno.land/r/aib/ibc/core"
402//   }
403// ]