Skip to content

fix: move token account creation to preinit#2194

Merged
ananas-block merged 1 commit intomainfrom
jorrit/fix-token-macro
Jan 21, 2026
Merged

fix: move token account creation to preinit#2194
ananas-block merged 1 commit intomainfrom
jorrit/fix-token-macro

Conversation

@ananas-block
Copy link
Contributor

@ananas-block ananas-block commented Jan 21, 2026

Summary by CodeRabbit

  • Refactor
    • Consolidated account initialization logic for PDAs, mints, and token accounts into a unified pre-initialization flow, improving code organization and ensuring all accounts are available earlier during instruction execution.
    • Simplified internal code generation by removing redundant finalization paths and centralizing account creation.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 21, 2026

📝 Walkthrough

Walkthrough

This PR refactors the light_pdas macro system to consolidate account creation (PDAs, mints, token accounts, and ATAs) into a unified pre_init phase. Previously, account creation was split across pre_init and finalize stages. The builder now provides orchestration methods and internal body generators to eliminate code duplication and simplify the control flow.

Changes

Cohort / File(s) Summary
Pre-Init Orchestration & Builder Refactoring
sdk-libs/macros/src/light_pdas/accounts/builder.rs
Added new public generate_pre_init_all() orchestrator that selects the appropriate code path based on PDAs, mints, and token/ATA presence. Introduced three private body generators (generate_pre_init_pdas_and_mints_body, generate_pre_init_pdas_only_body, generate_pre_init_mints_only_body) to eliminate code duplication. Refactored existing public wrappers to delegate to body methods and append Ok(true). Removed token finalization helpers in favor of unified pre_init flow.
Integration & Test Updates
sdk-libs/macros/src/light_pdas/accounts/derive.rs
Integrated new generate_pre_init_all() orchestrator, replacing per-variant pre_init generation. Shifted finalize stage to no-op; all account creation now occurs in pre_init. Updated tests to verify token/ATA creation happens in pre_init (LightPreInit) rather than finalize, and adjusted expectations for mark-only scenarios.
Token Account Method Signature Changes
sdk-libs/macros/src/light_pdas/accounts/token.rs
Renamed needs_finalize to needs_creation and replaced generate_finalize_body with generate_pre_init_token_creation (returns Option<TokenStream> instead of TokenStream). Shifted token/ATA creation code generation from finalize to pre_init phase.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • #2181 — Reorganizes token-account/ATA changes by moving token finalize logic into unified pre_init flow with corresponding builder method updates.
  • #2174 — Refactors the same macro-level paths (token/mint/PDAs creation) with similar finalize→pre_init migration and rentfree-oriented codegen changes.
  • #2180 — Modifies the same light_pdas accounts codegen paths with related method renames and finalize→pre_init function signature updates.

Suggested labels

ai-review

Suggested reviewers

  • sergeytimoshin
  • SwenSchaeferjohann

Poem

🎯 Pre-init takes the stage,
Account creation's new home,
No more split-brain age—
All creation zones condense to one chrome.
Finalize bows out with grace,
Builders dance in unified place. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the primary change: moving token account creation logic from the finalize phase to the preinit phase, which is the core refactoring across all modified files.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 70.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch jorrit/fix-token-macro

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ananas-block ananas-block merged commit dd28349 into main Jan 21, 2026
20 checks passed
@ananas-block ananas-block deleted the jorrit/fix-token-macro branch January 21, 2026 17:24
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