Skip to content

Commit

Permalink
anchor parsing helpers (#70)
Browse files Browse the repository at this point in the history
* Add functions to parse anchor account and transaction
* Add nice types for DecodedTransactions
* Fix type errors
* Add anchor helpers to readme
* Format readme for better overview
* Update README.md
* Use correct BN reference
* Formatting
* Remove duplicate error check
  • Loading branch information
Woody4618 authored Jan 16, 2025
1 parent 20bc17f commit e7654af
Show file tree
Hide file tree
Showing 4 changed files with 532 additions and 10 deletions.
97 changes: 88 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,42 @@ The `helpers` package contains Solana helper functions, for use in the browser a

## What can I do with this module?

Account & Keypair Management:
[Make multiple keypairs at once](#make-multiple-keypairs-at-once)

[Make a token mint with metadata](#make-a-token-mint-with-metadata)
[Get a keypair from a keypair file](#get-a-keypair-from-a-keypair-file)

[Create multiple accounts with balances of different tokens in a single step](#create-users-mints-and-token-accounts-in-a-single-step)
[Get a keypair from an environment variable](#get-a-keypair-from-an-environment-variable)

[Resolve a custom error message](#resolve-a-custom-error-message)
[Add a new keypair to an env file](#add-a-new-keypair-to-an-env-file)

[Get an airdrop if your balance is below some amount](#get-an-airdrop-if-your-balance-is-below-some-amount)
[Load or create a keypair and airdrop to it if needed](#load-or-create-a-keypair-and-airdrop-to-it-if-needed)

[Get a Solana Explorer link for a transaction, address, or block](#get-a-solana-explorer-link-for-a-transaction-address-or-block)
Token Operations:
[Make a token mint with metadata](#make-a-token-mint-with-metadata)

[Create multiple accounts with balances of different tokens in a single step](#create-users-mints-and-token-accounts-in-a-single-step)

Transaction & Compute Management:
[Confirm a transaction](#confirm-a-transaction)

[Get the logs for a transaction](#get-the-logs-for-a-transaction)

[Get simulated compute units (CUs) for transaction instructions](#get-simulated-compute-units-cus-for-transaction-instructions)

[Get a keypair from a keypair file](#get-a-keypair-from-a-keypair-file)
[Get an airdrop if your balance is below some amount](#get-an-airdrop-if-your-balance-is-below-some-amount)

[Get a keypair from an environment variable](#get-a-keypair-from-an-environment-variable)
Error Handling & Utilities:
[Resolve a custom error message](#resolve-a-custom-error-message)

[Add a new keypair to an env file](#add-a-new-keypair-to-an-env-file)
[Get a Solana Explorer link for a transaction, address, or block](#get-a-solana-explorer-link-for-a-transaction-address-or-block)

[Load or create a keypair and airdrop to it if needed](#load-or-create-a-keypair-and-airdrop-to-it-if-needed)
Anchor Program Interaction:
[Parse account data with IDL](#parse-account-data-with-idl)

[Parse transaction events](#parse-transaction-events)

[Decode Anchor transaction](#decode-anchor-transaction)

## Installation

Expand Down Expand Up @@ -556,3 +567,71 @@ await prepareTransactionWithCompute(
```

Both functions help with common transaction handling tasks in Solana, making it easier to send reliable transactions with appropriate compute unit settings.

## Anchor IDL Utilities

### Parse Account Data with IDL

Usage:

```typescript
const accountData = await getIdlParsedAccountData(
"./idl/program.json",
"counter",
accountAddress,
connection,
);

// Decoded Data: { count: <BN: 2> }
```

Fetches and parses an account's data using an Anchor IDL file. This is useful when you need to decode account data from Anchor programs.

### Parse Transaction Events

Usage:

```typescript
const events = await parseAnchorTransactionEvents(
"./idl/program.json",
signature,
connection,
);

// Events will be an array of:
// {
// name: "GameCreated",
// data: { gameId: "123", player: "..." }
// }
```

Parses all Anchor events emitted in a transaction. This helps you track and verify program events after transaction execution.

### Decode Anchor Transaction

Usage:

```typescript
const decoded = await decodeAnchorTransaction(
"./idl/program.json",
signature,
connection,
);

// Print human-readable format
console.log(decoded.toString());

// Access specific instruction data
decoded.instructions.forEach((ix) => {
console.log(`Instruction: ${ix.name}`);
console.log(`Arguments: ${JSON.stringify(ix.data)}`);
console.log(`Accounts: ${ix.accounts.map((acc) => acc.name).join(", ")}`);
});
```

Provides detailed decoding of all Anchor instructions in a transaction, including:

- Instruction names and arguments
- All involved accounts with their roles (signer/writable)
- Account data for program-owned accounts
- Human-readable string representation
158 changes: 158 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
"@solana/spl-token-metadata": "^0.1.4",
"@solana/web3.js": "^1.98.0",
"bs58": "^6.0.0",
"dotenv": "^16.4.5"
"dotenv": "^16.4.5",
"@coral-xyz/anchor": "^0.30.1"

},
"devDependencies": {
"@types/node": "^20.16.1",
Expand Down
Loading

0 comments on commit e7654af

Please sign in to comment.