From 8e196e5e77a7949f181000b3505847954cb4bc16 Mon Sep 17 00:00:00 2001 From: hanako mumei <81144685+2501babe@users.noreply.github.com> Date: Thu, 25 Aug 2022 11:11:03 -0700 Subject: [PATCH] token-cli: use token client for command_burn --- token/cli/src/main.rs | 42 +++++------------- token/client/src/token.rs | 53 ++++++++++------------- token/program-2022-test/tests/burn.rs | 50 ++++++++++++++++++--- token/program-2022-test/tests/delegate.rs | 13 +++++- 4 files changed, 88 insertions(+), 70 deletions(-) diff --git a/token/cli/src/main.rs b/token/cli/src/main.rs index 906c4c65..ff23c693 100644 --- a/token/cli/src/main.rs +++ b/token/cli/src/main.rs @@ -1061,40 +1061,22 @@ async fn command_burn( let mint_address = config.check_account(&source, mint_address).await?; let mint_info = config.get_mint_info(&mint_address, mint_decimals).await?; let amount = spl_token::ui_amount_to_amount(ui_amount, mint_info.decimals); - - let mut instructions = if use_unchecked_instruction { - vec![burn( - &mint_info.program_id, - &source, - &mint_info.address, - &source_owner, - &config.multisigner_pubkeys, - amount, - )?] + let decimals = if use_unchecked_instruction { + None } else { - vec![burn_checked( - &mint_info.program_id, - &source, - &mint_info.address, - &source_owner, - &config.multisigner_pubkeys, - amount, - mint_info.decimals, - )?] + Some(mint_info.decimals) }; + + let token = token_client_from_config(config, &mint_info.program_id, &mint_info.address); if let Some(text) = memo { - instructions.push(spl_memo::build_memo(text.as_bytes(), &[&config.fee_payer])); + token.with_memo(text); } - let tx_return = handle_tx( - &CliSignerInfo { - signers: bulk_signers, - }, - config, - false, - 0, - instructions, - ) - .await?; + + let res = token + .burn(&source, &source_owner, amount, decimals, &bulk_signers) + .await?; + + let tx_return = finish_tx(config, &res, false).await?; Ok(match tx_return { TransactionReturnData::CliSignature(signature) => { config.output_format.formatted_string(&signature) diff --git a/token/client/src/token.rs b/token/client/src/token.rs index b052fd41..2fdc5f4a 100644 --- a/token/client/src/token.rs +++ b/token/client/src/token.rs @@ -685,47 +685,38 @@ where } /// Burn tokens from account - pub async fn burn( + pub async fn burn( &self, source: &Pubkey, - authority: &S, + authority: &Pubkey, amount: u64, + decimals: Option, + signing_keypairs: &S, ) -> TokenResult { - self.process_ixs( - &[instruction::burn( - &self.program_id, - source, - &self.pubkey, - &authority.pubkey(), - &[], - amount, - )?], - &[authority], - ) - .await - } + let multisig_signers = self.get_multisig_signers(authority, signing_keypairs); - /// Burn tokens from account - pub async fn burn_checked( - &self, - source: &Pubkey, - authority: &S, - amount: u64, - decimals: u8, - ) -> TokenResult { - self.process_ixs( - &[instruction::burn_checked( + let instructions = if let Some(decimals) = decimals { + [instruction::burn_checked( &self.program_id, source, &self.pubkey, - &authority.pubkey(), - &[], + authority, + &multisig_signers.iter().collect::>(), amount, decimals, - )?], - &[authority], - ) - .await + )?] + } else { + [instruction::burn( + &self.program_id, + source, + &self.pubkey, + authority, + &multisig_signers.iter().collect::>(), + amount, + )?] + }; + + self.process_ixs(&instructions, signing_keypairs).await } /// Approve a delegate to spend tokens diff --git a/token/program-2022-test/tests/burn.rs b/token/program-2022-test/tests/burn.rs index e0e43c35..2ba44f96 100644 --- a/token/program-2022-test/tests/burn.rs +++ b/token/program-2022-test/tests/burn.rs @@ -42,17 +42,32 @@ async fn run_basic(context: TestContext) { .unwrap(); // unchecked is ok - token.burn(&alice_account, &alice, 1).await.unwrap(); + token + .burn(&alice_account, &alice.pubkey(), 1, None, &vec![&alice]) + .await + .unwrap(); // checked is ok token - .burn_checked(&alice_account, &alice, 1, decimals) + .burn( + &alice_account, + &alice.pubkey(), + 1, + Some(decimals), + &vec![&alice], + ) .await .unwrap(); // burn too much is not ok let error = token - .burn_checked(&alice_account, &alice, amount, decimals) + .burn( + &alice_account, + &alice.pubkey(), + amount, + Some(decimals), + &vec![&alice], + ) .await .unwrap_err(); assert_eq!( @@ -67,7 +82,13 @@ async fn run_basic(context: TestContext) { // wrong signer let error = token - .burn_checked(&alice_account, &bob, 1, decimals) + .burn( + &alice_account, + &bob.pubkey(), + 1, + Some(decimals), + &vec![&bob], + ) .await .unwrap_err(); assert_eq!( @@ -131,11 +152,20 @@ async fn run_self_owned(context: TestContext) { .unwrap(); // unchecked is ok - token.burn(&alice_account, &alice, 1).await.unwrap(); + token + .burn(&alice_account, &alice.pubkey(), 1, None, &vec![&alice]) + .await + .unwrap(); // checked is ok token - .burn_checked(&alice_account, &alice, 1, decimals) + .burn( + &alice_account, + &alice.pubkey(), + 1, + Some(decimals), + &vec![&alice], + ) .await .unwrap(); } @@ -223,7 +253,13 @@ async fn run_burn_and_close_system_or_incinerator(context: TestContext, non_owne // but anyone can burn it token - .burn_checked(&non_owner_account, &carlos, 1, decimals) + .burn( + &non_owner_account, + &carlos.pubkey(), + 1, + Some(decimals), + &vec![&carlos], + ) .await .unwrap(); diff --git a/token/program-2022-test/tests/delegate.rs b/token/program-2022-test/tests/delegate.rs index a2a37680..1a72e528 100644 --- a/token/program-2022-test/tests/delegate.rs +++ b/token/program-2022-test/tests/delegate.rs @@ -131,9 +131,18 @@ async fn run_basic( .unwrap(); // burn is ok - token.burn(&alice_account, &bob, 1).await.unwrap(); token - .burn_checked(&alice_account, &bob, 1, decimals) + .burn(&alice_account, &bob.pubkey(), 1, None, &vec![&bob]) + .await + .unwrap(); + token + .burn( + &alice_account, + &bob.pubkey(), + 1, + Some(decimals), + &vec![&bob], + ) .await .unwrap();