Home » Blog » Framework: Anchor
Framework: Anchor
2202 ,4 rebmeceD

Framework: Anchor

Anchor is a framework which allows users to create secure projects on Solana and develop smart contracts in Rust for the Solana blockchain. It helps to do it quickly thanks to a number of boilerplates (e.g. account serialization and deserialization or instruction data). It also offers security checks and keeps them separately from the business logic. This allows developers to decrease  the time spent on working with raw code and focus on the larger scheme of the project.

To work on it, it is necessary to have Rust, Solana CLI, yarn and Anchor packages already installed.

The Anchor program architecture contains:

  • A system account (an account declaration, which the program uses to store on-chain data)
  • Instruction wrappers or Account contexts (to specify accounts for instructions, also allows to add constraints and access controls)
  • Processor (the main program entry, contains business logic)

Anchor tools and modules

Anchor provides users and developers with a range of convenient tools and contains a number of modules and macros, which make working on a project easier and faster.

The tools include the following:

  • Rust eDSL (helps to write secure and clean programs on Solana)
  • IDL specification
  • TypeScript (to generate clients)
  • CLI and workplace management (for application development)

There are also useful modules which, among the other, include:

  • ‘Accounts’ (this special type of accounts may be used in the account validation structure)
  • ‘Context’ (a specific data structure, used to provide program endpoints with non-argument inputs)
  • ‘Prelude’ (this module contains the most frequently used components of the crate)

The list of macros includes a broad selection of macros of different types. Among them are:

  • ‘General macros’ (such as ‘declare_id’ to define the program ID, ‘emit’, ‘err’, which returns with the error and is used with custom error types, and ‘error’, several ‘require’ types to work with non-pubkey values)
  • Attribute macros (such as ‘Access_control’, which runs an access control method before launching the decorated instruction handler, 'Constant', used to mark constant values, 'Error_code’, ‘Interface’ and so on)
  • Derive macros (‘Accounts’, which implements an ‘Accounts’ deserializer on the particular structure and provides functionality by means of the attributes, ‘AnchorSerialize’ and ‘AccountDeserialize’, which are the default serialization formats for accounts and instructions)
  • Traits (including ‘AccountSerialize’ and ‘AccountDeserialize’, ‘AnchorSerialize’ and ‘AnchorDeserialize’, ‘InstructionData’, which calculates the data for an instruction invocation, ‘ToAccountInfo’, ‘ZeroCopy’ etc.)

We use cookies on our website to improve user experience and analyze website traffic. By clicking “Accept“, you agree to our website's cookie use as described in our Privacy Policy.