Skip to content

codama-idl/codama

Repository files navigation

Codama

npm npm-downloads ci

Codama is a tool that describes any Solana program in a standardised format called a Codama IDL.

A Codama IDL can be used to:

  • ✨ Generate program clients in various languages and frameworks.
  • 📚 Generate documentation and tooling for your programs.
  • 🗃️ Register your IDL for others to discover and index.
  • 🔭 Provide valuable information to explorers and wallets.

Codama header: A small double-sided mind-map with the Codama logo in the middle. On the left, we see the various ways to get a Codama IDL from your Solana programs such as "Codama Macros" and "Anchor Program". On the right, we see the various utility tools that are offered for the IDL such as "Generate clients" or "Register IDL".

Table of contents

Getting started

Installation

To get started with Codama, simply install codama to your project and run the init command like so:

pnpm install codama
codama init

You will be prompted for the path of your IDL and asked to select any script presets you would like to use. This will create a Codama configuration file at the root of your project.

Usage

You may then use the codama run command to execute any script defined in your configuration file.

codama run --all # Run all Codama scripts.
codama run js    # Generates a JavaScript client.
codama run rust  # Generates a Rust client.

Coming from Anchor

If you are using Anchor or Shank macros, then you should already have an Anchor IDL. To make it work with Codama, you simply need to provide the path to your Anchor IDL in your Codama configuration file. Codama will automatically identify this as an Anchor IDL and will convert it to a Codama IDL before executing your scripts.

{
    "idl": "path/to/my/anchor/idl.json"
}

Codama scripts

You can use your Codama configuration file to define any script you want by using one or more visitors that will be invoked when the script is ran.

Visitors are objects that will visit your Codama IDL and either perform some operation — like generating a program client — or update the IDL further — like renaming accounts. You can either use visitors from external packages or from a local file, and — in both cases — you can provide any argument the visitor may require.

For instance, the example script below will invoke three visitors:

  • The first will use the default import from the my-external-visitor package and pass 42 as the first argument.
  • The second will use the withDefaults import from the my-external-visitor package.
  • The third will use a local visitor located next to the configuration file.
{
    "scripts": {
        "my-script": [
            { "from": "my-external-visitor", "args": [42] },
            "my-external-visitor#withDefaults",
            "./my-local-visitor.js"
        ]
    }
}

Note that if an external visitor in your script isn’t installed locally, you will be asked to install it next time you try to run that script.

❯ codama run my-script

▲ Your script requires additional dependencies.
▲ Install command: pnpm install my-external-visitor
? Install dependencies? › (Y/n)

You can learn more about the Codama CLI here.

Available visitors

The tables below aim to help you discover visitors from the Codama ecosystem that you can use in your scripts.

Feel free to PR your own visitor here for others to discover. Note that they are ordered alphabetically.

Generates documentation

Visitor Description Maintainer
@codama/renderers-demo (docs) Generates simple documentation as a template to help others create new visitors. Codama

Generates program clients

Visitor Description Maintainer
@codama/renderers-js (docs) Generates a JavaScript client compatible with Solana Kit. Anza
@codama/renderers-js-umi (docs) Generates a JavaScript client compatible with the Umi framework. Metaplex
@codama/renderers-rust (docs) Generates a Rust client compatible with the Solana SDK. Anza
@codama/renderers-vixen-parser (docs) Generates Yellowstone account and instruction parsers. Triton One
codama-py (docs) Generates a Python client. Solar

Provides utility

Visitor Description Maintainer
@codama/visitors#* (docs) Provides a big library of utility visitors that can be used to manipulate Codama IDLs. For instance, updateErrorsVisitor can be used to update error messages in your IDL . Check out the docs to see all available visitors. Codama
@codama/visitors-core#* (docs) Everything included in visitors-core is also included in visitors. The helpers offered in this package are slightly more advanced and can be used to help you build your own visitors. Codama

Getting a Codama IDL

We are currently working on a set of transparent macros that can be added to any program in order to extract a Codama IDL from it. There are still a lot more macros and scenarios for us to support but most programs can already benefit from these macros. You can then extract an IDL from the provided crate path using the Codama API like so:

use codama::Codama;

let codama = Codama::load(crate_path)?;
let idl_json = codama.get_json_idl()?;

We will add documentation on Codama macros when they are fully implemented but feel free to check this example that extract a Codama IDL from the System program interface using a build script.

Codama's architecture

The Codama IDL is designed as a tree of nodes starting with the RootNode which contains a ProgramNode and additional data such as the Codama version used when the IDL was created. Codama provides over 60 different types of nodes that help describe nearly every aspect of your Solana programs. You can read more about the Codama nodes here.

A small example of a Codama IDL as a tree of nodes. It starts with a RootNode and goes down to ProgramNode, AccountNode, InstructionNode, etc.

Because everything is designed as a Node, we can transform the IDL, aggregate information, and output various utility tools using special objects that can traverse node trees known as visitors. See this documentation to learn more about Codama visitors.

A small example of how a visitor can transform a Codama IDL into another Codama IDL. This example illustrates the "deleteNodesVisitor" which recursively removes NumberTypeNodes from a tree of nested TypleTypeNodes.

Other resources

About

Generate clients, CLIs, documentation and more from your Solana programs

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 24