|
| 1 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 2 | +import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core'; |
| 3 | +import { TransactionInstruction } from '@solana/web3.js'; |
| 4 | +import { Transaction } from './transaction'; |
| 5 | +import { TransactionBuilder } from './transactionBuilder'; |
| 6 | +import { InstructionBuilderTypes } from './constants'; |
| 7 | +import { CustomInstruction } from './iface'; |
| 8 | +import assert from 'assert'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Transaction builder for custom Solana instructions. |
| 12 | + * Allows building transactions with any set of raw Solana instructions. |
| 13 | + */ |
| 14 | +export class CustomInstructionBuilder extends TransactionBuilder { |
| 15 | + private _customInstructions: CustomInstruction[] = []; |
| 16 | + |
| 17 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 18 | + super(_coinConfig); |
| 19 | + } |
| 20 | + |
| 21 | + protected get transactionType(): TransactionType { |
| 22 | + return TransactionType.Send; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Initialize the builder from an existing transaction |
| 27 | + */ |
| 28 | + initBuilder(tx: Transaction): void { |
| 29 | + super.initBuilder(tx); |
| 30 | + |
| 31 | + for (const instruction of this._instructionsData) { |
| 32 | + if (instruction.type === InstructionBuilderTypes.CustomInstruction) { |
| 33 | + const customInstruction = instruction as CustomInstruction; |
| 34 | + this.addCustomInstruction(customInstruction.params.instruction); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Add a custom Solana instruction to the transaction |
| 41 | + * |
| 42 | + * @param instruction - The raw Solana TransactionInstruction |
| 43 | + * @returns This transaction builder |
| 44 | + */ |
| 45 | + addCustomInstruction(instruction: TransactionInstruction): this { |
| 46 | + if (!instruction) { |
| 47 | + throw new BuildTransactionError('Instruction cannot be null or undefined'); |
| 48 | + } |
| 49 | + |
| 50 | + if (!instruction.programId) { |
| 51 | + throw new BuildTransactionError('Instruction must have a valid programId'); |
| 52 | + } |
| 53 | + |
| 54 | + if (!instruction.keys || !Array.isArray(instruction.keys)) { |
| 55 | + throw new BuildTransactionError('Instruction must have valid keys array'); |
| 56 | + } |
| 57 | + |
| 58 | + if (!instruction.data || !Buffer.isBuffer(instruction.data)) { |
| 59 | + throw new BuildTransactionError('Instruction must have valid data buffer'); |
| 60 | + } |
| 61 | + |
| 62 | + const customInstruction: CustomInstruction = { |
| 63 | + type: InstructionBuilderTypes.CustomInstruction, |
| 64 | + params: { |
| 65 | + instruction, |
| 66 | + }, |
| 67 | + }; |
| 68 | + |
| 69 | + this._customInstructions.push(customInstruction); |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Add multiple custom Solana instructions to the transaction |
| 75 | + * |
| 76 | + * @param instructions - Array of raw Solana TransactionInstructions |
| 77 | + * @returns This transaction builder |
| 78 | + */ |
| 79 | + addCustomInstructions(instructions: TransactionInstruction[]): this { |
| 80 | + if (!Array.isArray(instructions)) { |
| 81 | + throw new BuildTransactionError('Instructions must be an array'); |
| 82 | + } |
| 83 | + |
| 84 | + for (const instruction of instructions) { |
| 85 | + this.addCustomInstruction(instruction); |
| 86 | + } |
| 87 | + |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Clear all custom instructions |
| 93 | + * |
| 94 | + * @returns This transaction builder |
| 95 | + */ |
| 96 | + clearInstructions(): this { |
| 97 | + this._customInstructions = []; |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get the current custom instructions |
| 103 | + * |
| 104 | + * @returns Array of custom instructions |
| 105 | + */ |
| 106 | + getInstructions(): CustomInstruction[] { |
| 107 | + return [...this._customInstructions]; |
| 108 | + } |
| 109 | + |
| 110 | + /** @inheritdoc */ |
| 111 | + protected async buildImplementation(): Promise<Transaction> { |
| 112 | + assert(this._customInstructions.length > 0, 'At least one custom instruction must be specified'); |
| 113 | + |
| 114 | + // Set the instructions data to our custom instructions |
| 115 | + this._instructionsData = [...this._customInstructions]; |
| 116 | + |
| 117 | + return await super.buildImplementation(); |
| 118 | + } |
| 119 | +} |
0 commit comments