Skip to content

Commit c0f95a5

Browse files
authored
Merge pull request #5 from near-examples/remove-old-keys
Add removing old keys
2 parents 20f8427 + 51f94ba commit c0f95a5

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[toolchain]
2-
channel = "1.81.0"
2+
channel = "1.86.0"
33
components = ["rustfmt"]
44
targets = ["wasm32-unknown-unknown"]

basic-updates/base/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use near_sdk::{env, AccountId, NearToken};
77

88
const POINT_ONE: NearToken = NearToken::from_millinear(100);
99

10+
const MESSAGES_PREFIX: &[u8] = b"m";
11+
const PAYMENTS_PREFIX: &[u8] = b"p";
12+
1013
#[near(serializers=[json, borsh])]
1114
pub struct PostedMessage {
1215
pub premium: bool,
@@ -23,8 +26,8 @@ pub struct GuestBook {
2326
impl Default for GuestBook {
2427
fn default() -> Self {
2528
Self {
26-
messages: Vector::new(b"m"),
27-
payments: Vector::new(b"p"),
29+
messages: Vector::new(MESSAGES_PREFIX),
30+
payments: Vector::new(PAYMENTS_PREFIX),
2831
}
2932
}
3033
}

basic-updates/update/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
use near_sdk::near;
22

33
use near_sdk::collections::Vector;
4-
use near_sdk::json_types::{U64, U128};
4+
use near_sdk::json_types::{U128, U64};
55

66
use near_sdk::{env, AccountId, NearToken};
77

88
mod migrate;
99

1010
const POINT_ONE: NearToken = NearToken::from_millinear(100);
1111

12+
const MESSAGES_PREFIX: &[u8] = b"m";
13+
1214
#[near(serializers=[json, borsh])]
1315
pub struct PostedMessage {
1416
pub payment: NearToken,
@@ -25,7 +27,7 @@ pub struct GuestBook {
2527
impl Default for GuestBook {
2628
fn default() -> Self {
2729
Self {
28-
messages: Vector::new(b"m"),
30+
messages: Vector::new(MESSAGES_PREFIX),
2931
}
3032
}
3133
}

basic-updates/update/src/migrate.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,30 @@ impl GuestBook {
1919
#[init(ignore_state)]
2020
pub fn migrate() -> Self {
2121
// retrieve the current state from the contract
22-
let old_state: OldState = env::state_read().expect("failed");
22+
let mut old_state: OldState = env::state_read().expect("failed");
2323

24-
// iterate through the state migrating it to the new version
25-
let mut new_messages: Vector<PostedMessage> = Vector::new(b"p");
24+
// new messages vector to hold the migrated messages
25+
let mut new_messages: Vector<PostedMessage> = Vector::new(MESSAGES_PREFIX);
2626

27+
// iterate through the state migrating it to the new version
2728
for (idx, posted) in old_state.messages.iter().enumerate() {
28-
let payment = old_state
29-
.payments
30-
.get(idx as u64)
31-
.unwrap_or(NearToken::from_near(0));
29+
// get the payment and remove it from the old state payments vector so it won't be left in the new state
30+
let payment = old_state.payments.get(idx as u64)
31+
.expect("failed to get payment")
32+
.clone();
3233

34+
// push the new message to the new messages vector
3335
new_messages.push(&PostedMessage {
3436
payment,
3537
premium: posted.premium,
36-
sender: posted.sender,
37-
text: posted.text,
38+
sender: posted.sender.clone(),
39+
text: posted.text.clone(),
3840
})
3941
}
4042

43+
// remove the payments from the old state
44+
old_state.payments.clear();
45+
4146
// return the new state
4247
Self {
4348
messages: new_messages,

basic-updates/update/tests/workspaces.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ async fn base_contract() -> Common {
2323
fs::create_dir_all("../../target/near/base").unwrap();
2424
let contract_wasm = near_workspaces::compile_project("../base").await.unwrap();
2525
let root = sandbox.root_account().unwrap();
26-
let guest_book_account = root.create_subaccount("gbook").initial_balance(FIVE_NEAR).transact().await.unwrap().unwrap();
26+
let guest_book_account = root
27+
.create_subaccount("gbook")
28+
.initial_balance(FIVE_NEAR)
29+
.transact()
30+
.await
31+
.unwrap()
32+
.unwrap();
2733

2834
let contract = guest_book_account
2935
.deploy(&contract_wasm)
@@ -32,7 +38,13 @@ async fn base_contract() -> Common {
3238
.into_result()
3339
.unwrap();
3440

35-
let alice = root.create_subaccount("alice").initial_balance(FIVE_NEAR).transact().await.unwrap().unwrap();
41+
let alice = root
42+
.create_subaccount("alice")
43+
.initial_balance(FIVE_NEAR)
44+
.transact()
45+
.await
46+
.unwrap()
47+
.unwrap();
3648

3749
let guest_book_message_outcome = guest_book_account
3850
.call(contract.id(), "add_message")

0 commit comments

Comments
 (0)