codama-renderers-dart 0.5.0 → 0.5.3
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.
- package/{readme.md → README.md} +38 -42
- package/dist/index.browser.cjs +628 -147
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +628 -148
- package/dist/index.browser.js.map +1 -1
- package/dist/index.node.cjs +628 -147
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +628 -148
- package/dist/index.node.js.map +1 -1
- package/dist/index.react-native.js +628 -148
- package/dist/index.react-native.js.map +1 -1
- package/dist/types/fragments/accountPage.d.ts.map +1 -1
- package/dist/types/fragments/instructionPage.d.ts.map +1 -1
- package/dist/types/fragments/typePage.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/utils/discriminators.d.ts +10 -0
- package/dist/types/utils/discriminators.d.ts.map +1 -0
- package/dist/types/utils/exactDecoder.d.ts +17 -0
- package/dist/types/utils/exactDecoder.d.ts.map +1 -0
- package/dist/types/utils/formatCode.d.ts +1 -1
- package/dist/types/utils/formatCode.d.ts.map +1 -1
- package/dist/types/utils/index.d.ts +4 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/nameTransformers.d.ts +5 -0
- package/dist/types/utils/nameTransformers.d.ts.map +1 -1
- package/dist/types/utils/normalizeRootNode.d.ts +11 -0
- package/dist/types/utils/normalizeRootNode.d.ts.map +1 -0
- package/dist/types/utils/valueNodes.d.ts +16 -0
- package/dist/types/utils/valueNodes.d.ts.map +1 -0
- package/dist/types/visitors/getTypeManifestVisitor.d.ts +1 -1
- package/dist/types/visitors/getTypeManifestVisitor.d.ts.map +1 -1
- package/dist/types/visitors/renderVisitor.d.ts.map +1 -1
- package/package.json +21 -18
- package/src/fragments/accountPage.ts +90 -17
- package/src/fragments/instructionPage.ts +112 -79
- package/src/fragments/typePage.ts +134 -31
- package/src/index.ts +1 -0
- package/src/utils/discriminators.ts +121 -0
- package/src/utils/exactDecoder.ts +82 -0
- package/src/utils/formatCode.ts +7 -11
- package/src/utils/index.ts +4 -0
- package/src/utils/nameTransformers.ts +28 -1
- package/src/utils/normalizeRootNode.ts +76 -0
- package/src/utils/valueNodes.ts +87 -0
- package/src/visitors/getRenderMapVisitor.ts +1 -1
- package/src/visitors/getTypeManifestVisitor.ts +37 -14
- package/src/visitors/renderVisitor.ts +4 -1
package/{readme.md → README.md}
RENAMED
|
@@ -6,23 +6,21 @@ A [Codama](https://github.com/codama-idl/codama) renderer that generates Dart co
|
|
|
6
6
|
|
|
7
7
|
Given a Codama IDL (Interface Description Language) describing a Solana program, this renderer produces a complete Dart package with typed account classes, instruction builders, codec functions, error definitions, PDA helpers, and barrel exports.
|
|
8
8
|
|
|
9
|
+
Fixed-size Codama types generate non-truncating encoders. Values within the declared byte capacity are zero-padded, while over-capacity values throw before any malformed bytes can be emitted. For UTF-8 strings, capacity is measured in encoded bytes rather than Dart code units.
|
|
10
|
+
|
|
9
11
|
## Installation
|
|
10
12
|
|
|
11
13
|
```bash
|
|
12
14
|
pnpm add codama-renderers-dart
|
|
13
15
|
# or
|
|
14
|
-
|
|
15
|
-
[](https://pub.dev/packages/codama-renderers-dart)
|
|
16
|
-
[](https://github.com/openbudgetfun/solana_kit/actions/workflows/ci.yml)
|
|
17
|
-
[](https://codecov.io/gh/openbudgetfun/solana_kit?flag=codama-renderers-dart)
|
|
18
16
|
npm install codama-renderers-dart
|
|
19
17
|
```
|
|
20
18
|
|
|
21
|
-
## Quick
|
|
19
|
+
## Quick start
|
|
22
20
|
|
|
23
21
|
### Programmatic API
|
|
24
22
|
|
|
25
|
-
```
|
|
23
|
+
```ts
|
|
26
24
|
import { renderVisitor } from "codama-renderers-dart";
|
|
27
25
|
import { visit } from "@codama/visitors-core";
|
|
28
26
|
import { rootNode, programNode /* ... */ } from "@codama/nodes";
|
|
@@ -37,6 +35,21 @@ visit(root, renderVisitor("lib/src/generated", {
|
|
|
37
35
|
}));
|
|
38
36
|
```
|
|
39
37
|
|
|
38
|
+
### Serialized Pina IDLs
|
|
39
|
+
|
|
40
|
+
`renderVisitor` accepts parsed Codama JSON as well as roots made with Codama constructors. Pina omits empty collections from its serialized IDLs; the renderer restores those structural defaults on an internal copy before traversal, leaving the parsed object unchanged.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { readFile } from "node:fs/promises";
|
|
44
|
+
import { visit } from "@codama/visitors-core";
|
|
45
|
+
import { renderVisitor } from "codama-renderers-dart";
|
|
46
|
+
|
|
47
|
+
const idl = JSON.parse(await readFile("target/codama/my_program.json", "utf8"));
|
|
48
|
+
visit(idl, renderVisitor("lib/src/generated"));
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Use `normalizeRootNode(idl)` when calling lower-level visitors such as `getRenderMapVisitor` directly.
|
|
52
|
+
|
|
40
53
|
### Codama CLI
|
|
41
54
|
|
|
42
55
|
Create a `codama.json` configuration file:
|
|
@@ -59,7 +72,7 @@ Then run:
|
|
|
59
72
|
codama run dart
|
|
60
73
|
```
|
|
61
74
|
|
|
62
|
-
## Generated
|
|
75
|
+
## Generated output structure
|
|
63
76
|
|
|
64
77
|
For a program called `myProgram`, the renderer generates:
|
|
65
78
|
|
|
@@ -87,7 +100,7 @@ lib/src/generated/
|
|
|
87
100
|
my_pda.dart # PDA seeds class + finder function
|
|
88
101
|
```
|
|
89
102
|
|
|
90
|
-
## Generated
|
|
103
|
+
## Generated code patterns
|
|
91
104
|
|
|
92
105
|
### Accounts
|
|
93
106
|
|
|
@@ -139,7 +152,17 @@ Instruction getTransferInstruction({
|
|
|
139
152
|
TransferInstructionData parseTransferInstruction(Instruction instruction) { ... }
|
|
140
153
|
```
|
|
141
154
|
|
|
142
|
-
###
|
|
155
|
+
### Discriminators and omitted defaults
|
|
156
|
+
|
|
157
|
+
Generated codecs treat Codama discriminators as wire invariants rather than caller input. A struct field or instruction argument with a default value strategy of `omitted` is not accepted by the generated constructor or instruction builder. Its declared default is always written by the encoder.
|
|
158
|
+
|
|
159
|
+
Account and instruction decoders validate every declared constant, field, and size discriminator before returning typed data. Invalid discriminator bytes and unexpected discriminator sizes throw a `SolanaError` instead of decoding as the wrong account or instruction type. Generation fails when a field discriminator has no deterministic default or uses a value form the renderer cannot encode safely.
|
|
160
|
+
|
|
161
|
+
Top-level instruction decoders require exact input consumption. Account decoders always reject truncated data, but accept unread trailing capacity unless the account declares a `sizeDiscriminatorNode`; size-discriminated accounts reject both truncation and suffix bytes.
|
|
162
|
+
|
|
163
|
+
Optional instruction accounts use Codama's `programId` strategy by default. When an optional account is absent, the builder emits a readonly program-address placeholder so every later account keeps its declared index. The account slot is removed only when the instruction explicitly selects the legacy `omitted` strategy.
|
|
164
|
+
|
|
165
|
+
### Scalar enums
|
|
143
166
|
|
|
144
167
|
Scalar enums (all-empty variants) generate a Dart `enum` with index-based encoder/decoder:
|
|
145
168
|
|
|
@@ -154,7 +177,9 @@ Encoder<AccountStatus> getAccountStatusEncoder() { ... }
|
|
|
154
177
|
Decoder<AccountStatus> getAccountStatusDecoder() { ... }
|
|
155
178
|
```
|
|
156
179
|
|
|
157
|
-
|
|
180
|
+
The generated enum API is the same for `u8`, `u16`, `u32`, and `u64` discriminators. The renderer converts `u64` indices to and from `BigInt` at the codec boundary and rejects out-of-range discriminators before converting them to Dart enum indices.
|
|
181
|
+
|
|
182
|
+
### Data enums (discriminated unions)
|
|
158
183
|
|
|
159
184
|
Data enums generate Dart 3 `sealed class` hierarchies:
|
|
160
185
|
|
|
@@ -202,7 +227,7 @@ Future<(Address, int)> findMyPdaPda({
|
|
|
202
227
|
}) async { ... }
|
|
203
228
|
```
|
|
204
229
|
|
|
205
|
-
## Type
|
|
230
|
+
## Type mapping
|
|
206
231
|
|
|
207
232
|
| Codama Type | Dart Type | Codec |
|
|
208
233
|
| ----------------------------------- | --------------- | -------------------------------- |
|
|
@@ -237,7 +262,7 @@ Future<(Address, int)> findMyPdaPda({
|
|
|
237
262
|
|
|
238
263
|
All naming conventions are customizable:
|
|
239
264
|
|
|
240
|
-
```
|
|
265
|
+
```ts
|
|
241
266
|
import { renderVisitor, createDartNameApi } from "codama-renderers-dart";
|
|
242
267
|
|
|
243
268
|
const nameApi = {
|
|
@@ -248,7 +273,7 @@ const nameApi = {
|
|
|
248
273
|
visit(root, renderVisitor("output", { nameApi }));
|
|
249
274
|
```
|
|
250
275
|
|
|
251
|
-
## Target
|
|
276
|
+
## Target packages
|
|
252
277
|
|
|
253
278
|
Generated Dart code depends on these solana_kit packages:
|
|
254
279
|
|
|
@@ -282,39 +307,10 @@ Key source files:
|
|
|
282
307
|
## Development
|
|
283
308
|
|
|
284
309
|
```bash
|
|
285
|
-
# Install dependencies
|
|
286
|
-
|
|
287
|
-
[](https://pub.dev/packages/codama-renderers-dart)
|
|
288
|
-
[](https://github.com/openbudgetfun/solana_kit/actions/workflows/ci.yml)
|
|
289
|
-
[](https://codecov.io/gh/openbudgetfun/solana_kit?flag=codama-renderers-dart)
|
|
290
310
|
pnpm install
|
|
291
|
-
|
|
292
|
-
# Type check
|
|
293
|
-
|
|
294
|
-
[](https://pub.dev/packages/codama-renderers-dart)
|
|
295
|
-
[](https://github.com/openbudgetfun/solana_kit/actions/workflows/ci.yml)
|
|
296
|
-
[](https://codecov.io/gh/openbudgetfun/solana_kit?flag=codama-renderers-dart)
|
|
297
311
|
npx tsc --noEmit
|
|
298
|
-
|
|
299
|
-
# Run tests
|
|
300
|
-
|
|
301
|
-
[](https://pub.dev/packages/codama-renderers-dart)
|
|
302
|
-
[](https://github.com/openbudgetfun/solana_kit/actions/workflows/ci.yml)
|
|
303
|
-
[](https://codecov.io/gh/openbudgetfun/solana_kit?flag=codama-renderers-dart)
|
|
304
312
|
pnpm test
|
|
305
|
-
|
|
306
|
-
# Watch mode
|
|
307
|
-
|
|
308
|
-
[](https://pub.dev/packages/codama-renderers-dart)
|
|
309
|
-
[](https://github.com/openbudgetfun/solana_kit/actions/workflows/ci.yml)
|
|
310
|
-
[](https://codecov.io/gh/openbudgetfun/solana_kit?flag=codama-renderers-dart)
|
|
311
313
|
pnpm test:watch
|
|
312
|
-
|
|
313
|
-
# Build
|
|
314
|
-
|
|
315
|
-
[](https://pub.dev/packages/codama-renderers-dart)
|
|
316
|
-
[](https://github.com/openbudgetfun/solana_kit/actions/workflows/ci.yml)
|
|
317
|
-
[](https://codecov.io/gh/openbudgetfun/solana_kit?flag=codama-renderers-dart)
|
|
318
314
|
pnpm build
|
|
319
315
|
```
|
|
320
316
|
|