@@ -211,6 +211,12 @@ module zkgm::zkgm {
211
211
salt: vector <u8 >,
212
212
}
213
213
214
+ public struct SendCtx {
215
+ channel_id: u32 ,
216
+ salt: vector <u8 >,
217
+ instructions: vector <Instruction >
218
+ }
219
+
214
220
public struct AckCtx {
215
221
packet_ctxs: vector <ZkgmPacketAckCtx >,
216
222
packets: vector <Packet >,
@@ -333,41 +339,71 @@ module zkgm::zkgm {
333
339
);
334
340
}
335
341
342
+ public fun begin_send (
343
+ channel_id: u32 ,
344
+ salt: vector <u8 >,
345
+ ): SendCtx {
346
+ SendCtx {
347
+ channel_id,
348
+ salt,
349
+ instructions: vector ::empty ()
350
+ }
351
+ }
352
+
336
353
public fun send_with_coin <T >(
337
354
zkgm: &mut RelayStore ,
338
355
ibc_store: &mut ibc::IBCStore ,
339
- clock: &Clock ,
340
356
coin: Coin <T >,
341
- channel_id: u32 ,
342
- timeout_height: u64 ,
343
- timeout_timestamp: u64 ,
344
- salt: vector <u8 >,
345
357
version: u8 ,
346
358
opcode: u8 ,
347
359
operand: vector <u8 >,
360
+ mut send_ctx: SendCtx ,
348
361
ctx: &mut TxContext
349
- ) {
362
+ ): SendCtx {
350
363
let instruction = instruction::new (version, opcode, operand);
351
364
let sender = ctx.sender ();
352
365
let coin = zkgm.verify_internal <T >(
353
366
ibc_store,
354
367
option::some (coin),
355
368
sender,
356
- channel_id,
369
+ send_ctx. channel_id,
357
370
0 ,
358
371
instruction,
359
372
ctx
360
373
);
361
374
// This guarantees that the coin is used by some instruction
362
375
coin.destroy_none ();
363
376
364
- let zkgm_pack = zkgm_packet::new (salt, 0 , instruction);
377
+ send_ctx.instructions.push_back (instruction);
378
+
379
+ send_ctx
380
+ }
381
+
382
+ public fun end_send (
383
+ ibc_store: &mut ibc::IBCStore ,
384
+ clock: &Clock ,
385
+ timeout_height: u64 ,
386
+ timeout_timestamp: u64 ,
387
+ send_ctx: SendCtx ,
388
+ ctx: &mut TxContext ,
389
+ ) {
390
+ let SendCtx { channel_id, salt, instructions } = send_ctx;
391
+
392
+ let instruction = if (instructions.length () > 1 ) {
393
+ instructions.do_ref !(|instr| {
394
+ assert !(helper::is_allowed_batch_instruction (instr.opcode ()), E_INVALID_BATCH_INSTRUCTION );
395
+ });
396
+ instruction::new (INSTR_VERSION_0 , OP_BATCH , batch::new (instructions).encode ())
397
+ } else {
398
+ instructions[0 ]
399
+ };
400
+
365
401
ibc_store.send_packet (
366
402
clock,
367
403
channel_id,
368
404
timeout_height,
369
405
timeout_timestamp,
370
- zkgm_packet::encode (&zkgm_pack ),
406
+ zkgm_packet::encode (&zkgm_packet:: new (salt, 0 , instruction) ),
371
407
IbcAppWitness {},
372
408
ctx
373
409
);
0 commit comments