Skip to content

Commit a1a326d

Browse files
authored
Merge pull request #2 from nearuaguild/add_advanced_example
feat: add advanced example of contract state migration
2 parents b03e75c + 1b34407 commit a1a326d

File tree

24 files changed

+1040
-20
lines changed

24 files changed

+1040
-20
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
uses: actions-rs/toolchain@v1
2323
with:
2424
profile: minimal
25-
toolchain: stable
25+
toolchain: 1.81.0
2626
default: true
2727
target: wasm32-unknown-unknown
2828

Cargo.lock

Lines changed: 61 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ members = [
1414
"enum-updates/update",
1515
"self-updates/base",
1616
"self-updates/update",
17-
]
17+
"advanced-multi-version-updates/v1",
18+
"advanced-multi-version-updates/v2",
19+
"advanced-multi-version-updates/v3",
20+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "advanced-v1"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
[dependencies]
11+
near-sdk = "5.6"
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Guest Book Contract
2+
3+
The smart contract stores messages, keeping track of how much money was deposited when adding the message.
4+
5+
```rust
6+
#[payable]
7+
pub fn add_message(&mut self, text: String) {
8+
let payment = env::attached_deposit();
9+
let premium = payment >= POINT_ONE;
10+
let sender = env::predecessor_account_id();
11+
12+
let message = PostedMessage {
13+
premium,
14+
sender,
15+
text,
16+
};
17+
self.messages.push(message);
18+
self.payments.push(payment);
19+
}
20+
```
21+
22+
<br />
23+
24+
# Quickstart
25+
26+
## 1. Build and Deploy the Contract
27+
28+
Install [`cargo-near`](https://github.com/near/cargo-near) and run:
29+
30+
```bash
31+
# from repo root
32+
cd advanced-multi-version-updates/v1
33+
cargo near build --no-docker
34+
```
35+
36+
Build and deploy:
37+
38+
```bash
39+
# `update-migrate-rust-advanced-multiversion-updates.testnet` was used as example of <target-account-id>
40+
cargo near deploy --no-docker <target-account-id> without-init-call network-config testnet sign-with-keychain send
41+
```
42+
43+
## 2. How to interact?
44+
45+
_In this example we will be using [NEAR CLI](https://github.com/near/near-cli)
46+
to intract with the NEAR blockchain and the smart contract and [near-cli-rs](https://near.cli.rs)
47+
which provides more control over interactions and has interactive menus for subcommands selection_
48+
49+
### 1. Add a Message
50+
51+
```bash
52+
# NEAR CLI
53+
near call <target-account-id> add_message '{"text": "a message"}' --amount 0.1 --accountId <account>
54+
# near-cli-rs
55+
near contract call-function as-transaction <target-account-id> add_message json-args '{"text": "a message"}' prepaid-gas '100.0 Tgas' attached-deposit '0.1 NEAR' sign-as <account> network-config testnet sign-with-keychain send
56+
```
57+
58+
<br />
59+
60+
### 2. Retrieve the Stored Messages & Payments
61+
62+
`get_messages` and `get_payments` are read-only method (`view` method)
63+
64+
```bash
65+
# NEAR CLI
66+
near view <target-account-id> get_messages
67+
# near-cli-rs
68+
near contract call-function as-read-only <target-account-id> get_messages json-args {} network-config testnet now
69+
# NEAR CLI
70+
near view <target-account-id> get_payments
71+
# near-cli-rs
72+
near contract call-function as-read-only <target-account-id> get_payments json-args {} network-config testnet now
73+
```
74+
75+
<br />
76+
77+
### 3. Continue in the V2 Folder
78+
79+
Navigate to the [v2](../v2/) folder to continue
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[toolchain]
2+
channel = "1.81.0"
3+
components = ["rustfmt"]
4+
targets = ["wasm32-unknown-unknown"]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use near_sdk::{near, BorshStorageKey};
2+
3+
use near_sdk::borsh::BorshSerialize;
4+
use near_sdk::json_types::{U128, U64};
5+
use near_sdk::store::Vector;
6+
7+
use near_sdk::{env, AccountId, NearToken};
8+
9+
const POINT_ONE: NearToken = NearToken::from_millinear(100);
10+
11+
#[derive(BorshSerialize, BorshStorageKey)]
12+
#[borsh(crate = "near_sdk::borsh")]
13+
pub enum StorageKey {
14+
Messages,
15+
Payments,
16+
}
17+
18+
#[near(serializers=[json, borsh])]
19+
pub struct PostedMessage {
20+
pub premium: bool,
21+
pub sender: AccountId,
22+
pub text: String,
23+
}
24+
25+
#[near(contract_state)]
26+
pub struct GuestBook {
27+
messages: Vector<PostedMessage>,
28+
payments: Vector<NearToken>,
29+
}
30+
31+
impl Default for GuestBook {
32+
fn default() -> Self {
33+
Self {
34+
messages: Vector::new(StorageKey::Messages),
35+
payments: Vector::new(StorageKey::Payments),
36+
}
37+
}
38+
}
39+
40+
#[near]
41+
impl GuestBook {
42+
#[payable]
43+
pub fn add_message(&mut self, text: String) {
44+
let payment = env::attached_deposit();
45+
let premium = payment >= POINT_ONE;
46+
let sender = env::predecessor_account_id();
47+
48+
let message = PostedMessage {
49+
premium,
50+
sender,
51+
text,
52+
};
53+
self.messages.push(message);
54+
self.payments.push(payment);
55+
}
56+
57+
pub fn get_messages(
58+
&self,
59+
from_index: Option<U128>,
60+
limit: Option<U64>,
61+
) -> Vec<&PostedMessage> {
62+
let from = u128::from(from_index.unwrap_or(U128(0)));
63+
64+
self.messages
65+
.iter()
66+
.skip(from as usize)
67+
.take(u64::from(limit.unwrap_or(U64::from(10))) as usize)
68+
.collect()
69+
}
70+
71+
pub fn get_payments(&self, from_index: Option<U128>, limit: Option<U64>) -> Vec<U128> {
72+
let from = u128::from(from_index.unwrap_or(U128(0)));
73+
74+
self.payments
75+
.iter()
76+
.skip(from as usize)
77+
.take(u64::from(limit.unwrap_or(U64::from(10))) as usize)
78+
.map(|x| U128(x.as_yoctonear()))
79+
.collect()
80+
}
81+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "advanced-v2"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
[dependencies]
11+
near-sdk = "5.6"

0 commit comments

Comments
 (0)