1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Command `reply`
use crate::{
    api::{
        generated::api::{
            gear::{calls::SendReply, Event as GearEvent},
            runtime_types::gear_core::ids::MessageId,
        },
        signer::Signer,
        Api,
    },
    result::Result,
};
use structopt::StructOpt;

/// Sends a reply message.
///
/// The origin must be Signed and the sender must have sufficient funds to pay
/// for `gas` and `value` (in case the latter is being transferred).
///
/// Parameters:
/// - `reply_to_id`: the original message id.
/// - `payload`: data expected by the original sender.
/// - `gas_limit`: maximum amount of gas the program can spend before it is halted.
/// - `value`: balance to be transferred to the program once it's been created.
///
/// - `DispatchMessageEnqueued(H256)` when dispatch message is placed in the queue.
#[derive(StructOpt, Debug)]
pub struct Reply {
    /// Reply to
    reply_to_id: String,
    /// Reply payload
    #[structopt(default_value = "0x")]
    payload: String,
    /// Reply gas limit
    #[structopt(default_value = "0")]
    gas_limit: u64,
    /// Reply value
    #[structopt(default_value = "0")]
    value: u128,
}

impl Reply {
    pub async fn exec(&self, signer: Signer) -> Result<()> {
        let events = signer.events().await?;
        let r = tokio::try_join!(
            self.send_reply(&signer),
            Api::wait_for(events, |event| {
                matches!(event, GearEvent::MessagesDispatched { .. })
            })
        );

        r?;

        Ok(())
    }

    async fn send_reply(&self, signer: &Signer) -> Result<()> {
        let mut reply_to_id = [0; 32];
        reply_to_id
            .copy_from_slice(hex::decode(self.reply_to_id.trim_start_matches("0x"))?.as_ref());

        signer
            .send_reply(SendReply {
                reply_to_id: MessageId(reply_to_id),
                payload: hex::decode(self.payload.trim_start_matches("0x"))?,
                gas_limit: self.gas_limit,
                value: self.value,
            })
            .await?;

        Ok(())
    }
}