Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit bd1c823

Browse files
authored
[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()
1 parent de2e356 commit bd1c823

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

token/client/src/token.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,12 @@ where
979979
let signing_pubkeys = signing_keypairs.pubkeys();
980980
let multisig_signers = self.get_multisig_signers(authority, &signing_pubkeys);
981981

982+
let fetch_account_data_fn = |address| {
983+
self.client
984+
.get_account(address)
985+
.map_ok(|opt| opt.map(|acc| acc.data))
986+
};
987+
982988
if *destination != self.get_associated_token_address(destination_owner) {
983989
return Err(TokenError::AccountInvalidAssociatedAddress);
984990
}
@@ -1006,16 +1012,36 @@ where
10061012
fee,
10071013
)?);
10081014
} else if let Some(decimals) = self.decimals {
1009-
instructions.push(instruction::transfer_checked(
1010-
&self.program_id,
1011-
source,
1012-
&self.pubkey,
1013-
destination,
1014-
authority,
1015-
&multisig_signers,
1016-
amount,
1017-
decimals,
1018-
)?);
1015+
instructions.push(
1016+
if let Some(transfer_hook_accounts) = &self.transfer_hook_accounts {
1017+
let mut instruction = instruction::transfer_checked(
1018+
&self.program_id,
1019+
source,
1020+
self.get_address(),
1021+
destination,
1022+
authority,
1023+
&multisig_signers,
1024+
amount,
1025+
decimals,
1026+
)?;
1027+
instruction.accounts.extend(transfer_hook_accounts.clone());
1028+
instruction
1029+
} else {
1030+
offchain::create_transfer_checked_instruction_with_extra_metas(
1031+
&self.program_id,
1032+
source,
1033+
self.get_address(),
1034+
destination,
1035+
authority,
1036+
&multisig_signers,
1037+
amount,
1038+
decimals,
1039+
fetch_account_data_fn,
1040+
)
1041+
.await
1042+
.map_err(|_| TokenError::AccountNotFound)?
1043+
},
1044+
);
10191045
} else {
10201046
#[allow(deprecated)]
10211047
instructions.push(instruction::transfer(

0 commit comments

Comments
 (0)