2021-01-01 07:48:06 +08:00
# Anchor ⚓
2021-01-07 03:52:09 +08:00
[![Build Status ](https://travis-ci.com/project-serum/anchor.svg?branch=master )](https://travis-ci.com/project-serum/anchor)
2021-01-15 17:35:35 +08:00
[![Docs.rs ](https://docs.rs/anchor-lang/badge.svg )](https://docs.rs/anchor-lang)
2021-01-07 03:52:09 +08:00
[![Docs ](https://img.shields.io/badge/docs-tutorials-orange )](https://project-serum.github.io/anchor/)
2021-01-05 13:18:08 +08:00
[![Chat ](https://img.shields.io/discord/739225212658122886?color=blueviolet )](https://discord.com/channels/739225212658122886)
2021-01-24 09:38:23 +08:00
[![License ](https://img.shields.io/github/license/project-serum/anchor?color=ff69b4 )](https://opensource.org/licenses/Apache-2.0)
2021-01-03 10:14:38 +08:00
2021-01-05 13:18:08 +08:00
Anchor is a framework for Solana's [Sealevel ](https://medium.com/solana-labs/sealevel-parallel-processing-thousands-of-smart-contracts-d814b378192 ) runtime providing several convenient developer tools.
2021-01-01 08:14:35 +08:00
2021-01-23 16:18:50 +08:00
- Rust eDSL for writing Solana programs
2021-01-05 13:18:08 +08:00
- [IDL ](https://en.wikipedia.org/wiki/Interface_description_language ) specification
- TypeScript package for generating clients from IDL
- CLI and workspace management for developing complete applications
2021-01-01 08:14:35 +08:00
2021-01-05 13:18:08 +08:00
If you're familiar with developing in Ethereum's [Solidity ](https://docs.soliditylang.org/en/v0.7.4/ ), [Truffle ](https://www.trufflesuite.com/ ), [web3.js ](https://github.com/ethereum/web3.js ) or Parity's [Ink! ](https://github.com/paritytech/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.
2021-01-01 07:48:06 +08:00
2021-01-05 13:18:08 +08:00
## Getting Started
2021-01-01 07:48:06 +08:00
2021-01-05 13:18:08 +08:00
For a quickstart guide and in depth tutorials, see the guided [documentation ](https://project-serum.github.io/anchor/getting-started/introduction.html ).
2021-01-24 11:31:44 +08:00
To jump straight to examples, go [here ](https://github.com/project-serum/anchor/tree/master/examples ). For the latest Rust API documentation, see [docs.rs ](https://docs.rs/anchor-lang ).
2021-01-24 09:38:23 +08:00
## Packages
| Package | Version | Description|
| :-- | :-- | :--|
| `@project-serum/anchor` | [![npm ](https://img.shields.io/npm/v/@project-serum/anchor.svg?color=blue )](https://www.npmjs.com/package/@project-serum/anchor) | TypeScript client generator for Anchor programs |
| `anchor-lang` | [![Crates.io ](https://img.shields.io/crates/v/anchor-lang?color=blue )](https://crates.io/crates/anchor-lang) | Rust primitives for writing programs on Solana |
| `anchor-spl` | ![crates ](https://img.shields.io/badge/crates.io-unpublished-blue.svg ) | CPI clients for SPL programs on Solana |
2021-01-05 13:18:08 +08:00
## Note
2021-01-01 07:48:06 +08:00
2021-01-05 13:18:08 +08:00
* **Anchor is in active development, so all APIs are subject to change.**
* **This code is unaudited. Use at your own risk.**
2021-01-01 07:48:06 +08:00
2021-01-23 16:18:50 +08:00
## 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.
```rust
#[program]
mod counter {
#[state]
pub struct Counter {
authority: Pubkey,
count: u64,
}
pub fn new(ctx: Context< Auth > ) -> Result< Self > {
Ok(Self {
2021-01-23 23:50:42 +08:00
auth: *ctx.accounts.authority.key
2021-01-23 16:18:50 +08:00
})
}
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.
2021-01-01 07:48:06 +08:00
2021-01-15 17:35:35 +08:00
```rust
2021-01-05 13:18:08 +08:00
use anchor::prelude::*;
2021-01-01 07:48:06 +08:00
#[program]
2021-01-23 16:18:50 +08:00
mod counter {
2021-01-05 13:18:08 +08:00
use super::*;
2021-01-24 09:38:23 +08:00
pub fn initialize(ctx: Context< Initialize > , authority: Pubkey) -> Result< ()> {
2021-01-23 16:18:50 +08:00
let counter = & mut ctx.accounts.counter;
counter.authority = authority;
counter.count = 0;
2021-01-05 13:18:08 +08:00
Ok(())
}
2021-01-24 09:38:23 +08:00
pub fn increment(ctx: Context< Increment > ) -> Result< ()> {
2021-01-23 16:18:50 +08:00
let counter = & mut ctx.accounts.counter;
counter += 1;
2021-01-10 14:31:40 +08:00
Ok(())
2021-01-01 07:48:06 +08:00
}
}
2021-01-01 08:14:35 +08:00
#[derive(Accounts)]
2021-01-01 07:48:06 +08:00
pub struct Initialize< 'info> {
2021-01-10 14:31:40 +08:00
#[account(init)]
2021-01-23 16:18:50 +08:00
pub counter: ProgramAccount< 'info, Counter>,
2021-01-15 14:35:50 +08:00
pub rent: Sysvar< 'info, Rent>,
2021-01-05 13:18:08 +08:00
}
#[derive(Accounts)]
2021-01-23 16:18:50 +08:00
pub struct Increment< 'info> {
2021-01-21 09:38:23 +08:00
#[account(mut, has_one = authority)]
2021-01-23 16:18:50 +08:00
pub counter: ProgramAccount< 'info, Counter>,
2021-01-05 13:18:08 +08:00
#[account(signer)]
pub authority: AccountInfo< 'info>,
2021-01-01 07:48:06 +08:00
}
2021-01-10 14:31:40 +08:00
#[account]
2021-01-23 16:18:50 +08:00
pub struct Counter {
2021-01-10 14:31:40 +08:00
pub authority: Pubkey,
2021-01-23 16:18:50 +08:00
pub count: u64,
2021-01-01 07:48:06 +08:00
}
2021-01-24 09:38:23 +08:00
#[error]
pub enum ErrorCode {
#[msg("You are not authorized to perform this action.")]
Unauthorized,
}
2021-01-01 07:48:06 +08:00
```
2021-01-23 16:18:50 +08:00
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.
2021-01-24 09:38:23 +08:00
For more, see the [examples ](https://github.com/project-serum/anchor/tree/master/examples )
directory.
2021-01-01 08:14:35 +08:00
## Accounts attribute syntax.
2021-01-01 07:48:06 +08:00
2021-01-24 09:38:23 +08:00
There are several inert attributes that can be specified on a struct deriving `Accounts` .
2021-01-01 07:48:06 +08:00
2021-01-24 09:38:23 +08:00
| Attribute | Location | Description |
2021-01-01 07:48:06 +08:00
|:--|:--|:--|
2021-01-01 08:14:35 +08:00
| `#[account(signer)]` | On raw `AccountInfo` structs. | Checks the given account signed the transaction. |
2021-01-21 09:38:23 +08:00
| `#[account(mut)]` | On `AccountInfo` , `ProgramAccount` or `CpiAccount` structs. | Marks the account as mutable and persists the state transition. |
2021-01-10 14:03:14 +08:00
| `#[account(init)]` | On `ProgramAccount` structs. | Marks the account as being initialized, skipping the account discriminator check. |
2021-01-21 09:38:23 +08:00
| `#[account(belongs_to = <target>)]` | On `ProgramAccount` or `CpiAccount` structs | Checks the `target` field on the account matches the `target` field in the struct deriving `Accounts` . |
| `#[account(has_one = <target>)]` | On `ProgramAccount` or `CpiAccount` structs | Semantically different, but otherwise the same as `belongs_to` . |
| `#[account(seeds = [<seeds>])]` | On `AccountInfo` structs | Seeds for the program derived address an `AccountInfo` struct represents. |
2021-01-15 14:35:50 +08:00
| `#[account(owner = program \| skip)]` | On `AccountInfo` structs | Checks the owner of the account is the current program or skips the check. |
2021-01-21 09:38:23 +08:00
| `#[account("<literal>")]` | On any type deriving `Accounts` | Executes the given code literal as a constraint. The literal should evaluate to a boolean. |
| `#[account(rent_exempt = <skip>)]` | On `AccountInfo` or `ProgramAccount` structs | Optional attribute to skip the rent exemption check. By default, all accounts marked with `#[account(init)]` will be rent exempt, and so this should rarely (if ever) be used. Similarly, omitting `= skip` will mark the account rent exempt. |
2021-01-01 08:48:44 +08:00
## License
2021-01-05 13:18:08 +08:00
Anchor is licensed under [Apache 2.0 ](./LICENSE ).