@@ -10,7 +10,9 @@ use solana_account_decoder::{
10
10
use solana_clap_utils:: {
11
11
fee_payer:: fee_payer_arg,
12
12
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
+ } ,
14
16
keypair:: { pubkey_from_path, signer_from_path, DefaultSigner } ,
15
17
nonce:: * ,
16
18
offline:: { self , * } ,
@@ -423,16 +425,35 @@ fn resolve_mint_info(
423
425
fn command_transfer (
424
426
config : & Config ,
425
427
sender : Pubkey ,
426
- ui_amount : f64 ,
428
+ ui_amount : Option < f64 > ,
427
429
recipient : Pubkey ,
428
430
fund_recipient : bool ,
429
431
mint_address : Option < Pubkey > ,
430
432
mint_decimals : Option < u8 > ,
431
433
) -> 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
+
432
446
println ! (
433
447
"Transfer {} tokens\n Sender: {}\n Recipient: {}" ,
434
448
ui_amount, sender, recipient
435
449
) ;
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
+ }
436
457
437
458
let ( mint_pubkey, decimals) = resolve_mint_info ( config, & sender, mint_address, mint_decimals) ?;
438
459
let amount = spl_token:: ui_amount_to_amount ( ui_amount, decimals) ;
@@ -1287,12 +1308,12 @@ fn main() {
1287
1308
)
1288
1309
. arg (
1289
1310
Arg :: with_name ( "amount" )
1290
- . validator ( is_amount )
1311
+ . validator ( is_amount_or_all )
1291
1312
. value_name ( "TOKEN_AMOUNT" )
1292
1313
. takes_value ( true )
1293
1314
. index ( 2 )
1294
1315
. required ( true )
1295
- . help ( "Amount to send, in tokens" ) ,
1316
+ . help ( "Amount to send, in tokens; accepts keyword ALL " ) ,
1296
1317
)
1297
1318
. arg (
1298
1319
Arg :: with_name ( "recipient" )
@@ -1804,7 +1825,11 @@ fn main() {
1804
1825
let sender = pubkey_of_signer ( arg_matches, "sender" , & mut wallet_manager)
1805
1826
. unwrap ( )
1806
1827
. 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
+ } ;
1808
1833
let recipient = pubkey_of_signer ( arg_matches, "recipient" , & mut wallet_manager)
1809
1834
. unwrap ( )
1810
1835
. unwrap ( ) ;
0 commit comments