anchor/README.md

120 lines
6.4 KiB
Markdown
Raw Permalink Normal View History

2021-09-21 14:05:56 -07:00
<div align="center">
2022-06-15 12:49:14 -07:00
<img height="170x" src="https://pbs.twimg.com/media/FVUVaO9XEAAulvK?format=png&name=small" />
2020-12-31 15:48:06 -08:00
2021-09-21 14:05:56 -07:00
<h1>Anchor</h1>
<p>
<strong>Solana Sealevel Framework</strong>
</p>
<p>
<a href="https://github.com/coral-xyz/anchor/actions"><img alt="Build Status" src="https://github.com/coral-xyz/anchor/actions/workflows/tests.yaml/badge.svg" /></a>
<a href="https://anchor-lang.com"><img alt="Tutorials" src="https://img.shields.io/badge/docs-tutorials-blueviolet" /></a>
2023-03-06 03:19:08 -08:00
<a href="https://discord.gg/NHHGSXAnXk"><img alt="Discord Chat" src="https://img.shields.io/discord/889577356681945098?color=blueviolet" /></a>
<a href="https://opensource.org/licenses/Apache-2.0"><img alt="License" src="https://img.shields.io/github/license/coral-xyz/anchor?color=blueviolet" /></a>
2021-09-21 14:05:56 -07:00
</p>
</div>
2021-01-02 18:14:38 -08:00
2021-09-07 22:57:02 -07: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 for writing smart contracts.
2020-12-31 16:14:35 -08:00
2021-01-23 00:18:50 -08:00
- Rust eDSL for writing Solana programs
2021-01-04 21: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
2020-12-31 16:14:35 -08:00
2021-09-07 22:57:02 -07: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), 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.
2020-12-31 15:48:06 -08:00
2021-01-04 21:18:08 -08:00
## Getting Started
2020-12-31 15:48:06 -08:00
For a quickstart guide and in depth tutorials, see the [anchor book](https://book.anchor-lang.com) and the older [documentation](https://anchor-lang.com) that is being phased out.
To jump straight to examples, go [here](https://github.com/coral-xyz/anchor/tree/master/examples). For the latest Rust and TypeScript API documentation, see [docs.rs](https://docs.rs/anchor-lang) and the [typedoc](https://coral-xyz.github.io/anchor/ts/index.html).
2021-01-23 17:38:23 -08:00
## Packages
| Package | Description | Version | Docs |
| :---------------------- | :------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------- |
| `anchor-lang` | Rust primitives for writing programs on Solana | [![Crates.io](https://img.shields.io/crates/v/anchor-lang?color=blue)](https://crates.io/crates/anchor-lang) | [![Docs.rs](https://docs.rs/anchor-lang/badge.svg)](https://docs.rs/anchor-lang) |
| `anchor-spl` | CPI clients for SPL programs on Solana | [![crates](https://img.shields.io/crates/v/anchor-spl?color=blue)](https://crates.io/crates/anchor-spl) | [![Docs.rs](https://docs.rs/anchor-spl/badge.svg)](https://docs.rs/anchor-spl) |
| `anchor-client` | Rust client for Anchor programs | [![crates](https://img.shields.io/crates/v/anchor-client?color=blue)](https://crates.io/crates/anchor-client) | [![Docs.rs](https://docs.rs/anchor-client/badge.svg)](https://docs.rs/anchor-client) |
| `@coral-xyz/anchor` | TypeScript client for Anchor programs | [![npm](https://img.shields.io/npm/v/@coral-xyz/anchor.svg?color=blue)](https://www.npmjs.com/package/@coral-xyz/anchor) | [![Docs](https://img.shields.io/badge/docs-typedoc-blue)](https://coral-xyz.github.io/anchor/ts/index.html) |
| `@coral-xyz/anchor-cli` | CLI to support building and managing an Anchor workspace | [![npm](https://img.shields.io/npm/v/@coral-xyz/anchor-cli.svg?color=blue)](https://www.npmjs.com/package/@coral-xyz/anchor-cli) | [![Docs](https://img.shields.io/badge/docs-typedoc-blue)](https://coral-xyz.github.io/anchor/cli/commands.html) |
2021-05-10 13:12:20 -07:00
2021-01-04 21:18:08 -08:00
## Note
2020-12-31 15:48:06 -08:00
- **Anchor is in active development, so all APIs are subject to change.**
- **This code is unaudited. Use at your own risk.**
2020-12-31 15:48:06 -08:00
2021-01-23 00:18:50 -08:00
## Examples
2021-07-11 14:07:19 -07:00
Here's a counter program, where only the designated `authority`
2021-01-23 00:18:50 -08:00
can increment the count.
2021-01-15 01:35:35 -08:00
```rust
use anchor_lang::prelude::*;
2020-12-31 15:48:06 -08:00
2021-09-07 22:57:02 -07:00
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
2020-12-31 15:48:06 -08:00
#[program]
2021-01-23 00:18:50 -08:00
mod counter {
2021-01-04 21:18:08 -08:00
use super::*;
pub fn initialize(ctx: Context<Initialize>, start: u64) -> Result<()> {
2021-01-23 00:18:50 -08:00
let counter = &mut ctx.accounts.counter;
2021-09-04 06:13:41 -07:00
counter.authority = *ctx.accounts.authority.key;
counter.count = start;
2021-01-04 21:18:08 -08:00
Ok(())
}
pub fn increment(ctx: Context<Increment>) -> Result<()> {
2021-01-23 00:18:50 -08:00
let counter = &mut ctx.accounts.counter;
2021-07-16 10:35:01 -07:00
counter.count += 1;
2021-01-09 22:31:40 -08:00
Ok(())
2020-12-31 15:48:06 -08:00
}
}
2020-12-31 16:14:35 -08:00
#[derive(Accounts)]
2020-12-31 15:48:06 -08:00
pub struct Initialize<'info> {
2021-09-04 06:13:41 -07:00
#[account(init, payer = authority, space = 48)]
2021-09-07 22:57:02 -07:00
pub counter: Account<'info, Counter>,
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
2021-01-04 21:18:08 -08:00
}
#[derive(Accounts)]
2021-01-23 00:18:50 -08:00
pub struct Increment<'info> {
2021-01-20 17:38:23 -08:00
#[account(mut, has_one = authority)]
2021-09-07 22:57:02 -07:00
pub counter: Account<'info, Counter>,
pub authority: Signer<'info>,
2020-12-31 15:48:06 -08:00
}
2021-01-09 22:31:40 -08:00
#[account]
2021-01-23 00:18:50 -08:00
pub struct Counter {
2021-01-09 22:31:40 -08:00
pub authority: Pubkey,
2021-01-23 00:18:50 -08:00
pub count: u64,
2020-12-31 15:48:06 -08:00
}
```
For more, see the [examples](https://github.com/coral-xyz/anchor/tree/master/examples)
and [tests](https://github.com/coral-xyz/anchor/tree/master/tests) directories.
2021-01-23 17:38:23 -08:00
2022-02-17 17:07:32 -08:00
## License
Anchor is licensed under [Apache 2.0](./LICENSE).
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.
2021-04-07 10:11:26 -07:00
## Contribution
Thank you for your interest in contributing to Anchor!
Please see the [CONTRIBUTING.md](./CONTRIBUTING.md) to learn how.
2021-04-07 10:11:26 -07:00
2022-02-17 17:07:32 -08:00
### Thanks ❤️
2021-02-22 08:21:21 -08:00
2022-02-17 17:07:32 -08:00
<div align="center">
<a href="https://github.com/coral-xyz/anchor/graphs/contributors">
<img src="https://contrib.rocks/image?repo=coral-xyz/anchor" width="100%" />
2022-02-17 17:07:32 -08:00
</a>
</div>