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

Commit e13db27

Browse files
author
Joe C
authored
token client: refactor transfer to use new offchain helper
This PR continues the necessary repairs for addressing #6064 by refactoring the Token Client's `transfer(..)` function to use the new offchain transfer hook helpers. If transfer hook accounts are provided, in either case they're appended to the instruction like before. If no transfer hook accounts are provided: - If decimals are provided, Token2022's offchain helper `create_transfer_checked_instruction_with_extra_metas(..)` is used. - If decimals are not provided, SPL Transfer Hook interface's `add_extra_account_metas_for_execute(..)` is used. In either case where no transfer hook accounts are provided, the new, non-deprecated helpers are used.
1 parent 3e6a9b8 commit e13db27

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

token/client/src/token.rs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -913,17 +913,41 @@ where
913913
let signing_pubkeys = signing_keypairs.pubkeys();
914914
let multisig_signers = self.get_multisig_signers(authority, &signing_pubkeys);
915915

916-
let mut instruction = if let Some(decimals) = self.decimals {
917-
instruction::transfer_checked(
918-
&self.program_id,
919-
source,
920-
&self.pubkey,
921-
destination,
922-
authority,
923-
&multisig_signers,
924-
amount,
925-
decimals,
926-
)?
916+
let fetch_account_data_fn = |address| {
917+
self.client
918+
.get_account(address)
919+
.map_ok(|opt| opt.map(|acc| acc.data))
920+
};
921+
922+
let instruction = if let Some(decimals) = self.decimals {
923+
if let Some(transfer_hook_accounts) = &self.transfer_hook_accounts {
924+
let mut instruction = instruction::transfer_checked(
925+
&self.program_id,
926+
source,
927+
self.get_address(),
928+
destination,
929+
authority,
930+
&multisig_signers,
931+
amount,
932+
decimals,
933+
)?;
934+
instruction.accounts.extend(transfer_hook_accounts.clone());
935+
instruction
936+
} else {
937+
offchain::create_transfer_checked_instruction_with_extra_metas(
938+
&self.program_id,
939+
source,
940+
self.get_address(),
941+
destination,
942+
authority,
943+
&multisig_signers,
944+
amount,
945+
decimals,
946+
fetch_account_data_fn,
947+
)
948+
.await
949+
.map_err(|_| TokenError::AccountNotFound)?
950+
}
927951
} else {
928952
#[allow(deprecated)]
929953
instruction::transfer(
@@ -935,22 +959,6 @@ where
935959
amount,
936960
)?
937961
};
938-
if let Some(transfer_hook_accounts) = &self.transfer_hook_accounts {
939-
instruction.accounts.extend(transfer_hook_accounts.clone());
940-
} else {
941-
#[allow(deprecated)]
942-
offchain::resolve_extra_transfer_account_metas(
943-
&mut instruction,
944-
|address| {
945-
self.client
946-
.get_account(address)
947-
.map_ok(|opt| opt.map(|acc| acc.data))
948-
},
949-
self.get_address(),
950-
)
951-
.await
952-
.map_err(|_| TokenError::AccountNotFound)?;
953-
};
954962

955963
self.process_ixs(&[instruction], signing_keypairs).await
956964
}

0 commit comments

Comments
 (0)