Skip to content
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

Implement StoredMap for EvmSystem #64

Merged
merged 1 commit into from
May 18, 2023
Merged
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
25 changes: 24 additions & 1 deletion frame/evm-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

use sp_runtime::{traits::One, RuntimeDebug, DispatchResult};
use frame_support::traits::StoredMap;
use sp_runtime::{traits::One, RuntimeDebug, DispatchResult, DispatchError};
use scale_codec::{Encode, Decode, MaxEncodedLen, FullCodec};
use scale_info::TypeInfo;

Expand Down Expand Up @@ -172,6 +173,28 @@ impl<T: Config> Pallet<T> {
}
}

impl<T: Config> StoredMap<<T as Config>::AccountId, <T as Config>::AccountData> for Pallet<T> {
fn get(k: &<T as Config>::AccountId) -> <T as Config>::AccountData {
FullAccount::<T>::get(k).data
}

fn try_mutate_exists<R, E: From<DispatchError>>(
k: &<T as Config>::AccountId,
f: impl FnOnce(&mut Option<<T as Config>::AccountData>) -> Result<R, E>,
) -> Result<R, E> {
let mut maybe_account_data = if Self::account_exists(k) {
Some(FullAccount::<T>::get(k).data)
} else {
None
};
let r = f(&mut maybe_account_data)?;
if let Some(account_data) = maybe_account_data {
FullAccount::<T>::mutate(k, |a| a.data = account_data);
}
Ok(r)
}
}

/// Interface to handle account creation.
pub trait OnNewAccount<AccountId> {
/// A new account `who` has been registered.
Expand Down