token-cli: use token client for command_burn

This commit is contained in:
hanako mumei 2022-08-25 11:11:03 -07:00 committed by hana
parent e540b172db
commit 8e196e5e77
4 changed files with 88 additions and 70 deletions

View File

@ -1061,40 +1061,22 @@ async fn command_burn(
let mint_address = config.check_account(&source, mint_address).await?; let mint_address = config.check_account(&source, mint_address).await?;
let mint_info = config.get_mint_info(&mint_address, mint_decimals).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 amount = spl_token::ui_amount_to_amount(ui_amount, mint_info.decimals);
let decimals = if use_unchecked_instruction {
let mut instructions = if use_unchecked_instruction { None
vec![burn(
&mint_info.program_id,
&source,
&mint_info.address,
&source_owner,
&config.multisigner_pubkeys,
amount,
)?]
} else { } else {
vec![burn_checked( Some(mint_info.decimals)
&mint_info.program_id,
&source,
&mint_info.address,
&source_owner,
&config.multisigner_pubkeys,
amount,
mint_info.decimals,
)?]
}; };
let token = token_client_from_config(config, &mint_info.program_id, &mint_info.address);
if let Some(text) = memo { 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 { let res = token
signers: bulk_signers, .burn(&source, &source_owner, amount, decimals, &bulk_signers)
}, .await?;
config,
false, let tx_return = finish_tx(config, &res, false).await?;
0,
instructions,
)
.await?;
Ok(match tx_return { Ok(match tx_return {
TransactionReturnData::CliSignature(signature) => { TransactionReturnData::CliSignature(signature) => {
config.output_format.formatted_string(&signature) config.output_format.formatted_string(&signature)

View File

@ -685,47 +685,38 @@ where
} }
/// Burn tokens from account /// Burn tokens from account
pub async fn burn<S: Signer>( pub async fn burn<S: Signers>(
&self, &self,
source: &Pubkey, source: &Pubkey,
authority: &S, authority: &Pubkey,
amount: u64, amount: u64,
decimals: Option<u8>,
signing_keypairs: &S,
) -> TokenResult<T::Output> { ) -> TokenResult<T::Output> {
self.process_ixs( let multisig_signers = self.get_multisig_signers(authority, signing_keypairs);
&[instruction::burn(
&self.program_id,
source,
&self.pubkey,
&authority.pubkey(),
&[],
amount,
)?],
&[authority],
)
.await
}
/// Burn tokens from account let instructions = if let Some(decimals) = decimals {
pub async fn burn_checked<S: Signer>( [instruction::burn_checked(
&self,
source: &Pubkey,
authority: &S,
amount: u64,
decimals: u8,
) -> TokenResult<T::Output> {
self.process_ixs(
&[instruction::burn_checked(
&self.program_id, &self.program_id,
source, source,
&self.pubkey, &self.pubkey,
&authority.pubkey(), authority,
&[], &multisig_signers.iter().collect::<Vec<_>>(),
amount, amount,
decimals, decimals,
)?], )?]
&[authority], } else {
) [instruction::burn(
.await &self.program_id,
source,
&self.pubkey,
authority,
&multisig_signers.iter().collect::<Vec<_>>(),
amount,
)?]
};
self.process_ixs(&instructions, signing_keypairs).await
} }
/// Approve a delegate to spend tokens /// Approve a delegate to spend tokens

View File

@ -42,17 +42,32 @@ async fn run_basic(context: TestContext) {
.unwrap(); .unwrap();
// unchecked is ok // 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 // checked is ok
token token
.burn_checked(&alice_account, &alice, 1, decimals) .burn(
&alice_account,
&alice.pubkey(),
1,
Some(decimals),
&vec![&alice],
)
.await .await
.unwrap(); .unwrap();
// burn too much is not ok // burn too much is not ok
let error = token let error = token
.burn_checked(&alice_account, &alice, amount, decimals) .burn(
&alice_account,
&alice.pubkey(),
amount,
Some(decimals),
&vec![&alice],
)
.await .await
.unwrap_err(); .unwrap_err();
assert_eq!( assert_eq!(
@ -67,7 +82,13 @@ async fn run_basic(context: TestContext) {
// wrong signer // wrong signer
let error = token let error = token
.burn_checked(&alice_account, &bob, 1, decimals) .burn(
&alice_account,
&bob.pubkey(),
1,
Some(decimals),
&vec![&bob],
)
.await .await
.unwrap_err(); .unwrap_err();
assert_eq!( assert_eq!(
@ -131,11 +152,20 @@ async fn run_self_owned(context: TestContext) {
.unwrap(); .unwrap();
// unchecked is ok // 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 // checked is ok
token token
.burn_checked(&alice_account, &alice, 1, decimals) .burn(
&alice_account,
&alice.pubkey(),
1,
Some(decimals),
&vec![&alice],
)
.await .await
.unwrap(); .unwrap();
} }
@ -223,7 +253,13 @@ async fn run_burn_and_close_system_or_incinerator(context: TestContext, non_owne
// but anyone can burn it // but anyone can burn it
token token
.burn_checked(&non_owner_account, &carlos, 1, decimals) .burn(
&non_owner_account,
&carlos.pubkey(),
1,
Some(decimals),
&vec![&carlos],
)
.await .await
.unwrap(); .unwrap();

View File

@ -131,9 +131,18 @@ async fn run_basic(
.unwrap(); .unwrap();
// burn is ok // burn is ok
token.burn(&alice_account, &bob, 1).await.unwrap();
token 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 .await
.unwrap(); .unwrap();