|
@@ -4,7 +4,7 @@
|
|
|
[](https://docs.rs/anchor-lang)
|
|
|
[](https://project-serum.github.io/anchor/)
|
|
|
[](https://discord.com/channels/739225212658122886)
|
|
|
-[](https://opensource.org/licenses/Apache-2.0)
|
|
|
+[](https://opensource.org/licenses/Apache-2.0)
|
|
|
|
|
|
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.
|
|
|
|
|
@@ -18,7 +18,15 @@ If you're familiar with developing in Ethereum's [Solidity](https://docs.solidit
|
|
|
## Getting Started
|
|
|
|
|
|
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). For the latest Rust API documentation, see [docs.rs](https://docs.rs/anchor-lang).
|
|
|
+To jump straight to examples, go [here](https://github.com/project-serum/anchor/tree/master/examples/lockup). For the latest Rust API documentation, see [docs.rs](https://docs.rs/anchor-lang).
|
|
|
+
|
|
|
+## Packages
|
|
|
+
|
|
|
+| Package | Version | Description|
|
|
|
+| :-- | :-- | :--|
|
|
|
+| `@project-serum/anchor` | [](https://www.npmjs.com/package/@project-serum/anchor) | TypeScript client generator for Anchor programs |
|
|
|
+| `anchor-lang` | [](https://crates.io/crates/anchor-lang) | Rust primitives for writing programs on Solana |
|
|
|
+| `anchor-spl` |  | CPI clients for SPL programs on Solana |
|
|
|
|
|
|
## Note
|
|
|
|
|
@@ -78,13 +86,11 @@ can be rewritten as follows.
|
|
|
```rust
|
|
|
use anchor::prelude::*;
|
|
|
|
|
|
-// Define instruction handlers.
|
|
|
-
|
|
|
#[program]
|
|
|
mod counter {
|
|
|
use super::*;
|
|
|
|
|
|
- pub fn initialize(ctx: Context<Initialize>, authority: Pubkey) -> ProgramResult {
|
|
|
+ pub fn initialize(ctx: Context<Initialize>, authority: Pubkey) -> Result<()> {
|
|
|
let counter = &mut ctx.accounts.counter;
|
|
|
|
|
|
counter.authority = authority;
|
|
@@ -93,7 +99,7 @@ mod counter {
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
- pub fn increment(ctx: Context<Update>) -> ProgramResult {
|
|
|
+ pub fn increment(ctx: Context<Increment>) -> Result<()> {
|
|
|
let counter = &mut ctx.accounts.counter;
|
|
|
|
|
|
counter += 1;
|
|
@@ -102,8 +108,6 @@ mod counter {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// Define accounts for each handler.
|
|
|
-
|
|
|
#[derive(Accounts)]
|
|
|
pub struct Initialize<'info> {
|
|
|
#[account(init)]
|
|
@@ -119,13 +123,17 @@ pub struct Increment<'info> {
|
|
|
pub authority: AccountInfo<'info>,
|
|
|
}
|
|
|
|
|
|
-// Define program owned accounts.
|
|
|
-
|
|
|
#[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
|
|
@@ -133,12 +141,14 @@ 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](https://github.com/project-serum/anchor/tree/master/examples)
|
|
|
+directory.
|
|
|
+
|
|
|
## 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`.
|
|
|
+There are several inert attributes that can be specified on a struct deriving `Accounts`.
|
|
|
|
|
|
-| Attribute | Where Applicable | Description |
|
|
|
+| Attribute | Location | Description |
|
|
|
|:--|:--|:--|
|
|
|
| `#[account(signer)]` | On raw `AccountInfo` structs. | Checks the given account signed the transaction. |
|
|
|
| `#[account(mut)]` | On `AccountInfo`, `ProgramAccount` or `CpiAccount` structs. | Marks the account as mutable and persists the state transition. |
|