Skip to content

Add event codegen support#96

Open
ioxde wants to merge 3 commits intocodama-idl:mainfrom
ioxde:add-event-codegen
Open

Add event codegen support#96
ioxde wants to merge 3 commits intocodama-idl:mainfrom
ioxde:add-event-codegen

Conversation

@ioxde
Copy link
Copy Markdown

@ioxde ioxde commented Apr 15, 2026

NOTE: I don't like throwing out AI generated PRs, but I think in this instance it is the right move. I made the typescript and rust version at the same time. Close if this is not the direction you want to go in.

What this does

Generates Rust code from Codama event definitions:

  • Per-event files (events/{name}.rs) — Borsh-derived structs, discriminator constants, and a conditional from_bytes() method
  • Program-level aggregation (events/{program}_events.rs) — enum of all events, identify_*() to match raw data against discriminators, and try_parse_*() to deserialize into typed variants

Follows the same visitor pattern used for instructions and accounts. Events reuse typeManifestVisitor by wrapping
event data in a synthetic definedTypeNode.

Discriminator handling

Supports constant, field, and size discriminators (and combinations).

from_bytes() is only generated when the event has at least one constant discriminator inside a hiddenPrefixTypeNode with all fixed-size entries. If the prefix contains variable-length fields, from_bytes() is skipped and a warning is logged.

Program-level identify / try_parse are only generated for events with resolvable discriminators. try_parse deserializes via BorshDeserialize directly rather than delegating to per-event from_bytes() — this keeps the program-level API self-contained and avoids double-checking discriminators.

Key implementation details

  • constantDiscriminatorName and getDiscriminatorConditions were extracted from discriminatorConstant.ts as shared utilities — instructions only needed constants, events also need match conditions for the program-level identify function
  • Program nodes are visited before render maps are merged so programsWithEventEnum is populated before eventsMod.njk checks membership

Limitations

  • Non-fixed-size hidden prefixes skip from_bytes() generation (can't calculate a static skip offset)
  • Field discriminators without a defaultValue are excluded from program-level aggregation (no default = no way to generate a match condition)

Tests

eventsPage.test.ts — 17 test cases covering struct generation, all discriminator variants, hidden prefix offset
logic, program-level enum/identify/parse, and module structure. E2E fixtures for Raydium CPMM (LpChangeEvent,
SwapEvent).

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 15, 2026

🦋 Changeset detected

Latest commit: 1f2038e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@codama/renderers-rust Minor

Not sure what this means? Click here to learn what changesets are.

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

leavesOnly: !renderParentInstructions,
});
const definedTypesToExport = getAllDefinedTypes(node);
const eventsToExport = getAllEvents(node).filter(Boolean);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Same issue as the renderers-js PR.

This guard and the *events ?? [] pattern should probably be fixed upstream so they can be removed.

See codama-idl/codama#991

@ioxde ioxde force-pushed the add-event-codegen branch from 7e56daa to 1f2038e Compare April 17, 2026 05:27
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.

1 participant