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
This PR continues the necessary repairs for addressing #6064 by refactoring the
Token Client's `transfer(..)` function to use the new offchain transfer hook
helpers.

If transfer hook accounts are provided, in either case they're appended to the
instruction like before.

If no transfer hook accounts are provided:
- If decimals are provided, Token2022's offchain helper
  `create_transfer_checked_instruction_with_extra_metas(..)` is used.
- If decimals are not provided, SPL Transfer Hook interface's
  `add_extra_account_metas_for_execute(..)` is used.

In either case where no transfer hook accounts are provided, the new,
non-deprecated helpers are used.
  • Loading branch information
Joe C authored Jan 11, 2024
1 parent 3e6a9b8 commit e13db27
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,17 +913,41 @@ 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 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.get_address(),
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(
Expand All @@ -935,22 +959,6 @@ where
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)?;
};

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

0 comments on commit e13db27

Please sign in to comment.