Skip to content

[2.x] Adapt dynamic-codecs to Codama v2 - #1185

Merged
lorisleiva merged 1 commit into
mainfrom
09-28-adapt_dynamic-codecs_to_codama_v2
Sep 30, 2026
Merged

lorisleiva merged 1 commit into
mainfrom
09-28-adapt_dynamic-codecs_to_codama_v2

Conversation

@lorisleiva

@lorisleiva lorisleiva commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

This PR adapts @codama/dynamic-codecs to Codama v2.

  • Renames getNodeCodec and getNodeCodecVisitor to getNodeValueCodec and getNodeValueCodecVisitor.
  • Decodes every integer as a bigint and respects endian. Adds floatTypeNode, fixed points, date-times and durations, which encode as their raw integer.
  • Decodes enums as { __kind, __discriminator, data } using raw identifiers. Struct keys are raw identifiers too.
  • Applies type node transforms from the innermost to the outermost, including those of link nodes on top of the linked type.
  • Adds sentinelCountNode support for arrays, sets and maps.
  • Encodes an instruction's data, and resolves injected values from the provides of enclosing instructions.
  • Adds ProvidedScope.resolveOrThrow to @codama/visitors-core and the INJECTED_VALUE_NOT_PROVIDED error, thrown when a required injected value is not provided.
  • Restores the node stack when a visit throws, so reused visitors keep resolving links correctly.
  • Bumps @solana/* dependencies to 8.4.0.

@changeset-bot

changeset-bot Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 027a158

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@lorisleiva

Copy link
Copy Markdown
Member Author

@trevor-cortex

@trevor-cortex trevor-cortex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Ports @codama/dynamic-codecs to the v2 node model. The visitor is restructured around three helpers (visitLinkedNode, getCollectionCodec, applyTransform), with the per-type-node transforms applied via an interceptVisitor layer that wraps every visit — so transforms compose innermost-first and link-node transforms naturally stack on top of the linked type's own. Integers now always decode to bigint (with a transformCodec shim for the sub-64-bit sizes) and honour endian; float/fixed-point/date-time/duration are added; enums move to { __kind, __discriminator, data } with raw identifiers; sentinelCountNode is wired through to kit's sentinel array size. On the visitors-core side, ProvidedScope gains resolveOrThrow (sharing a resolveOutcome core with resolve), and resolveWithin now tracks which injection dead-ended so the new CODAMA_ERROR__INJECTED_VALUE_NOT_PROVIDED names the innermost culprit rather than the enclosing struct.

The code is clean and the test coverage is solid — I checked the pre/post-offset hex expectations against kit's offsetCodec semantics (preOffset in the post-offset scope is the original offset, so ff000000aa00 for the preOffset strategy is right), and the injected-value tests cover the standalone-type, linked-from-instruction, and parent/child-instruction cases well.

Things I verified

  • interceptVisitor binds this to the outermost visitor, so visit(node.type, this) inside baseVisitor re-enters through the transform/scope/stack layers at every depth. Transforms on nested fields, enum variant data, count prefixes, etc. all get applied.
  • getNodeValueCodec opens frames only for ancestor instructions and leaves the last path node to recordProvidedScopeVisitor, so there's no double-push when the path ends at an instruction.
  • Frames are ordered root→leaf and ProvidedScope.find searches innermost-first, so shadowing works as documented.
  • resolve's docblock ("all-or-nothing … undefined is returned") is still accurate after the Outcome refactor.

Non-blocking notes

Changeset. I don't see a .changeset/*.md in the file list, and CONTRIBUTING.md asks for one per user-facing concern. This PR renames the public getNodeCodec/getNodeCodecVisitor API, adds ProvidedScope.resolveOrThrow to @codama/visitors-core, and adds an error code to @codama/errors. If the 2.x branch is covered by a seeded major changeset and per-PR entries aren't wanted during candidacy, ignore this — just flagging in case it was an oversight.

Enum links inside provided values resolve against the injection site. In values.ts, visitInjectedValue visits the resolved node with this, so any enumValueNode inside a provided value has its enum link resolved via the current stack.getPath(). If an instruction in program A provides an enumValueNode('foo', …) (relative link) and the injection lives inside a defined type in program B that A links to, the link would resolve in B, not A. Probably fine to leave — it's an edge case and the value author can use an explicit program on the link — but worth knowing for whoever wires up dynamic-parsers/dynamic-instructions on top of this.

Map keys. Maps encode via Object.entries, so keys always reach the key codec as strings. Kit's number encoders coerce, so an integer-keyed map should still round-trip as { "1": … }, but there's no test for non-string keys. Not a regression from v1 as far as I can tell.

For subsequent reviewers

  • The enum value shape (data nesting, raw identifiers instead of pascalCase) is a wire-format change for anything consuming this package's output. dynamic-parsers, dynamic-instructions and dynamic-client only get @solana/* bumps here, so presumably their own v2 adaptation follows in later PRs of the stack — worth confirming nothing in those packages currently depends on the old __kind: 'PascalCase' / spread-fields shape.
  • pnpm-lock.yaml churn is the 8.3→8.4 @solana/* bump; I didn't audit it line by line.

Comment thread packages/dynamic-codecs/src/codecs.ts Outdated
@lorisleiva
lorisleiva force-pushed the 09-28-adapt_dynamic-codecs_to_codama_v2 branch 2 times, most recently from 1416323 to dfad4fa Compare September 30, 2026 14:09
@lorisleiva
lorisleiva force-pushed the 09-25-add_relations_nested_argument_seeds_and_legacy_pdas_to_nodes-from-anchor branch from 2bb3538 to 0554451 Compare September 30, 2026 14:09
@lorisleiva
lorisleiva force-pushed the 09-28-adapt_dynamic-codecs_to_codama_v2 branch from dfad4fa to d814828 Compare September 30, 2026 14:11
@lorisleiva
lorisleiva force-pushed the 09-25-add_relations_nested_argument_seeds_and_legacy_pdas_to_nodes-from-anchor branch 2 times, most recently from df64b9f to 9c2e80a Compare September 30, 2026 14:12
@lorisleiva
lorisleiva force-pushed the 09-28-adapt_dynamic-codecs_to_codama_v2 branch 4 times, most recently from ed7bb21 to 7cf264c Compare September 30, 2026 14:14
@lorisleiva
lorisleiva force-pushed the 09-25-add_relations_nested_argument_seeds_and_legacy_pdas_to_nodes-from-anchor branch from 3dd3fd7 to 0d35b27 Compare September 30, 2026 14:14
@lorisleiva
lorisleiva force-pushed the 09-28-adapt_dynamic-codecs_to_codama_v2 branch from 7cf264c to ea8ce7d Compare September 30, 2026 14:15
@lorisleiva
lorisleiva force-pushed the 09-25-add_relations_nested_argument_seeds_and_legacy_pdas_to_nodes-from-anchor branch 2 times, most recently from 497ad44 to 8955bbe Compare September 30, 2026 14:16
@lorisleiva
lorisleiva force-pushed the 09-28-adapt_dynamic-codecs_to_codama_v2 branch from ea8ce7d to 22d0df8 Compare September 30, 2026 14:16
@lorisleiva
lorisleiva force-pushed the 09-25-add_relations_nested_argument_seeds_and_legacy_pdas_to_nodes-from-anchor branch from 8955bbe to e9f57f4 Compare September 30, 2026 14:17
@lorisleiva
lorisleiva force-pushed the 09-28-adapt_dynamic-codecs_to_codama_v2 branch from 22d0df8 to 26f1b77 Compare September 30, 2026 14:17
Base automatically changed from 09-25-add_relations_nested_argument_seeds_and_legacy_pdas_to_nodes-from-anchor to main September 30, 2026 14:17
@lorisleiva
lorisleiva force-pushed the 09-28-adapt_dynamic-codecs_to_codama_v2 branch from 26f1b77 to 027a158 Compare September 30, 2026 14:18
@lorisleiva
lorisleiva marked this pull request as ready for review September 30, 2026 14:18
@lorisleiva
lorisleiva merged commit 1c512e1 into main Sep 30, 2026
0 of 2 checks passed
@lorisleiva
lorisleiva deleted the 09-28-adapt_dynamic-codecs_to_codama_v2 branch September 30, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants