gcli 2.0.0-pre.1

Gear program CLI
Documentation
// Copyright (C) Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

//! Command `send`
use crate::{app::App, utils::HexBytes};
use anyhow::Result;
use clap::Parser;
use colored::Colorize;
use gear_core::ids::ActorId;

/// Send a message.
#[derive(Clone, Debug, Parser)]
pub struct Send {
    /// Destination address, in SS58 or hex format.
    destination: ActorId,

    /// Message payload, as hex string.
    #[arg(short, long, default_value = "0x")]
    payload: HexBytes,

    /// Operation gas limit.
    ///
    /// Defaults to the estimated gas limit
    /// required for the operation.
    #[arg(short, long)]
    gas_limit: Option<u64>,

    /// Value to send with the message.
    #[arg(short, long, default_value = "0")]
    value: u128,
}

impl Send {
    pub async fn exec(self, app: &mut App) -> Result<()> {
        let api = app.signed_api().await?;
        let gas_limit = if let Some(gas_limit) = self.gas_limit {
            gas_limit
        } else {
            api.calculate_handle_gas(self.destination, &self.payload, self.value, false)
                .await?
                .min_limit
        };

        let message_id = api
            .send_message_bytes(
                self.destination,
                self.payload.clone(),
                gas_limit,
                self.value,
            )
            .await?
            .value;

        println!("Successfully sent the message");
        println!();
        println!("{} {}", "Message ID:".bold(), message_id);
        Ok(())
    }
}