🚨 GitBross is launching the **Open Source Backup Campaign** for #Solana and beyond 🚨

armaniferrante 301d2593af Add .travis.yml vor 4 Jahren
attributes 736c4912e1 Init repo vor 4 Jahren
cli 8caee03251 Pin syn dep vor 4 Jahren
derive 7f02eb13af syn: Rename derive related modules anchor -> accounts vor 4 Jahren
docs 16b5d751eb Fix broken links vor 4 Jahren
examples 7f02eb13af syn: Rename derive related modules anchor -> accounts vor 4 Jahren
src c2d2041759 E2E tests for custom types and options vor 4 Jahren
syn 7f02eb13af syn: Rename derive related modules anchor -> accounts vor 4 Jahren
ts ce6d647fdf Run prettier vor 4 Jahren
.gitignore e5ab0684f8 Finish tutorial-1 draft vor 4 Jahren
.travis.yml 301d2593af Add .travis.yml vor 4 Jahren
Cargo.lock 8caee03251 Pin syn dep vor 4 Jahren
Cargo.toml 16b5d751eb Fix broken links vor 4 Jahren
LICENSE e91a9e148b Tutorial 2 init vor 4 Jahren
README.md 301d2593af Add .travis.yml vor 4 Jahren

README.md

Anchor ⚓

Build Status Docs Chat License

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

  • Rust DSL 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.

Note

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

Example

use anchor::prelude::*;

// Define the program's RPC handlers.

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

    #[access_control(not_zero(data))]
    pub fn initialize(ctx: Context<Initialize>, authority: Pubkey) -> ProgramResult {
        let my_account = &mut ctx.accounts.my_account;
        my_account.initialized = true;
        my_account.authority = authority;
        Ok(())
    }

    pub fn update(ctx: Context<Update>, data: u64) -> ProgramResult {
        let my_account = &mut ctx.accounts.my_account;
        my_account.data = data;
    }
}

// Define the validated accounts for each handler.

#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(mut, "!my_account.initialized")]
    pub my_account: ProgramAccount<'info, MyAccount>,
}

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

// Define program owned accounts.

#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct MyAccount {
    pub initialized: bool,
    pub data: u64,
}

// Define auxiliary access control checks.

fn not_zero(data: u64) -> ProgramResult {
    if data == 0 {
        return Err(ProgramError::InvalidInstructionData);
    }
    Ok(())
}

Accounts attribute syntax.

There are several inert attributes (attributes that are consumed only for the purposes of the Accounts macro) that can be specified on a struct deriving Accounts.

Attribute Where Applicable Description
#[account(signer)] On raw AccountInfo structs. Checks the given account signed the transaction.
#[account(mut)] On ProgramAccount structs. Marks the account as mutable and persists the state transition.
#[account(belongs_to = <target>)] On ProgramAccount structs Checks the target field on the account matches the target field in the accounts array.
#[account(owner = program \| skip)] On ProgramAccount and AccountInfo structs Checks the owner of the account is the current program or skips the check.
#[account("<literal>")] On ProgramAccount structs Executes the given code literal as a constraint. The literal should evaluate to a boolean.

License

Anchor is licensed under Apache 2.0.