|
74 | 74 | Ok(transfer_instruction)
|
75 | 75 | }
|
76 | 76 |
|
| 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 | + |
77 | 143 | /// Offchain helper to add required account metas to an instruction, including
|
78 | 144 | /// the ones required by the transfer hook.
|
79 | 145 | ///
|
|
0 commit comments