Refactor create-token to go through token client

* cli: remove offline args from commands that create accounts
* cli: implement finish_tx, to be a replacement for handle_tx, which branches on the client return
* client: validate a transaction is fully signed before sending over rpc
* client: convert payer from Signer to Arc<dyn Signer> to support cli signers
* client: support nonce mode
* client: change create_mint to be an object method that returns SendTransaction::Output
* client: rename S2 to S everywhere that Token<T, S> is Token<T>
This commit is contained in:
hanako mumei 2022-08-04 19:21:40 -07:00 committed by hana
parent 65769ce23d
commit c426ed9749
7 changed files with 306 additions and 207 deletions

View File

@ -46,6 +46,7 @@ use spl_token_2022::{
instruction::*,
state::{Account, Mint, Multisig},
};
use spl_token_client::{client::RpcClientResponse, token::Token};
use std::{
collections::HashMap, fmt::Display, process::exit, str::FromStr, string::ToString, sync::Arc,
};
@ -327,59 +328,51 @@ pub fn signers_of(
async fn command_create_token(
config: &Config<'_>,
decimals: u8,
token: Pubkey,
token_pubkey: Pubkey,
authority: Pubkey,
enable_freeze: bool,
memo: Option<String>,
bulk_signers: Vec<Arc<dyn Signer>>,
) -> CommandResult {
println_display(config, format!("Creating token {}", token));
println_display(config, format!("Creating token {}", token_pubkey));
let minimum_balance_for_rent_exemption = if !config.sign_only {
config
.program_client
.get_minimum_balance_for_rent_exemption(Mint::LEN)
.await?
let token = Token::new(
config.program_client.clone(),
&config.program_id,
&token_pubkey,
config.default_signer.clone(),
);
let token = if let (Some(nonce_account), Some(nonce_authority)) =
(config.nonce_account, config.nonce_authority)
{
token.with_nonce(&nonce_account, &nonce_authority)
} else {
0
token
};
let freeze_authority_pubkey = if enable_freeze { Some(authority) } else { None };
let mut instructions = vec![
system_instruction::create_account(
&config.fee_payer,
&token,
minimum_balance_for_rent_exemption,
Mint::LEN as u64,
&config.program_id,
),
initialize_mint(
&config.program_id,
&token,
&authority,
freeze_authority_pubkey.as_ref(),
decimals,
)?,
];
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,
minimum_balance_for_rent_exemption,
instructions,
let freeze_authority = if enable_freeze { Some(authority) } else { None };
let res = token
.create_mint(
&authority,
freeze_authority.as_ref(),
decimals,
vec![],
&bulk_signers,
)
.await?;
let tx_return = finish_tx(config, &res, false).await?;
Ok(match tx_return {
TransactionReturnData::CliSignature(cli_signature) => format_output(
CliMint {
address: token.to_string(),
address: token_pubkey.to_string(),
decimals,
transaction_data: cli_signature,
},
@ -1970,7 +1963,6 @@ fn app<'a, 'b>(
)
.nonce_args(true)
.arg(memo_arg())
.offline_args(),
)
.subcommand(
SubCommand::with_name(CommandName::CreateAccount.into())
@ -1998,7 +1990,6 @@ fn app<'a, 'b>(
)
.arg(owner_address_arg())
.nonce_args(true)
.offline_args(),
)
.subcommand(
SubCommand::with_name(CommandName::CreateMultisig.into())
@ -2036,7 +2027,6 @@ fn app<'a, 'b>(
),
)
.nonce_args(true)
.offline_args(),
)
.subcommand(
SubCommand::with_name(CommandName::Authorize.into())
@ -2147,7 +2137,7 @@ fn app<'a, 'b>(
.arg(owner_keypair_arg_with_value_name("SENDER_TOKEN_OWNER_KEYPAIR")
.help(
"Specify the owner of the sending token account. \
This may be a keypair file, the ASK keyword. \
This may be a keypair file or the ASK keyword. \
Defaults to the client keypair.",
),
)
@ -2218,7 +2208,7 @@ fn app<'a, 'b>(
.arg(owner_keypair_arg_with_value_name("SOURCE_TOKEN_OWNER_KEYPAIR")
.help(
"Specify the source token owner account. \
This may be a keypair file, the ASK keyword. \
This may be a keypair file or the ASK keyword. \
Defaults to the client keypair.",
),
)
@ -2486,7 +2476,7 @@ fn app<'a, 'b>(
.help(
"Specify the token's close authority if it has one, \
otherwise specify the token's owner keypair. \
This may be a keypair file, the ASK keyword. \
This may be a keypair file or the ASK keyword. \
Defaults to the client keypair.",
),
)
@ -3129,6 +3119,8 @@ enum TransactionReturnData {
CliSignature(CliSignature),
CliSignOnlyData(CliSignOnlyData),
}
// XXX this goes away once everything is converted to token client
async fn handle_tx<'a>(
signer_info: &CliSignerInfo,
config: &Config<'a>,
@ -3184,6 +3176,43 @@ async fn handle_tx<'a>(
}
}
async fn finish_tx<'a>(
config: &Config<'a>,
rpc_response: &RpcClientResponse,
no_wait: bool,
) -> Result<TransactionReturnData, Error> {
match rpc_response {
RpcClientResponse::Transaction(transaction) => {
Ok(TransactionReturnData::CliSignOnlyData(return_signers_data(
transaction,
&ReturnSignersConfig {
dump_transaction_message: config.dump_transaction_message,
},
)))
}
RpcClientResponse::Signature(signature) if no_wait => {
Ok(TransactionReturnData::CliSignature(CliSignature {
signature: signature.to_string(),
}))
}
RpcClientResponse::Signature(signature) => {
let blockhash = config.program_client.get_latest_blockhash().await?;
config
.rpc_client
.confirm_transaction_with_spinner(
signature,
&blockhash,
config.rpc_client.commitment(),
)
.await?;
Ok(TransactionReturnData::CliSignature(CliSignature {
signature: signature.to_string(),
}))
}
}
}
#[cfg(test)]
mod tests {
use {

View File

@ -79,6 +79,10 @@ impl SendTransactionRpc for ProgramRpcClientSendTransaction {
transaction: &'a Transaction,
) -> BoxFuture<'a, ProgramClientResult<Self::Output>> {
Box::pin(async move {
if !transaction.is_signed() {
return Err("Cannot send transaction: not fully signed".into());
}
client
.send_and_confirm_transaction(transaction)
.await

View File

@ -6,6 +6,7 @@ use {
epoch_info::EpochInfo,
hash::Hash,
instruction::Instruction,
message::Message,
program_error::ProgramError,
pubkey::Pubkey,
signer::{signers::Signers, Signer, SignerError},
@ -64,11 +65,18 @@ impl PartialEq for TokenError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
// TODO not great, but workable for tests
// currently missing: proof error, signer error
(Self::Client(ref a), Self::Client(ref b)) => a.to_string() == b.to_string(),
(Self::Program(ref a), Self::Program(ref b)) => a == b,
(Self::AccountNotFound, Self::AccountNotFound) => true,
(Self::AccountInvalidOwner, Self::AccountInvalidOwner) => true,
(Self::AccountInvalidMint, Self::AccountInvalidMint) => true,
(
Self::MaximumDepositTransferAmountExceeded,
Self::MaximumDepositTransferAmountExceeded,
) => true,
(Self::AccountDecryption, Self::AccountDecryption) => true,
(Self::NotEnoughFunds, Self::NotEnoughFunds) => true,
_ => false,
}
}
@ -164,18 +172,17 @@ impl ExtensionInitializationParams {
pub type TokenResult<T> = Result<T, TokenError>;
pub struct Token<T, S> {
pub struct Token<T> {
client: Arc<dyn ProgramClient<T>>,
pubkey: Pubkey, /*token mint*/
payer: S,
payer: Arc<dyn Signer>,
program_id: Pubkey,
nonce_account: Option<Pubkey>,
nonce_authority: Option<Pubkey>,
memo: Arc<RwLock<Option<String>>>,
}
impl<T, S> fmt::Debug for Token<T, S>
where
S: Signer,
{
impl<T> fmt::Debug for Token<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Token")
.field("pubkey", &self.pubkey)
@ -185,22 +192,23 @@ where
}
}
impl<T, S> Token<T, S>
impl<T> Token<T>
where
T: SendTransaction,
S: Signer,
{
pub fn new(
client: Arc<dyn ProgramClient<T>>,
program_id: &Pubkey,
address: &Pubkey,
payer: S,
payer: Arc<dyn Signer>,
) -> Self {
Token {
client,
pubkey: *address,
payer,
program_id: *program_id,
nonce_account: None,
nonce_authority: None,
memo: Arc::new(RwLock::new(None)),
}
}
@ -210,12 +218,26 @@ where
&self.pubkey
}
pub fn with_payer<S2: Signer>(&self, payer: S2) -> Token<T, S2> {
pub fn with_payer(&self, payer: Arc<dyn Signer>) -> Token<T> {
Token {
client: Arc::clone(&self.client),
pubkey: self.pubkey,
payer,
program_id: self.program_id,
nonce_account: self.nonce_account,
nonce_authority: self.nonce_authority,
memo: Arc::new(RwLock::new(None)),
}
}
pub fn with_nonce(&self, nonce_account: &Pubkey, nonce_authority: &Pubkey) -> Token<T> {
Token {
client: Arc::clone(&self.client),
pubkey: self.pubkey,
payer: self.payer.clone(),
program_id: self.program_id,
nonce_account: Some(*nonce_account),
nonce_authority: Some(*nonce_authority),
memo: Arc::new(RwLock::new(None)),
}
}
@ -259,85 +281,118 @@ where
))))
}
pub async fn process_ixs<S2: Signers>(
pub async fn construct_tx<S: Signers>(
&self,
token_instructions: &[Instruction],
signing_keypairs: &S2,
) -> TokenResult<T::Output> {
signing_keypairs: &S,
) -> TokenResult<Transaction> {
let mut instructions = vec![];
let payer_key = self.payer.pubkey();
let fee_payer = Some(&payer_key);
{
let mut w_memo = self.memo.write().unwrap();
if let Some(memo) = w_memo.take() {
instructions.push(spl_memo::build_memo(memo.as_bytes(), &[]));
instructions.push(spl_memo::build_memo(memo.as_bytes(), &[&payer_key]));
}
}
instructions.extend_from_slice(token_instructions);
let latest_blockhash = self
.client
.get_latest_blockhash()
.await
.map_err(TokenError::Client)?;
let mut tx = Transaction::new_with_payer(&instructions, Some(&self.payer.pubkey()));
tx.try_partial_sign(&[&self.payer], latest_blockhash)
let message = if let (Some(nonce_account), Some(nonce_authority)) =
(self.nonce_account, self.nonce_authority)
{
let mut message = Message::new_with_nonce(
token_instructions.to_vec(),
fee_payer,
&nonce_account,
&nonce_authority,
);
message.recent_blockhash = latest_blockhash;
message
} else {
Message::new_with_blockhash(&instructions, fee_payer, &latest_blockhash)
};
let mut transaction = Transaction::new_unsigned(message);
transaction
.try_partial_sign(&vec![self.payer.clone()], latest_blockhash)
.map_err(|error| TokenError::Client(error.into()))?;
tx.try_sign(signing_keypairs, latest_blockhash)
transaction
.try_partial_sign(signing_keypairs, latest_blockhash)
.map_err(|error| TokenError::Client(error.into()))?;
Ok(transaction)
}
pub async fn process_ixs<S: Signers>(
&self,
token_instructions: &[Instruction],
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let transaction = self
.construct_tx(token_instructions, signing_keypairs)
.await?;
self.client
.send_transaction(&tx)
.send_transaction(&transaction)
.await
.map_err(TokenError::Client)
}
/// Create and initialize a token.
#[allow(clippy::too_many_arguments)]
pub async fn create_mint<'a, S2: Signer>(
client: Arc<dyn ProgramClient<T>>,
program_id: &'a Pubkey,
payer: S,
mint_account: &'a S2,
pub async fn create_mint<'a, S: Signers>(
&self,
mint_authority: &'a Pubkey,
freeze_authority: Option<&'a Pubkey>,
decimals: u8,
extension_initialization_params: Vec<ExtensionInitializationParams>,
) -> TokenResult<Self> {
let mint_pubkey = mint_account.pubkey();
signing_keypairs: &S,
) -> TokenResult<T::Output> {
let extension_types = extension_initialization_params
.iter()
.map(|e| e.extension())
.collect::<Vec<_>>();
let space = ExtensionType::get_account_len::<Mint>(&extension_types);
let token = Self::new(client, program_id, &mint_account.pubkey(), payer);
let mut instructions = vec![system_instruction::create_account(
&token.payer.pubkey(),
&mint_pubkey,
token
.client
&self.payer.pubkey(),
&self.pubkey,
self.client
.get_minimum_balance_for_rent_exemption(space)
.await
.map_err(TokenError::Client)?,
space as u64,
program_id,
&self.program_id,
)];
for params in extension_initialization_params {
instructions.push(params.instruction(program_id, &mint_pubkey)?);
instructions.push(params.instruction(&self.program_id, &self.pubkey)?);
}
instructions.push(instruction::initialize_mint(
program_id,
&mint_pubkey,
&self.program_id,
&self.pubkey,
mint_authority,
freeze_authority,
decimals,
)?);
token.process_ixs(&instructions, &[mint_account]).await?;
Ok(token)
self.process_ixs(&instructions, signing_keypairs).await
}
/// Create native mint
pub async fn create_native_mint(
client: Arc<dyn ProgramClient<T>>,
program_id: &Pubkey,
payer: S,
payer: Arc<dyn Signer>,
) -> TokenResult<Self> {
let token = Self::new(client, program_id, &native_mint::id(), payer);
token
@ -377,7 +432,7 @@ where
/// Create and initialize a new token account.
pub async fn create_auxiliary_token_account(
&self,
account: &S,
account: &dyn Signer,
owner: &Pubkey,
) -> TokenResult<Pubkey> {
self.create_auxiliary_token_account_with_extension_space(account, owner, vec![])
@ -387,7 +442,7 @@ where
/// Create and initialize a new token account.
pub async fn create_auxiliary_token_account_with_extension_space(
&self,
account: &S,
account: &dyn Signer,
owner: &Pubkey,
extensions: Vec<ExtensionType>,
) -> TokenResult<Pubkey> {
@ -420,7 +475,7 @@ where
owner,
)?,
],
&[account],
&vec![account],
)
.await
.map(|_| account.pubkey())
@ -481,12 +536,12 @@ where
}
/// Assign a new authority to the account.
pub async fn set_authority<S2: Signer>(
pub async fn set_authority<S: Signer>(
&self,
account: &Pubkey,
new_authority: Option<&Pubkey>,
authority_type: instruction::AuthorityType,
owner: &S2,
owner: &S,
) -> TokenResult<()> {
self.process_ixs(
&[instruction::set_authority(
@ -504,10 +559,10 @@ where
}
/// Mint new tokens
pub async fn mint_to<S2: Signer>(
pub async fn mint_to<S: Signer>(
&self,
destination: &Pubkey,
authority: &S2,
authority: &S,
amount: u64,
) -> TokenResult<()> {
self.process_ixs(
@ -526,11 +581,11 @@ where
}
/// Transfer tokens to another account
pub async fn transfer_unchecked<S2: Signer>(
pub async fn transfer_unchecked<S: Signer>(
&self,
source: &Pubkey,
destination: &Pubkey,
authority: &S2,
authority: &S,
amount: u64,
) -> TokenResult<T::Output> {
self.process_ixs(
@ -549,11 +604,11 @@ where
}
/// Transfer tokens to another account
pub async fn transfer_checked<S2: Signer>(
pub async fn transfer_checked<S: Signer>(
&self,
source: &Pubkey,
destination: &Pubkey,
authority: &S2,
authority: &S,
amount: u64,
decimals: u8,
) -> TokenResult<T::Output> {
@ -574,11 +629,11 @@ where
}
/// Transfer tokens to another account, given an expected fee
pub async fn transfer_checked_with_fee<S2: Signer>(
pub async fn transfer_checked_with_fee<S: Signer>(
&self,
source: &Pubkey,
destination: &Pubkey,
authority: &S2,
authority: &S,
amount: u64,
decimals: u8,
fee: u64,
@ -601,10 +656,10 @@ where
}
/// Burn tokens from account
pub async fn burn<S2: Signer>(
pub async fn burn<S: Signer>(
&self,
source: &Pubkey,
authority: &S2,
authority: &S,
amount: u64,
) -> TokenResult<T::Output> {
self.process_ixs(
@ -622,10 +677,10 @@ where
}
/// Burn tokens from account
pub async fn burn_checked<S2: Signer>(
pub async fn burn_checked<S: Signer>(
&self,
source: &Pubkey,
authority: &S2,
authority: &S,
amount: u64,
decimals: u8,
) -> TokenResult<T::Output> {
@ -645,11 +700,11 @@ where
}
/// Approve a delegate to spend tokens
pub async fn approve<S2: Signer>(
pub async fn approve<S: Signer>(
&self,
source: &Pubkey,
delegate: &Pubkey,
authority: &S2,
authority: &S,
amount: u64,
) -> TokenResult<T::Output> {
self.process_ixs(
@ -667,11 +722,11 @@ where
}
/// Approve a delegate to spend tokens, with decimal check
pub async fn approve_checked<S2: Signer>(
pub async fn approve_checked<S: Signer>(
&self,
source: &Pubkey,
delegate: &Pubkey,
authority: &S2,
authority: &S,
amount: u64,
decimals: u8,
) -> TokenResult<T::Output> {
@ -692,10 +747,10 @@ where
}
/// Revoke a delegate
pub async fn revoke<S2: Signer>(
pub async fn revoke<S: Signer>(
&self,
source: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[instruction::revoke(
@ -710,11 +765,11 @@ where
}
/// Close account into another
pub async fn close_account<S2: Signer>(
pub async fn close_account<S: Signer>(
&self,
account: &Pubkey,
destination: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[instruction::close_account(
@ -730,10 +785,10 @@ where
}
/// Freeze a token account
pub async fn freeze_account<S2: Signer>(
pub async fn freeze_account<S: Signer>(
&self,
account: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[instruction::freeze_account(
@ -749,10 +804,10 @@ where
}
/// Thaw / unfreeze a token account
pub async fn thaw_account<S2: Signer>(
pub async fn thaw_account<S: Signer>(
&self,
account: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[instruction::thaw_account(
@ -777,9 +832,9 @@ where
}
/// Set transfer fee
pub async fn set_transfer_fee<S2: Signer>(
pub async fn set_transfer_fee<S: Signer>(
&self,
authority: &S2,
authority: &S,
transfer_fee_basis_points: u16,
maximum_fee: u64,
) -> TokenResult<T::Output> {
@ -798,9 +853,9 @@ where
}
/// Set default account state on mint
pub async fn set_default_account_state<S2: Signer>(
pub async fn set_default_account_state<S: Signer>(
&self,
authority: &S2,
authority: &S,
state: &AccountState,
) -> TokenResult<T::Output> {
self.process_ixs(
@ -835,10 +890,10 @@ where
}
/// Withdraw withheld tokens from mint
pub async fn withdraw_withheld_tokens_from_mint<S2: Signer>(
pub async fn withdraw_withheld_tokens_from_mint<S: Signer>(
&self,
destination: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[
@ -856,10 +911,10 @@ where
}
/// Withdraw withheld tokens from accounts
pub async fn withdraw_withheld_tokens_from_accounts<S2: Signer>(
pub async fn withdraw_withheld_tokens_from_accounts<S: Signer>(
&self,
destination: &Pubkey,
authority: &S2,
authority: &S,
sources: &[&Pubkey],
) -> TokenResult<T::Output> {
self.process_ixs(
@ -879,10 +934,10 @@ where
}
/// Reallocate a token account to be large enough for a set of ExtensionTypes
pub async fn reallocate<S2: Signer>(
pub async fn reallocate<S: Signer>(
&self,
account: &Pubkey,
authority: &S2,
authority: &S,
extension_types: &[ExtensionType],
) -> TokenResult<T::Output> {
self.process_ixs(
@ -900,10 +955,10 @@ where
}
/// Require memos on transfers into this account
pub async fn enable_required_transfer_memos<S2: Signer>(
pub async fn enable_required_transfer_memos<S: Signer>(
&self,
account: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[memo_transfer::instruction::enable_required_transfer_memos(
@ -918,10 +973,10 @@ where
}
/// Stop requiring memos on transfers into this account
pub async fn disable_required_transfer_memos<S2: Signer>(
pub async fn disable_required_transfer_memos<S: Signer>(
&self,
account: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[memo_transfer::instruction::disable_required_transfer_memos(
@ -936,9 +991,9 @@ where
}
/// Update interest rate
pub async fn update_interest_rate<S2: Signer>(
pub async fn update_interest_rate<S: Signer>(
&self,
authority: &S2,
authority: &S,
new_rate: i16,
) -> TokenResult<T::Output> {
self.process_ixs(
@ -955,11 +1010,11 @@ where
}
/// Update confidential transfer mint
pub async fn confidential_transfer_update_mint<S2: Signer>(
pub async fn confidential_transfer_update_mint<S: Signer>(
&self,
authority: &S2,
authority: &S,
new_ct_mint: confidential_transfer::ConfidentialTransferMint,
new_authority: Option<&S2>,
new_authority: Option<&S>,
) -> TokenResult<T::Output> {
let mut signers = vec![authority];
if let Some(new_authority) = new_authority {
@ -978,10 +1033,10 @@ where
}
/// Configures confidential transfers for a token account
pub async fn confidential_transfer_configure_token_account<S2: Signer>(
pub async fn confidential_transfer_configure_token_account<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<T::Output> {
let maximum_pending_balance_credit_counter =
2 << confidential_transfer::MAXIMUM_DEPOSIT_TRANSFER_AMOUNT_BIT_LENGTH;
@ -994,10 +1049,10 @@ where
.await
}
pub async fn confidential_transfer_configure_token_account_with_pending_counter<S2: Signer>(
pub async fn confidential_transfer_configure_token_account_with_pending_counter<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S2,
authority: &S,
maximum_pending_balance_credit_counter: u64,
) -> TokenResult<T::Output> {
let elgamal_pubkey = ElGamalKeypair::new(authority, token_account)
@ -1018,11 +1073,11 @@ where
}
pub async fn confidential_transfer_configure_token_account_with_pending_counter_and_keypair<
S2: Signer,
S: Signer,
>(
&self,
token_account: &Pubkey,
authority: &S2,
authority: &S,
maximum_pending_balance_credit_counter: u64,
elgamal_pubkey: ElGamalPubkey,
decryptable_zero_balance: AeCiphertext,
@ -1044,10 +1099,10 @@ where
}
/// Approves a token account for confidential transfers
pub async fn confidential_transfer_approve_account<S2: Signer>(
pub async fn confidential_transfer_approve_account<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[confidential_transfer::instruction::approve_account(
@ -1062,10 +1117,10 @@ where
}
/// Prepare a token account with the confidential transfer extension for closing
pub async fn confidential_transfer_empty_account<S2: Signer>(
pub async fn confidential_transfer_empty_account<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<T::Output> {
let elgamal_keypair =
ElGamalKeypair::new(authority, token_account).map_err(TokenError::Key)?;
@ -1077,10 +1132,10 @@ where
.await
}
pub async fn confidential_transfer_empty_account_with_keypair<S2: Signer>(
pub async fn confidential_transfer_empty_account_with_keypair<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S2,
authority: &S,
elgamal_keypair: &ElGamalKeypair,
) -> TokenResult<T::Output> {
let state = self.get_account_info(token_account).await.unwrap();
@ -1108,10 +1163,10 @@ where
/// Fetch and decrypt the available balance of a confidential token account using the uniquely
/// derived decryption key from a signer
pub async fn confidential_transfer_get_available_balance<S2: Signer>(
pub async fn confidential_transfer_get_available_balance<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<u64> {
let authenticated_encryption_key =
AeKey::new(authority, token_account).map_err(TokenError::Key)?;
@ -1147,10 +1202,10 @@ where
/// Fetch and decrypt the pending balance of a confidential token account using the uniquely
/// derived decryption key from a signer
pub async fn confidential_transfer_get_pending_balance<S2: Signer>(
pub async fn confidential_transfer_get_pending_balance<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<u64> {
let elgamal_keypair =
ElGamalKeypair::new(authority, token_account).map_err(TokenError::Key)?;
@ -1187,9 +1242,9 @@ where
Ok(pending_balance)
}
pub async fn confidential_transfer_get_withheld_amount<S2: Signer>(
pub async fn confidential_transfer_get_withheld_amount<S: Signer>(
&self,
withdraw_withheld_authority: &S2,
withdraw_withheld_authority: &S,
sources: &[&Pubkey],
) -> TokenResult<u64> {
let withdraw_withheld_authority_elgamal_keypair =
@ -1229,7 +1284,7 @@ where
}
/// Fetch the ElGamal public key associated with a confidential token account
pub async fn confidential_transfer_get_encryption_pubkey<S2: Signer>(
pub async fn confidential_transfer_get_encryption_pubkey<S: Signer>(
&self,
token_account: &Pubkey,
) -> TokenResult<ElGamalPubkey> {
@ -1245,7 +1300,7 @@ where
}
/// Fetch the ElGamal pubkey key of the auditor associated with a confidential token mint
pub async fn confidential_transfer_get_auditor_encryption_pubkey<S2: Signer>(
pub async fn confidential_transfer_get_auditor_encryption_pubkey<S: Signer>(
&self,
) -> TokenResult<ElGamalPubkey> {
let mint_state = self.get_mint_info().await.unwrap();
@ -1262,7 +1317,7 @@ where
/// Fetch the ElGamal pubkey key of the withdraw withheld authority associated with a
/// confidential token mint
pub async fn confidential_transfer_get_withdraw_withheld_authority_encryption_pubkey<
S2: Signer,
S: Signer,
>(
&self,
) -> TokenResult<ElGamalPubkey> {
@ -1278,11 +1333,11 @@ where
}
/// Deposit SPL Tokens into the pending balance of a confidential token account
pub async fn confidential_transfer_deposit<S2: Signer>(
pub async fn confidential_transfer_deposit<S: Signer>(
&self,
source_token_account: &Pubkey,
destination_token_account: &Pubkey,
source_token_authority: &S2,
source_token_authority: &S,
amount: u64,
decimals: u8,
) -> TokenResult<T::Output> {
@ -1309,11 +1364,11 @@ where
/// Withdraw SPL Tokens from the available balance of a confidential token account using the
/// uniquely derived decryption key from a signer
#[allow(clippy::too_many_arguments)]
pub async fn confidential_transfer_withdraw<S2: Signer>(
pub async fn confidential_transfer_withdraw<S: Signer>(
&self,
source_token_account: &Pubkey,
destination_token_account: &Pubkey,
source_token_authority: &S2,
source_token_authority: &S,
amount: u64,
source_available_balance: u64,
source_available_balance_ciphertext: &ElGamalCiphertext,
@ -1342,11 +1397,11 @@ where
/// Withdraw SPL Tokens from the available balance of a confidential token account using custom
/// keys
#[allow(clippy::too_many_arguments)]
pub async fn confidential_transfer_withdraw_with_key<S2: Signer>(
pub async fn confidential_transfer_withdraw_with_key<S: Signer>(
&self,
source_token_account: &Pubkey,
destination_token_account: &Pubkey,
source_token_authority: &S2,
source_token_authority: &S,
amount: u64,
decimals: u8,
source_available_balance: u64,
@ -1388,11 +1443,11 @@ where
/// Transfer tokens confidentially using the uniquely derived decryption keys from a signer
#[allow(clippy::too_many_arguments)]
pub async fn confidential_transfer_transfer<S2: Signer>(
pub async fn confidential_transfer_transfer<S: Signer>(
&self,
source_token_account: &Pubkey,
destination_token_account: &Pubkey,
source_token_authority: &S2,
source_token_authority: &S,
amount: u64,
source_available_balance: u64,
source_available_balance_ciphertext: &ElGamalCiphertext,
@ -1422,11 +1477,11 @@ where
/// Transfer tokens confidentially using custom decryption keys
#[allow(clippy::too_many_arguments)]
pub async fn confidential_transfer_transfer_with_key<S2: Signer>(
pub async fn confidential_transfer_transfer_with_key<S: Signer>(
&self,
source_token_account: &Pubkey,
destination_token_account: &Pubkey,
source_token_authority: &S2,
source_token_authority: &S,
amount: u64,
source_available_balance: u64,
source_available_balance_ciphertext: &ElGamalCiphertext,
@ -1475,11 +1530,11 @@ where
/// Transfer tokens confidentially with fee using the uniquely derived decryption keys from a
/// signer
#[allow(clippy::too_many_arguments)]
pub async fn confidential_transfer_transfer_with_fee<S2: Signer>(
pub async fn confidential_transfer_transfer_with_fee<S: Signer>(
&self,
source_token_account: &Pubkey,
destination_token_account: &Pubkey,
source_token_authority: &S2,
source_token_authority: &S,
amount: u64,
source_available_balance: u64,
source_available_balance_ciphertext: &ElGamalCiphertext,
@ -1513,11 +1568,11 @@ where
/// Transfer tokens confidential with fee using custom decryption keys
#[allow(clippy::too_many_arguments)]
pub async fn confidential_transfer_transfer_with_fee_with_key<S2: Signer>(
pub async fn confidential_transfer_transfer_with_fee_with_key<S: Signer>(
&self,
source_token_account: &Pubkey,
destination_token_account: &Pubkey,
source_token_authority: &S2,
source_token_authority: &S,
amount: u64,
source_available_balance: u64,
source_available_balance_ciphertext: &ElGamalCiphertext,
@ -1579,10 +1634,10 @@ where
/// Applies the confidential transfer pending balance to the available balance using the
/// uniquely derived decryption key
pub async fn confidential_transfer_apply_pending_balance<S2: Signer>(
pub async fn confidential_transfer_apply_pending_balance<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S2,
authority: &S,
available_balance: u64,
pending_balance: u64,
expected_pending_balance_credit_counter: u64,
@ -1603,10 +1658,10 @@ where
/// Applies the confidential transfer pending balance to the available balance using a custom
/// decryption key
pub async fn confidential_transfer_apply_pending_balance_with_key<S2: Signer>(
pub async fn confidential_transfer_apply_pending_balance_with_key<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S2,
authority: &S,
available_balance: u64,
pending_balance: u64,
expected_pending_balance_credit_counter: u64,
@ -1631,10 +1686,10 @@ where
}
/// Enable confidential transfer `Deposit` and `Transfer` instructions for a token account
pub async fn confidential_transfer_enable_balance_credits<S2: Signer>(
pub async fn confidential_transfer_enable_balance_credits<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[confidential_transfer::instruction::enable_balance_credits(
@ -1649,10 +1704,10 @@ where
}
/// Disable confidential transfer `Deposit` and `Transfer` instructions for a token account
pub async fn confidential_transfer_disable_balance_credits<S2: Signer>(
pub async fn confidential_transfer_disable_balance_credits<S: Signer>(
&self,
token_account: &Pubkey,
authority: &S2,
authority: &S,
) -> TokenResult<T::Output> {
self.process_ixs(
&[confidential_transfer::instruction::disable_balance_credits(
@ -1667,9 +1722,9 @@ where
}
/// Withdraw withheld confidential tokens from mint using the uniquely derived decryption key
pub async fn confidential_transfer_withdraw_withheld_tokens_from_mint<S2: Signer>(
pub async fn confidential_transfer_withdraw_withheld_tokens_from_mint<S: Signer>(
&self,
withdraw_withheld_authority: &S2,
withdraw_withheld_authority: &S,
destination_token_account: &Pubkey,
destination_elgamal_pubkey: &ElGamalPubkey,
withheld_amount: u64,
@ -1692,9 +1747,9 @@ where
}
/// Withdraw withheld confidential tokens from mint using a custom decryption key
pub async fn confidential_transfer_withdraw_withheld_tokens_from_mint_with_key<S2: Signer>(
pub async fn confidential_transfer_withdraw_withheld_tokens_from_mint_with_key<S: Signer>(
&self,
withdraw_withheld_authority: &S2,
withdraw_withheld_authority: &S,
destination_token_account: &Pubkey,
destination_elgamal_pubkey: &ElGamalPubkey,
withheld_amount: u64,
@ -1725,9 +1780,9 @@ where
/// Withdraw withheld confidential tokens from accounts using the uniquely derived decryption
/// key
pub async fn confidential_transfer_withdraw_withheld_tokens_from_accounts<S2: Signer>(
pub async fn confidential_transfer_withdraw_withheld_tokens_from_accounts<S: Signer>(
&self,
withdraw_withheld_authority: &S2,
withdraw_withheld_authority: &S,
destination_token_account: &Pubkey,
destination_elgamal_pubkey: &ElGamalPubkey,
aggregate_withheld_amount: u64,
@ -1753,10 +1808,10 @@ where
/// Withdraw withheld confidential tokens from accounts using a custom decryption key
#[allow(clippy::too_many_arguments)]
pub async fn confidential_transfer_withdraw_withheld_tokens_from_accounts_with_key<
S2: Signer,
S: Signer,
>(
&self,
withdraw_withheld_authority: &S2,
withdraw_withheld_authority: &S,
destination_token_account: &Pubkey,
destination_elgamal_pubkey: &ElGamalPubkey,
aggregate_withheld_amount: u64,

View File

@ -18,7 +18,7 @@ use {
struct TestContext {
pub decimals: u8,
pub mint_authority: Keypair,
pub token: Token<ProgramBanksClientProcessTransaction, Keypair>,
pub token: Token<ProgramBanksClientProcessTransaction>,
pub alice: Keypair,
pub bob: Keypair,
@ -44,15 +44,20 @@ impl TestContext {
let mint_authority = Keypair::new();
let mint_authority_pubkey = mint_authority.pubkey();
let token = Token::create_mint(
let token = Token::new(
Arc::clone(&client),
&spl_token_2022::id(),
keypair_clone(&payer),
&mint_account,
&mint_account.pubkey(),
Arc::new(keypair_clone(&payer)),
);
token
.create_mint(
&mint_authority_pubkey,
None,
decimals,
vec![],
&[&mint_account],
)
.await
.expect("failed to create mint");

View File

@ -14,7 +14,7 @@ use {
pub struct TokenContext {
pub decimals: u8,
pub mint_authority: Keypair,
pub token: Token<ProgramBanksClientProcessTransaction, Keypair>,
pub token: Token<ProgramBanksClientProcessTransaction>,
pub alice: Keypair,
pub bob: Keypair,
pub freeze_authority: Option<Keypair>,
@ -75,17 +75,23 @@ impl TestContext {
.as_ref()
.map(|authority| authority.pubkey());
let token = Token::create_mint(
let token = Token::new(
Arc::clone(&client),
&id(),
payer,
&mint_account,
&mint_account.pubkey(),
Arc::new(payer),
);
token
.create_mint(
&mint_authority_pubkey,
freeze_authority_pubkey.as_ref(),
decimals,
extension_init_params,
&[&mint_account],
)
.await?;
self.token_context = Some(TokenContext {
decimals,
mint_authority,
@ -106,7 +112,7 @@ impl TestContext {
ProgramBanksClientProcessTransaction,
));
let token = Token::create_native_mint(Arc::clone(&client), &id(), payer).await?;
let token = Token::create_native_mint(Arc::clone(&client), &id(), Arc::new(payer)).await?;
self.token_context = Some(TokenContext {
decimals: native_mint::DECIMALS,
mint_authority: Keypair::new(), /*bogus*/

View File

@ -17,7 +17,7 @@ use {
};
async fn run_basic(
token: Token<ProgramBanksClientProcessTransaction, Keypair>,
token: Token<ProgramBanksClientProcessTransaction>,
context: Arc<Mutex<ProgramTestContext>>,
account: Pubkey,
) {

View File

@ -74,7 +74,7 @@ fn test_transfer_fee_config_with_keypairs() -> TransferFeeConfigWithKeypairs {
struct TokenWithAccounts {
context: TestContext,
token: Token<ProgramBanksClientProcessTransaction, Keypair>,
token: Token<ProgramBanksClientProcessTransaction>,
transfer_fee_config: TransferFeeConfig,
withdraw_withheld_authority: Keypair,
freeze_authority: Keypair,
@ -1093,7 +1093,7 @@ async fn no_fees_from_self_transfer() {
}
async fn create_and_transfer_to_account(
token: &Token<ProgramBanksClientProcessTransaction, Keypair>,
token: &Token<ProgramBanksClientProcessTransaction>,
source: &Pubkey,
authority: &Keypair,
owner: &Pubkey,