UNPKG

@wuwei-labs/srsly

Version:
71 lines 2.49 kB
/** * Shared utilities for preparing instructions with compute budget * @module utils/instructions */ import { InstructionResult } from './instructionResult'; /** * Compute Budget Program ID */ const COMPUTE_BUDGET_PROGRAM = 'ComputeBudget111111111111111111111111111111'; /** * SetComputeUnitLimit instruction discriminator */ const SET_COMPUTE_UNIT_LIMIT_DISCRIMINATOR = 0x02; /** * Create a SetComputeUnitLimit instruction manually * * @param units - Compute units to allocate * @returns Instruction for setting compute unit limit */ function createComputeBudgetInstruction(units) { // Encode u32 in little-endian format const data = new Uint8Array(5); data[0] = SET_COMPUTE_UNIT_LIMIT_DISCRIMINATOR; data[1] = units & 0xff; data[2] = (units >> 8) & 0xff; data[3] = (units >> 16) & 0xff; data[4] = (units >> 24) & 0xff; return { programAddress: COMPUTE_BUDGET_PROGRAM, data, }; } /** * Prepare instructions for transaction submission * * This utility handles the common workflow of: * 1. Normalizing single instruction to array * 2. Optionally prepending compute budget instruction * 3. Returning an InstructionResult that can be used directly with @solana/kit * or converted to web3.js format via .toLegacy() * * @param instructions - Single instruction or array of instructions * @param options - Optional configuration for compute budget * @returns InstructionResult (extends Instruction[], has .toLegacy() method) * * @example * ```typescript * // Kit users - use result directly as Instruction[] * const ixs = prepareInstructions(acceptRentalIx); * // ixs works with appendTransactionMessageInstruction() * * // Add compute budget * const ixs = prepareInstructions(acceptRentalIx, { computeUnits: 400_000 }); * * // Web3.js users - call .toLegacy() * import { PublicKey } from '@solana/web3.js'; * const legacyIxs = prepareInstructions(acceptRentalIx).toLegacy(PublicKey); * ``` */ export function prepareInstructions(instructions, options) { // Normalize to array const ixArray = Array.isArray(instructions) ? instructions : [instructions]; // Optionally prepend compute budget instruction let finalIxs = ixArray; if (options?.computeUnits) { const computeBudgetIx = createComputeBudgetInstruction(options.computeUnits); finalIxs = [computeBudgetIx, ...ixArray]; } return new InstructionResult(finalIxs); } //# sourceMappingURL=instructions.js.map