Codama from an Anchor IDL
Codama transforms Anchor IDL into @solana/kit 7.0.0 compatible TypeScript: instruction codecs, account decoders, and error enums.
Recipe
anchor build
npx @codama/cli@latest run --idl target/idl/my_program.json --out clients/ts/src/generatedimport { createSolanaRpc, pipe } from "@solana/kit";
import { getIncrementInstruction } from "./generated/instructions";When to reach for this: Greenfield dApps standardize on @solana/kit instead of web3.js v1.
Working Example
import { address, createSolanaRpc, pipe, setTransactionMessageLifetime } from "@solana/kit";
import { getIncrementInstruction } from "./generated/instructions";
const rpc = createSolanaRpc("https://api.devnet.solana.com");
const programId = address("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
const ix = getIncrementInstruction({
counter: counterPda,
authority: signerAddress,
});
// Pipe into transaction message builders (kit 7 patterns)What this demonstrates:
- Codama reads Anchor IDL JSON
- Outputs kit-native instruction helpers
- Regenerate when IDL changes
- Pair with surfpool/litesvm tests
Deep Dive
Pipeline
anchor buildproduces IDL.- Codama generates TS modules.
- App imports generated codecs only (no hand encoding).
CI
Fail build if generated folder differs from fresh Codama run.
Gotchas
- Stale generated folder - Wrong discriminators.. Fix: Codama step in CI prebuild.
- IDL features Codama does not map - Manual patches needed.. Fix: Track Codama release notes.
- Mixing kit and web3 in one tx - Type friction.. Fix: Migrate transaction assembly to kit.
- Wrong rpc types with generated decoders - Decode failures.. Fix: Use kit rpc throughout.
- Not pinning Codama version - Codegen drift.. Fix: Lock CLI version in package.json.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
| @coral-xyz/anchor Program | anchor test legacy | Kit-first apps |
| Gill helpers | Higher-level kit wrapper | Raw kit codecs |
FAQs
Anchor version?
0.32.1 across advanced topics.
When to use zero_copy?
Large fixed layouts when Borsh CU too high.
Is realloc reversible?
Not easily; plan forward growth.
Are remaining accounts in IDL?
No; document separately.
declare_program! needs what?
Callee IDL JSON at compile time.
Can Anchor CPI to Pinocchio?
Yes with correct account metas.
Feature flags and IDL?
Avoid different IDL per feature on mainnet.
How to measure CU?
LiteSVM, Surfpool, devnet simulation.
Custom account types common?
No; last resort.
Next steps?
See Related links for codama.
Related
- solana/kit Codama and gill - crates
- Building Instructions - kit ix
- Generating TS Clients - Anchor client
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.