From e13db27d32cfa22dfe841f935747fa0f482fc889 Mon Sep 17 00:00:00 2001 From: Joe C Date: Thu, 11 Jan 2024 15:18:30 -0600 Subject: [PATCH] token client: refactor transfer to use new offchain helper 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. --- token/client/src/token.rs | 62 ++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/token/client/src/token.rs b/token/client/src/token.rs index d09ca78a8b7..1f41aa31b80 100644 --- a/token/client/src/token.rs +++ b/token/client/src/token.rs @@ -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( @@ -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 }