Expand description

Anchor ⚓ is a framework for Solana’s Sealevel runtime providing several convenient developer tools.

  • Rust eDSL for writing safe, secure, and high level Solana programs
  • IDL specification
  • TypeScript package for generating clients from IDL
  • CLI and workspace management for developing complete applications

If you’re familiar with developing in Ethereum’s Solidity, Truffle, web3.js or Parity’s Ink!, then the experience will be familiar. Although the syntax and semantics are targeted at Solana, the high level workflow of writing RPC request handlers, emitting an IDL, and generating clients from IDL is the same.

For detailed tutorials and examples on how to use Anchor, see the guided tutorials or examples in the GitHub repository.

Presented here are the Rust primitives for building on Solana.

Re-exports

Modules

idl.rs defines the instructions and account state used to store a program’s IDL.

The prelude contains all commonly used components of the crate. All programs should include it via anchor_lang::prelude::*;.

Structs

Provides non-argument inputs to the program.

Container for any account not owned by the current program.

Context speciying non-argument inputs for cross-program-invocations.

The Ctor accounts that can be used to create any account within the program itself (instead of creating the account on the client).

Boxed container for a deserialized account. Use this to reference any account owned by the currently executing program.

Boxed container for the program state singleton.

Container for sysvars.

Enums

Traits

A data structure that can be deserialized and stored into account storage, i.e. an AccountInfo’s mutable data slice.

A data structure that can be serialized and stored into account storage, i.e. an AccountInfo’s mutable data slice.

A data structure of validated accounts that can be deserialized from the input to a Solana program. Implementations of this trait should perform any and all requisite constraint checks on accounts to ensure the accounts maintain any invariants required for the program to run securely. In most cases, it’s recommended to use the Accounts derive macro to implement this trait.

The exit procedure for an account. Any cleanup or persistance to storage should be done here.

A data structure of accounts providing a one time deserialization upon account initialization, i.e., when the data array for a given account is zeroed. Any subsequent call to try_accounts_init should fail. For all subsequent deserializations, it’s expected that Accounts is used.

Borsh is the default serialization format for instructions and accounts. A data-structure that can be de-serialized from binary format by NBOR.

Borsh is the default serialization format for instructions and accounts. A data-structure that can be serialized into binary format by NBOR.

Calculates the data for an instruction invocation, where the data is Sha256(<namespace>::<method_name>)[..8] || BorshSerialize(args). args is a borsh serialized struct of named fields for each argument given to an instruction.

Transformation to an AccountInfo struct.

Transformation to AccountInfo structs.

Transformation to AccountMeta structs.

Attribute Macros

Executes the given access control method before running the decorated instruction handler. Any method in scope of the attribute can be invoked with any arguments from the associated instruction handler.

A data structure representing a Solana account, implementing various traits:

Generates Error and type Result<T> = Result<T, Error> types to be used as return types from Anchor instruction handlers. Importantly, the attribute implements From on the ErrorCode to support converting from the user defined error enum into the generated Error.

The #[interface] attribute allows one to define an external program dependency, without having any knowledge about the program, other than the fact that it implements the given trait.

The #[program] attribute defines the module containing all instruction handlers defining all entries into a Solana program.

The #[state] attribute defines the program’s state struct, i.e., the program’s global account singleton giving the program the illusion of state.

Derive Macros

Implements an Accounts deserializer on the given struct, applying any constraints specified via inert #[account(..)] attributes upon deserialization.

Borsh is the default serialization format for instructions and accounts.

Borsh is the default serialization format for instructions and accounts.