Skip to content

Commit c36a7ba

Browse files
committed
Add addComputeInstructions to readme
1 parent 3d85c60 commit c36a7ba

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,33 @@ const units = await getSimulationComputeUnits(
327327

328328
You can then use `ComputeBudgetProgram.setComputeUnitLimit({ units })` as the first instruction in your transaction. See [How to Request Optimal Compute Budget](https://solana.com/developers/guides/advanced/how-to-request-optimal-compute) for more information on compute units.
329329

330+
### `addComputeInstructions`
331+
332+
Adds compute unit instructions for a transaction if they don't already exist:
333+
334+
```typescript
335+
const updatedInstructions = await addComputeInstructions(
336+
connection,
337+
instructions,
338+
lookupTables,
339+
payer.publicKey,
340+
10000, // priority fee default 10000 microLamports
341+
{ multiplier: 1.1 }, // compute unit buffer default adds 10%
342+
);
343+
344+
// Returns instructions array with:
345+
// 1. setComputeUnitPrice instruction (if not present)
346+
// 2. setComputeUnitLimit instruction based on simulation (if not present)
347+
// The limit is calculated by simulating the transaction and adding the specified buffer
348+
```
349+
350+
This function:
351+
352+
1. Adds priority fee instruction if not present
353+
2. Simulates transaction to determine required compute units
354+
3. Adds compute unit limit instruction with buffer
355+
4. Returns the updated instructions array
356+
330357
## node.js specific helpers
331358

332359
### Get a keypair from a keypair file

src/lib/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ export async function sendVersionedTransaction(
353353
}
354354

355355
/**
356-
* Adds compute unit price and limit instructions to the transaction
356+
* Adds compute unit price and limit instructions and returns the updated instructions
357357
*
358358
* @param connection - The Solana connection object
359359
* @param instructions - Array of instructions to which compute unit instructions will be added

0 commit comments

Comments
 (0)