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

Commit 80819fa

Browse files
Transfer from ATA by default (#1487)
* Transfer from ATA by default * Update token/cli/src/main.rs
1 parent 603a943 commit 80819fa

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

token/cli/src/main.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,17 @@ fn resolve_mint_info(
455455
.rpc_client
456456
.get_token_account(&token_account)?
457457
.ok_or_else(|| format!("Could not find token account {}", token_account))?;
458-
Ok((
459-
Pubkey::from_str(&source_account.mint)?,
460-
source_account.token_amount.decimals,
461-
))
458+
let source_mint = Pubkey::from_str(&source_account.mint)?;
459+
if let Some(mint) = mint_address {
460+
if source_mint != mint {
461+
return Err(format!(
462+
"Source {:?} does not contain {:?} tokens",
463+
token_account, mint
464+
)
465+
.into());
466+
}
467+
}
468+
Ok((source_mint, source_account.token_amount.decimals))
462469
} else {
463470
Ok((
464471
mint_address.unwrap_or_default(),
@@ -470,15 +477,20 @@ fn resolve_mint_info(
470477
#[allow(clippy::too_many_arguments)]
471478
fn command_transfer(
472479
config: &Config,
473-
sender: Pubkey,
480+
token: Pubkey,
474481
ui_amount: Option<f64>,
475482
recipient: Pubkey,
483+
sender: Option<Pubkey>,
476484
allow_unfunded_recipient: bool,
477485
fund_recipient: bool,
478-
mint_address: Option<Pubkey>,
479486
mint_decimals: Option<u8>,
480487
) -> CommandResult {
481-
let (mint_pubkey, decimals) = resolve_mint_info(config, &sender, mint_address, mint_decimals)?;
488+
let sender = if let Some(sender) = sender {
489+
sender
490+
} else {
491+
get_associated_token_address(&config.owner, &token)
492+
};
493+
let (mint_pubkey, decimals) = resolve_mint_info(config, &sender, Some(token), mint_decimals)?;
482494
let sender_token_amount = config
483495
.rpc_client
484496
.get_token_account_balance(&sender)
@@ -1399,13 +1411,13 @@ fn main() {
13991411
SubCommand::with_name("transfer")
14001412
.about("Transfer tokens between accounts")
14011413
.arg(
1402-
Arg::with_name("sender")
1414+
Arg::with_name("token")
14031415
.validator(is_valid_pubkey)
1404-
.value_name("SENDER_TOKEN_ACCOUNT_ADDRESS")
1416+
.value_name("TOKEN_ADDRESS")
14051417
.takes_value(true)
14061418
.index(1)
14071419
.required(true)
1408-
.help("The token account address of the sender"),
1420+
.help("Token to transfer"),
14091421
)
14101422
.arg(
14111423
Arg::with_name("amount")
@@ -1427,6 +1439,15 @@ fn main() {
14271439
Otherwise assume the recipient address is a user wallet and transfer to \
14281440
the associated token account")
14291441
)
1442+
.arg(
1443+
Arg::with_name("from")
1444+
.validator(is_valid_pubkey)
1445+
.value_name("SENDER_TOKEN_ACCOUNT_ADDRESS")
1446+
.takes_value(true)
1447+
.long("from")
1448+
.help("Specify the sending token account \
1449+
[default: owner's associated token account]")
1450+
)
14301451
.arg(
14311452
Arg::with_name("allow_unfunded_recipient")
14321453
.long("allow-unfunded-recipient")
@@ -1446,9 +1467,9 @@ fn main() {
14461467
.help("Create the associated token account for the recipient if doesn't already exist")
14471468
)
14481469
.arg(multisig_signer_arg())
1449-
.mint_args()
1470+
.arg(mint_decimals_arg())
14501471
.nonce_args(true)
1451-
.offline_args_config(&SignOnlyNeedsFullMintSpec{}),
1472+
.offline_args_config(&SignOnlyNeedsMintDecimals{}),
14521473
)
14531474
.subcommand(
14541475
SubCommand::with_name("burn")
@@ -1964,31 +1985,29 @@ fn main() {
19641985
)
19651986
}
19661987
("transfer", Some(arg_matches)) => {
1967-
let sender = pubkey_of_signer(arg_matches, "sender", &mut wallet_manager)
1988+
let token = pubkey_of_signer(arg_matches, "token", &mut wallet_manager)
19681989
.unwrap()
19691990
.unwrap();
1970-
19711991
let amount = match matches.value_of("amount").unwrap() {
19721992
"ALL" => None,
19731993
amount => Some(amount.parse::<f64>().unwrap()),
19741994
};
19751995
let recipient = pubkey_of_signer(arg_matches, "recipient", &mut wallet_manager)
19761996
.unwrap()
19771997
.unwrap();
1978-
let mint_address =
1979-
pubkey_of_signer(arg_matches, MINT_ADDRESS_ARG.name, &mut wallet_manager).unwrap();
1998+
let sender = pubkey_of_signer(arg_matches, "from", &mut wallet_manager).unwrap();
19801999
let mint_decimals = value_of::<u8>(&arg_matches, MINT_DECIMALS_ARG.name);
19812000
let fund_recipient = matches.is_present("fund_recipient");
19822001
let allow_unfunded_recipient = matches.is_present("allow_empty_recipient")
19832002
|| matches.is_present("allow_unfunded_recipient");
19842003
command_transfer(
19852004
&config,
1986-
sender,
2005+
token,
19872006
amount,
19882007
recipient,
2008+
sender,
19892009
allow_unfunded_recipient,
19902010
fund_recipient,
1991-
mint_address,
19922011
mint_decimals,
19932012
)
19942013
}

0 commit comments

Comments
 (0)