|
| 1 | +# `AccountNode` |
| 2 | + |
| 3 | +This node defines an on-chain account. It is characterized by its name, data structure, and optional attributes such as PDA definition and account discriminators. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Attributes |
| 8 | + |
| 9 | +### Data |
| 10 | + |
| 11 | +| Attribute | Type | Description | |
| 12 | +| --------- | ----------------- | ----------------------------------------------------------------------------------- | |
| 13 | +| `kind` | `"accountNode"` | The node discriminator. | |
| 14 | +| `name` | `CamelCaseString` | The name of the account. | |
| 15 | +| `docs` | `string[]` | Markdown documentation for the account. | |
| 16 | +| `size` | `number` | (Optional) The size of the account in bytes, if the account's data length is fixed. | |
| 17 | + |
| 18 | +### Children |
| 19 | + |
| 20 | +| Attribute | Type | Description | |
| 21 | +| ---------------- | ---------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | |
| 22 | +| `data` | [`NestedTypeNode`](./typeNodes/NestedTypeNode.md)<[`StructTypeNode`](./typeNodes/StructTypeNode.md)> | The type node that describes the account's data. Note that it must be a struct so we can access its fields via other nodes. | |
| 23 | +| `pda` | [`PdaLinkNode`](./linkNodes/PdaLinkNode.md) | (Optional) The link node that describes the account's PDA, if its address is derived from one. | |
| 24 | +| `discriminators` | [`DiscriminatorNode`](./discriminatorNodes/README.md)[] | (Optional) The nodes that distinguish this account from others in the program. If multiple discriminators are provided, they are combined using a logical AND operation. | |
| 25 | + |
| 26 | +## Functions |
| 27 | + |
| 28 | +### `accountNode(input)` |
| 29 | + |
| 30 | +Helper function that creates a `AccountNode` object from an input object. |
| 31 | + |
| 32 | +```ts |
| 33 | +const node = accountNode({ |
| 34 | + name: 'myCounter', |
| 35 | + data: structTypeNode([ |
| 36 | + structFieldTypeNode({ name: 'authority', type: publicKeyTypeNode() }), |
| 37 | + structFieldTypeNode({ name: 'value', type: numberTypeNode('u64') }), |
| 38 | + ]), |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +## Examples |
| 43 | + |
| 44 | +### A fixed-size account |
| 45 | + |
| 46 | +```ts |
| 47 | +const node = accountNode({ |
| 48 | + name: 'token', |
| 49 | + data: structTypeNode([ |
| 50 | + structFieldTypeNode({ name: 'mint', type: publicKeyTypeNode() }), |
| 51 | + structFieldTypeNode({ name: 'owner', type: publicKeyTypeNode() }), |
| 52 | + structFieldTypeNode({ name: 'amount', type: numberTypeNode('u64') }), |
| 53 | + ]), |
| 54 | + discriminators: [sizeDiscriminatorNode(72)], |
| 55 | + size: 72, |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +### An account with a linked PDA |
| 60 | + |
| 61 | +```ts |
| 62 | +programNode({ |
| 63 | + name: 'myProgram', |
| 64 | + accounts: [ |
| 65 | + accountNode({ |
| 66 | + name: 'token', |
| 67 | + data: structTypeNode([structFieldTypeNode({ name: 'authority', type: publicKeyTypeNode() })]), |
| 68 | + pda: pdaLinkNode('myPda'), |
| 69 | + }), |
| 70 | + ], |
| 71 | + pdas: [ |
| 72 | + pdaNode({ |
| 73 | + name: 'myPda', |
| 74 | + seeds: [ |
| 75 | + constantPdaSeedNodeFromString('utf8', 'token'), |
| 76 | + variablePdaSeedNode('authority', publicKeyTypeNode()), |
| 77 | + ], |
| 78 | + }), |
| 79 | + ], |
| 80 | +}); |
| 81 | +``` |
0 commit comments