Skip to content

feat: Auto-extract inline PDAs from v01 Anchor instruction accounts#984

Open
amilz wants to merge 4 commits intocodama-idl:mainfrom
amilz:feat/auto-extract-pdas-v01
Open

feat: Auto-extract inline PDAs from v01 Anchor instruction accounts#984
amilz wants to merge 4 commits intocodama-idl:mainfrom
amilz:feat/auto-extract-pdas-v01

Conversation

@amilz
Copy link

@amilz amilz commented Mar 18, 2026

  • Auto-extract inline PDA definitions from v01 Anchor instruction accounts into program-level PdaNodes
  • Instruction accounts are rewritten to reference extracted PDAs via pdaLinkNode
  • Deduplicate PDAs across instructions by seed structure fingerprint
  • Exclude ATA program PDAs from extraction (they should still auto-derive in the ix)
  • Handle name collisions (same name, different seeds) with instruction-name suffix + warning
  • Opt-out via extractPdas: false option on rootNodeFromAnchor(in case someone has a addPdasVisitor setup and doesn't want to change).

Resolves #70 (Part A)

How it works

After building instruction nodes from the Anchor IDL, extractPdasFromProgram post-processes the ProgramNode:

  1. Walks all instruction accounts looking for pdaValueNode with inline pdaNode
  2. Fingerprints each PDA by { seeds, programId } for structural dedup
  3. Promotes unique PDAs to the program's pdas array
  4. Replaces inline pdaNode with pdaLinkNode on each instruction account

Downstream renderers already generate findXPda() helpers from program-level PdaNodes — no renderer changes needed. addPdasVisitor continues to take precedence since it runs after extraction.

Test plan

  • Single PDA extracted to program level, account uses pdaLinkNode
  • Same PDA across multiple instructions deduplicates to one PdaNode
  • Same seeds with different account names keeps first name
  • Same name with different seeds warns and suffixes
  • ATA program PDAs excluded
  • Cross-program PDA with static programId preserved on PdaNode
  • Dynamic programId stays on pdaValueNode, not on PdaNode
  • extractPdas: false preserves inline pdaNodes
  • No PDA accounts → empty pdas array
  • Verified against real-world IDL (let_me_buy — 8 instructions, 1 deduplicated PDA, ATA accounts correctly filtered)

Walk instruction accounts after building the ProgramNode, collect
inline pdaNode definitions, deduplicate by seed structure, and
promote them to program-level PdaNodes. Instruction accounts are
rewritten to use pdaLinkNode references. ATA program PDAs are
excluded. Controlled via `extractPdas` option (default: true).
@changeset-bot
Copy link

changeset-bot bot commented Mar 18, 2026

🦋 Changeset detected

Latest commit: aca5c77

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

This PR includes changesets to release 1 package
Name Type
@codama/nodes-from-anchor 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

amilz added 2 commits March 18, 2026 13:02
Address feedback:
- Convert extractPdasFromProgram into an extractPdasVisitor using
  bottomUpTransformerVisitor, matching the existing visitor pattern
- Add it to the defaultVisitor pipeline alongside other post-processing
  visitors — opt-out is now via rootNodeFromAnchorWithoutDefaultVisitor
- Use getUniqueHashStringVisitor for deterministic PDA fingerprinting
  instead of JSON.stringify on raw nodes
- Remove option threading (extractPdas flag) from ProgramNode, RootNode,
  and public API since the defaultVisitor handles this naturally
- Revert ProgramNode.test.ts to pre-extraction expectations since
  programNodeFromAnchorV01 no longer runs extraction
@amilz amilz marked this pull request as ready for review March 19, 2026 14:41
- Merge extracted PDAs with existing program.pdas instead of replacing,
  so v00 programs (which already populate pdas) are not regressed
- Track all used PDA names (existing + extracted) and loop suffix
  until unique, preventing ambiguous link resolution
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.

[nodes-from-anchor] Improve node data for inlined PdaValueNodes

1 participant