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
74
75
76
77
78
//! Command `send`
use crate::{
    api::{
        generated::api::{
            gear::{calls::SendMessage, Event as GearEvent},
            runtime_types::gear_core::ids::ProgramId,
        },
        signer::Signer,
        Api,
    },
    result::Result,
};
use structopt::StructOpt;

/// Sends a message to a program or to another account.
///
/// 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).
///
/// To avoid an undefined behavior a check is made that the destination address
/// is not a program in uninitialized state. If the opposite holds true,
/// the message is not enqueued for processing.
///
/// Parameters:
/// - `destination`: the message destination.
/// - `payload`: in case of a program destination, parameters of the `handle` function.
/// - `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.
///
/// Emits the following events:
/// - `DispatchMessageEnqueued(MessageInfo)` when dispatch message is placed in the queue.
#[derive(StructOpt, Debug)]
pub struct Send {
    /// Send to
    pub destination: String,
    /// Send payload
    #[structopt(default_value = "0x")]
    pub payload: String,
    /// Send gas limit
    #[structopt(default_value = "0")]
    pub gas_limit: u64,
    /// Send value
    #[structopt(default_value = "0")]
    pub value: u128,
}

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

        r?;

        Ok(())
    }

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

        signer
            .send_message(SendMessage {
                destination: ProgramId(destination),
                payload: hex::decode(self.payload.trim_start_matches("0x"))?,
                gas_limit: self.gas_limit,
                value: self.value,
            })
            .await?;

        Ok(())
    }
}