diff --git a/basic-updates/base/rust-toolchain.toml b/basic-updates/base/rust-toolchain.toml index 1966b4e..24238bd 100644 --- a/basic-updates/base/rust-toolchain.toml +++ b/basic-updates/base/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "1.81.0" +channel = "1.86.0" components = ["rustfmt"] targets = ["wasm32-unknown-unknown"] diff --git a/basic-updates/base/src/lib.rs b/basic-updates/base/src/lib.rs index 4b2b50b..d407bf1 100644 --- a/basic-updates/base/src/lib.rs +++ b/basic-updates/base/src/lib.rs @@ -7,6 +7,9 @@ use near_sdk::{env, AccountId, NearToken}; const POINT_ONE: NearToken = NearToken::from_millinear(100); +const MESSAGES_PREFIX: &[u8] = b"m"; +const PAYMENTS_PREFIX: &[u8] = b"p"; + #[near(serializers=[json, borsh])] pub struct PostedMessage { pub premium: bool, @@ -23,8 +26,8 @@ pub struct GuestBook { impl Default for GuestBook { fn default() -> Self { Self { - messages: Vector::new(b"m"), - payments: Vector::new(b"p"), + messages: Vector::new(MESSAGES_PREFIX), + payments: Vector::new(PAYMENTS_PREFIX), } } } diff --git a/basic-updates/update/src/lib.rs b/basic-updates/update/src/lib.rs index b150340..67b8f1e 100644 --- a/basic-updates/update/src/lib.rs +++ b/basic-updates/update/src/lib.rs @@ -1,7 +1,7 @@ use near_sdk::near; use near_sdk::collections::Vector; -use near_sdk::json_types::{U64, U128}; +use near_sdk::json_types::{U128, U64}; use near_sdk::{env, AccountId, NearToken}; @@ -9,6 +9,8 @@ mod migrate; const POINT_ONE: NearToken = NearToken::from_millinear(100); +const MESSAGES_PREFIX: &[u8] = b"m"; + #[near(serializers=[json, borsh])] pub struct PostedMessage { pub payment: NearToken, @@ -25,7 +27,7 @@ pub struct GuestBook { impl Default for GuestBook { fn default() -> Self { Self { - messages: Vector::new(b"m"), + messages: Vector::new(MESSAGES_PREFIX), } } } diff --git a/basic-updates/update/src/migrate.rs b/basic-updates/update/src/migrate.rs index d734536..1005471 100644 --- a/basic-updates/update/src/migrate.rs +++ b/basic-updates/update/src/migrate.rs @@ -19,25 +19,30 @@ impl GuestBook { #[init(ignore_state)] pub fn migrate() -> Self { // retrieve the current state from the contract - let old_state: OldState = env::state_read().expect("failed"); + let mut old_state: OldState = env::state_read().expect("failed"); - // iterate through the state migrating it to the new version - let mut new_messages: Vector = Vector::new(b"p"); + // new messages vector to hold the migrated messages + let mut new_messages: Vector = Vector::new(MESSAGES_PREFIX); + // iterate through the state migrating it to the new version for (idx, posted) in old_state.messages.iter().enumerate() { - let payment = old_state - .payments - .get(idx as u64) - .unwrap_or(NearToken::from_near(0)); + // get the payment and remove it from the old state payments vector so it won't be left in the new state + let payment = old_state.payments.get(idx as u64) + .expect("failed to get payment") + .clone(); + // push the new message to the new messages vector new_messages.push(&PostedMessage { payment, premium: posted.premium, - sender: posted.sender, - text: posted.text, + sender: posted.sender.clone(), + text: posted.text.clone(), }) } + // remove the payments from the old state + old_state.payments.clear(); + // return the new state Self { messages: new_messages, diff --git a/basic-updates/update/tests/workspaces.rs b/basic-updates/update/tests/workspaces.rs index ec475c1..7bf173d 100644 --- a/basic-updates/update/tests/workspaces.rs +++ b/basic-updates/update/tests/workspaces.rs @@ -23,7 +23,13 @@ async fn base_contract() -> Common { fs::create_dir_all("../../target/near/base").unwrap(); let contract_wasm = near_workspaces::compile_project("../base").await.unwrap(); let root = sandbox.root_account().unwrap(); - let guest_book_account = root.create_subaccount("gbook").initial_balance(FIVE_NEAR).transact().await.unwrap().unwrap(); + let guest_book_account = root + .create_subaccount("gbook") + .initial_balance(FIVE_NEAR) + .transact() + .await + .unwrap() + .unwrap(); let contract = guest_book_account .deploy(&contract_wasm) @@ -32,7 +38,13 @@ async fn base_contract() -> Common { .into_result() .unwrap(); - let alice = root.create_subaccount("alice").initial_balance(FIVE_NEAR).transact().await.unwrap().unwrap(); + let alice = root + .create_subaccount("alice") + .initial_balance(FIVE_NEAR) + .transact() + .await + .unwrap() + .unwrap(); let guest_book_message_outcome = guest_book_account .call(contract.id(), "add_message")