Skip to content

Commit

Permalink
[token-client] Added transfer-hook compatibility to `create_recipient…
Browse files Browse the repository at this point in the history
…_associated_account_and_transfer` (solana-labs#6120)

added extra metas resolution to create_recipient_associated_account_and_transfer()
  • Loading branch information
tonton-sol authored Jan 12, 2024
1 parent de2e356 commit bd1c823
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit bd1c823

Please sign in to comment.