diff --git a/clients/js/src/estimateAndSetComputeLimit.ts b/clients/js/src/estimateAndSetComputeLimit.ts index a984c20..2aaf53a 100644 --- a/clients/js/src/estimateAndSetComputeLimit.ts +++ b/clients/js/src/estimateAndSetComputeLimit.ts @@ -4,7 +4,7 @@ import { EstimateComputeUnitLimitFactoryFunction, EstimateComputeUnitLimitFactoryFunctionConfig, } from './estimateComputeLimitInternal'; -import { getSetComputeUnitLimitInstructionIndexAndUnits } from './internal'; +import { findSetComputeUnitLimitInstructionIndexAndUnits } from './introspect'; import { updateOrAppendSetComputeUnitLimitInstruction } from './setComputeLimit'; type EstimateAndUpdateProvisoryComputeUnitLimitFactoryFunction = < @@ -36,7 +36,7 @@ export function estimateAndUpdateProvisoryComputeUnitLimitFactory( estimateComputeUnitLimit: EstimateComputeUnitLimitFactoryFunction, ): EstimateAndUpdateProvisoryComputeUnitLimitFactoryFunction { return async function fn(transactionMessage, config) { - const instructionDetails = getSetComputeUnitLimitInstructionIndexAndUnits(transactionMessage); + const instructionDetails = findSetComputeUnitLimitInstructionIndexAndUnits(transactionMessage); // If the transaction message already has a compute unit limit instruction // which is set to a specific value — i.e. not 0 or the maximum limit — diff --git a/clients/js/src/index.ts b/clients/js/src/index.ts index ae5658d..9d8d894 100644 --- a/clients/js/src/index.ts +++ b/clients/js/src/index.ts @@ -3,5 +3,6 @@ export * from './generated'; export * from './constants'; export * from './estimateAndSetComputeLimit'; export * from './estimateComputeLimit'; +export * from './introspect'; export * from './setComputeLimit'; export * from './setComputePrice'; diff --git a/clients/js/src/internal.ts b/clients/js/src/introspect.ts similarity index 66% rename from clients/js/src/internal.ts rename to clients/js/src/introspect.ts index 49e00e7..952d77e 100644 --- a/clients/js/src/internal.ts +++ b/clients/js/src/introspect.ts @@ -18,10 +18,10 @@ import { * Finds the index of the first `SetComputeUnitLimit` instruction in a transaction message * and its set limit, if any. */ -export function getSetComputeUnitLimitInstructionIndexAndUnits( +export function findSetComputeUnitLimitInstructionIndexAndUnits( transactionMessage: TransactionMessage, ): { index: number; units: number } | null { - const index = getSetComputeUnitLimitInstructionIndex(transactionMessage); + const index = transactionMessage.instructions.findIndex(isSetComputeUnitLimitInstruction); if (index < 0) { return null; } @@ -31,13 +31,6 @@ export function getSetComputeUnitLimitInstructionIndexAndUnits( return { index, units }; } -/** - * Finds the index of the first `SetComputeUnitLimit` instruction in a transaction message, if any. - */ -export function getSetComputeUnitLimitInstructionIndex(transactionMessage: TransactionMessage) { - return transactionMessage.instructions.findIndex(isSetComputeUnitLimitInstruction); -} - /** * Checks if the given instruction is a `SetComputeUnitLimit` instruction. */ @@ -55,10 +48,10 @@ export function isSetComputeUnitLimitInstruction( * Finds the index of the first `SetComputeUnitPrice` instruction in a transaction message * and its set micro-lamports, if any. */ -export function getSetComputeUnitPriceInstructionIndexAndMicroLamports( +export function findSetComputeUnitPriceInstructionIndexAndMicroLamports( transactionMessage: TransactionMessage, ): { index: number; microLamports: MicroLamports } | null { - const index = getSetComputeUnitPriceInstructionIndex(transactionMessage); + const index = transactionMessage.instructions.findIndex(isSetComputeUnitPriceInstruction); if (index < 0) { return null; } @@ -71,19 +64,10 @@ export function getSetComputeUnitPriceInstructionIndexAndMicroLamports( return { index, microLamports }; } -/** - * Finds the index of the first `SetComputeUnitPrice` instruction in a transaction message, if any. - */ -export function getSetComputeUnitPriceInstructionIndex(transactionMessage: TransactionMessage) { - return transactionMessage.instructions.findIndex(isSetComputeUnitPriceInstruction); -} - /** * Checks if the given instruction is a `SetComputeUnitPrice` instruction. */ -export function isSetComputeUnitPriceInstruction( - instruction: Instruction, -): instruction is SetComputeUnitPriceInstruction { +function isSetComputeUnitPriceInstruction(instruction: Instruction): instruction is SetComputeUnitPriceInstruction { return ( instruction.programAddress === COMPUTE_BUDGET_PROGRAM_ADDRESS && identifyComputeBudgetInstruction(instruction.data as Uint8Array) === diff --git a/clients/js/src/setComputeLimit.ts b/clients/js/src/setComputeLimit.ts index 605e3e7..f88a324 100644 --- a/clients/js/src/setComputeLimit.ts +++ b/clients/js/src/setComputeLimit.ts @@ -1,7 +1,7 @@ import { appendTransactionMessageInstruction, TransactionMessage } from '@solana/kit'; import { PROVISORY_COMPUTE_UNIT_LIMIT } from './constants'; import { getSetComputeUnitLimitInstruction } from './generated'; -import { getSetComputeUnitLimitInstructionIndexAndUnits } from './internal'; +import { findSetComputeUnitLimitInstructionIndexAndUnits } from './introspect'; /** * Appends a `SetComputeUnitLimit` instruction with a provisory @@ -50,7 +50,7 @@ export function updateOrAppendSetComputeUnitLimitInstruction typeof units === 'function' ? units(previousUnits) : units; - const instructionDetails = getSetComputeUnitLimitInstructionIndexAndUnits(transactionMessage); + const instructionDetails = findSetComputeUnitLimitInstructionIndexAndUnits(transactionMessage); if (!instructionDetails) { return appendTransactionMessageInstruction( diff --git a/clients/js/src/setComputePrice.ts b/clients/js/src/setComputePrice.ts index c81aefb..24f8ca5 100644 --- a/clients/js/src/setComputePrice.ts +++ b/clients/js/src/setComputePrice.ts @@ -1,6 +1,6 @@ import { appendTransactionMessageInstruction, TransactionMessage, MicroLamports } from '@solana/kit'; import { getSetComputeUnitPriceInstruction } from './generated'; -import { getSetComputeUnitPriceInstructionIndexAndMicroLamports } from './internal'; +import { findSetComputeUnitPriceInstructionIndexAndMicroLamports } from './introspect'; /** * Sets the compute unit price of a transaction message in micro-Lamports. @@ -48,7 +48,7 @@ export function updateOrAppendSetComputeUnitPriceInstruction typeof microLamports === 'function' ? microLamports(previousMicroLamports) : microLamports; - const instructionDetails = getSetComputeUnitPriceInstructionIndexAndMicroLamports(transactionMessage); + const instructionDetails = findSetComputeUnitPriceInstructionIndexAndMicroLamports(transactionMessage); if (!instructionDetails) { return appendTransactionMessageInstruction(