Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Solana development skill covering @solana/kit v5, Anchor programs, LiteSVM testing, and security patterns.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/kit/programs/compute-budget.md
1---2title: Compute Budget Program3description: Kit-compatible @solana-program/compute-budget client for CU limits, priority fees, heap frames, CU estimation, and retry strategies.4---56# Compute Budget Program78Program address: `ComputeBudget111111111111111111111111111111`910```ts11import { COMPUTE_BUDGET_PROGRAM_ADDRESS } from '@solana-program/compute-budget';12```1314Correctly budgeting compute units for your transaction increases the probability it gets accepted for processing. Without a declared CU limit, validators assume 200K CU per instruction. Since validators pack blocks to maximize throughput, they prefer transactions with tight budgets that clearly fit in remaining block space. Pairing a tight CU limit with a priority fee gives validators direct incentive to include your transaction. Total fee = base fee (5,000 lamports/sig) + (CU consumed × price per CU in micro-lamports).1516## Instructions1718### Set Compute Unit Limit1920If you're using a Kit client built with the `solanaRpc` / `solanaLocalRpc` / `solanaDevnetRpc` / `solanaMainnetRpc` plugins (from `@solana/kit-plugin-rpc`), CU estimation is handled automatically via `client.sendTransaction()` — you don't need this. Use the manual instructions below when building transactions with `pipe()` or when you need direct control. See [overview.md](../overview.md) and [plugins.md](../plugins.md).2122Always set based on simulation — overestimate wastes block space, underestimate fails the transaction.2324```ts25import { getSetComputeUnitLimitInstruction } from '@solana-program/compute-budget';2627const ix = getSetComputeUnitLimitInstruction({ units: 200_000 });28```2930### Set Compute Unit Price (Priority Fee)3132Price per CU in micro-lamports — higher values improve inclusion during congestion.3334```ts35import { getSetComputeUnitPriceInstruction } from '@solana-program/compute-budget';3637const ix = getSetComputeUnitPriceInstruction({ microLamports: 1000n });38```3940### Request Heap Frame4142Increases BPF heap beyond the default 32 KB — only needed when programs allocate large data structures (large account deserialization, Merkle trees, ZK proofs). Most transactions don't need this.4344```ts45import { getRequestHeapFrameInstruction } from '@solana-program/compute-budget';4647const ix = getRequestHeapFrameInstruction({ bytes: 256 * 1024 }); // 256 KB48```4950## CU Estimation Helpers5152Simulate before sending to set a tight CU limit and avoid overpaying on priority fees.5354### Basic Estimator5556```ts57import { estimateComputeUnitLimitFactory } from '@solana-program/compute-budget';5859const estimateCU = estimateComputeUnitLimitFactory({ rpc });60const estimatedUnits = await estimateCU(transactionMessage);61```6263### Auto-Update Estimator6465Estimates CU and updates the transaction message automatically:6667```ts68import {69estimateComputeUnitLimitFactory,70estimateAndUpdateProvisoryComputeUnitLimitFactory,71} from '@solana-program/compute-budget';7273const estimateAndUpdateCU = estimateAndUpdateProvisoryComputeUnitLimitFactory(74estimateComputeUnitLimitFactory({ rpc })75);7677// Returns message with CU limit instruction added/updated78const updatedMessage = await estimateAndUpdateCU(transactionMessage);79```8081## Transaction Helpers8283### Update or Append Instructions8485```ts86import {87updateOrAppendSetComputeUnitLimitInstruction,88updateOrAppendSetComputeUnitPriceInstruction,89} from '@solana-program/compute-budget';9091// Update CU limit (or add if not present)92const msg1 = updateOrAppendSetComputeUnitLimitInstruction(93(current) => current === null ? 200_000 : current,94transactionMessage95);9697// Update priority fee dynamically98const msg2 = updateOrAppendSetComputeUnitPriceInstruction(99(current) => current === null ? 1000n : current * 2n, // Double on retry100transactionMessage101);102```103104## Full Pattern: Build, Estimate, Send105106Build with priority fee → estimate CU via simulation → refresh blockhash (simulation consumed time) → sign and send.107108```ts109import {110pipe, createTransactionMessage, setTransactionMessageFeePayerSigner,111setTransactionMessageLifetimeUsingBlockhash, appendTransactionMessageInstruction,112prependTransactionMessageInstruction, signTransactionMessageWithSigners,113sendAndConfirmTransactionFactory, assertIsTransactionWithBlockhashLifetime,114} from '@solana/kit';115import {116getSetComputeUnitPriceInstruction,117estimateComputeUnitLimitFactory,118estimateAndUpdateProvisoryComputeUnitLimitFactory,119} from '@solana-program/compute-budget';120121async function sendWithComputeBudget(rpc, rpcSubscriptions, signer, instruction) {122// Setup CU estimator123const estimateAndUpdateCU = estimateAndUpdateProvisoryComputeUnitLimitFactory(124estimateComputeUnitLimitFactory({ rpc })125);126127// 1. Build base message with priority fee128const { value: simBlockhash } = await rpc.getLatestBlockhash().send();129130let message = pipe(131createTransactionMessage({ version: 0 }),132m => setTransactionMessageFeePayerSigner(signer, m),133m => setTransactionMessageLifetimeUsingBlockhash(simBlockhash, m),134m => appendTransactionMessageInstruction(instruction, m),135m => prependTransactionMessageInstruction(136getSetComputeUnitPriceInstruction({ microLamports: 1000n }),137m138),139);140141// 2. Estimate CU via simulation (adds/updates CU limit instruction)142message = await estimateAndUpdateCU(message);143144// 3. IMPORTANT: Refresh blockhash after estimation145const { value: freshBlockhash } = await rpc.getLatestBlockhash().send();146message = setTransactionMessageLifetimeUsingBlockhash(freshBlockhash, message);147148// 4. Sign and send149const sendAndConfirm = sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions });150const signed = await signTransactionMessageWithSigners(message);151assertIsTransactionWithBlockhashLifetime(signed);152return sendAndConfirm(signed, { commitment: 'confirmed' });153}154```155156## Priority Fee Estimation157158Don't hardcode priority fees — use your RPC provider's fee estimation API to set competitive rates for current network conditions:159160- [Helius Priority Fee API](https://docs.helius.dev/solana-apis/priority-fee-api)161- [QuickNode Priority Fee Add-on](https://marketplace.quicknode.com/add-on/solana-priority-fee)162- [Triton Priority Fees API](https://docs.triton.one/chains/solana/improved-priority-fees-api)163