@codama/dynamic-instructions 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +97 -0
  3. package/dist/index.browser.cjs +752 -0
  4. package/dist/index.browser.cjs.map +1 -0
  5. package/dist/index.browser.mjs +741 -0
  6. package/dist/index.browser.mjs.map +1 -0
  7. package/dist/index.node.cjs +752 -0
  8. package/dist/index.node.cjs.map +1 -0
  9. package/dist/index.node.mjs +741 -0
  10. package/dist/index.node.mjs.map +1 -0
  11. package/dist/index.react-native.mjs +741 -0
  12. package/dist/index.react-native.mjs.map +1 -0
  13. package/dist/types/accounts/create-account-meta.d.ts +11 -0
  14. package/dist/types/accounts/create-account-meta.d.ts.map +1 -0
  15. package/dist/types/accounts/index.d.ts +3 -0
  16. package/dist/types/accounts/index.d.ts.map +1 -0
  17. package/dist/types/accounts/validate-accounts-input.d.ts +9 -0
  18. package/dist/types/accounts/validate-accounts-input.d.ts.map +1 -0
  19. package/dist/types/arguments/encode-instruction-arguments.d.ts +13 -0
  20. package/dist/types/arguments/encode-instruction-arguments.d.ts.map +1 -0
  21. package/dist/types/arguments/index.d.ts +4 -0
  22. package/dist/types/arguments/index.d.ts.map +1 -0
  23. package/dist/types/arguments/resolve-argument-from-custom-resolvers.d.ts +9 -0
  24. package/dist/types/arguments/resolve-argument-from-custom-resolvers.d.ts.map +1 -0
  25. package/dist/types/arguments/shared.d.ts +4 -0
  26. package/dist/types/arguments/shared.d.ts.map +1 -0
  27. package/dist/types/arguments/validate-arguments-input.d.ts +11 -0
  28. package/dist/types/arguments/validate-arguments-input.d.ts.map +1 -0
  29. package/dist/types/index.d.ts +6 -0
  30. package/dist/types/index.d.ts.map +1 -0
  31. package/dist/types/instructions-builder.d.ts +18 -0
  32. package/dist/types/instructions-builder.d.ts.map +1 -0
  33. package/dist/types/shared/types.d.ts +15 -0
  34. package/dist/types/shared/types.d.ts.map +1 -0
  35. package/dist/types/shared/util.d.ts +14 -0
  36. package/dist/types/shared/util.d.ts.map +1 -0
  37. package/dist/types/validators.d.ts +16 -0
  38. package/dist/types/validators.d.ts.map +1 -0
  39. package/package.json +76 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Codama
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # Codama ➤ Dynamic Instructions
2
+
3
+ [![npm][npm-image]][npm-url]
4
+ [![npm-downloads][npm-downloads-image]][npm-url]
5
+
6
+ [npm-downloads-image]: https://img.shields.io/npm/dm/@codama/dynamic-instructions.svg?style=flat
7
+ [npm-image]: https://img.shields.io/npm/v/@codama/dynamic-instructions.svg?style=flat&label=%40codama%2Fdynamic-instructions
8
+ [npm-url]: https://www.npmjs.com/package/@codama/dynamic-instructions
9
+
10
+ This package provides a runtime Solana instruction builder that dynamically constructs `Instruction` (`@solana/instructions`). It provides instruction arguments encoding and validation, accounts resolution. Powers [`@codama/dynamic-client`](../dynamic-client/README.md) with `InstructionsBuilder`.
11
+
12
+ ## Installation
13
+
14
+ ```sh
15
+ pnpm install @codama/dynamic-instructions
16
+ ```
17
+
18
+ > [!NOTE]
19
+ > This package is **not** included in the main [`codama`](../library) package.
20
+
21
+ ## Types generation
22
+
23
+ > [!NOTE]
24
+ > For now, per-instruction types (`*Args`, `*Accounts`, `*Resolvers`) can be produced via [`@codama/dynamic-client`](../dynamic-client/README.md)'s `generate-client-types` command, which emits a `<idl-name>-idl-types.ts` file. A type generation for this package will be added in a follow-up release.
25
+
26
+ ## Functions
27
+
28
+ ### `createInstructionsBuilder(root, ixNode)`
29
+
30
+ Creates an async instruction builder function for a given `InstructionNode`. The returned function validates inputs, resolves defaults, encodes arguments, and assembles the final `Instruction`.
31
+
32
+ **Untyped:**
33
+
34
+ ```ts
35
+ const build = createInstructionsBuilder(root, ixNode);
36
+ const instruction = await build(args, accounts, signers, resolvers);
37
+ ```
38
+
39
+ **Typed:**
40
+
41
+ > Types are generated via [`generate-client-types`](#types-generation).
42
+
43
+ ```ts
44
+ import type { CreateItemAccounts, CreateItemArgs, CreateItemResolvers } from './generated/<idl-name>-idl-types';
45
+
46
+ const build = createInstructionsBuilder<CreateItemArgs, CreateItemAccounts, [], CreateItemResolvers>(root, ixNode);
47
+ const instruction = await build({ name: 'item' }, { authority }, [], {
48
+ resolveOwner: async (args, accounts) => accounts.authority,
49
+ });
50
+ ```
51
+
52
+ ### `createAccountMeta(root, ixNode, argumentsInput?, accountsInput?, signers?, resolversInput?)`
53
+
54
+ Resolves and builds `AccountMeta[]` for an instruction. Handles PDA derivation, default value resolution, optional accounts, and signer disambiguation.
55
+
56
+ **Untyped:**
57
+
58
+ ```ts
59
+ const accountMetas = await createAccountMeta(root, ixNode, args, accounts, ['owner'], resolvers);
60
+ ```
61
+
62
+ **Typed:**
63
+
64
+ > Types are generated via [`generate-client-types`](#types-generation).
65
+
66
+ ```ts
67
+ import type { CreateItemAccounts, CreateItemArgs, CreateItemResolvers } from './generated/<idl-name>-idl-types';
68
+
69
+ const accountMetas = await createAccountMeta<CreateItemAccounts, CreateItemArgs, CreateItemResolvers>(
70
+ root,
71
+ ixNode,
72
+ { name: 'item' },
73
+ { authority },
74
+ ['owner'],
75
+ { resolveOwner: async (args, accounts) => accounts.authority },
76
+ );
77
+ ```
78
+
79
+ ### `encodeInstructionArguments(root, ixNode, argumentsInput?)`
80
+
81
+ Encodes instruction arguments into a `ReadonlyUint8Array` buffer according to the Codama schema. Auto-encodes arguments with `defaultValueStrategy: 'omitted'` (e.g. discriminators).
82
+
83
+ **Untyped:**
84
+
85
+ ```ts
86
+ const data = encodeInstructionArguments(root, ixNode, { amount: 1_000_000_000 });
87
+ ```
88
+
89
+ **Typed:**
90
+
91
+ > Types are generated via [`generate-client-types`](#types-generation).
92
+
93
+ ```ts
94
+ import type { TransferArgs } from './generated/<idl-name>-idl-types';
95
+
96
+ const data = encodeInstructionArguments<TransferArgs>(root, ixNode, { amount: 1_000_000_000n });
97
+ ```