From 93c52cfb7ab05da05734579203e49d4b98a717df Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 10 Jan 2024 08:44:53 -0600 Subject: [PATCH 1/2] 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 From e2efa930f056effc7e4dbf0aef2999bd378c59b4 Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 10 Jan 2024 16:02:10 -0600 Subject: [PATCH 2/2] remove transfer hook from deprecate transfer branch --- token/client/src/token.rs | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/token/client/src/token.rs b/token/client/src/token.rs index 357cb1717b6..1f41aa31b80 100644 --- a/token/client/src/token.rs +++ b/token/client/src/token.rs @@ -950,34 +950,14 @@ where } } else { #[allow(deprecated)] - let mut instruction = instruction::transfer( + 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 { - 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