From 8bda3edabb3010d090006ecd2b026e1a5e2b2e0d Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 10 Jan 2024 08:44:53 -0600 Subject: [PATCH] token client: refactor transfer to use new offchain helper --- token/client/src/token.rs | 86 ++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/token/client/src/token.rs b/token/client/src/token.rs index d09ca78a8b7..357cb1717b6 100644 --- a/token/client/src/token.rs +++ b/token/client/src/token.rs @@ -913,43 +913,71 @@ 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( + 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 { + let mint = self.get_mint_info().await?; + 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