Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 0bafe98

Browse files
mvinesmergify[bot]
authored andcommitted
spl-token transfer now supports ALL keyword
1 parent 43d808c commit 0bafe98

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

token/cli/src/main.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use solana_account_decoder::{
1010
use solana_clap_utils::{
1111
fee_payer::fee_payer_arg,
1212
input_parsers::{pubkey_of_signer, pubkeys_of_multiple_signers, signer_of, value_of},
13-
input_validators::{is_amount, is_parsable, is_url, is_valid_pubkey, is_valid_signer},
13+
input_validators::{
14+
is_amount, is_amount_or_all, is_parsable, is_url, is_valid_pubkey, is_valid_signer,
15+
},
1416
keypair::{pubkey_from_path, signer_from_path, DefaultSigner},
1517
nonce::*,
1618
offline::{self, *},
@@ -423,16 +425,35 @@ fn resolve_mint_info(
423425
fn command_transfer(
424426
config: &Config,
425427
sender: Pubkey,
426-
ui_amount: f64,
428+
ui_amount: Option<f64>,
427429
recipient: Pubkey,
428430
fund_recipient: bool,
429431
mint_address: Option<Pubkey>,
430432
mint_decimals: Option<u8>,
431433
) -> CommandResult {
434+
let sender_balance = config
435+
.rpc_client
436+
.get_token_account_balance(&sender)
437+
.map_err(|err| {
438+
format!(
439+
"Error: Failed to get token balance of sender address {}: {}",
440+
sender, err
441+
)
442+
})?
443+
.ui_amount;
444+
let ui_amount = ui_amount.unwrap_or(sender_balance);
445+
432446
println!(
433447
"Transfer {} tokens\n Sender: {}\n Recipient: {}",
434448
ui_amount, sender, recipient
435449
);
450+
if ui_amount > sender_balance {
451+
return Err(format!(
452+
"Error: Sender has insufficient funds, current balance is {}",
453+
sender_balance
454+
)
455+
.into());
456+
}
436457

437458
let (mint_pubkey, decimals) = resolve_mint_info(config, &sender, mint_address, mint_decimals)?;
438459
let amount = spl_token::ui_amount_to_amount(ui_amount, decimals);
@@ -1287,12 +1308,12 @@ fn main() {
12871308
)
12881309
.arg(
12891310
Arg::with_name("amount")
1290-
.validator(is_amount)
1311+
.validator(is_amount_or_all)
12911312
.value_name("TOKEN_AMOUNT")
12921313
.takes_value(true)
12931314
.index(2)
12941315
.required(true)
1295-
.help("Amount to send, in tokens"),
1316+
.help("Amount to send, in tokens; accepts keyword ALL"),
12961317
)
12971318
.arg(
12981319
Arg::with_name("recipient")
@@ -1804,7 +1825,11 @@ fn main() {
18041825
let sender = pubkey_of_signer(arg_matches, "sender", &mut wallet_manager)
18051826
.unwrap()
18061827
.unwrap();
1807-
let amount = value_t_or_exit!(arg_matches, "amount", f64);
1828+
1829+
let amount = match matches.value_of("amount").unwrap() {
1830+
"ALL" => None,
1831+
amount => Some(amount.parse::<f64>().unwrap()),
1832+
};
18081833
let recipient = pubkey_of_signer(arg_matches, "recipient", &mut wallet_manager)
18091834
.unwrap()
18101835
.unwrap();

0 commit comments

Comments
 (0)