anchor/README.md

101 lines
4.0 KiB
Markdown
Raw Normal View History

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)
[![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)
[![License](https://img.shields.io/github/license/project-serum/anchor?color=blue)](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-05 13:18:08 +08:00
- Rust DSL for writing Solana programs
- [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).
To jump straight to examples, go [here](https://github.com/project-serum/anchor/tree/master/examples/tutorial).
## 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
## Example
2021-01-05 13:18:08 +08:00
```Rust
use anchor::prelude::*;
2021-01-01 07:48:06 +08:00
2021-01-05 13:18:08 +08:00
// Define the program's RPC handlers.
2021-01-01 07:48:06 +08:00
#[program]
2021-01-05 13:18:08 +08:00
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;
2021-01-01 07:48:06 +08:00
}
}
2021-01-05 13:18:08 +08:00
// Define the validated accounts for each handler.
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-05 13:18:08 +08:00
#[account(mut, "!my_account.initialized")]
pub my_account: ProgramAccount<'info, MyAccount>,
}
#[derive(Accounts)]
pub struct Update<'info> {
#[account(signer)]
pub authority: AccountInfo<'info>,
2021-01-01 07:48:06 +08:00
}
2021-01-05 13:18:08 +08:00
// Define program owned accounts.
2021-01-01 07:48:06 +08:00
2021-01-01 08:03:34 +08:00
#[derive(AnchorSerialize, AnchorDeserialize)]
2021-01-05 13:18:08 +08:00
pub struct MyAccount {
2021-01-01 07:48:06 +08:00
pub initialized: bool,
pub data: u64,
}
2021-01-05 13:18:08 +08:00
// Define auxiliary access control checks.
2021-01-01 07:48:06 +08:00
2021-01-05 13:18:08 +08:00
fn not_zero(data: u64) -> ProgramResult {
if data == 0 {
return Err(ProgramError::InvalidInstructionData);
}
Ok(())
2021-01-01 07:48:06 +08:00
}
```
2021-01-01 08:14:35 +08:00
## Accounts attribute syntax.
2021-01-01 07:48:06 +08:00
2021-01-05 13:18:08 +08:00
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`.
2021-01-01 07:48:06 +08:00
| Attribute | Where Applicable | Description |
|:--|:--|:--|
2021-01-01 08:14:35 +08:00
| `#[account(signer)]` | On raw `AccountInfo` structs. | Checks the given account signed the transaction. |
2021-01-05 13:18:08 +08:00
| `#[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. |
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).