From bd1c82397ff1565af082756222ac8c1e02998b69 Mon Sep 17 00:00:00 2001 From: tonton <19677766+tonton-sol@users.noreply.github.com> Date: Fri, 12 Jan 2024 15:17:57 +0900 Subject: [PATCH] [token-client] Added transfer-hook compatibility to `create_recipient_associated_account_and_transfer` (#6120) added extra metas resolution to create_recipient_associated_account_and_transfer() --- token/client/src/token.rs | 46 ++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/token/client/src/token.rs b/token/client/src/token.rs index 1f41aa31b80..f985700a75c 100644 --- a/token/client/src/token.rs +++ b/token/client/src/token.rs @@ -979,6 +979,12 @@ where let signing_pubkeys = signing_keypairs.pubkeys(); let multisig_signers = self.get_multisig_signers(authority, &signing_pubkeys); + let fetch_account_data_fn = |address| { + self.client + .get_account(address) + .map_ok(|opt| opt.map(|acc| acc.data)) + }; + if *destination != self.get_associated_token_address(destination_owner) { return Err(TokenError::AccountInvalidAssociatedAddress); } @@ -1006,16 +1012,36 @@ where fee, )?); } else if let Some(decimals) = self.decimals { - instructions.push(instruction::transfer_checked( - &self.program_id, - source, - &self.pubkey, - destination, - authority, - &multisig_signers, - amount, - decimals, - )?); + instructions.push( + 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)] instructions.push(instruction::transfer(