Search Apps Documentation Source Content File Folder Download Copy Actions Download

z0c_voucher_send_filetest.gno

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