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

Commit 2ec0b9b

Browse files
committed
added create_transfer_checked_with_fee_instruction_with_extra_metas
1 parent 72260e4 commit 2ec0b9b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

token/program-2022/src/offchain.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,72 @@ where
7474
Ok(transfer_instruction)
7575
}
7676

77+
/// Offchain helper to create a `TransferCheckedWithFee` instruction with all
78+
/// additional required account metas for a transfer, including the ones
79+
/// required by the transfer hook.
80+
///
81+
/// To be client-agnostic and to avoid pulling in the full solana-sdk, this
82+
/// simply takes a function that will return its data as `Future<Vec<u8>>` for
83+
/// the given address. Can be called in the following way:
84+
///
85+
/// ```rust,ignore
86+
/// let instruction = create_transfer_checked_with_fee_instruction_with_extra_metas(
87+
/// &spl_token_2022::id(),
88+
/// &source,
89+
/// &mint,
90+
/// &destination,
91+
/// &authority,
92+
/// &[],
93+
/// amount,
94+
/// decimals,
95+
/// fee,
96+
/// |address| self.client.get_account(&address).map_ok(|opt| opt.map(|acc| acc.data)),
97+
/// )
98+
/// .await?
99+
/// ```
100+
#[allow(clippy::too_many_arguments)]
101+
pub async fn create_transfer_checked_with_fee_instruction_with_extra_metas<F, Fut>(
102+
token_program_id: &Pubkey,
103+
source_pubkey: &Pubkey,
104+
mint_pubkey: &Pubkey,
105+
destination_pubkey: &Pubkey,
106+
authority_pubkey: &Pubkey,
107+
signer_pubkeys: &[&Pubkey],
108+
amount: u64,
109+
decimals: u8,
110+
fee: u64,
111+
fetch_account_data_fn: F,
112+
) -> Result<Instruction, AccountFetchError>
113+
where
114+
F: Fn(Pubkey) -> Fut,
115+
Fut: Future<Output = AccountDataResult>,
116+
{
117+
let mut transfer_instruction = transfer_fee::instruction::transfer_checked_with_fee(
118+
token_program_id,
119+
source_pubkey,
120+
mint_pubkey,
121+
destination_pubkey,
122+
authority_pubkey,
123+
signer_pubkeys,
124+
amount,
125+
decimals,
126+
fee,
127+
)?;
128+
129+
add_extra_account_metas(
130+
&mut transfer_instruction,
131+
source_pubkey,
132+
mint_pubkey,
133+
destination_pubkey,
134+
authority_pubkey,
135+
amount,
136+
fetch_account_data_fn,
137+
)
138+
.await?;
139+
140+
Ok(transfer_instruction)
141+
}
142+
77143
/// Offchain helper to add required account metas to an instruction, including
78144
/// the ones required by the transfer hook.
79145
///

0 commit comments

Comments
 (0)