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

Commit a3e8af2

Browse files
spl-token-cli: use ATA by default in wrap/unwrap commands (#1492)
* Wrap to ATA by default * Unwrap ATA by default * Review comments
1 parent 5df3461 commit a3e8af2

File tree

1 file changed

+69
-30
lines changed

1 file changed

+69
-30
lines changed

token/cli/src/main.rs

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -688,38 +688,68 @@ fn command_thaw(config: &Config, account: Pubkey, mint_address: Option<Pubkey>)
688688
Ok(Some((0, vec![instructions])))
689689
}
690690

691-
fn command_wrap(config: &Config, sol: f64, account: Pubkey) -> CommandResult {
691+
fn command_wrap(config: &Config, sol: f64, account: Option<Pubkey>) -> CommandResult {
692692
let lamports = sol_to_lamports(sol);
693-
println!("Wrapping {} SOL into {}", sol, account);
694693

695-
let instructions = vec![
696-
system_instruction::create_account(
697-
&config.owner,
698-
&account,
699-
lamports,
700-
Account::LEN as u64,
701-
&spl_token::id(),
702-
),
703-
initialize_account(
704-
&spl_token::id(),
705-
&account,
706-
&native_mint::id(),
707-
&config.owner,
708-
)?,
709-
];
694+
let instructions = if let Some(account) = account {
695+
println!("Wrapping {} SOL into {}", sol, account);
696+
vec![
697+
system_instruction::create_account(
698+
&config.owner,
699+
&account,
700+
lamports,
701+
Account::LEN as u64,
702+
&spl_token::id(),
703+
),
704+
initialize_account(
705+
&spl_token::id(),
706+
&account,
707+
&native_mint::id(),
708+
&config.owner,
709+
)?,
710+
]
711+
} else {
712+
let account = get_associated_token_address(&config.owner, &native_mint::id());
713+
714+
if !config.sign_only {
715+
if let Some(account_data) = config
716+
.rpc_client
717+
.get_account_with_commitment(&account, config.rpc_client.commitment())?
718+
.value
719+
{
720+
if account_data.owner != system_program::id() {
721+
return Err(format!("Error: Account already exists: {}", account).into());
722+
}
723+
}
724+
}
725+
726+
println!("Wrapping {} SOL into {}", sol, account);
727+
vec![
728+
system_instruction::transfer(&config.owner, &account, lamports),
729+
create_associated_token_account(&config.fee_payer, &config.owner, &native_mint::id()),
730+
]
731+
};
710732
if !config.sign_only {
711733
check_owner_balance(config, lamports)?;
712734
}
713735
Ok(Some((0, vec![instructions])))
714736
}
715737

716-
fn command_unwrap(config: &Config, address: Pubkey) -> CommandResult {
738+
fn command_unwrap(config: &Config, address: Option<Pubkey>) -> CommandResult {
739+
let use_associated_account = address.is_none();
740+
let address =
741+
address.unwrap_or_else(|| get_associated_token_address(&config.owner, &native_mint::id()));
717742
println!("Unwrapping {}", address);
718743
if !config.sign_only {
719-
println!(
720-
" Amount: {} SOL",
721-
lamports_to_sol(config.rpc_client.get_balance(&address)?),
722-
);
744+
let lamports = config.rpc_client.get_balance(&address)?;
745+
if lamports == 0 {
746+
if use_associated_account {
747+
return Err("No wrapped SOL in associated account; did you mean to specify an auxiliary address?".to_string().into());
748+
} else {
749+
return Err(format!("No wrapped SOL in {}", address).into());
750+
}
751+
}
752+
println!(" Amount: {} SOL", lamports_to_sol(lamports),);
723753
}
724754
println!(" Recipient: {}", &config.owner);
725755

@@ -1577,6 +1607,12 @@ fn main() {
15771607
.required(true)
15781608
.help("Amount of SOL to wrap"),
15791609
)
1610+
.arg(
1611+
Arg::with_name("create_aux_account")
1612+
.takes_value(false)
1613+
.long("create-aux-account")
1614+
.help("Wrap SOL in an auxillary account instead of associated token account"),
1615+
)
15801616
.nonce_args(true)
15811617
.offline_args(),
15821618
)
@@ -1589,8 +1625,8 @@ fn main() {
15891625
.value_name("TOKEN_ACCOUNT_ADDRESS")
15901626
.takes_value(true)
15911627
.index(1)
1592-
.required(true)
1593-
.help("The address of the token account to unwrap"),
1628+
.help("The address of the auxiliary token account to unwrap \
1629+
[default: associated token account for --owner]"),
15941630
)
15951631
.arg(multisig_signer_arg())
15961632
.nonce_args(true)
@@ -2050,15 +2086,18 @@ fn main() {
20502086
}
20512087
("wrap", Some(arg_matches)) => {
20522088
let amount = value_t_or_exit!(arg_matches, "amount", f64);
2053-
let (signer, account) = new_throwaway_signer();
2054-
let account = account.unwrap();
2055-
bulk_signers.push(signer);
2089+
let account = if arg_matches.is_present("create_aux_account") {
2090+
let (signer, account) = new_throwaway_signer();
2091+
bulk_signers.push(signer);
2092+
account
2093+
} else {
2094+
// No need to add a signer when creating an associated token account
2095+
None
2096+
};
20562097
command_wrap(&config, amount, account)
20572098
}
20582099
("unwrap", Some(arg_matches)) => {
2059-
let address = pubkey_of_signer(arg_matches, "address", &mut wallet_manager)
2060-
.unwrap()
2061-
.unwrap();
2100+
let address = pubkey_of_signer(arg_matches, "address", &mut wallet_manager).unwrap();
20622101
command_unwrap(&config, address)
20632102
}
20642103
("approve", Some(arg_matches)) => {

0 commit comments

Comments
 (0)