Skip to content

Add removing old keys #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion basic-updates/base/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.81.0"
channel = "1.86.0"
components = ["rustfmt"]
targets = ["wasm32-unknown-unknown"]
7 changes: 5 additions & 2 deletions basic-updates/base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions basic-updates/update/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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};

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,
Expand All @@ -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),
}
}
}
Expand Down
23 changes: 14 additions & 9 deletions basic-updates/update/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostedMessage> = Vector::new(b"p");
// new messages vector to hold the migrated messages
let mut new_messages: Vector<PostedMessage> = 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,
Expand Down
16 changes: 14 additions & 2 deletions basic-updates/update/tests/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
Loading