Skip to content

Commit 77b1fc9

Browse files
author
dj8yf0μl
committed
chore: update root README
1 parent 42709fa commit 77b1fc9

File tree

1 file changed

+84
-66
lines changed

1 file changed

+84
-66
lines changed

README.md

+84-66
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,37 @@ The examples at [./contracts/basic-updates](./contracts/basic-updates) show how
1616
between contract updates.
1717

1818
It is composed by 2 contracts:
19-
1. Base: A Guest Book were people can write messages.
19+
1. Base: A Guest Book where people can write messages.
2020
2. Update: An update in which we remove a parameter and change the internal structure.
2121

2222
```rust
2323
#[private]
2424
#[init(ignore_state)]
2525
pub fn migrate() -> Self {
26-
let old_state: OldState = env::state_read().expect("failed");
27-
let mut new_messages: Vector<PostedMessage> = Vector::new(b"p");
28-
29-
// iterate through the messages of the previous state
30-
for (idx, posted) in old_state.messages.iter().enumerate() {
31-
// get the payment using the message index
32-
let payment = old_state.payments.get(idx as u64).unwrap_or(0);
33-
34-
// Create a PostedMessage with the new format and push it
35-
new_messages.push(&PostedMessage {
36-
payment,
37-
premium: posted.premium,
38-
sender: posted.sender,
39-
text: posted.text,
40-
})
41-
}
42-
43-
// return the new state
44-
Self {
45-
messages: new_messages,
46-
}
26+
// retrieve the current state from the contract
27+
let old_state: OldState = env::state_read().expect("failed");
28+
29+
// iterate through the state migrating it to the new version
30+
let mut new_messages: Vector<PostedMessage> = Vector::new(b"p");
31+
32+
for (idx, posted) in old_state.messages.iter().enumerate() {
33+
let payment = old_state
34+
.payments
35+
.get(idx as u64)
36+
.unwrap_or(NearToken::from_near(0));
37+
38+
new_messages.push(&PostedMessage {
39+
payment,
40+
premium: posted.premium,
41+
sender: posted.sender,
42+
text: posted.text,
43+
})
44+
}
45+
46+
// return the new state
47+
Self {
48+
messages: new_messages,
49+
}
4750
}
4851
```
4952

@@ -64,22 +67,22 @@ The example is composed by 2 contracts:
6467
#[derive(BorshSerialize, BorshDeserialize)]
6568
#[borsh(crate = "near_sdk::borsh")]
6669
pub enum VersionedPostedMessage {
67-
V1(PostedMessageV1),
68-
V2(PostedMessageV2),
70+
V1(PostedMessageV1),
71+
V2(PostedMessageV2),
6972
}
7073

7174
impl From<VersionedPostedMessage> for PostedMessageV2 {
72-
fn from(message: VersionedPostedMessage) -> Self {
73-
match message {
74-
VersionedPostedMessage::V2(posted) => posted,
75-
VersionedPostedMessage::V1(posted) => PostedMessageV2 {
76-
payment: if posted.premium { POINT_ONE } else { 0 },
77-
premium: posted.premium,
78-
sender: posted.sender,
79-
text: posted.text,
80-
},
75+
fn from(message: VersionedPostedMessage) -> Self {
76+
match message {
77+
VersionedPostedMessage::V2(posted) => posted,
78+
VersionedPostedMessage::V1(posted) => PostedMessageV2 {
79+
payment: NearToken::from_near(0),
80+
premium: posted.premium,
81+
sender: posted.sender,
82+
text: posted.text,
83+
},
84+
}
8185
}
82-
}
8386
}
8487
```
8588

@@ -95,26 +98,26 @@ It is composed by 2 contracts:
9598

9699
```rust
97100
pub fn update_contract(&self) -> Promise {
98-
// Check the caller is authorized to update the code
99-
assert!(
100-
env::predecessor_account_id() == self.manager,
101-
"Only the manager can update the code"
102-
);
103-
104-
// Receive the code directly from the input to avoid the
105-
// GAS overhead of deserializing parameters
106-
let code = env::input().expect("Error: No input").to_vec();
107-
108-
// Deploy the contract on self
109-
Promise::new(env::current_account_id())
110-
.deploy_contract(code)
111-
.function_call(
112-
"migrate".to_string(),
113-
NO_ARGS,
114-
0,
115-
CALL_GAS
116-
)
117-
.as_return()
101+
// Check the caller is authorized to update the code
102+
assert!(
103+
env::predecessor_account_id() == self.manager,
104+
"Only the manager can update the code"
105+
);
106+
107+
// Receive the code directly from the input to avoid the
108+
// GAS overhead of deserializing parameters
109+
let code = env::input().expect("Error: No input").to_vec();
110+
111+
// Deploy the contract on self
112+
Promise::new(env::current_account_id())
113+
.deploy_contract(code)
114+
.function_call(
115+
"migrate".to_string(),
116+
NO_ARGS,
117+
NearToken::from_near(0),
118+
CALL_GAS,
119+
)
120+
.as_return()
118121
}
119122
```
120123

@@ -123,31 +126,46 @@ pub fn update_contract(&self) -> Promise {
123126

124127
# Quickstart
125128

126-
Clone this repository locally or [**open it in gitpod**](https://gitpod.io/#/github.com/near-examples/multiple-cross-contract-calls). Then follow these steps:
129+
Clone this repository locally. Then follow these steps:
127130

128-
### 0. Accounts for deploying contracts from these examples can be created by:
131+
132+
### 0. Test the Contract
133+
Deploy your contract in a sandbox and simulate interactions from users.
129134

130135
```bash
131-
# NEAR CLI
132-
near create-account <target-account-id> --useFaucet
133-
# near-cli-rs
134-
near account create-account sponsor-by-faucet-service <target-account-id> autogenerate-new-keypair save-to-keychain network-config testnet create
136+
cd contracts
137+
cargo test --workspace
135138
```
136139

137-
### 1. Install Dependencies
140+
### 1. Examples' cli-s versions
141+
142+
Commands in each contract's `README` are valid for following versions of programs.
143+
138144
```bash
139-
npm install
145+
# NEAR CLI
146+
❯ near --version
147+
4.0.10
148+
149+
# near-cli-rs
150+
❯ near --version
151+
near-cli-rs 0.8.1
152+
153+
❯ cargo near --version
154+
cargo-near-near 0.6.1
140155
```
141156

142-
### 2. Test the Contract
143-
Deploy your contract in a sandbox and simulate interactions from users.
157+
### 2. Accounts for deploying contracts from these examples can be created by:
144158

145159
```bash
146-
npm test
160+
# NEAR CLI
161+
near create-account <target-account-id> --useFaucet
162+
# near-cli-rs
163+
near account create-account sponsor-by-faucet-service <target-account-id> autogenerate-new-keypair save-to-keychain network-config testnet create
147164
```
148165

166+
149167
---
150168

151169
# Learn More
152-
1. Learn more on each contract's [README](./contract/README.md).
170+
1. Learn more on each contract's `README`.
153171
2. Check [**our documentation**](https://docs.near.org/develop/welcome).

0 commit comments

Comments
 (0)