2021-01-02 16:24:35 -08:00
|
|
|
# Tutorial 0: A Minimal Example
|
|
|
|
|
|
|
|
Here, we introduce a minimal example demonstrating the Anchor workflow and core syntax
|
|
|
|
elements. This tutorial assumes all [prerequisites](./prerequisites.md) are installed and
|
|
|
|
a local network is running.
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
## Clone the Repo
|
2021-01-02 16:24:35 -08:00
|
|
|
|
|
|
|
To get started, clone the repo.
|
|
|
|
|
|
|
|
```bash
|
2021-01-02 16:29:23 -08:00
|
|
|
git clone https://github.com/project-serum/anchor
|
2021-01-02 16:24:35 -08:00
|
|
|
```
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
And change directories to the [example](https://github.com/project-serum/anchor/tree/master/examples/tutorial/basic-0).
|
2021-01-02 16:24:35 -08:00
|
|
|
|
|
|
|
```bash
|
|
|
|
cd anchor/examples/tutorial/basic-0
|
|
|
|
```
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
## Defining a Program
|
2021-01-02 16:24:35 -08:00
|
|
|
|
|
|
|
We define the minimum viable program as follows.
|
|
|
|
|
|
|
|
<<< @/../examples/tutorial/basic-0/program/src/lib.rs
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
* `#[program]` First, notice that a program is defined with the `#[program]` attribute, where each
|
2021-01-02 16:24:35 -08:00
|
|
|
inner method defines an RPC request handler, or, in Solana parlance, an "instruction"
|
|
|
|
handler. These handlers are the entrypoints to your program that clients may invoke, as
|
|
|
|
we will see soon.
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
* `Context<Initialize>` The first parameter of _every_ RPC handler is the `Context` struct, which is a simple
|
2021-01-02 18:14:38 -08:00
|
|
|
container for the currently executing `program_id` generic over
|
2021-01-02 16:24:35 -08:00
|
|
|
`Accounts`--here, the `Initialize` struct.
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
* `#[derive(Accounts)]` The `Accounts` derive macro marks a struct containing all the accounts that must be
|
2021-01-02 16:24:35 -08:00
|
|
|
specified for a given instruction. To understand Accounts on Solana, see the
|
|
|
|
[docs](https://docs.solana.com/developing/programming-model/accounts).
|
|
|
|
In subsequent tutorials, we'll demonstrate how an `Accounts` struct can be used to
|
|
|
|
specify constraints on accounts given to your program. Since this example doesn't touch any
|
|
|
|
accounts, we skip this (important) detail.
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
## Building and Emitting an IDL
|
2021-01-02 16:24:35 -08:00
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
After creating a program, one can use the `anchor` CLI to build and emit an IDL, from which clients
|
|
|
|
can be generated.
|
2021-01-02 16:24:35 -08:00
|
|
|
|
|
|
|
```bash
|
2021-01-02 22:40:17 -08:00
|
|
|
anchor build
|
2021-01-02 16:24:35 -08:00
|
|
|
```
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
::: details
|
|
|
|
The `build` command is a convenience combining two steps.
|
2021-01-02 16:24:35 -08:00
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
1) `cargo build-bpf`
|
|
|
|
2) `anchor idl -f src/lib.rs -o basic.json`.
|
|
|
|
:::
|
2021-01-02 18:14:38 -08:00
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
Once run, you should see your build artifacts, as usual, in your `target/` directory. Additionally,
|
|
|
|
a `basic.json` file is created. Inspecting its contents you should see
|
2021-01-02 16:24:35 -08:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"version": "0.0.0",
|
|
|
|
"name": "basic",
|
|
|
|
"instructions": [
|
|
|
|
{
|
|
|
|
"name": "initialize",
|
|
|
|
"accounts": [],
|
|
|
|
"args": []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
```
|
2021-01-02 18:14:38 -08:00
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
From which a client can be generated. Note that this file is created by parsing the `src/lib.rs`
|
|
|
|
file in your program's crate.
|
|
|
|
|
|
|
|
::: tip
|
|
|
|
If you've developed on Ethereum, the IDL is analogous to the `abi.json`.
|
|
|
|
:::
|
|
|
|
|
|
|
|
## Deploying a program
|
|
|
|
|
|
|
|
Once built, we can deploy the program using the `solana deploy` command.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
solana deploy <path-to-your-repo>/anchor/target/deploy/basic_program_0.so
|
|
|
|
```
|
|
|
|
|
|
|
|
Making sure to susbstitute paths to match your local filesystem. Now, save the address
|
|
|
|
the program was deployed with. It will be useful later.
|
2021-01-02 16:24:35 -08:00
|
|
|
|
|
|
|
## Generating a Client
|
|
|
|
|
|
|
|
Now that we have an IDL, we can use it to create a client.
|
|
|
|
|
|
|
|
<<< @/../examples/tutorial/basic-0/app/client.js#main
|
|
|
|
|
|
|
|
Notice how the program dynamically created the `initialize` method under
|
|
|
|
the `rpc` namespace.
|
|
|
|
|
|
|
|
## Next Steps
|
|
|
|
|
|
|
|
So far we've seen the basics of how to create, deploy, and make RPCs to a program on Solana
|
|
|
|
using Anchor. But a program isn't all that interesting without interacting with it's
|
|
|
|
peristent state, which is what we'll cover next.
|