|
1 | 1 | import { struct, u8 } from '@solana/buffer-layout';
|
2 |
| -import type { PublicKey, Signer } from '@solana/web3.js'; |
| 2 | +import type { Commitment, Connection, PublicKey, Signer } from '@solana/web3.js'; |
3 | 3 | import { TransactionInstruction } from '@solana/web3.js';
|
4 |
| -import { programSupportsExtensions, TOKEN_2022_PROGRAM_ID } from '../../constants.js'; |
| 4 | +import { programSupportsExtensions, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from '../../constants.js'; |
5 | 5 | import { TokenUnsupportedInstructionError } from '../../errors.js';
|
6 | 6 | import { addSigners } from '../../instructions/internal.js';
|
7 | 7 | import { TokenInstruction } from '../../instructions/types.js';
|
8 | 8 | import { publicKey } from '@solana/buffer-layout-utils';
|
| 9 | +import { createTransferCheckedInstruction } from '../../instructions/transferChecked.js'; |
| 10 | +import { createTransferCheckedWithFeeInstruction } from '../transferFee/instructions.js'; |
| 11 | +import { getMint } from '../../state/mint.js'; |
| 12 | +import { getExtraAccountMetaAccount, getExtraAccountMetas, getTransferHook, resolveExtraAccountMeta } from './state.js'; |
9 | 13 |
|
10 | 14 | export enum TransferHookInstruction {
|
11 | 15 | Initialize = 0,
|
@@ -112,3 +116,158 @@ export function createUpdateTransferHookInstruction(
|
112 | 116 |
|
113 | 117 | return new TransactionInstruction({ keys, programId, data });
|
114 | 118 | }
|
| 119 | + |
| 120 | +/** |
| 121 | + * Add extra accounts needed for transfer hook to an instruction |
| 122 | + * |
| 123 | + * @param connection Connection to use |
| 124 | + * @param instruction The transferChecked instruction to add accounts to |
| 125 | + * @param commitment Commitment to use |
| 126 | + * @param programId SPL Token program account |
| 127 | + * |
| 128 | + * @return Instruction to add to a transaction |
| 129 | + */ |
| 130 | +export async function addExtraAccountsToInstruction( |
| 131 | + connection: Connection, |
| 132 | + instruction: TransactionInstruction, |
| 133 | + mint: PublicKey, |
| 134 | + commitment?: Commitment, |
| 135 | + programId = TOKEN_PROGRAM_ID |
| 136 | +): Promise<TransactionInstruction> { |
| 137 | + if (!programSupportsExtensions(programId)) { |
| 138 | + throw new TokenUnsupportedInstructionError(); |
| 139 | + } |
| 140 | + |
| 141 | + const mintInfo = await getMint(connection, mint, commitment, programId); |
| 142 | + const transferHook = getTransferHook(mintInfo); |
| 143 | + if (transferHook == null) { |
| 144 | + return instruction; |
| 145 | + } |
| 146 | + |
| 147 | + const extraAccountsAccount = getExtraAccountMetaAccount(transferHook.programId, mint); |
| 148 | + const extraAccountsInfo = await connection.getAccountInfo(extraAccountsAccount, commitment); |
| 149 | + if (extraAccountsInfo == null) { |
| 150 | + return instruction; |
| 151 | + } |
| 152 | + |
| 153 | + const extraAccountMetas = getExtraAccountMetas(extraAccountsInfo); |
| 154 | + |
| 155 | + const accountMetas = instruction.keys; |
| 156 | + accountMetas.push({ pubkey: extraAccountsAccount, isSigner: false, isWritable: false }); |
| 157 | + |
| 158 | + for (const extraAccountMeta of extraAccountMetas) { |
| 159 | + const accountMeta = resolveExtraAccountMeta( |
| 160 | + extraAccountMeta, |
| 161 | + accountMetas, |
| 162 | + instruction.data, |
| 163 | + transferHook.programId |
| 164 | + ); |
| 165 | + accountMetas.push(accountMeta); |
| 166 | + } |
| 167 | + accountMetas.push({ pubkey: transferHook.programId, isSigner: false, isWritable: false }); |
| 168 | + |
| 169 | + return new TransactionInstruction({ keys: accountMetas, programId, data: instruction.data }); |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * Construct an transferChecked instruction with extra accounts for transfer hook |
| 174 | + * |
| 175 | + * @param connection Connection to use |
| 176 | + * @param source Source account |
| 177 | + * @param mint Mint to update |
| 178 | + * @param destination Destination account |
| 179 | + * @param authority The mint's transfer hook authority |
| 180 | + * @param amount The amount of tokens to transfer |
| 181 | + * @param decimals Number of decimals in transfer amount |
| 182 | + * @param multiSigners The signer account(s) for a multisig |
| 183 | + * @param commitment Commitment to use |
| 184 | + * @param programId SPL Token program account |
| 185 | + * |
| 186 | + * @return Instruction to add to a transaction |
| 187 | + */ |
| 188 | +export async function createTransferCheckedWithTransferHookInstruction( |
| 189 | + connection: Connection, |
| 190 | + source: PublicKey, |
| 191 | + mint: PublicKey, |
| 192 | + destination: PublicKey, |
| 193 | + authority: PublicKey, |
| 194 | + amount: bigint, |
| 195 | + decimals: number, |
| 196 | + multiSigners: (Signer | PublicKey)[] = [], |
| 197 | + commitment?: Commitment, |
| 198 | + programId = TOKEN_PROGRAM_ID |
| 199 | +) { |
| 200 | + const rawInstruction = createTransferCheckedInstruction( |
| 201 | + source, |
| 202 | + mint, |
| 203 | + destination, |
| 204 | + authority, |
| 205 | + amount, |
| 206 | + decimals, |
| 207 | + multiSigners, |
| 208 | + programId |
| 209 | + ); |
| 210 | + |
| 211 | + const hydratedInstruction = await addExtraAccountsToInstruction( |
| 212 | + connection, |
| 213 | + rawInstruction, |
| 214 | + mint, |
| 215 | + commitment, |
| 216 | + programId |
| 217 | + ); |
| 218 | + |
| 219 | + return hydratedInstruction; |
| 220 | +} |
| 221 | + |
| 222 | +/** |
| 223 | + * Construct an transferChecked instruction with extra accounts for transfer hook |
| 224 | + * |
| 225 | + * @param connection Connection to use |
| 226 | + * @param source Source account |
| 227 | + * @param mint Mint to update |
| 228 | + * @param destination Destination account |
| 229 | + * @param authority The mint's transfer hook authority |
| 230 | + * @param amount The amount of tokens to transfer |
| 231 | + * @param decimals Number of decimals in transfer amount |
| 232 | + * @param fee The calculated fee for the transfer fee extension |
| 233 | + * @param multiSigners The signer account(s) for a multisig |
| 234 | + * @param commitment Commitment to use |
| 235 | + * @param programId SPL Token program account |
| 236 | + * |
| 237 | + * @return Instruction to add to a transaction |
| 238 | + */ |
| 239 | +export async function createTransferCheckedWithFeeAndTransferHookInstruction( |
| 240 | + connection: Connection, |
| 241 | + source: PublicKey, |
| 242 | + mint: PublicKey, |
| 243 | + destination: PublicKey, |
| 244 | + authority: PublicKey, |
| 245 | + amount: bigint, |
| 246 | + decimals: number, |
| 247 | + fee: bigint, |
| 248 | + multiSigners: (Signer | PublicKey)[] = [], |
| 249 | + commitment?: Commitment, |
| 250 | + programId = TOKEN_PROGRAM_ID |
| 251 | +) { |
| 252 | + const rawInstruction = createTransferCheckedWithFeeInstruction( |
| 253 | + source, |
| 254 | + mint, |
| 255 | + destination, |
| 256 | + authority, |
| 257 | + amount, |
| 258 | + decimals, |
| 259 | + fee, |
| 260 | + multiSigners, |
| 261 | + programId |
| 262 | + ); |
| 263 | + |
| 264 | + const hydratedInstruction = await addExtraAccountsToInstruction( |
| 265 | + connection, |
| 266 | + rawInstruction, |
| 267 | + mint, |
| 268 | + commitment, |
| 269 | + programId |
| 270 | + ); |
| 271 | + |
| 272 | + return hydratedInstruction; |
| 273 | +} |
0 commit comments