Codama Solana Framework

1.6.0 · active · verified Sun Apr 19

Codama is a Solana framework designed to facilitate the building of standardized programs through specifications and code generation. It aims to streamline the development of on-chain programs by providing a structured approach to defining program interfaces and interactions. The package `codama` (currently at version 1.6.0) serves as the main entry point, re-exporting core modules like `@codama/errors`, `@codama/nodes`, `@codama/validators`, and `@codama/visitors`, bundling them into a cohesive API. It offers utilities for managing Solana IDLs, allowing developers to create, manipulate, and validate program definitions from a `RootNode` or JSON representation. The project appears to have an active release cadence, with recent minor updates introducing new node types (e.g., `EventNode`) and functionality like a dynamic client for instruction building, indicating ongoing development and feature expansion within the Solana ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating a Codama IDL instance from both JSON and a programmatic `RootNode`, then displaying and cloning it.

import { createFromJson, createFromRoot, programNode, type RootNode } from 'codama';
import { getDebugStringVisitor, consoleLogVisitor } from '@codama/visitors';

// Example 1: Creating from a JSON string
const jsonIdlString = JSON.stringify({
  version: '0.1.0',
  name: 'my_program',
  instructions: [
    {
      name: 'initialize',
      accounts: [],
      args: []
    }
  ],
  accounts: [],
  events: []
});

const codamaFromJson = createFromJson(jsonIdlString);
console.log('Codama from JSON:');
codamaFromJson.accept(consoleLogVisitor(getDebugStringVisitor({ indent: true })));

// Example 2: Creating from a RootNode programmatically
const myProgramNode: RootNode = programNode({
  name: 'myProgram',
  version: '1.0.0',
  docs: ['My example program.'],
  instructions: [],
  accounts: [],
  errors: [],
  types: [],
  events: []
});

const codamaFromRoot = createFromRoot(myProgramNode);
console.log('\nCodama from RootNode:');
codamaFromRoot.accept(consoleLogVisitor(getDebugStringVisitor({ indent: true })));

// Example 3: Modifying a Codama instance (e.g., cloning)
const clonedCodama = codamaFromRoot.clone();
console.log('\nCloned Codama JSON:');
console.log(clonedCodama.getJson());

view raw JSON →