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_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)

View File

@ -685,47 +685,38 @@ where
}
/// Burn tokens from account
pub async fn burn<S: Signer>(
pub async fn burn<S: Signers>(
&self,
source: &Pubkey,
authority: &S,
authority: &Pubkey,
amount: u64,
decimals: Option<u8>,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
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<S: Signer>(
&self,
source: &Pubkey,
authority: &S,
amount: u64,
decimals: u8,
) -> TokenResult<T::Output> {
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::<Vec<_>>(),
amount,
decimals,
)?],
&[authority],
)
.await
)?]
} else {
[instruction::burn(
&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

View File

@ -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();

View File

@ -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();