Skip to content

Commit

Permalink
Merge pull request #10 from xgreenx/feature/fix-flush-and-load
Browse files Browse the repository at this point in the history
Adapted `flush` and `load` for ink! 4.0
  • Loading branch information
xiyu1984 authored Sep 29, 2022
2 parents fddb9cb + 3ad1e7d commit c214658
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions contracts/callee/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,20 @@ mod callee {
/// So if you want to flush the correct state of the contract,
/// you have to this method on storage struct.
fn flush(&self) {
let root_key = ::ink_primitives::Key::from(12);
::ink_storage::traits::push_spread_root::<Self>(self, &root_key);
let root_key = <Self as ::ink::storage::traits::StorageKey>::KEY;
::ink::env::set_contract_storage::<::ink::primitives::Key, Self>(
&root_key,
self,
);
}

/// Method loads the current state of `Self` from storage.
/// ink! recursively calculate a key of each field.
/// So if you want to load the correct state of the contract,
/// you have to this method on storage struct.
fn load(&mut self) {
let root_key = ::ink_primitives::Key::from(12);
let mut state = ::ink_storage::traits::pull_spread_root::<Self>(&root_key);
let root_key = <Self as ::ink::storage::traits::StorageKey>::KEY;
let mut state = ::ink::env::get_contract_storage(&root_key).unwrap().unwrap();
core::mem::swap(self, &mut state);
let _ = core::mem::ManuallyDrop::new(state);
}
Expand Down

0 comments on commit c214658

Please sign in to comment.