Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clients/js/src/estimateAndSetComputeLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
EstimateComputeUnitLimitFactoryFunction,
EstimateComputeUnitLimitFactoryFunctionConfig,
} from './estimateComputeLimitInternal';
import { getSetComputeUnitLimitInstructionIndexAndUnits } from './internal';
import { findSetComputeUnitLimitInstructionIndexAndUnits } from './introspect';
import { updateOrAppendSetComputeUnitLimitInstruction } from './setComputeLimit';

type EstimateAndUpdateProvisoryComputeUnitLimitFactoryFunction = <
Expand Down Expand Up @@ -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 —
Expand Down
1 change: 1 addition & 0 deletions clients/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
26 changes: 5 additions & 21 deletions clients/js/src/internal.ts → clients/js/src/introspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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.
*/
Expand All @@ -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;
}
Expand All @@ -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) ===
Expand Down
4 changes: 2 additions & 2 deletions clients/js/src/setComputeLimit.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -50,7 +50,7 @@ export function updateOrAppendSetComputeUnitLimitInstruction<TTransactionMessage
): TTransactionMessage {
const getUnits = (previousUnits: number | null): number =>
typeof units === 'function' ? units(previousUnits) : units;
const instructionDetails = getSetComputeUnitLimitInstructionIndexAndUnits(transactionMessage);
const instructionDetails = findSetComputeUnitLimitInstructionIndexAndUnits(transactionMessage);

if (!instructionDetails) {
return appendTransactionMessageInstruction(
Expand Down
4 changes: 2 additions & 2 deletions clients/js/src/setComputePrice.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -48,7 +48,7 @@ export function updateOrAppendSetComputeUnitPriceInstruction<TTransactionMessage
): TTransactionMessage {
const getMicroLamports = (previousMicroLamports: MicroLamports | null): MicroLamports =>
typeof microLamports === 'function' ? microLamports(previousMicroLamports) : microLamports;
const instructionDetails = getSetComputeUnitPriceInstructionIndexAndMicroLamports(transactionMessage);
const instructionDetails = findSetComputeUnitPriceInstructionIndexAndMicroLamports(transactionMessage);

if (!instructionDetails) {
return appendTransactionMessageInstruction(
Expand Down