pallet_evm_chain_id/
lib.rs

1// This file is part of Frontier.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! # EVM chain ID pallet
19//!
20//! The pallet that stores the numeric Ethereum-style chain id in the runtime.
21//! It can simplify setting up multiple networks with different chain ID by configuring the
22//! chain spec without requiring changes to the runtime config.
23//!
24//! **NOTE**: we recommend that the production chains still use the const parameter type, as
25//! this extra storage access would imply some performance penalty.
26
27// Ensure we're `no_std` when compiling for Wasm.
28#![cfg_attr(not(feature = "std"), no_std)]
29#![warn(unused_crate_dependencies)]
30
31pub use pallet::*;
32
33#[frame_support::pallet]
34pub mod pallet {
35	use frame_support::pallet_prelude::*;
36
37	const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);
38
39	#[pallet::pallet]
40	#[pallet::storage_version(STORAGE_VERSION)]
41	pub struct Pallet<T>(PhantomData<T>);
42
43	#[pallet::config]
44	pub trait Config: frame_system::Config {}
45
46	impl<T: Config> Get<u64> for Pallet<T> {
47		fn get() -> u64 {
48			<ChainId<T>>::get()
49		}
50	}
51
52	/// The EVM chain ID.
53	#[pallet::storage]
54	pub type ChainId<T> = StorageValue<_, u64, ValueQuery>;
55
56	#[pallet::genesis_config]
57	#[derive(frame_support::DefaultNoBound)]
58	pub struct GenesisConfig<T> {
59		pub chain_id: u64,
60		#[serde(skip)]
61		pub _marker: PhantomData<T>,
62	}
63
64	#[pallet::genesis_build]
65	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
66		fn build(&self) {
67			ChainId::<T>::put(self.chain_id);
68		}
69	}
70}