@solana-developers/helpers
Version:
Solana helper functions
158 lines • 7.1 kB
TypeScript
import { AddressLookupTableAccount, Commitment, Connection, Keypair, PublicKey, SignatureStatus, Transaction, TransactionInstruction } from "@solana/web3.js";
export declare const confirmTransaction: (connection: Connection, signature: string, commitment?: Commitment) => Promise<string>;
/**
* Check if a given transaction contains a SetComputeUnitLimit instruction
*/
export declare function hasSetComputeLimitInstruction(instructions: Array<TransactionInstruction>): boolean;
/**
* Check if a given transaction contains a SetComputeUnitLimit instruction
*/
export declare function hasSetComputeUnitPriceInstruction(instructions: Array<TransactionInstruction>): boolean;
export declare const getSimulationComputeUnits: (connection: Connection, instructions: Array<TransactionInstruction>, payer: PublicKey, lookupTables: Array<AddressLookupTableAccount> | [], commitment?: Commitment) => Promise<number | null>;
/**
* Constants for transaction retry configuration
*/
export declare const RETRY_INTERVAL_MS = 2000;
export declare const RETRY_INTERVAL_INCREASE = 200;
export declare const MAX_RETRIES = 15;
/**
* Represents the different states of a transaction during its lifecycle
* @property status - The current status of the transaction
* @property signature - The transaction signature (only present when status is "sent")
* @property result - The signature status (only present when status is "confirmed")
*/
export type TxStatusUpdate = {
status: "created";
} | {
status: "signed";
} | {
status: "sent";
signature: string;
} | {
status: "retry";
signature: string | null;
} | {
status: "confirmed";
result: SignatureStatus;
};
/**
* Configuration options for transaction retry mechanism
* @property maxRetries - Maximum number of retry attempts
* @property initialDelayMs - Delay between retries in milliseconds
* @property commitment - Desired commitment level for the transaction
* @property skipPreflight - Whether to skip transaction simulation
* @property onStatusUpdate - Callback function to receive transaction status updates
*/
export type SendTransactionOptions = Partial<{
maxRetries: number;
initialDelayMs: number;
commitment: Commitment;
onStatusUpdate: (status: TxStatusUpdate) => void;
skipPreflight: boolean;
}>;
/**
* Configuration for compute unit buffer calculation
* @property multiplier - Multiply simulated units by this value (e.g., 1.1 adds 10%)
* @property fixed - Add this fixed amount of compute units
*/
export type ComputeUnitBuffer = {
multiplier?: number;
fixed?: number;
};
/**
* Default configuration values for transaction sending
*/
export declare const DEFAULT_SEND_OPTIONS: Required<Omit<SendTransactionOptions, "onStatusUpdate">>;
/**
* Sends a transaction with compute unit optimization and automatic retries
*
* @param connection - The Solana connection object
* @param transaction - The transaction to send
* @param signers - Array of signers needed for the transaction
* @param priorityFee - Priority fee in microLamports (default: 10000)
* @param options - Optional configuration for retry mechanism and compute units
* @returns Promise that resolves to the transaction signature
*
* @example
* ```typescript
* const signature = await sendTransaction(
* connection,
* transaction,
* [payer],
* 10000,
* {
* computeUnitBuffer: { multiplier: 1.1 },
* onStatusUpdate: (status) => console.log(status),
* }
* );
* ```
*/
export declare function sendTransaction(connection: Connection, transaction: Transaction, signers: Keypair[], priorityFee?: number, options?: SendTransactionOptions & {
computeUnitBuffer?: ComputeUnitBuffer;
}): Promise<string>;
/**
* Sends a versioned transaction with compute unit optimization and automatic retries
*
* @param connection - The Solana connection object
* @param instructions - Array of instructions to include in the transaction
* @param signers - Array of signers needed for the transaction
* @param priorityFee - Priority fee in microLamports (default: 10000)
* @param lookupTables - Optional array of address lookup tables for account compression
* @param options - Optional configuration for retry mechanism and compute units
* @returns Promise that resolves to the transaction signature
*
* @remarks
* This function:
* 1. Automatically calculates and adds compute unit instructions if not present
* 2. Creates a v0 transaction message with the provided instructions
* 3. Signs and sends the transaction with automatic retries
* 4. Provides status updates through the callback
*
* Status updates include:
* - "computeUnitBufferAdded": Compute unit instructions were added
* - "created": Transaction was created
* - "signed": Transaction was signed
* - "sent": Transaction was sent (includes signature)
* - "confirmed": Transaction was confirmed
*
* @example
* ```typescript
* const signature = await sendVersionedTransaction(
* connection,
* instructions,
* [payer],
* 10000,
* lookupTables,
* {
* computeUnitBuffer: { multiplier: 1.1 },
* onStatusUpdate: (status) => console.log(status),
* }
* );
* ```
*/
export declare function sendVersionedTransaction(connection: Connection, instructions: Array<TransactionInstruction>, signers: Keypair[], priorityFee?: number, lookupTables?: Array<AddressLookupTableAccount>, options?: SendTransactionOptions & {
computeUnitBuffer?: ComputeUnitBuffer;
}): Promise<string>;
/**
* Adds compute unit price and limit instructions and returns the updated instructions
*
* @param connection - The Solana connection object
* @param instructions - Array of instructions to which compute unit instructions will be added
* @param lookupTables - Optional array of address lookup tables for account compression
* @param payer - The public key of the transaction payer
* @param priorityFee - Priority fee in microLamports (default: 10000)
* @param computeUnitBuffer - Optional buffer to add to simulated compute units
* @param commitment - Desired commitment level for the transaction
* @returns Array of instructions with compute unit instructions added
*/
export declare function addComputeInstructions(connection: Connection, instructions: Array<TransactionInstruction>, lookupTables: Array<AddressLookupTableAccount>, payer: PublicKey, priorityFee?: number, computeUnitBuffer?: ComputeUnitBuffer, commitment?: Commitment): Promise<Array<TransactionInstruction>>;
/**
* Creates a new address lookup table and extends it with additional addresses
*
* @param connection - The Solana connection object
* @param sender - The keypair of the transaction sender
* @param additionalAddresses - Array of additional addresses to include in the lookup table
* @returns A tuple containing the lookup table address and the lookup table account
*/
export declare function createLookupTable(connection: Connection, sender: Keypair, additionalAddresses: PublicKey[], priorityFee?: number): Promise<[PublicKey, AddressLookupTableAccount]>;
//# sourceMappingURL=transaction.d.ts.map