Helpers to dynamically identify and parse accounts and instructions
npm install @codama/dynamic-parsers[![npm][npm-image]][npm-url]
[![npm-downloads][npm-downloads-image]][npm-url]
[npm-downloads-image]: https://img.shields.io/npm/dm/@codama/dynamic-parsers.svg?style=flat
[npm-image]: https://img.shields.io/npm/v/@codama/dynamic-parsers.svg?style=flat&label=%40codama%2Fdynamic-parsers
[npm-url]: https://www.npmjs.com/package/@codama/dynamic-parsers
This package provides a set of helpers that, given any Codama IDL, dynamically identifies and parses any byte array into deserialized accounts and instructions.
``sh`
pnpm install @codama/dynamic-parsers
> [!NOTE]
> This package is not included in the main codama package.
This type represents the result of identifying and parsing a byte array from a given root node. It provides us with the full NodePath of the identified node, as well as the data deserialized from the provided bytes.
`ts`
type ParsedData
data: unknown;
path: NodePath
};
Given a RootNode and a byte array, this function will attempt to identify the correct account node and use it to deserialize the provided bytes. Therefore, it returns a ParsedData object if the parsing was successful, or undefined otherwise.
`ts
const parsedData = parseAccountData(rootNode, bytes);
// ^ ParsedData
if (parsedData) {
const accountNode: AccountNode = getLastNodeFromPath(parsedData.path);
const decodedData: unknown = parsedData.data;
}
`
Similarly to parseAccountData, this function will match the provided bytes to an instruction node and deserialize them accordingly. It returns a ParsedData object if the parsing was successful, or undefined otherwise.
`ts
const parsedData = parseInstructionData(rootNode, bytes);
// ^ ParsedData
if (parsedData) {
const instructionNode: InstructionNode = getLastNodeFromPath(parsedData.path);
const decodedData: unknown = parsedData.data;
}
`
This function accepts a RootNode and an Instruction type — as defined in @solana/instructions — in order to return a ParsedData object that also includes an accounts array that match each AccountMeta with its corresponding account name.
`ts
const parsedData = parseInstruction(rootNode, instruction);
if (parsedData) {
const namedAccounts = parsedData.accounts;
// ^ Array
}
`
This function tries to match the provided bytes to an account node, returning a NodePath object if the identification was successful, or undefined otherwise. It is used by the parseAccountData function under the hood.
`ts
const path = identifyAccountData(root, bytes);
// ^ NodePath
if (path) {
const accountNode: AccountNode = getLastNodeFromPath(path);
}
`
This function tries to match the provided bytes to an instruction node, returning a NodePath object if the identification was successful, or undefined otherwise. It is used by the parseInstructionData function under the hood.
`ts
const path = identifyInstructionData(root, bytes);
// ^ NodePath
if (path) {
const instructionNode: InstructionNode = getLastNodeFromPath(path);
}
``