0%
Overview page background
HomeOverviewsSolana
What is Anchor and the Anchor Program Registry?

What is Anchor and the Anchor Program Registry?

Written by Pari

Brady Werkheiser headshot

Reviewed by Brady Werkheiser

Published on 2023-01-264 min read

Anchor is leading Solana development frameworks for writing safe, secure, and efficient high-level programs on Solana. A program in Anchor is a smart contract on the Solana blockchain that enables users to anchor data to the Solana blockchain. Due to Solana’s massive developer community, verified programs on Solana are hosted on the Anchor Program Registry for easy access.

This article will help you understand how to use one of the most important Solana developer tools, Anchor, and the Anchor Program Registry for building on Solana. We will also comparison Anchor and Seahorse, Python-based development framework for interacting with Solana.

What is the Solana account model?

Everything developers build on the Solana blockchain involves programs and accounts, which execute and store data on the blockchain. There are different account categories in Solana including:

  • Data accounts - store data

  • Program accounts - store executable programs

  • Native accounts - indicate native programs on Solana

And every account stores a set of information:

  • lamports - number of lamports owned by the account

  • owner - program owner of the account

  • executable - checks whether the account can process instructions or not

  • data - raw data byte array stored by the account

  • rent_epoch - next epoch that the account will owe rent fees

What is Anchor?

Anchor is a framework used to write safe, secure, and high-level programs on Solana, which abstracts the low-level construction of accounts and modification of the interfaces to your Solana programs.

A framework acts as a foundation so that the users don't have to create unnecessary logic from scratch, helps users avoid redundant code, and help them write clean and secure programs. Specifically, Anchor provides a deserialized accounts and instruction data through boilerplates, a command line interface (CLI), and a workspace for developing Solana dapps.

How does Anchor work?

Anchor uses macros and traits to generate boilerplate Rust code for developer. Every Anchor program consists of three components:

  1. declare_id -a macro used for declaring the program’s on-chain address

  2. #[program] - attribute macro used to denote the module containing the program’s instruction logic

  3. #[account] - attribute macro used to define custom account types for the program

Here is the basic structure of an Anchor program:

Basic structure of an Anchor program
Basic structure of an Anchor program

1. Create a Default Keypair

When we build an Anchor program for the first time, it generates a new key pair which serves as a default keypair to deploy the program.

Copied
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS")

The public key should be used as the programID specified in the declare_id! Marco.

2. Instruct the Program

Then, we can separately instruct the program using the #[program] attribute. Each instruction function requires a parameter of type Context and can optionally include additional function parameters representing instruction data. Anchor will automatically handle instruction data deserialization so that the user can work with instruction data as Rust types.

Copied
#[program]mod hello_anchor {    use super::*;    pub fn initialize(_ctx: Context<Initialize>) -> Result<()> {        Ok(())    }}

3. Implement an Account Deserializer

#[derive(Accounts)] implements an Account deserializer on the given struct and removes the need to deserialize each account manually. This is responsible for performing all requisite constraint checks and ensuring the accounts meet the conditions required for the program to run securely.

Copied
#[derive(Accounts)]pub struct Initialize {}

4. Invoke the Initialize Function

When the Initialize function is invoked, the program:

  • Checks that the accounts passed into the instruction match the account types specified in the Initialize struct

  • Checks the accounts against any additional constraints specified

This is how an Anchor program works and allows you to get started with building Anchor programs. To know more about Anchor programs, it is highly recommended to check out their documentation.

What is the Anchor Program Registry (APR)?

The Anchor Program Registry (APR) is a directory of Anchor programs that have been registered on the Solana blockchain, allowing users to easily locate and access them. APR ensures that Anchor programs are registered and authenticated on the blockchain.

There are a number of programs in the Anchor Program Registry to make your job easier. Let us consider a few examples of them:

  • token_signer - provides a suite of programs for Solana key management and security.

  • spl_governance - a collection of on-chain programs targeting the Sealevel parallel runtime.

  • crate_token - allows anyone to create, manage, and trade a tokenized basket of assets.

How do Solana developers use the Anchor Program Registry?

Solana developers use the Anchor Program Registry to access verified programs on the Solana blockchain. The Anchor Program Registry catalogs source code for verified programs, allowing developers to access a repository of working code.

You can follow the flow provided below in order to use Anchor Program Registry:

  1. Visit the address of an Anchor Program

  2. Connect your wallet

  3. Access the UI

  4. Sign and send a transaction to execute an instruction

The APR interface provides a list of instructions that Anchor program supports and form fields you can use to configure the inputs for a given instruction.

What is Seahorse?

Seahorse is a Solana development framework like Anchor, but it is built for Python developers. Seahorse provides a set of tools for building, deploying, and interacting with programs and accounts on the Solana blockchain. It supports different programming languages like JavaScript, Rust, and Python, complete with resources and documentation for new developers.

Anchor vs. Seahorse

Comparison between Anchor and Seahorse
Comparison between Anchor and Seahorse

Resources to Learn Anchor

There are several resources available to help you learn about the Anchor framework:

  1. Anchor Docs - find the most relevant and up-to-date information

  2. Soldev - start developing smart contracts with Anchor with example code

  3. Sol Playground - start developing programs with Anchor using an online IDE

There are plenty of resources available to help you learn about the Anchor framework and start building your own decentralized applications.

Start Developing with Anchor and Alchemy

The Anchor framework is a valuable tool for building reliable and secure Solana programs (smart contracts). Further, the Anchor Program Registry is a directory for verified programs on the Solana blockchain, and is a critical resource for enabling better composability.

Anchor framework’s versatility and scalability make it an attractive choice for Solana developers. If you’re ready to start with your Solana development journey, sign up for a free Solana RPC account today!

Overview cards background graphic
Section background image

Build blockchain magic

Alchemy combines the most powerful web3 developer products and tools with resources, community and legendary support.

Get your API key