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

Commit 697a9e3

Browse files
committed
token-cli: convert create-account to use client
1 parent 9205a61 commit 697a9e3

File tree

1 file changed

+22
-56
lines changed

1 file changed

+22
-56
lines changed

token/cli/src/main.rs

Lines changed: 22 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -499,77 +499,43 @@ async fn command_set_interest_rate(
499499

500500
async fn command_create_account(
501501
config: &Config<'_>,
502-
token: Pubkey,
502+
token_pubkey: Pubkey,
503503
owner: Pubkey,
504504
maybe_account: Option<Pubkey>,
505505
bulk_signers: Vec<Arc<dyn Signer>>,
506506
) -> CommandResult {
507-
let minimum_balance_for_rent_exemption = if !config.sign_only {
508-
config
509-
.program_client
510-
.get_minimum_balance_for_rent_exemption(Account::LEN)
511-
.await?
512-
} else {
513-
0
514-
};
507+
let token = token_client_from_config(config, &token_pubkey);
515508

516-
let mint_info = config.get_mint_info(&token, None).await?;
517-
let (account, system_account_ok, instructions) = if let Some(account) = maybe_account {
518-
println_display(config, format!("Creating account {}", account));
519-
(
520-
account,
521-
false,
522-
vec![
523-
system_instruction::create_account(
524-
&config.fee_payer.pubkey(),
525-
&account,
526-
minimum_balance_for_rent_exemption,
527-
Account::LEN as u64,
528-
&mint_info.program_id,
529-
),
530-
initialize_account(&mint_info.program_id, &account, &token, &owner)?,
531-
],
532-
)
509+
let (account, associated) = if let Some(account) = maybe_account {
510+
(account, false)
533511
} else {
534-
let account =
535-
get_associated_token_address_with_program_id(&owner, &token, &mint_info.program_id);
536-
println_display(config, format!("Creating account {}", account));
537-
(
538-
account,
539-
true,
540-
vec![create_associated_token_account(
541-
&config.fee_payer.pubkey(),
542-
&owner,
543-
&token,
544-
&mint_info.program_id,
545-
)],
546-
)
512+
(token.get_associated_token_address(&owner), true)
547513
};
548514

515+
println_display(config, format!("Creating account {}", account));
516+
549517
if !config.sign_only {
550-
if let Some(account_data) = config
551-
.rpc_client
552-
.get_account_with_commitment(&account, config.rpc_client.commitment())
553-
.await?
554-
.value
555-
{
556-
if !(account_data.owner == system_program::id() && system_account_ok) {
518+
if let Some(account_data) = config.program_client.get_account(account).await? {
519+
if account_data.owner != system_program::id() || !associated {
557520
return Err(format!("Error: Account already exists: {}", account).into());
558521
}
559522
}
560523
}
561524

562-
let tx_return = handle_tx(
563-
&CliSignerInfo {
564-
signers: bulk_signers,
565-
},
566-
config,
567-
false,
568-
minimum_balance_for_rent_exemption,
569-
instructions,
570-
)
571-
.await?;
525+
let res = if associated {
526+
token.create_associated_token_account(&owner).await
527+
} else {
528+
let signer = bulk_signers
529+
.iter()
530+
.find(|signer| signer.pubkey() == account)
531+
.unwrap_or_else(|| panic!("No signer provided for account {}", account));
572532

533+
token
534+
.create_auxiliary_token_account_with_extension_space(&**signer, &owner, vec![])
535+
.await
536+
}?;
537+
538+
let tx_return = finish_tx(config, &res, false).await?;
573539
Ok(match tx_return {
574540
TransactionReturnData::CliSignature(signature) => {
575541
config.output_format.formatted_string(&signature)

0 commit comments

Comments
 (0)