Soldev

Solana clients

Last updated:

Clients are a public interface to your program and are usually generated from an Interface Definition Language (IDL) file.

There are two major IDL formats, Shank and Anchor.

A popular tool to parse these files is Codama. It is going to take your IDL and produce all the javascript files with functions required to produce valid instructions for your specific program.

import { createFromRoot } from 'codama';
import { rootNodeFromAnchor } from '@codama/nodes-from-anchor';
import anchorIdl from 'anchor-idl.json';

const codama = createFromRoot(rootNodeFromAnchor(anchorIdl));

Having this converted Codama IDL, we can then transform that into a generated client:

import { renderJavaScriptVisitor, renderRustVisitor } from '@codama/renderers';

codama.accept(renderJavaScriptVisitor('clients/js/src/generated', { ... }));
codama.accept(renderRustVisitor('clients/rust/src/generated', { ... }));

The visitor language comes from the visitor pattern in programming. This is a common pattern when parsing tree like structures. Codama is using the IDL to create an abstract syntax tree (AST) of the IDL.

This AST, along with the visitor pattern creates a nice API where we can more easily parse an IDL and then transform it into another form, like a set of javascript (or Rust) files.

Finally we can take these files and package them, along with the code that calls them, with our frontend bundle which the user would download and use.

You would then use this generated code to create a valid set of instructions, bundle them all up and send them (with signatures) as a transaction to an RPC node.

The way the programming model works on Solana means that clients have a lot of the responsibility of calling the program properly. A program might have many instructions that need to be bundled in a particular order, and its up to your front end to do this on behalf of the user.