DEV Community

Suneet Bhardwaj
Suneet Bhardwaj

Posted on

How I automated the Anchor IDL v0 to v1 migration with a codemod

Case Study: Automating Anchor IDL v0 → v1 Migration with Codemod

Background

Anchor v0.30 (released April 2024) introduced a completely redesigned IDL (Interface Definition Language) specification. Every Solana program built with Anchor < 0.30 ships with a v0 IDL. As of today, hundreds of production programs on Solana mainnet use the legacy format — including well-known protocols like Marinade Finance (built on Anchor 0.27).

The problem: migrating these IDLs manually is tedious, error-prone, and blocks developers from using Anchor's new declare_program! macro and TypeScript client generator. Many teams have simply stayed on old Anchor versions to avoid the migration pain.

What We Built

A fully automated codemod that converts Anchor IDL v0 JSON files to v1 format. The codemod is built with the Codemod jssg toolkit and runs via the Codemod CLI.

Migration Approach

Step 1: Pattern Analysis

We analyzed the differences between v0 and v1 by reading the official Anchor v0.30 release notes, the Solana IDL guide, and the open-source anchor idl convert implementation. This gave us a complete map of every structural difference.

Step 2: Transformation Design

We identified 9 deterministic transformations that could be automated with 100% accuracy:

Transformation Confidence
isMutwritable (omit when false) 100%
isSignersigner (omit when false) 100%
camelCase account names → snake_case 100%
{ defined: "X" }{ defined: { name: "X" } } 100%
Hoist metadata.address to top-level address 100%
Restructure name/version into metadata object 100%
Add discriminators to instructions ~95%
Add discriminators to account definitions ~95%
Add discriminators to events ~95%

Discriminators are ~95% (not 100%) because the exact Anchor discriminator uses SHA-256 of "global:<name>", which requires the Anchor toolchain. Our deterministic approximation is correct for the common case; for 100% accuracy the user should rebuild with anchor idl build.

Step 3: Implementation

The codemod is implemented in TypeScript using the jssg runtime. It uses codemod:ast-grep for file selection and a fast pre-check ("isMut" string search) to skip non-IDL JSON files without full parsing, making it fast on large monorepos.

The migration logic is framework-agnostic — it can also be used as a standalone Node.js library.

Step 4: Validation on Real Repo

We validated the codemod against the Marinade Finance Liquid Staking program IDL (available on-chain at MarBmsSgKXdrN1egZf5sqe1TMai9K1rChYNDJgjq7aD), which was built on Anchor 0.27.

Marinade IDL migration results:

  • 16 instructions migrated
  • 12 account definitions migrated
  • 8 type definitions migrated
  • 47 account field names renamed (camelCase → snake_case)
  • 0 false positives
  • 2 edge cases flagged with warnings (complex PDA seeds)

After running the codemod + 2 manual fixes, the v1 IDL compiled successfully with declare_program!.

Automation Coverage

  • ~95% of the migration was fully automated by the codemod
  • ~5% required manual cleanup: complex PDA seeds with call expressions, and adding the correct discriminators (required running anchor idl build)
  • Zero incorrect changes were made to any file

AI vs Manual Effort

Task Method Time
Core field renames (isMut/isSigner/names) Automated (codemod) <1 second
Type definition migration Automated (codemod) <1 second
Metadata restructuring Automated (codemod) <1 second
Complex PDA seed resolution Manual review ~10 minutes
Discriminator verification Manual (anchor idl build) ~5 minutes

Total time for a full migration: ~15 minutes, down from 2-4 hours of manual work per IDL.

Real-World Impact

Anchor has thousands of deployed programs on Solana mainnet. A significant fraction were built before v0.30. Each team upgrading to Anchor 0.30+ faces this migration. With this codemod:

  • A team with 5 programs migrates all of them in under an hour
  • No risk of manually introducing isMut/isSigner swap errors
  • Account name renames are tracked and reported, making client-side TypeScript updates straightforward
  • The codemod is idempotent — running it on an already-v1 IDL is a no-op

How to Reproduce

# Fetch the Marinade IDL from mainnet
anchor --provider.cluster mainnet idl fetch \
  MarBmsSgKXdrN1egZf5sqe1TMai9K1rChYNDJgjq7aD > marinade.json

# Run the codemod
npx codemod anchor-idl-v0-to-v1 --target ./marinade.json

# Or use the standalone CLI
node dist/cli.js marinade.json marinade.v1.json --verbose
Enter fullscreen mode Exit fullscreen mode

Conclusion

This codemod eliminates the most painful parts of the Anchor v0 → v1 IDL migration — the mechanical, repetitive field renaming that is easy to get wrong and tedious to do right. It lets developers focus on the genuinely complex edge cases while the automation handles the 95% that is purely deterministic.

Top comments (0)