Skip to content

Commit f089c1e

Browse files
authored
Merge pull request #77 from solana-developers/update-idl-functions
Support legacy IDL + Versioned Transactions
2 parents 2e835b2 + c36a7ba commit f089c1e

File tree

10 files changed

+1786
-540
lines changed

10 files changed

+1786
-540
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 2.8
2+
3+
- Added `sendVersionedTransaction()` to send a versioned transaction with lookup tables. Also adds priority fee support.
4+
- Added `createLookupTable()` to easily create a lookup table and extend it with additional addresses
5+
- Added `getIdlByProgramId()` to fetch an IDL from a program on-chain
6+
- Added `getIdlByPath()` to parse an IDL from a local file path
7+
- Added `getIdlParsedAccountData()` to parse account data using an IDL
8+
- Added `parseAnchorTransactionEvents()` to parse anchor transaction events using an IDL
9+
- Added `decodeAnchorTransaction()` to decode a transaction completely using an IDL
10+
- Fixed account data parsing in `decodeAnchorTransaction()`
11+
112
## 2.7
213

314
- Added `sendTransaction()` to send transactions with compute unit optimization and automatic retries.

README.md

Lines changed: 115 additions & 25 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
@@ -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

571666
Usage:
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
588684
Usage:
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
608701
Usage:
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
618708
console.log(decoded.toString());

package-lock.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@
6060
},
6161
"license": "MIT",
6262
"dependencies": {
63+
"@coral-xyz/anchor": "^0.30.1",
6364
"@solana/spl-token": "^0.4.8",
6465
"@solana/spl-token-metadata": "^0.1.4",
6566
"@solana/web3.js": "^1.98.0",
67+
"bn.js": "^5.2.1",
6668
"bs58": "^6.0.0",
67-
"dotenv": "^16.4.5",
68-
"@coral-xyz/anchor": "^0.30.1"
69+
"dotenv": "^16.4.5"
6970
},
7071
"devDependencies": {
7172
"@types/bn.js": "^5.1.6",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export * from './lib/explorer';
44
export * from './lib/airdrop';
55
export * from './lib/token';
66
export * from './lib/logs';
7+
export * from './lib/idl';
78
export * from './types';

0 commit comments

Comments
 (0)