From 07fd91b527643e39dd5c4c6971f9fe6fac0eb51e Mon Sep 17 00:00:00 2001 From: x100111010 <167847953+x100111010@users.noreply.github.com> Date: Sat, 26 Oct 2024 13:25:44 +0200 Subject: [PATCH] added `--send-all` option * added `--send-all` option to `send` command, allowing users to send their entire mature balance minus an optional priority fee * retains feature parity between Go and Rust implementations (the Go node has `--send-all`) --- cli/src/modules/guide.txt | 2 ++ cli/src/modules/send.rs | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cli/src/modules/guide.txt b/cli/src/modules/guide.txt index b5c9bf85b..5bc309c5a 100644 --- a/cli/src/modules/guide.txt +++ b/cli/src/modules/guide.txt @@ -41,6 +41,8 @@ the selected account to an account named 'pete' (starts with a 'p' letter). `send
` - Sends funds to a destination address. +`send
--send-all` - Sends all available funds to a destination address. + `estimate ` - Provides a fee and UTXO consumption estimate for a transaction of a given amount. `sweep` - Sweeps account UTXOs to reduce the UTXO size. diff --git a/cli/src/modules/send.rs b/cli/src/modules/send.rs index 773861dd4..652190424 100644 --- a/cli/src/modules/send.rs +++ b/cli/src/modules/send.rs @@ -12,13 +12,26 @@ impl Send { let account = ctx.wallet().account()?; if argv.len() < 2 { - tprintln!(ctx, "usage: send
"); + tprintln!(ctx, "usage: send
"); return Ok(()); } let address = Address::try_from(argv.first().unwrap().as_str())?; - let amount_sompi = try_parse_required_nonzero_kaspa_as_sompi_u64(argv.get(1))?; let priority_fee_sompi = try_parse_optional_kaspa_as_sompi_i64(argv.get(2))?.unwrap_or(0); + // handle --send-all + let amount_sompi = if argv.get(1).unwrap() == "--send-all" { + // get mature balance from account + let balance = account.balance().ok_or_else(|| Error::Custom("Failed to retrieve account balance".into()))?; + let mature_balance_sompi = balance.mature; + + // subtract priority fee from mature balance + mature_balance_sompi + .checked_sub(priority_fee_sompi.try_into().unwrap_or(0)) + .ok_or_else(|| Error::Custom("Insufficient funds to cover the priority fee.".into()))? + } else { + // parse amount if not using --send-all + try_parse_required_nonzero_kaspa_as_sompi_u64(argv.get(1))? + }; let outputs = PaymentOutputs::from((address.clone(), amount_sompi)); let abortable = Abortable::default(); let (wallet_secret, payment_secret) = ctx.ask_wallet_secret(Some(&account)).await?;