Skip to content

Commit

Permalink
token client: refactor transfer to use new offchain helper
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Jan 10, 2024
1 parent 0c3b21e commit e7bd4a3
Showing 1 changed file with 58 additions and 29 deletions.
87 changes: 58 additions & 29 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,43 +913,72 @@ where
let signing_pubkeys = signing_keypairs.pubkeys();
let multisig_signers = self.get_multisig_signers(authority, &signing_pubkeys);

let mut instruction = if let Some(decimals) = self.decimals {
instruction::transfer_checked(
&self.program_id,
source,
&self.pubkey,
destination,
authority,
&multisig_signers,
amount,
decimals,
)?
let fetch_account_data_fn = |address| {
self.client
.get_account(address)
.map_ok(|opt| opt.map(|acc| acc.data))
};

let mint = self.get_mint_info().await?;

let instruction = if let Some(decimals) = self.decimals {
if let Some(transfer_hook_accounts) = &self.transfer_hook_accounts {
let mut instruction = instruction::transfer_checked(
&self.program_id,
source,
&self.pubkey,
destination,
authority,
&multisig_signers,
amount,
decimals,
)?;
instruction.accounts.extend(transfer_hook_accounts.clone());
instruction
} else {
offchain::create_transfer_checked_instruction_with_extra_metas(
&self.program_id,
source,
self.get_address(),
destination,
authority,
&multisig_signers,
amount,
decimals,
fetch_account_data_fn,
)
.await
.map_err(|_| TokenError::AccountNotFound)?
}
} else {
#[allow(deprecated)]
instruction::transfer(
let mut instruction = instruction::transfer(
&self.program_id,
source,
destination,
authority,
&multisig_signers,
amount,
)?
};
if let Some(transfer_hook_accounts) = &self.transfer_hook_accounts {
instruction.accounts.extend(transfer_hook_accounts.clone());
} else {
#[allow(deprecated)]
offchain::resolve_extra_transfer_account_metas(
&mut instruction,
|address| {
self.client
.get_account(address)
.map_ok(|opt| opt.map(|acc| acc.data))
},
self.get_address(),
)
.await
.map_err(|_| TokenError::AccountNotFound)?;
)?;
if let Some(transfer_hook_accounts) = &self.transfer_hook_accounts {
instruction.accounts.extend(transfer_hook_accounts.clone());
} else {
if let Some(program_id) = transfer_hook::get_program_id(&mint) {
spl_transfer_hook_interface::offchain::add_extra_account_metas_for_execute(
&mut instruction,
&program_id,
source,
self.get_address(),
destination,
authority,
amount,
fetch_account_data_fn,
)
.await
.map_err(|_| TokenError::AccountNotFound)?;
}
};
instruction
};

self.process_ixs(&[instruction], signing_keypairs).await
Expand Down

0 comments on commit e7bd4a3

Please sign in to comment.