token-cli: Fix unexpected behavior when closing a wrapped SOL account with another wrapped SOL account as recipient (#3278)

* Check if recipient is a wrapped account

* Make clippy and fmt happy

* Avoided mutable, more functional clarity

* Add test for closing wrapped sol account

* wrap the default account

Co-authored-by: Jon Cinque <jon.cinque@gmail.com>

* Fix formatting and failing tests

Co-authored-by: Jon Cinque <jon.cinque@gmail.com>
This commit is contained in:
Alisamar Husain 2022-07-17 20:20:39 +05:30 committed by GitHub
parent 937b64da37
commit b39efe8fb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 51 additions and 3 deletions

View File

@ -1351,7 +1351,9 @@ fn command_close(
recipient: Pubkey,
bulk_signers: BulkSigners,
) -> CommandResult {
if !config.sign_only {
let is_recipient_wrapped = if config.sign_only {
false
} else {
let source_account = config
.rpc_client
.get_token_account(&account)?
@ -1375,15 +1377,24 @@ fn command_close(
)
.into());
}
}
let instructions = vec![close_account(
let recipient_account = config.rpc_client.get_token_account(&recipient)?;
recipient_account.map(|x| x.is_native).unwrap_or(false)
};
let mut instructions = vec![close_account(
&config.program_id,
&account,
&recipient,
&close_authority,
&config.multisigner_pubkeys,
)?];
if is_recipient_wrapped {
instructions.push(sync_native(&config.program_id, &recipient)?);
}
let tx_return = handle_tx(
&CliSignerInfo {
signers: bulk_signers,
@ -3635,6 +3646,43 @@ mod tests {
assert_eq!(ui_account.token_amount.amount, "90");
}
#[test]
fn close_wrapped_sol_account() {
let (test_validator, payer) = validator_for_test();
let config = test_config(&test_validator, &payer, &spl_token::id());
let bulk_signers: Vec<Box<dyn Signer>> = vec![Box::new(clone_keypair(&payer))];
let token = create_token(&config, &payer);
let source = create_associated_account(&config, &payer, token);
let ui_amount = 10.0;
command_wrap(&config, ui_amount, payer.pubkey(), None, bulk_signers).unwrap();
let recipient = get_associated_token_address_with_program_id(
&payer.pubkey(),
&native_mint::id(),
&config.program_id,
);
let _result = process_test_command(
&config,
&payer,
&[
"spl-token",
CommandName::Close.into(),
"--address",
&source.to_string(),
"--recipient",
&recipient.to_string(),
],
);
let ui_account = config
.rpc_client
.get_token_account(&recipient)
.unwrap()
.unwrap();
assert_eq!(ui_account.token_amount.amount, "10000000000");
}
#[test]
fn disable_mint_authority() {
let (test_validator, payer) = validator_for_test();