anchor-idl-to-ts

Crates.ioanchor-idl-to-ts
lib.rsanchor-idl-to-ts
version0.2.1
created_at2025-09-24 21:20:55.72719+00
updated_at2025-09-25 06:43:08.668073+00
descriptionA tool for generating a typescript file from an Anchor IDL json file.
homepage
repositoryhttps://github.com/ochaloup/anchor-idl-to-ts
max_upload_size
id1853846
size32,808
chalda (ochaloup)

documentation

README

anchor-idl-to-ts

A tool for generating a TypeScript (TS) file from a standalone Anchor IDL JSON file.

Motivation: Sometimes, after downloading an Anchor JSON IDL from the chain, you need a corresponding TS file to build your client.

This tool mimics how the Anchor CLI generates a TS file. Normally, running anchor build produces both the IDL JSON file (target/idl/<name>.json) and the TS file (target/types/<name>.ts). Given only the IDL JSON file as input, this tool generates the TS file exactly as anchor build would.

The Anchor code used to generate the TS file can be found at Solana Anchor repository.

Usage

cargo install anchor-idl-to-ts

anchor-idl-to-ts <path-to-idl.json> [-o <output-file.ts>]

Anchor version differences

The Anchor IDL format has changed over time. This tool supports IDLs both from before Anchor 0.30.x and from later versions.

One key difference in Anchor 0.30.x+ is the use of the metadata field, which requires an address field to bind the generated IDL to a specific program ID.

The @coral-xyz/anchor TypeScript library version is tied to the corresponding Anchor IDL version and format. There is no backward compatibility.


Example: Generating a TS file

As an example, consider the voter-stake-registry (VSR), a plugin for SPL Governance. The VSR program is built with Anchor version 0.26.x.

When the VSR program maintainer deployed the program, the Anchor IDL was uploaded and stored on-chain (see Anchor CLI idl init).

NOTE: By default, Anchor includes the IDL instruction in the program binary. However, you can disable it by enabling the no-idl cargo feature.

voter-stake-registry:  4Q6WW2ouZ6V3iaNm56MTd5n2tnTm4C5fiH8miFHnAFHo
anchor --provider.cluster mainnet idl fetch 4Q6WW2ouZ6V3iaNm56MTd5n2tnTm4C5fiH8miFHnAFHo > ./vsr-idl.json

From the downloaded IDL file vsr-idl.json we can generate the TS file compatible with @coral-xyz/anchor version 0.26.x.

cargo run --bin anchor-idl-to-ts ./vsr-idl.json

Example to convert for newer Anchor versions

In newer versions of Anchor (0.30.x+), the IDL format has changed, and the CLI now provides functionality to convert an older IDL format into the newer one.

# 'convert' cli argument is available only in Anchor 0.31.x+
avm use 0.31.0

anchor idl convert ./vsr-idl.json -o ./vsr-idl-0.31.json --program-id 4Q6WW2ouZ6V3iaNm56MTd5n2tnTm4C5fiH8miFHnAFHo

NOTE: The --program-id flag is required to populate the metadata field in the converted IDL. Without it, you will encounter the error: "Error: Program id missing in `idl.metadata.address` field".

With the new IDL file vsr-idl-0.31.json, we can generate a TS file compatible with @coral-xyz/anchor version 0.31.x.

cargo run --bin anchor-idl-to-ts ./vsr-idl-0.31.json -o ./vsr-idl-0.31.ts

Forked from nicholas-ewasiuk/simple-anchor-idl-ts Credits to anchor-gen that does all the heavy lifting.

Commit count: 12

cargo fmt