examples: Add basic-4
This commit is contained in:
parent
178807102d
commit
7bc07d292f
|
@ -54,6 +54,7 @@ module.exports = {
|
|||
"/tutorials/tutorial-1",
|
||||
"/tutorials/tutorial-2",
|
||||
"/tutorials/tutorial-3",
|
||||
"/tutorials/tutorial-4",
|
||||
],
|
||||
},
|
||||
],
|
||||
|
|
|
@ -11,7 +11,7 @@ To get started, clone the repo.
|
|||
git clone https://github.com/project-serum/anchor
|
||||
```
|
||||
|
||||
And change directories to the [example](https://github.com/project-serum/anchor/tree/master/examples/tutorial/basic-2).
|
||||
And change directories to the [example](https://github.com/project-serum/anchor/tree/master/examples/tutorial/basic-3).
|
||||
|
||||
```bash
|
||||
cd anchor/examples/tutorial/basic-3
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
# Tutorial 4: State structs
|
||||
|
||||
Up until now, we've treated programs on Solana as stateless, using accounts to persist
|
||||
state between instruction invocations. In this tutorial, we'll give Solana programs the
|
||||
illusion of state by introducing state structs, which define program account
|
||||
singletons that can be operated over like any other account.
|
||||
|
||||
## Clone the Repo
|
||||
|
||||
To get started, clone the repo.
|
||||
|
||||
```bash
|
||||
git clone https://github.com/project-serum/anchor
|
||||
```
|
||||
|
||||
And change directories to the [example](https://github.com/project-serum/anchor/tree/master/examples/tutorial/basic-4).
|
||||
|
||||
```bash
|
||||
cd anchor/examples/tutorial/basic-4
|
||||
```
|
||||
|
||||
## Defining a Program
|
||||
|
||||
<<< @/../examples/tutorial/basic-4/programs/basic-4/src/lib.rs#code
|
||||
|
||||
TODO: explain + add instructions (both manual instructions and instructions inside the impl block).
|
||||
|
||||
## Using the client
|
||||
|
||||
<<< @/../examples/tutorial/basic-4/tests/basic-4.js#code
|
||||
|
||||
TODO explain.
|
|
@ -0,0 +1,2 @@
|
|||
cluster = "localnet"
|
||||
wallet = "~/.config/solana/id.json"
|
|
@ -0,0 +1,4 @@
|
|||
[workspace]
|
||||
members = [
|
||||
"programs/*"
|
||||
]
|
|
@ -0,0 +1,16 @@
|
|||
[package]
|
||||
name = "basic-4"
|
||||
version = "0.1.0"
|
||||
description = "Created with Anchor"
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "lib"]
|
||||
name = "basic_4"
|
||||
|
||||
[features]
|
||||
no-entrypoint = []
|
||||
cpi = ["no-entrypoint"]
|
||||
|
||||
[dependencies]
|
||||
anchor-lang = { git = "https://github.com/project-serum/anchor", features = ["derive"] }
|
|
@ -0,0 +1,2 @@
|
|||
[target.bpfel-unknown-unknown.dependencies.std]
|
||||
features = []
|
|
@ -0,0 +1,21 @@
|
|||
#![feature(proc_macro_hygiene)]
|
||||
|
||||
// #region code
|
||||
use anchor_lang::prelude::*;
|
||||
|
||||
#[program]
|
||||
mod basic_4 {
|
||||
use super::*;
|
||||
|
||||
#[state]
|
||||
pub struct MyProgram {
|
||||
pub data: u64,
|
||||
}
|
||||
|
||||
impl MyProgram {
|
||||
pub fn new(data: u64) -> Result<Self, ProgramError> {
|
||||
Ok(Self { data })
|
||||
}
|
||||
}
|
||||
}
|
||||
// #endregion code
|
|
@ -0,0 +1,25 @@
|
|||
const assert = require('assert');
|
||||
const anchor = require('@project-serum/anchor');
|
||||
|
||||
describe('basic-4', () => {
|
||||
|
||||
// Configure the client to use the local cluster.
|
||||
anchor.setProvider(anchor.Provider.local());
|
||||
|
||||
it('Is initialized!', async () => {
|
||||
const program = anchor.workspace.Basic4;
|
||||
|
||||
// #region code
|
||||
// The data to set on the state struct.
|
||||
const data = new anchor.BN(1234);
|
||||
|
||||
// Initialize the program's state struct.
|
||||
await program.state.rpc.new(data);
|
||||
|
||||
// Fetch the state struct from the network.
|
||||
const state = await program.state();
|
||||
// #endregion code
|
||||
|
||||
assert.ok(state.data.eq(data));
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue