@@ -327,6 +327,33 @@ const units = await getSimulationComputeUnits(
327327
328328You 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
@@ -541,38 +568,107 @@ For RPC providers that support priority fees:
541568- Triton: see their [ priority fee API] ( https://docs.triton.one/chains/solana/improved-priority-fees-api )
542569- Quicknode: see their [ priority fee estimation] ( https://www.quicknode.com/docs/solana/qn_estimatePriorityFees )
543570
544- #### ` prepareTransactionWithCompute `
571+ #### ` sendVersionedTransaction `
545572
546- If you need more control, you can prepare compute units separately:
573+ Sends a versioned transaction with compute unit optimization and automatic retries.
547574
548575``` typescript
549- await prepareTransactionWithCompute (
576+ async function sendVersionedTransaction(
577+ connection : Connection ,
578+ instructions : Array <TransactionInstruction >,
579+ signers : Keypair [],
580+ priorityFee : number = 10000 ,
581+ lookupTables ? : Array <AddressLookupTableAccount > | [],
582+ options ? : SendTransactionOptions & {
583+ computeUnitBuffer? : ComputeUnitBuffer ;
584+ },
585+ ): Promise <string >;
586+ ```
587+
588+ Example:
589+
590+ ``` typescript
591+ const signature = await sendVersionedTransaction (
550592 connection ,
551- transaction ,
552- payer .publicKey ,
553- 10000 , // priority fee
593+ instructions ,
594+ [payer ],
595+ 10000 ,
596+ lookupTables ,
554597 {
555- multiplier: 1.1 , // add 10% buffer
556- fixed: 100 , // add fixed amount of CUs
598+ computeUnitBuffer: { multiplier: 1.1 },
599+ onStatusUpdate : ( status ) => console . log ( status ),
557600 },
558601);
559602```
560603
561- This will:
604+ #### ` createLookupTable `
562605
563- 1 . Simulate the transaction to determine required compute units
564- 2 . Add compute budget instructions for both price and unit limit
565- 3 . Apply any specified compute unit buffers
606+ Creates a new address lookup table and extends it with additional addresses.
607+
608+ ``` typescript
609+ async function createLookupTable(
610+ connection : Connection ,
611+ sender : Keypair ,
612+ additionalAddresses : PublicKey [],
613+ priorityFee : number = 10000 ,
614+ ): Promise <[PublicKey , AddressLookupTableAccount ]>;
615+ ```
616+
617+ Example:
618+
619+ ``` typescript
620+ const [lookupTableAddress, lookupTableAccount] = await createLookupTable (
621+ connection ,
622+ payer ,
623+ [account1 .publicKey , account2 .publicKey , account3 .publicKey ],
624+ );
625+ // Can either cache the lookup table address and lookup table account for reuse, or use them directly
626+ const signature = await sendVersionedTransaction (
627+ connection ,
628+ instructions ,
629+ [payer ],
630+ 10000 ,
631+ [lookupTableAccount ],
632+ {
633+ onStatusUpdate : (status ) => console .log (status ),
634+ },
635+ );
636+ ```
637+
638+ These utilities help with:
639+
640+ - Creating and sending versioned transactions
641+ - Managing compute units and priority fees
642+ - Using address lookup tables to fit more accounts in a single transaction
643+ - Automatic transaction retries and status updates
566644
567645## Anchor IDL Utilities
568646
569- ### Parse Account Data with IDL
647+ ### Loading IDLs
648+
649+ Get an IDL from a local file:
650+
651+ ``` typescript
652+ const idl = await getIdlByPath (" ./idl/program.json" );
653+ ```
654+
655+ Or fetch it from the chain:
656+
657+ ``` typescript
658+ const idl = await getIdlByProgramId (
659+ new PublicKey (" verifycLy8mB96wd9wqq3WDXQwM4oU6r42Th37Db9fC" ),
660+ connection ,
661+ );
662+ ```
663+
664+ ### Parse Account Data
570665
571666Usage:
572667
573668``` typescript
574- const accountData = await getIdlParsedAccountData (
575- " ./idl/program.json" ,
669+ const idl = await getIdlByProgramId (programId , connection );
670+ const data = await getIdlParsedAccountData (
671+ idl ,
576672 " counter" ,
577673 accountAddress ,
578674 connection ,
@@ -588,11 +684,8 @@ Fetches and parses an account's data using an Anchor IDL file. This is useful wh
588684Usage:
589685
590686``` typescript
591- const events = await parseAnchorTransactionEvents (
592- " ./idl/program.json" ,
593- signature ,
594- connection ,
595- );
687+ const idl = await getIdlByPath (" ./idl/program.json" );
688+ const events = await parseAnchorTransactionEvents (idl , signature , connection );
596689
597690// Events will be an array of:
598691// {
@@ -608,11 +701,8 @@ Parses all Anchor events emitted in a transaction. This helps you track and veri
608701Usage:
609702
610703``` typescript
611- const decoded = await decodeAnchorTransaction (
612- " ./idl/program.json" ,
613- signature ,
614- connection ,
615- );
704+ const idl = await getIdlByProgramId (programId , connection );
705+ const decoded = await decodeAnchorTransaction (idl , signature , connection );
616706
617707// Print human-readable format
618708console .log (decoded .toString ());
0 commit comments