Browse Source

examples: Add basic-4

Armani Ferrante 4 years ago
parent
commit
7bc07d292f

+ 1 - 0
docs/src/.vuepress/config.js

@@ -54,6 +54,7 @@ module.exports = {
           "/tutorials/tutorial-1",
           "/tutorials/tutorial-1",
           "/tutorials/tutorial-2",
           "/tutorials/tutorial-2",
           "/tutorials/tutorial-3",
           "/tutorials/tutorial-3",
+          "/tutorials/tutorial-4",
         ],
         ],
       },
       },
     ],
     ],

+ 1 - 1
docs/src/tutorials/tutorial-3.md

@@ -11,7 +11,7 @@ To get started, clone the repo.
 git clone https://github.com/project-serum/anchor
 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
 ```bash
 cd anchor/examples/tutorial/basic-3
 cd anchor/examples/tutorial/basic-3

+ 32 - 0
docs/src/tutorials/tutorial-4.md

@@ -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.

+ 2 - 0
examples/tutorial/basic-4/Anchor.toml

@@ -0,0 +1,2 @@
+cluster = "localnet"
+wallet = "~/.config/solana/id.json"

+ 4 - 0
examples/tutorial/basic-4/Cargo.toml

@@ -0,0 +1,4 @@
+[workspace]
+members = [
+    "programs/*"
+]

+ 16 - 0
examples/tutorial/basic-4/programs/basic-4/Cargo.toml

@@ -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"] }

+ 2 - 0
examples/tutorial/basic-4/programs/basic-4/Xargo.toml

@@ -0,0 +1,2 @@
+[target.bpfel-unknown-unknown.dependencies.std]
+features = []

+ 21 - 0
examples/tutorial/basic-4/programs/basic-4/src/lib.rs

@@ -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

+ 25 - 0
examples/tutorial/basic-4/tests/basic-4.js

@@ -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));
+  });
+});