kaspa_cli_lib/modules/
send.rs

1use crate::imports::*;
2
3#[derive(Default, Handler)]
4#[help("Send a Kaspa transaction to a public address")]
5pub struct Send;
6
7impl Send {
8    async fn main(self: Arc<Self>, ctx: &Arc<dyn Context>, argv: Vec<String>, _cmd: &str) -> Result<()> {
9        // address, amount, priority fee
10        let ctx = ctx.clone().downcast_arc::<KaspaCli>()?;
11
12        let account = ctx.wallet().account()?;
13
14        if argv.len() < 2 {
15            tprintln!(ctx, "usage: send <address> <amount> <priority fee>");
16            return Ok(());
17        }
18
19        let address = Address::try_from(argv.first().unwrap().as_str())?;
20        let amount_sompi = try_parse_required_nonzero_kaspa_as_sompi_u64(argv.get(1))?;
21        let priority_fee_sompi = try_parse_optional_kaspa_as_sompi_i64(argv.get(2))?.unwrap_or(0);
22        let outputs = PaymentOutputs::from((address.clone(), amount_sompi));
23        let abortable = Abortable::default();
24        let (wallet_secret, payment_secret) = ctx.ask_wallet_secret(Some(&account)).await?;
25
26        // let ctx_ = ctx.clone();
27        let (summary, _ids) = account
28            .send(
29                outputs.into(),
30                priority_fee_sompi.into(),
31                None,
32                wallet_secret,
33                payment_secret,
34                &abortable,
35                Some(Arc::new(move |_ptx| {
36                    // tprintln!(ctx_, "Sending transaction: {}", ptx.id());
37                })),
38            )
39            .await?;
40
41        tprintln!(ctx, "Send - {summary}");
42        tprintln!(ctx, "\nSending {} KAS to {address}, tx ids:", sompi_to_kaspa_string(amount_sompi));
43        // tprintln!(ctx, "{}\n", ids.into_iter().map(|a| a.to_string()).collect::<Vec<_>>().join("\n"));
44
45        Ok(())
46    }
47}