Go to file
Armani Ferrante 0de345f1a6
v0.4.4 (#205)
2021-04-18 17:42:01 -07:00
cli v0.4.4 (#205) 2021-04-18 17:42:01 -07:00
client v0.4.4 (#205) 2021-04-18 17:42:01 -07:00
docker Update solana tools to v1.6.6 (#210) 2021-04-18 12:13:07 -07:00
docs Update solana tools to v1.6.6 (#210) 2021-04-18 12:13:07 -07:00
examples examples: Chat (#211) 2021-04-18 12:56:25 -07:00
lang v0.4.4 (#205) 2021-04-18 17:42:01 -07:00
spl v0.4.4 (#205) 2021-04-18 17:42:01 -07:00
ts v0.4.4 (#205) 2021-04-18 17:42:01 -07:00
.gitignore ts: Update yarn.lock 2021-03-10 03:45:46 -08:00
.travis.yml examples: Chat (#211) 2021-04-18 12:56:25 -07:00
CHANGELOG.md v0.4.4 (#205) 2021-04-18 17:42:01 -07:00
Cargo.lock v0.4.4 (#205) 2021-04-18 17:42:01 -07:00
Cargo.toml Create lang dir 2021-01-30 05:23:23 -08:00
LICENSE Tutorial 2 init 2021-01-04 23:57:51 -08:00
README.md readme: Mention changelog in contribution guidelines 2021-04-07 12:23:09 -07:00

README.md

Anchor

Build Status Docs Chat License

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

  • Rust eDSL for writing 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 DSL syntax and semantics are targeted at Solana, the high level flow of writing RPC request handlers, emitting an IDL, and generating clients from IDL is the same.

Getting Started

For a quickstart guide and in depth tutorials, see the guided documentation. To jump straight to examples, go here. For the latest Rust API documentation, see docs.rs.

Packages

Package Description Version Docs
anchor-lang Rust primitives for writing programs on Solana Crates.io Docs.rs
anchor-spl CPI clients for SPL programs on Solana crates Docs.rs
anchor-client Rust client for Anchor programs crates Docs.rs
@project-serum/anchor TypeScript client for Anchor programs npm Docs

Note

  • Anchor is in active development, so all APIs are subject to change.
  • This code is unaudited. Use at your own risk.

Examples

Build stateful programs on Solana by defining a state struct with associated methods. Here's a classic counter example, where only the designated authority can increment the count.

#[program]
mod counter {

    #[state]
    pub struct Counter {
      authority: Pubkey,
      count: u64,
    }

    pub fn new(ctx: Context<Auth>) -> Result<Self> {
        Ok(Self {
            auth: *ctx.accounts.authority.key
        })
    }

    pub fn increment(&mut self, ctx: Context<Auth>) -> Result<()> {
        if &self.authority != ctx.accounts.authority.key {
            return Err(ErrorCode::Unauthorized.into());
        }

        self.count += 1;

        Ok(())
    }
}

#[derive(Accounts)]
pub struct Auth<'info> {
    #[account(signer)]
    authority: AccountInfo<'info>,
}

#[error]
pub enum ErrorCode {
    #[msg("You are not authorized to perform this action.")]
    Unauthorized,
}

Additionally, one can utilize the full power of Solana's parallel execution model by keeping the program stateless and working with accounts directly. The above example can be rewritten as follows.

use anchor::prelude::*;

#[program]
mod counter {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>, authority: Pubkey) -> Result<()> {
        let counter = &mut ctx.accounts.counter;

        counter.authority = authority;
        counter.count = 0;

        Ok(())
    }

    pub fn increment(ctx: Context<Increment>) -> Result<()> {
        let counter = &mut ctx.accounts.counter;

        counter += 1;

        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(init)]
    pub counter: ProgramAccount<'info, Counter>,
    pub rent: Sysvar<'info, Rent>,
}

#[derive(Accounts)]
pub struct Increment<'info> {
    #[account(mut, has_one = authority)]
    pub counter: ProgramAccount<'info, Counter>,
    #[account(signer)]
    pub authority: AccountInfo<'info>,
}

#[account]
pub struct Counter {
    pub authority: Pubkey,
    pub count: u64,
}

#[error]
pub enum ErrorCode {
    #[msg("You are not authorized to perform this action.")]
    Unauthorized,
}

Due to the fact that account sizes on Solana are fixed, some combination of the above is often required. For example, one can store store global state associated with the entire program in the #[state] struct and local state assocated with each user in individual #[account] structs.

For more, see the examples directory.

Contribution

Thank you for your interest in contributing to Anchor! All contributions are welcome no matter how big or small. This includes includes (but is not limited to) filing issues, adding documentation, fixing bugs, creating examples, and implementing features.

If you'd like to contribute, please claim an issue by commenting, forking, and opening a pull request, even if empty. This allows the maintainers to track who is working on what issue as to not overlap work. If you're looking to get started, check out good first issues or issues where help is wanted. For simple documentation changes, feel free to just open a pull request.

If you're considering larger changes or self motivated features, please file an issue and engage with the maintainers in Discord.

When contributing, please make sure your code adheres to some basic coding guidlines:

  • Code must be formatted with the configured formatters (e.g. rustfmt and prettier).
  • Comment lines should be no longer than 80 characters and written with proper grammar and punctuation.
  • Commit messages should be prefixed with the package(s) they modify. Changes affecting multiple packages should list all packages. In rare cases, changes may omit the package name prefix.
  • All notable changes should be documented in the Change Log.

License

Anchor is licensed under Apache 2.0.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Anchor by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.