token-client: move decimals from Token methods to struct

This commit is contained in:
hanako mumei 2022-10-05 11:02:21 -07:00 committed by hana
parent 4842217611
commit a5a565b56c
14 changed files with 198 additions and 348 deletions

View File

@ -96,6 +96,7 @@ async fn process_create_escrow_account(
program_client.clone(),
&new_program_id,
new_mint,
None,
payer.clone(),
);
@ -536,9 +537,15 @@ mod test {
client: Arc<dyn ProgramClient<T>>,
) -> Token<T> {
let mint_account = Keypair::new();
let token = Token::new(client, program_id, &mint_account.pubkey(), payer);
let token = Token::new(
client,
program_id,
&mint_account.pubkey(),
Some(decimals),
payer,
);
token
.create_mint(mint_authority, None, decimals, vec![], &[&mint_account])
.create_mint(mint_authority, None, vec![], &[&mint_account])
.await
.unwrap();
token
@ -670,7 +677,6 @@ mod test {
&burn_from,
&mint_authority.pubkey(),
amount,
Some(decimals),
&[&mint_authority],
)
.await
@ -688,7 +694,6 @@ mod test {
&escrow,
&mint_authority.pubkey(),
amount,
Some(decimals),
&[&mint_authority],
)
.await
@ -782,7 +787,6 @@ mod test {
&burn_from,
&mint_authority.pubkey(),
amount,
Some(decimals),
&[&mint_authority],
)
.await
@ -794,7 +798,6 @@ mod test {
&escrow,
&mint_authority.pubkey(),
amount,
Some(decimals),
&[&mint_authority],
)
.await

View File

@ -76,9 +76,15 @@ async fn setup_mint<T: SendTransaction>(
client: Arc<dyn ProgramClient<T>>,
) -> Token<T> {
let mint_account = Keypair::new();
let token = Token::new(client, program_id, &mint_account.pubkey(), payer);
let token = Token::new(
client,
program_id,
&mint_account.pubkey(),
Some(decimals),
payer,
);
token
.create_mint(mint_authority, None, decimals, vec![], &[&mint_account])
.create_mint(mint_authority, None, vec![], &[&mint_account])
.await
.unwrap();
token
@ -131,7 +137,6 @@ async fn success(original_program_id: Pubkey, new_program_id: Pubkey) {
&original_account,
&mint_authority_pubkey,
token_amount,
Some(decimals),
&[&mint_authority],
)
.await
@ -152,7 +157,6 @@ async fn success(original_program_id: Pubkey, new_program_id: Pubkey) {
&escrow_account,
&mint_authority_pubkey,
token_amount,
Some(decimals),
&[&mint_authority],
)
.await
@ -242,7 +246,6 @@ async fn fail_incorrect_escrow_derivation(original_program_id: Pubkey, new_progr
&original_account,
&mint_authority_pubkey,
token_amount,
Some(decimals),
&[&mint_authority],
)
.await
@ -263,7 +266,6 @@ async fn fail_incorrect_escrow_derivation(original_program_id: Pubkey, new_progr
&escrow_account,
&mint_authority_pubkey,
token_amount,
Some(decimals),
&[&mint_authority],
)
.await
@ -351,7 +353,6 @@ async fn fail_decimals_mismatch(original_program_id: Pubkey, new_program_id: Pub
&original_account,
&mint_authority_pubkey,
token_amount,
Some(original_decimals),
&[&mint_authority],
)
.await
@ -372,7 +373,6 @@ async fn fail_decimals_mismatch(original_program_id: Pubkey, new_program_id: Pub
&escrow_account,
&mint_authority_pubkey,
token_amount,
Some(new_decimals),
&[&mint_authority],
)
.await

View File

@ -320,11 +320,13 @@ pub fn signers_of(
fn token_client_from_config(
config: &Config<'_>,
token_pubkey: &Pubkey,
decimals: Option<u8>,
) -> Token<ProgramRpcClientSendTransaction> {
let token = Token::new(
config.program_client.clone(),
&config.program_id,
token_pubkey,
decimals,
config.fee_payer.clone(),
);
@ -373,7 +375,7 @@ async fn command_create_token(
),
);
let token = token_client_from_config(config, &token_pubkey);
let token = token_client_from_config(config, &token_pubkey, Some(decimals));
let freeze_authority = if enable_freeze { Some(authority) } else { None };
@ -400,7 +402,6 @@ async fn command_create_token(
.create_mint(
&authority,
freeze_authority.as_ref(),
decimals,
extensions,
&bulk_signers,
)
@ -430,7 +431,7 @@ async fn command_set_interest_rate(
rate_bps: i16,
bulk_signers: Vec<Arc<dyn Signer>>,
) -> CommandResult {
let token = token_client_from_config(config, &token_pubkey);
let token = token_client_from_config(config, &token_pubkey, None);
if !config.sign_only {
let mint_account = config.get_account_checked(&token_pubkey).await?;
@ -489,7 +490,7 @@ async fn command_create_account(
immutable_owner: bool,
bulk_signers: Vec<Arc<dyn Signer>>,
) -> CommandResult {
let token = token_client_from_config(config, &token_pubkey);
let token = token_client_from_config(config, &token_pubkey, None);
let mut extensions = vec![];
let (account, is_associated) = if let Some(account) = maybe_account {
@ -571,7 +572,7 @@ async fn command_create_multisig(
);
// default is safe here because create_multisig doesnt use it
let token = token_client_from_config(config, &Pubkey::default());
let token = token_client_from_config(config, &Pubkey::default(), None);
let res = token
.create_multisig(
@ -710,7 +711,7 @@ async fn command_authorize(
(Pubkey::default(), COption::None)
};
let token = token_client_from_config(config, &mint_pubkey);
let token = token_client_from_config(config, &mint_pubkey, None);
println_display(
config,
@ -771,7 +772,6 @@ async fn command_transfer(
no_wait: bool,
allow_non_system_account_recipient: bool,
) -> CommandResult {
let token = token_client_from_config(config, &token_pubkey);
let mint_info = config.get_mint_info(&token_pubkey, mint_decimals).await?;
// if the user got the decimals wrong, they may well have calculated the transfer amount wrong
@ -798,6 +798,8 @@ async fn command_transfer(
Some(mint_info.decimals)
};
let token = token_client_from_config(config, &token_pubkey, decimals);
// pubkey of the actual account we are sending from
let sender = if let Some(sender) = sender {
sender
@ -956,7 +958,6 @@ async fn command_transfer(
&recipient_owner,
&sender_owner,
transfer_balance,
decimals,
&bulk_signers,
)
.await?
@ -967,7 +968,6 @@ async fn command_transfer(
&recipient_token_account,
&sender_owner,
transfer_balance,
decimals,
&bulk_signers,
)
.await?
@ -1010,14 +1010,12 @@ async fn command_burn(
Some(mint_info.decimals)
};
let token = token_client_from_config(config, &mint_info.address);
let token = token_client_from_config(config, &mint_info.address, decimals);
if let Some(text) = memo {
token.with_memo(text, vec![config.default_signer.pubkey()]);
}
let res = token
.burn(&account, &owner, amount, decimals, &bulk_signers)
.await?;
let res = token.burn(&account, &owner, amount, &bulk_signers).await?;
let tx_return = finish_tx(config, &res, false).await?;
Ok(match tx_return {
@ -1057,13 +1055,13 @@ async fn command_mint(
Some(mint_info.decimals)
};
let token = token_client_from_config(config, &mint_info.address);
let token = token_client_from_config(config, &mint_info.address, decimals);
if let Some(text) = memo {
token.with_memo(text, vec![config.default_signer.pubkey()]);
}
let res = token
.mint_to(&recipient, &mint_authority, amount, decimals, &bulk_signers)
.mint_to(&recipient, &mint_authority, amount, &bulk_signers)
.await?;
let tx_return = finish_tx(config, &res, false).await?;
@ -1095,7 +1093,8 @@ async fn command_freeze(
),
);
let token = token_client_from_config(config, &mint_info.address);
// we dont use the decimals from mint_info because its not need and in sign-only its wrong
let token = token_client_from_config(config, &mint_info.address, None);
let res = token
.freeze(&account, &freeze_authority, &bulk_signers)
.await?;
@ -1129,7 +1128,8 @@ async fn command_thaw(
),
);
let token = token_client_from_config(config, &mint_info.address);
// we dont use the decimals from mint_info because its not need and in sign-only its wrong
let token = token_client_from_config(config, &mint_info.address, None);
let res = token
.thaw(&account, &freeze_authority, &bulk_signers)
.await?;
@ -1296,9 +1296,9 @@ async fn command_approve(
Some(mint_info.decimals)
};
let token = token_client_from_config(config, &mint_info.address);
let token = token_client_from_config(config, &mint_info.address, decimals);
let res = token
.approve(&account, &delegate, &owner, amount, decimals, &bulk_signers)
.approve(&account, &delegate, &owner, amount, &bulk_signers)
.await?;
let tx_return = finish_tx(config, &res, false).await?;
@ -1348,7 +1348,7 @@ async fn command_revoke(
return Err(format!("No delegate on account {}", account).into());
}
let token = token_client_from_config(config, &mint_pubkey);
let token = token_client_from_config(config, &mint_pubkey, None);
let res = token.revoke(&account, &owner, &bulk_signers).await?;
let tx_return = finish_tx(config, &res, false).await?;
@ -1390,7 +1390,7 @@ async fn command_close(
Pubkey::default()
};
let token = token_client_from_config(config, &mint_pubkey);
let token = token_client_from_config(config, &mint_pubkey, None);
let res = token
.close_account(&account, &recipient, &close_authority, &bulk_signers)
.await?;
@ -1448,7 +1448,7 @@ async fn command_close_mint(
}
}
let token = token_client_from_config(config, &token_pubkey);
let token = token_client_from_config(config, &token_pubkey, None);
let res = token
.close_account(&token_pubkey, &recipient, &close_authority, &bulk_signers)
.await?;
@ -1628,6 +1628,7 @@ async fn command_gc(
serde_json::from_value(parsed_account.parsed)
{
let frozen = ui_token_account.state == UiAccountState::Frozen;
let decimals = ui_token_account.token_amount.decimals;
let token = ui_token_account
.mint
@ -1648,25 +1649,19 @@ async fn command_gc(
.unwrap_or_else(|err| panic!("Invalid close authority: {}", err))
});
let entry = accounts_by_token.entry(token).or_insert_with(HashMap::new);
entry.insert(
token_account,
(
token_amount,
ui_token_account.token_amount.decimals,
frozen,
close_authority,
),
);
let entry = accounts_by_token
.entry((token, decimals))
.or_insert_with(HashMap::new);
entry.insert(token_account, (token_amount, frozen, close_authority));
}
}
}
let mut results = vec![];
for (token_pubkey, accounts) in accounts_by_token.into_iter() {
for ((token_pubkey, decimals), accounts) in accounts_by_token.into_iter() {
println_display(config, format!("Processing token: {}", token_pubkey));
let token = token_client_from_config(config, &token_pubkey);
let token = token_client_from_config(config, &token_pubkey, Some(decimals));
let total_balance: u64 = accounts.values().map(|account| account.0).sum();
let associated_token_account = token.get_associated_token_address(&owner);
@ -1674,7 +1669,7 @@ async fn command_gc(
token.create_associated_token_account(&owner).await?;
}
for (address, (amount, decimals, frozen, close_authority)) in accounts {
for (address, (amount, frozen, close_authority)) in accounts {
let is_associated = address == associated_token_account;
// only close the associated account if --close-empty-associated-accounts is provided
@ -1712,7 +1707,6 @@ async fn command_gc(
&owner,
&associated_token_account,
&owner,
decimals,
&bulk_signers,
)
.await,
@ -1725,7 +1719,6 @@ async fn command_gc(
&associated_token_account,
&owner,
amount,
Some(decimals),
&bulk_signers,
)
.await,
@ -1805,7 +1798,7 @@ async fn command_required_transfer_memos(
let current_account_len = account.data.len();
let state_with_extension = StateWithExtensionsOwned::<Account>::unpack(account.data)?;
let token = token_client_from_config(config, &state_with_extension.base.mint);
let token = token_client_from_config(config, &state_with_extension.base.mint, None);
// Reallocation (if needed)
let mut existing_extensions: Vec<ExtensionType> = state_with_extension.get_extension_types()?;

View File

@ -68,6 +68,10 @@ pub enum TokenError {
NotEnoughFunds,
#[error("missing memo signer")]
MissingMemoSigner,
#[error("decimals required, but missing")]
MissingDecimals,
#[error("decimals specified, but incorrect")]
InvalidDecimals,
}
impl PartialEq for TokenError {
fn eq(&self, other: &Self) -> bool {
@ -88,6 +92,8 @@ impl PartialEq for TokenError {
(Self::AccountDecryption, Self::AccountDecryption) => true,
(Self::NotEnoughFunds, Self::NotEnoughFunds) => true,
(Self::MissingMemoSigner, Self::MissingMemoSigner) => true,
(Self::MissingDecimals, Self::MissingDecimals) => true,
(Self::InvalidDecimals, Self::InvalidDecimals) => true,
_ => false,
}
}
@ -205,6 +211,7 @@ impl TokenMemo {
pub struct Token<T> {
client: Arc<dyn ProgramClient<T>>,
pubkey: Pubkey, /*token mint*/
decimals: Option<u8>,
payer: Arc<dyn Signer>,
program_id: Pubkey,
nonce_account: Option<Pubkey>,
@ -216,6 +223,7 @@ impl<T> fmt::Debug for Token<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Token")
.field("pubkey", &self.pubkey)
.field("decimals", &self.decimals)
.field("payer", &self.payer.pubkey())
.field("program_id", &self.program_id)
.field("nonce_account", &self.nonce_account)
@ -235,6 +243,16 @@ fn native_mint(program_id: &Pubkey) -> Pubkey {
}
}
fn native_mint_decimals(program_id: &Pubkey) -> u8 {
if program_id == &spl_token_2022::id() {
spl_token_2022::native_mint::DECIMALS
} else if program_id == &spl_token::id() {
spl_token::native_mint::DECIMALS
} else {
panic!("Unrecognized token program id: {}", program_id);
}
}
impl<T> Token<T>
where
T: SendTransaction,
@ -243,11 +261,13 @@ where
client: Arc<dyn ProgramClient<T>>,
program_id: &Pubkey,
address: &Pubkey,
decimals: Option<u8>,
payer: Arc<dyn Signer>,
) -> Self {
Token {
client,
pubkey: *address,
decimals,
payer,
program_id: *program_id,
nonce_account: None,
@ -261,7 +281,13 @@ where
program_id: &Pubkey,
payer: Arc<dyn Signer>,
) -> Self {
Self::new(client, program_id, &native_mint(program_id), payer)
Self::new(
client,
program_id,
&native_mint(program_id),
Some(native_mint_decimals(program_id)),
payer,
)
}
pub fn is_native(&self) -> bool {
@ -277,6 +303,7 @@ where
Token {
client: Arc::clone(&self.client),
pubkey: self.pubkey,
decimals: self.decimals,
payer,
program_id: self.program_id,
nonce_account: self.nonce_account,
@ -289,6 +316,7 @@ where
Token {
client: Arc::clone(&self.client),
pubkey: self.pubkey,
decimals: self.decimals,
payer: self.payer.clone(),
program_id: self.program_id,
nonce_account: Some(*nonce_account),
@ -431,10 +459,11 @@ where
&self,
mint_authority: &'a Pubkey,
freeze_authority: Option<&'a Pubkey>,
decimals: u8,
extension_initialization_params: Vec<ExtensionInitializationParams>,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let decimals = self.decimals.ok_or(TokenError::MissingDecimals)?;
let extension_types = extension_initialization_params
.iter()
.map(|e| e.extension())
@ -606,7 +635,16 @@ where
return Err(TokenError::AccountInvalidOwner);
}
StateWithExtensionsOwned::<Mint>::unpack(account.data).map_err(Into::into)
let mint_result =
StateWithExtensionsOwned::<Mint>::unpack(account.data).map_err(Into::into);
if let (Ok(mint), Some(decimals)) = (&mint_result, self.decimals) {
if decimals != mint.base.decimals {
return Err(TokenError::InvalidDecimals);
}
}
mint_result
}
/// Retrieve account information.
@ -675,13 +713,12 @@ where
destination: &Pubkey,
authority: &Pubkey,
amount: u64,
decimals: Option<u8>,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let signing_pubkeys = signing_keypairs.pubkeys();
let multisig_signers = self.get_multisig_signers(authority, &signing_pubkeys);
let instructions = if let Some(decimals) = decimals {
let instructions = if let Some(decimals) = self.decimals {
[instruction::mint_to_checked(
&self.program_id,
&self.pubkey,
@ -713,13 +750,12 @@ where
destination: &Pubkey,
authority: &Pubkey,
amount: u64,
decimals: Option<u8>,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let signing_pubkeys = signing_keypairs.pubkeys();
let multisig_signers = self.get_multisig_signers(authority, &signing_pubkeys);
let instructions = if let Some(decimals) = decimals {
let instructions = if let Some(decimals) = self.decimals {
[instruction::transfer_checked(
&self.program_id,
source,
@ -754,7 +790,6 @@ where
destination_owner: &Pubkey,
authority: &Pubkey,
amount: u64,
decimals: Option<u8>,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let signing_pubkeys = signing_keypairs.pubkeys();
@ -773,7 +808,7 @@ where
)),
];
if let Some(decimals) = decimals {
if let Some(decimals) = self.decimals {
instructions.push(instruction::transfer_checked(
&self.program_id,
source,
@ -807,12 +842,12 @@ where
destination: &Pubkey,
authority: &Pubkey,
amount: u64,
decimals: u8,
fee: u64,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let signing_pubkeys = signing_keypairs.pubkeys();
let multisig_signers = self.get_multisig_signers(authority, &signing_pubkeys);
let decimals = self.decimals.ok_or(TokenError::MissingDecimals)?;
self.process_ixs(
&[transfer_fee::instruction::transfer_checked_with_fee(
@ -837,13 +872,12 @@ where
source: &Pubkey,
authority: &Pubkey,
amount: u64,
decimals: Option<u8>,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let signing_pubkeys = signing_keypairs.pubkeys();
let multisig_signers = self.get_multisig_signers(authority, &signing_pubkeys);
let instructions = if let Some(decimals) = decimals {
let instructions = if let Some(decimals) = self.decimals {
[instruction::burn_checked(
&self.program_id,
source,
@ -874,13 +908,12 @@ where
delegate: &Pubkey,
authority: &Pubkey,
amount: u64,
decimals: Option<u8>,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let signing_pubkeys = signing_keypairs.pubkeys();
let multisig_signers = self.get_multisig_signers(authority, &signing_pubkeys);
let instructions = if let Some(decimals) = decimals {
let instructions = if let Some(decimals) = self.decimals {
[instruction::approve_checked(
&self.program_id,
source,
@ -970,7 +1003,6 @@ where
lamports_destination: &Pubkey,
tokens_destination: &Pubkey,
authority: &Pubkey,
decimals: u8,
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let signing_pubkeys = signing_keypairs.pubkeys();
@ -983,6 +1015,7 @@ where
if !self.is_native() && account_state.base.amount > 0 {
// if a separate close authority is being used, it must be a delegate also
if let Some(decimals) = self.decimals {
instructions.push(instruction::transfer_checked(
&self.program_id,
account_to_close,
@ -993,6 +1026,17 @@ where
account_state.base.amount,
decimals,
)?);
} else {
#[allow(deprecated)]
instructions.push(instruction::transfer(
&self.program_id,
account_to_close,
tokens_destination,
authority,
&multisig_signers,
account_state.base.amount,
)?);
}
}
instructions.push(instruction::close_account(

View File

@ -48,17 +48,12 @@ impl TestContext {
Arc::clone(&client),
&spl_token_2022::id(),
&mint_account.pubkey(),
Some(decimals),
Arc::new(keypair_clone(&payer)),
);
token
.create_mint(
&mint_authority_pubkey,
None,
decimals,
vec![],
&[&mint_account],
)
.create_mint(&mint_authority_pubkey, None, vec![], &[&mint_account])
.await
.expect("failed to create mint");
@ -146,7 +141,6 @@ async fn get_or_create_associated_token_account() {
#[tokio::test]
async fn set_authority() {
let TestContext {
decimals,
mint_authority,
token,
alice,
@ -165,7 +159,6 @@ async fn set_authority() {
&alice_vault,
&mint_authority.pubkey(),
1,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -195,7 +188,6 @@ async fn set_authority() {
&alice_vault,
&mint_authority.pubkey(),
2,
Some(decimals),
&vec![&mint_authority]
)
.await
@ -248,7 +240,6 @@ async fn mint_to() {
&alice_vault,
&mint_authority.pubkey(),
mint_amount,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -296,7 +287,6 @@ async fn transfer() {
&alice_vault,
&mint_authority.pubkey(),
mint_amount,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -309,7 +299,6 @@ async fn transfer() {
&bob_vault,
&alice.pubkey(),
transfer_amount,
Some(decimals),
&vec![&alice],
)
.await

View File

@ -14,9 +14,9 @@ use {
async fn run_basic(context: TestContext) {
let TokenContext {
decimals,
mint_authority,
token,
token_unchecked,
alice,
bob,
..
@ -36,39 +36,26 @@ async fn run_basic(context: TestContext) {
&alice_account,
&mint_authority.pubkey(),
amount,
Some(decimals),
&vec![&mint_authority],
)
.await
.unwrap();
// unchecked is ok
token
.burn(&alice_account, &alice.pubkey(), 1, None, &vec![&alice])
token_unchecked
.burn(&alice_account, &alice.pubkey(), 1, &vec![&alice])
.await
.unwrap();
// checked is ok
token
.burn(
&alice_account,
&alice.pubkey(),
1,
Some(decimals),
&vec![&alice],
)
.burn(&alice_account, &alice.pubkey(), 1, &vec![&alice])
.await
.unwrap();
// burn too much is not ok
let error = token
.burn(
&alice_account,
&alice.pubkey(),
amount,
Some(decimals),
&vec![&alice],
)
.burn(&alice_account, &alice.pubkey(), amount, &vec![&alice])
.await
.unwrap_err();
assert_eq!(
@ -83,13 +70,7 @@ async fn run_basic(context: TestContext) {
// wrong signer
let error = token
.burn(
&alice_account,
&bob.pubkey(),
1,
Some(decimals),
&vec![&bob],
)
.burn(&alice_account, &bob.pubkey(), 1, &vec![&bob])
.await
.unwrap_err();
assert_eq!(
@ -127,9 +108,9 @@ async fn basic_with_extension() {
async fn run_self_owned(context: TestContext) {
let TokenContext {
decimals,
mint_authority,
token,
token_unchecked,
alice,
..
} = context.token_context.unwrap();
@ -147,27 +128,20 @@ async fn run_self_owned(context: TestContext) {
&alice_account,
&mint_authority.pubkey(),
amount,
Some(decimals),
&vec![&mint_authority],
)
.await
.unwrap();
// unchecked is ok
token
.burn(&alice_account, &alice.pubkey(), 1, None, &vec![&alice])
token_unchecked
.burn(&alice_account, &alice.pubkey(), 1, &vec![&alice])
.await
.unwrap();
// checked is ok
token
.burn(
&alice_account,
&alice.pubkey(),
1,
Some(decimals),
&vec![&alice],
)
.burn(&alice_account, &alice.pubkey(), 1, &vec![&alice])
.await
.unwrap();
}
@ -196,7 +170,6 @@ async fn self_owned_with_extension() {
async fn run_burn_and_close_system_or_incinerator(context: TestContext, non_owner: &Pubkey) {
let TokenContext {
decimals,
mint_authority,
token,
alice,
@ -216,7 +189,6 @@ async fn run_burn_and_close_system_or_incinerator(context: TestContext, non_owne
&alice_account,
&mint_authority.pubkey(),
1,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -235,7 +207,6 @@ async fn run_burn_and_close_system_or_incinerator(context: TestContext, non_owne
&non_owner_account,
&alice.pubkey(),
1,
Some(decimals),
&vec![&alice],
)
.await
@ -264,13 +235,7 @@ async fn run_burn_and_close_system_or_incinerator(context: TestContext, non_owne
// but anyone can burn it
token
.burn(
&non_owner_account,
&carlos.pubkey(),
1,
Some(decimals),
&vec![&carlos],
)
.burn(&non_owner_account, &carlos.pubkey(), 1, &vec![&carlos])
.await
.unwrap();

View File

@ -182,7 +182,6 @@ impl ConfidentialTokenAccountMeta {
&meta.token_account,
&mint_authority.pubkey(),
amount,
Some(decimals),
&vec![mint_authority],
)
.await
@ -495,7 +494,6 @@ async fn ct_deposit() {
&alice_meta.token_account,
&mint_authority.pubkey(),
65537,
Some(decimals),
&vec![&mint_authority],
)
.await

View File

@ -37,9 +37,9 @@ async fn run_basic(
approve_mode: ApproveMode,
) {
let TokenContext {
decimals,
mint_authority,
token,
token_unchecked,
alice,
bob,
..
@ -76,7 +76,6 @@ async fn run_basic(
&alice_account,
&mint_authority.pubkey(),
amount,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -85,13 +84,12 @@ async fn run_basic(
// delegate to bob
let delegated_amount = 10;
match approve_mode {
ApproveMode::Unchecked => token
ApproveMode::Unchecked => token_unchecked
.approve(
&alice_account,
&bob.pubkey(),
&alice.pubkey(),
delegated_amount,
None,
&vec![&alice],
)
.await
@ -102,7 +100,6 @@ async fn run_basic(
&bob.pubkey(),
&alice.pubkey(),
delegated_amount,
Some(decimals),
&vec![&alice],
)
.await
@ -116,7 +113,6 @@ async fn run_basic(
&bob_account,
&bob.pubkey(),
delegated_amount.checked_add(1).unwrap(),
Some(decimals),
&vec![&bob],
)
.await
@ -133,44 +129,24 @@ async fn run_basic(
// transfer is ok
if transfer_mode == TransferMode::All {
token
.transfer(
&alice_account,
&bob_account,
&bob.pubkey(),
1,
None,
&vec![&bob],
)
token_unchecked
.transfer(&alice_account, &bob_account, &bob.pubkey(), 1, &vec![&bob])
.await
.unwrap();
}
token
.transfer(
&alice_account,
&bob_account,
&bob.pubkey(),
1,
Some(decimals),
&vec![&bob],
)
.transfer(&alice_account, &bob_account, &bob.pubkey(), 1, &vec![&bob])
.await
.unwrap();
// burn is ok
token
.burn(&alice_account, &bob.pubkey(), 1, None, &vec![&bob])
token_unchecked
.burn(&alice_account, &bob.pubkey(), 1, &vec![&bob])
.await
.unwrap();
token
.burn(
&alice_account,
&bob.pubkey(),
1,
Some(decimals),
&vec![&bob],
)
.burn(&alice_account, &bob.pubkey(), 1, &vec![&bob])
.await
.unwrap();
@ -182,7 +158,6 @@ async fn run_basic(
&bob_account,
&keypair.pubkey(),
1,
Some(decimals),
&vec![keypair],
)
.await
@ -205,14 +180,7 @@ async fn run_basic(
// now fails
let error = token
.transfer(
&alice_account,
&bob_account,
&bob.pubkey(),
2,
Some(decimals),
&vec![&bob],
)
.transfer(&alice_account, &bob_account, &bob.pubkey(), 2, &vec![&bob])
.await
.unwrap_err();
assert_eq!(

View File

@ -30,7 +30,6 @@ async fn test_memo_transfers(
bob_account: Pubkey,
) {
let TokenContext {
decimals,
mint_authority,
token,
alice,
@ -44,7 +43,6 @@ async fn test_memo_transfers(
&alice_account,
&mint_authority.pubkey(),
4242,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -67,7 +65,6 @@ async fn test_memo_transfers(
&bob_account,
&alice.pubkey(),
10,
None,
&vec![&alice],
)
.await
@ -137,7 +134,6 @@ async fn test_memo_transfers(
&bob_account,
&alice.pubkey(),
10,
None,
&vec![&alice],
)
.await
@ -185,7 +181,6 @@ async fn test_memo_transfers(
&bob_account,
&alice.pubkey(),
12,
None,
&vec![&alice],
)
.await

View File

@ -244,7 +244,6 @@ async fn fail_close_with_supply() {
.await
.unwrap();
let TokenContext {
decimals,
mint_authority,
token,
..
@ -263,7 +262,6 @@ async fn fail_close_with_supply() {
&account,
&mint_authority.pubkey(),
1,
Some(decimals),
&vec![&mint_authority],
)
.await

View File

@ -25,7 +25,6 @@ async fn transfer_checked() {
.unwrap();
let TokenContext {
decimals,
mint_authority,
token,
alice,
@ -56,7 +55,6 @@ async fn transfer_checked() {
&alice_account,
&mint_authority.pubkey(),
test_transfer_amount,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -78,7 +76,6 @@ async fn transfer_checked() {
&bob_account,
&mint_authority.pubkey(),
test_transfer_amount,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -91,7 +88,6 @@ async fn transfer_checked() {
&bob_account,
&bob.pubkey(),
test_transfer_amount,
Some(decimals),
&vec![&bob],
)
.await
@ -114,7 +110,6 @@ async fn transfer_checked() {
&alice_account,
&bob.pubkey(),
test_transfer_amount,
Some(decimals),
&vec![&bob],
)
.await
@ -161,7 +156,6 @@ async fn transfer_checked_with_fee() {
.unwrap();
let TokenContext {
decimals,
mint_authority,
token,
alice,
@ -195,7 +189,6 @@ async fn transfer_checked_with_fee() {
&alice_account,
&mint_authority.pubkey(),
test_transfer_amount,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -208,7 +201,6 @@ async fn transfer_checked_with_fee() {
&alice_account,
&alice.pubkey(),
test_transfer_amount,
Some(decimals),
&vec![&alice],
)
.await
@ -231,7 +223,6 @@ async fn transfer_checked_with_fee() {
&bob_account,
&alice.pubkey(),
test_transfer_amount,
Some(decimals),
&vec![&alice],
)
.await
@ -255,7 +246,6 @@ async fn transfer_checked_with_fee() {
&alice_account,
&alice.pubkey(),
test_transfer_amount,
decimals,
fee,
&vec![&alice],
)
@ -280,7 +270,6 @@ async fn transfer_checked_with_fee() {
&bob_account,
&alice.pubkey(),
test_transfer_amount,
decimals,
fee,
&vec![&alice],
)

View File

@ -15,6 +15,7 @@ pub struct TokenContext {
pub decimals: u8,
pub mint_authority: Keypair,
pub token: Token<ProgramBanksClientProcessTransaction>,
pub token_unchecked: Token<ProgramBanksClientProcessTransaction>,
pub alice: Keypair,
pub bob: Keypair,
pub freeze_authority: Option<Keypair>,
@ -79,6 +80,15 @@ impl TestContext {
Arc::clone(&client),
&id(),
&mint_account.pubkey(),
Some(decimals),
Arc::new(keypair_clone(&payer)),
);
let token_unchecked = Token::new(
Arc::clone(&client),
&id(),
&mint_account.pubkey(),
None,
Arc::new(payer),
);
@ -86,7 +96,6 @@ impl TestContext {
.create_mint(
&mint_authority_pubkey,
freeze_authority_pubkey.as_ref(),
decimals,
extension_init_params,
&[&mint_account],
)
@ -96,6 +105,7 @@ impl TestContext {
decimals,
mint_authority,
token,
token_unchecked,
alice: Keypair::new(),
bob: Keypair::new(),
freeze_authority,
@ -112,11 +122,16 @@ impl TestContext {
ProgramBanksClientProcessTransaction,
));
let token = Token::create_native_mint(Arc::clone(&client), &id(), Arc::new(payer)).await?;
let token =
Token::create_native_mint(Arc::clone(&client), &id(), Arc::new(keypair_clone(&payer)))
.await?;
// unchecked native is never needed because decimals is known statically
let token_unchecked = Token::new_native(Arc::clone(&client), &id(), Arc::new(payer));
self.token_context = Some(TokenContext {
decimals: native_mint::DECIMALS,
mint_authority: Keypair::new(), /*bogus*/
token,
token_unchecked,
alice: Keypair::new(),
bob: Keypair::new(),
freeze_authority: None,

View File

@ -20,9 +20,9 @@ enum TestMode {
async fn run_basic_transfers(context: TestContext, test_mode: TestMode) {
let TokenContext {
decimals,
mint_authority,
token,
token_unchecked,
alice,
bob,
..
@ -48,7 +48,6 @@ async fn run_basic_transfers(context: TestContext, test_mode: TestMode) {
&alice_account,
&mint_authority.pubkey(),
amount,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -56,13 +55,12 @@ async fn run_basic_transfers(context: TestContext, test_mode: TestMode) {
if test_mode == TestMode::All {
// unchecked is ok
token
token_unchecked
.transfer(
&alice_account,
&bob_account,
&alice.pubkey(),
1,
None,
&vec![&alice],
)
.await
@ -76,7 +74,6 @@ async fn run_basic_transfers(context: TestContext, test_mode: TestMode) {
&bob_account,
&alice.pubkey(),
1,
Some(decimals),
&vec![&alice],
)
.await
@ -89,7 +86,6 @@ async fn run_basic_transfers(context: TestContext, test_mode: TestMode) {
&bob_account,
&alice.pubkey(),
amount,
Some(decimals),
&vec![&alice],
)
.await
@ -106,14 +102,7 @@ async fn run_basic_transfers(context: TestContext, test_mode: TestMode) {
// wrong signer
let error = token
.transfer(
&alice_account,
&bob_account,
&bob.pubkey(),
1,
Some(decimals),
&vec![&bob],
)
.transfer(&alice_account, &bob_account, &bob.pubkey(), 1, &vec![&bob])
.await
.unwrap_err();
assert_eq!(
@ -151,9 +140,9 @@ async fn basic_with_extension() {
async fn run_self_transfers(context: TestContext, test_mode: TestMode) {
let TokenContext {
decimals,
mint_authority,
token,
token_unchecked,
alice,
..
} = context.token_context.unwrap();
@ -172,7 +161,6 @@ async fn run_self_transfers(context: TestContext, test_mode: TestMode) {
&alice_account,
&mint_authority.pubkey(),
amount,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -185,19 +173,17 @@ async fn run_self_transfers(context: TestContext, test_mode: TestMode) {
&alice_account,
&alice.pubkey(),
1,
Some(decimals),
&vec![&alice],
)
.await
.unwrap();
if test_mode == TestMode::All {
token
token_unchecked
.transfer(
&alice_account,
&alice_account,
&alice.pubkey(),
1,
None,
&vec![&alice],
)
.await
@ -211,7 +197,6 @@ async fn run_self_transfers(context: TestContext, test_mode: TestMode) {
&alice_account,
&alice.pubkey(),
amount.checked_add(1).unwrap(),
Some(decimals),
&vec![&alice],
)
.await
@ -251,9 +236,9 @@ async fn self_transfer_with_extension() {
async fn run_self_owned(context: TestContext, test_mode: TestMode) {
let TokenContext {
decimals,
mint_authority,
token,
token_unchecked,
alice,
bob,
..
@ -278,7 +263,6 @@ async fn run_self_owned(context: TestContext, test_mode: TestMode) {
&alice_account,
&mint_authority.pubkey(),
amount,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -286,13 +270,12 @@ async fn run_self_owned(context: TestContext, test_mode: TestMode) {
if test_mode == TestMode::All {
// unchecked is ok
token
token_unchecked
.transfer(
&alice_account,
&bob_account,
&alice.pubkey(),
1,
None,
&vec![&alice],
)
.await
@ -306,7 +289,6 @@ async fn run_self_owned(context: TestContext, test_mode: TestMode) {
&bob_account,
&alice.pubkey(),
1,
Some(decimals),
&vec![&alice],
)
.await
@ -319,7 +301,6 @@ async fn run_self_owned(context: TestContext, test_mode: TestMode) {
&alice_account,
&alice.pubkey(),
1,
Some(decimals),
&vec![&alice],
)
.await
@ -353,7 +334,6 @@ async fn transfer_with_fee_on_mint_without_fee_configured() {
let mut context = TestContext::new().await;
context.init_token_with_mint(vec![]).await.unwrap();
let TokenContext {
decimals,
mint_authority,
token,
alice,
@ -381,7 +361,6 @@ async fn transfer_with_fee_on_mint_without_fee_configured() {
&alice_account,
&mint_authority.pubkey(),
amount,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -394,7 +373,6 @@ async fn transfer_with_fee_on_mint_without_fee_configured() {
&bob_account,
&alice.pubkey(),
1,
decimals,
0,
&vec![&alice],
)
@ -408,7 +386,6 @@ async fn transfer_with_fee_on_mint_without_fee_configured() {
&bob_account,
&alice.pubkey(),
2,
decimals,
1,
&vec![&alice],
)

View File

@ -75,13 +75,13 @@ fn test_transfer_fee_config_with_keypairs() -> TransferFeeConfigWithKeypairs {
struct TokenWithAccounts {
context: TestContext,
token: Token<ProgramBanksClientProcessTransaction>,
token_unchecked: Token<ProgramBanksClientProcessTransaction>,
transfer_fee_config: TransferFeeConfig,
withdraw_withheld_authority: Keypair,
freeze_authority: Keypair,
alice: Keypair,
alice_account: Pubkey,
bob_account: Pubkey,
decimals: u8,
}
async fn create_mint_with_accounts(alice_amount: u64) -> TokenWithAccounts {
@ -108,10 +108,10 @@ async fn create_mint_with_accounts(alice_amount: u64) -> TokenWithAccounts {
.await
.unwrap();
let TokenContext {
decimals,
mint_authority,
freeze_authority,
token,
token_unchecked,
alice,
bob,
..
@ -136,7 +136,6 @@ async fn create_mint_with_accounts(alice_amount: u64) -> TokenWithAccounts {
&alice_account,
&mint_authority.pubkey(),
alice_amount,
Some(decimals),
&vec![&mint_authority],
)
.await
@ -144,13 +143,13 @@ async fn create_mint_with_accounts(alice_amount: u64) -> TokenWithAccounts {
TokenWithAccounts {
context,
token,
token_unchecked,
transfer_fee_config,
withdraw_withheld_authority,
freeze_authority: freeze_authority.unwrap(),
alice,
alice_account,
bob_account,
decimals,
}
}
@ -841,22 +840,21 @@ async fn transfer_checked() {
let mut alice_amount = maximum_fee * 100;
let TokenWithAccounts {
token,
token_unchecked,
transfer_fee_config,
alice,
alice_account,
bob_account,
decimals,
..
} = create_mint_with_accounts(alice_amount).await;
// fail unchecked always
let error = token
let error = token_unchecked
.transfer(
&alice_account,
&bob_account,
&alice.pubkey(),
maximum_fee,
None,
&vec![&alice],
)
.await
@ -878,7 +876,6 @@ async fn transfer_checked() {
&bob_account,
&alice.pubkey(),
alice_amount + 1,
Some(decimals),
&vec![&alice],
)
.await
@ -906,7 +903,6 @@ async fn transfer_checked() {
&bob_account,
&alice.pubkey(),
maximum_fee,
Some(decimals),
&vec![&alice],
)
.await
@ -935,7 +931,6 @@ async fn transfer_checked() {
&bob_account,
&alice.pubkey(),
transfer_amount,
Some(decimals),
&vec![&alice],
)
.await
@ -969,7 +964,6 @@ async fn transfer_checked() {
&bob_account,
&alice.pubkey(),
transfer_amount,
Some(decimals),
&vec![&alice],
)
.await
@ -993,7 +987,6 @@ async fn transfer_checked() {
&bob_account,
&alice.pubkey(),
alice_amount - 1,
Some(decimals),
&vec![&alice],
)
.await
@ -1017,7 +1010,6 @@ async fn transfer_checked() {
&bob_account,
&alice.pubkey(),
1,
Some(decimals),
&vec![&alice],
)
.await
@ -1043,7 +1035,6 @@ async fn transfer_checked_with_fee() {
alice,
alice_account,
bob_account,
decimals,
..
} = create_mint_with_accounts(alice_amount).await;
@ -1059,7 +1050,6 @@ async fn transfer_checked_with_fee() {
&bob_account,
&alice.pubkey(),
transfer_amount,
decimals,
fee,
&vec![&alice],
)
@ -1086,7 +1076,6 @@ async fn transfer_checked_with_fee() {
&bob_account,
&alice.pubkey(),
transfer_amount,
decimals,
fee,
&vec![&alice],
)
@ -1113,7 +1102,6 @@ async fn transfer_checked_with_fee() {
&bob_account,
&alice.pubkey(),
alice_amount + 1,
decimals,
fee,
&vec![&alice],
)
@ -1139,7 +1127,6 @@ async fn transfer_checked_with_fee() {
&bob_account,
&alice.pubkey(),
transfer_amount,
decimals,
fee,
&vec![&alice],
)
@ -1164,7 +1151,6 @@ async fn no_fees_from_self_transfer() {
transfer_fee_config,
alice,
alice_account,
decimals,
..
} = create_mint_with_accounts(alice_amount).await;
@ -1176,7 +1162,6 @@ async fn no_fees_from_self_transfer() {
&alice_account,
&alice.pubkey(),
amount,
decimals,
fee,
&vec![&alice],
)
@ -1194,7 +1179,6 @@ async fn create_and_transfer_to_account(
authority: &Keypair,
owner: &Pubkey,
amount: u64,
decimals: u8,
) -> Pubkey {
let account = Keypair::new();
token
@ -1208,7 +1192,6 @@ async fn create_and_transfer_to_account(
&account,
&authority.pubkey(),
amount,
Some(decimals),
&vec![authority],
)
.await
@ -1226,7 +1209,6 @@ async fn harvest_withheld_tokens_to_mint() {
transfer_fee_config,
alice,
alice_account,
decimals,
..
} = create_mint_with_accounts(alice_amount).await;
@ -1238,14 +1220,8 @@ async fn harvest_withheld_tokens_to_mint() {
// harvest from one account
let accumulated_fees = transfer_fee_config.calculate_epoch_fee(0, amount).unwrap();
let account = create_and_transfer_to_account(
&token,
&alice_account,
&alice,
&alice.pubkey(),
amount,
decimals,
)
let account =
create_and_transfer_to_account(&token, &alice_account, &alice, &alice.pubkey(), amount)
.await;
token
.harvest_withheld_tokens_to_mint(&[&account])
@ -1260,14 +1236,8 @@ async fn harvest_withheld_tokens_to_mint() {
// no fail harvesting from account belonging to different mint, but nothing
// happens
let account = create_and_transfer_to_account(
&token,
&alice_account,
&alice,
&alice.pubkey(),
amount,
decimals,
)
let account =
create_and_transfer_to_account(&token, &alice_account, &alice, &alice.pubkey(), amount)
.await;
context
.init_token_with_mint(vec![ExtensionInitializationParams::TransferFeeConfig {
@ -1297,7 +1267,6 @@ async fn max_harvest_withheld_tokens_to_mint() {
transfer_fee_config,
alice,
alice_account,
decimals,
..
} = create_mint_with_accounts(alice_amount).await;
@ -1306,14 +1275,8 @@ async fn max_harvest_withheld_tokens_to_mint() {
let mut accounts = vec![];
let max_accounts = 34;
for _ in 0..max_accounts {
let account = create_and_transfer_to_account(
&token,
&alice_account,
&alice,
&alice.pubkey(),
amount,
decimals,
)
let account =
create_and_transfer_to_account(&token, &alice_account, &alice, &alice.pubkey(), amount)
.await;
accounts.push(account);
}
@ -1344,7 +1307,6 @@ async fn max_withdraw_withheld_tokens_from_accounts() {
transfer_fee_config,
alice,
alice_account,
decimals,
..
} = create_mint_with_accounts(alice_amount).await;
@ -1360,14 +1322,8 @@ async fn max_withdraw_withheld_tokens_from_accounts() {
let mut accounts = vec![];
let max_accounts = 32;
for _ in 0..max_accounts {
let account = create_and_transfer_to_account(
&token,
&alice_account,
&alice,
&alice.pubkey(),
amount,
decimals,
)
let account =
create_and_transfer_to_account(&token, &alice_account, &alice, &alice.pubkey(), amount)
.await;
accounts.push(account);
}
@ -1404,7 +1360,6 @@ async fn withdraw_withheld_tokens_from_mint() {
freeze_authority,
alice,
alice_account,
decimals,
bob_account,
..
} = create_mint_with_accounts(alice_amount).await;
@ -1428,14 +1383,8 @@ async fn withdraw_withheld_tokens_from_mint() {
// transfer + harvest to mint
let fee = transfer_fee_config.calculate_epoch_fee(0, amount).unwrap();
let account = create_and_transfer_to_account(
&token,
&alice_account,
&alice,
&alice.pubkey(),
amount,
decimals,
)
let account =
create_and_transfer_to_account(&token, &alice_account, &alice, &alice.pubkey(), amount)
.await;
let state = token.get_account_info(&account).await.unwrap();
@ -1485,14 +1434,8 @@ async fn withdraw_withheld_tokens_from_mint() {
);
// fail frozen account
let account = create_and_transfer_to_account(
&token,
&alice_account,
&alice,
&alice.pubkey(),
amount,
decimals,
)
let account =
create_and_transfer_to_account(&token, &alice_account, &alice, &alice.pubkey(), amount)
.await;
token
.freeze(
@ -1521,14 +1464,8 @@ async fn withdraw_withheld_tokens_from_mint() {
);
// set to none, fail
let account = create_and_transfer_to_account(
&token,
&alice_account,
&alice,
&alice.pubkey(),
amount,
decimals,
)
let account =
create_and_transfer_to_account(&token, &alice_account, &alice, &alice.pubkey(), amount)
.await;
token
.set_authority(
@ -1598,7 +1535,6 @@ async fn withdraw_withheld_tokens_from_accounts() {
withdraw_withheld_authority,
alice,
alice_account,
decimals,
..
} = create_mint_with_accounts(alice_amount).await;
@ -1637,14 +1573,8 @@ async fn withdraw_withheld_tokens_from_accounts() {
assert_eq!(state.base.amount, alice_amount);
// self-harvest from one account
let account = create_and_transfer_to_account(
&token,
&alice_account,
&alice,
&alice.pubkey(),
amount,
decimals,
)
let account =
create_and_transfer_to_account(&token, &alice_account, &alice, &alice.pubkey(), amount)
.await;
token
.withdraw_withheld_tokens_from_accounts(
@ -1681,14 +1611,8 @@ async fn withdraw_withheld_tokens_from_accounts() {
// no fail harvesting from account belonging to different mint, but nothing
// happens
let account = create_and_transfer_to_account(
&token,
&alice_account,
&alice,
&alice.pubkey(),
amount,
decimals,
)
let account =
create_and_transfer_to_account(&token, &alice_account, &alice, &alice.pubkey(), amount)
.await;
context
.init_token_with_mint(vec![ExtensionInitializationParams::TransferFeeConfig {
@ -1749,19 +1673,12 @@ async fn fail_close_with_withheld() {
transfer_fee_config,
alice,
alice_account,
decimals,
..
} = create_mint_with_accounts(alice_amount).await;
// accrue withheld fees on new account
let account = create_and_transfer_to_account(
&token,
&alice_account,
&alice,
&alice.pubkey(),
amount,
decimals,
)
let account =
create_and_transfer_to_account(&token, &alice_account, &alice, &alice.pubkey(), amount)
.await;
// empty the account
@ -1772,7 +1689,6 @@ async fn fail_close_with_withheld() {
&alice_account,
&alice.pubkey(),
amount - fee,
Some(decimals),
&vec![&alice],
)
.await