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§
pub use solana_program;
Modules§
- accounts
- Account types that can be used in the account validation struct.
- context
- Data structures that are used to provide non-argument inputs to program endpoints
- error
- prelude
- The prelude contains all commonly used components of the crate.
All programs should include it via
anchor_lang::prelude::*;
. - system_
program
Macros§
- declare_
id - Defines the program’s ID. This should be used at the root of all Anchor based programs.
- declare_
program - Declare an external program based on its IDL.
- emit
- Logs an event that can be subscribed to by clients.
Uses the
sol_log_data
syscall which results in the following log: - emit_
cpi event-cpi
- Log an event by making a self-CPI that can be subscribed to by clients.
- err
- Returns with the given error. Use this with a custom error type.
- error
- Generates an
Error::AnchorError
that includes file and line information. - pubkey
- Convenience macro to define a static public key.
- require
- Ensures a condition is true, otherwise returns with the given error. Use this with or without a custom error type.
- require_
eq - Ensures two NON-PUBKEY values are equal.
- require_
gt - Ensures the first NON-PUBKEY value is greater than the second NON-PUBKEY value.
- require_
gte - Ensures the first NON-PUBKEY value is greater than or equal to the second NON-PUBKEY value.
- require_
keys_ eq - Ensures two pubkeys values are equal.
- require_
keys_ neq - Ensures two pubkeys are not equal.
- require_
neq - Ensures two NON-PUBKEY values are not equal.
- source
- Creates a
Source
Structs§
Traits§
- Account
Deserialize - A data structure that can be deserialized and stored into account storage,
i.e. an
AccountInfo
’s mutable data slice. - Account
Serialize - A data structure that can be serialized and stored into account storage,
i.e. an
AccountInfo
’s mutable data slice. - Accounts
- 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. - Accounts
Close - The close procedure to initiate garabage collection of an account, allowing one to retrieve the rent exemption.
- Accounts
Exit - The exit procedure for an account. Any cleanup or persistence to storage should be done here.
- Anchor
Deserialize - Borsh is the default serialization format for instructions and accounts. A data-structure that can be de-serialized from binary format by NBOR.
- Anchor
Serialize - A data-structure that can be serialized into binary format by NBOR.
- Bump
- Bump seed for program derived addresses.
- Bumps
- Associated bump seeds for
Accounts
. - CheckId
- Defines a trait for checking the id of a program.
- Check
Owner - Defines a trait for checking the owner of a program.
- Discriminator
- Unique identifier for a type.
- Event
- An event that can be emitted via a Solana log. See
emit!
for an example. - Id
- Defines the id of a program.
- IdlBuild
idl-build
- A trait that types must implement in order to include the type in the IDL definition.
- Ids
- Defines the possible ids of a program.
- Instruction
Data - Calculates the data for an instruction invocation, where the data is
Discriminator + BorshSerialize(args)
.args
is a borsh serialized struct of named fields for each argument given to an instruction. - Key
- Defines the Pubkey of an account.
- Lamports
- Lamports related utility methods for accounts.
- Owner
- Defines an address expected to own an account.
- Owners
- Defines a list of addresses expected to own an account.
- Space
- Defines the space of an account for initialization.
- ToAccount
Info - Transformation to an
AccountInfo
struct. - ToAccount
Infos - Transformation to
AccountInfo
structs. - ToAccount
Metas - Transformation to
AccountMeta
structs. - Zero
Copy - An account data structure capable of zero copy deserialization.
Type Aliases§
Attribute Macros§
- access_
control - 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.
- account
- An attribute for a data structure representing a Solana account.
- constant
- A marker attribute used to mark const values that should be included in the generated IDL but functionally does nothing.
- error_
code - Generates
Error
andtype Result<T> = Result<T, Error>
types to be used as return types from Anchor instruction handlers. Importantly, the attribute implementsFrom
on theErrorCode
to support converting from the user defined error enum into the generatedError
. - event
- The event attribute allows a struct to be used with emit! so that programs can log significant events in their programs that clients can subscribe to. Currently, this macro is for structs only.
- event_
cpi event-cpi
- An attribute macro to add necessary event CPI accounts to the given accounts struct.
- instruction
- This attribute is used to override the Anchor defaults of program instructions.
- interface
interface-instructions
- The
#[interface]
attribute is used to mark an instruction as belonging to an interface implementation, thus transforming its discriminator to the proper bytes for that interface instruction. - program
- The
#[program]
attribute defines the module containing all instruction handlers defining all entries into a Solana program. - zero_
copy - A data structure that can be used as an internal field for a zero copy
deserialized account, i.e., a struct marked with
#[account(zero_copy)]
.
Derive Macros§
- Accounts
- Implements an
Accounts
deserializer on the given struct. Can provide further functionality through the use of attributes. - Anchor
Deserialize - Anchor
Serialize - Init
Space - Implements a
Space
trait on the given struct or enum.