1.实现Instance类型
instantiable pallets必须调用decl_storage!宏才能Instance创建类型。
Instance在 Config trait、Event 类型和 Store trait 中添加一个泛型类型。 下面的代码片段显示了pallet的Config和Store trait所需的修改:
pub trait Config<I: Instance>: frame_system::Config {
type Event: From<Event> + Into<::Event>;
}
/// Include the I: Instance type parameter in storage declaration
decl_storage! {
trait Store for Module<T: Config, I: Instance> as MintToken {
/ --snip-- /
配置你的运行时
包括mint_tokenin 的两个实例runtime/src/lib.rs:
/ --snip-- /
// Instance1 of mint_token
impl mint_token::Config<mint_token::Instance1> for Runtime {
type Event = Event;
}
// Instance2 of mint_token
impl mint_token::Config<mint_token::Instance2> for Runtime {
type Event = Event;
}
/ --snip-- /
MintToken1: mint_token::::{Pallet, Call, Storage, Event},
MintToken2: mint_token::::{Pallet, Call, Storage, Event},
/ --snip-- /
示例-波卡的运行时
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see http://www.gnu.org/licenses/.
//! The Polkadot runtime. This can be compiled with #[no_std]
, ready for Wasm.
![cfg_attr(not(feature = "std"), no_std)]
// construct_runtime!
does a lot of recursion and requires us to increase the limit to 256.
![recursion_limit = "256"]
use pallet_transaction_payment::CurrencyAdapter;
use runtime_common::{
auctions, claims, crowdloan, impl_runtime_weights, impls::DealWithFees, paras_registrar,
prod_or_fast, slots, BlockHashCount, BlockLength, CurrencyToVote, SlowAdjustingFeeUpdate,
};
use runtime_parachains::{
configuration as parachains_configuration, disputes as parachains_disputes,
dmp as parachains_dmp, hrmp as parachains_hrmp, inclusion as parachains_inclusion,
initializer as parachains_initializer, origin as parachains_origin, paras as parachains_paras,
paras_inherent as parachains_paras_inherent, reward_points as parachains_reward_points,
runtime_api_impl::v2 as parachains_runtime_api_impl, scheduler as parachains_scheduler,
session_info as parachains_session_info, shared as parachains_shared, ump as parachains_ump,
};
use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId;
use beefy_primitives::crypto::AuthorityId as BeefyId;
use frame_election_provider_support::{generate_solution_type, onchain, SequentialPhragmen};
use frame_support::{
construct_runtime, parameter_types,
traits::{
Contains, EitherOfDiverse, InstanceFilter, KeyOwnerProofSystem, LockIdentifier,
PrivilegeCmp,
},
weights::ConstantMultiplier,
PalletId, RuntimeDebug,
};
use frame_system::EnsureRoot;
use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId};
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use pallet_session::historical as session_historical;
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use primitives::v2::{
AccountId, AccountIndex, Balance, BlockNumber, CandidateEvent, CandidateHash,
CommittedCandidateReceipt, CoreState, DisputeState, GroupRotationInfo, Hash, Id as ParaId,
InboundDownwardMessage, InboundHrmpMessage, Moment, Nonce, OccupiedCoreAssumption,
PersistedValidationData, ScrapedOnChainVotes, SessionInfo, Signature, ValidationCode,
ValidationCodeHash, ValidatorId, ValidatorIndex,
};
use sp_core::OpaqueMetadata;
use sp_mmr_primitives as mmr;
use sp_runtime::{
create_runtime_str,
curve::PiecewiseLinear,
generic, impl_opaque_keys,
traits::{
AccountIdLookup, BlakeTwo256, Block as BlockT, ConvertInto, Extrinsic as ExtrinsicT,
OpaqueKeys, SaturatedConversion, Verify,
},
transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity},
ApplyExtrinsicResult, KeyTypeId, Perbill, Percent, Permill,
};
use sp_staking::SessionIndex;
use sp_std::{cmp::Ordering, collections::btree_map::BTreeMap, prelude::*};
[cfg(any(feature = "std", test))]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use static_assertions::const_assert;
pub use frame_system::Call as SystemCall;
pub use pallet_balances::Call as BalancesCall;
pub use pallet_election_provider_multi_phase::Call as EPMCall;
[cfg(feature = "std")]
pub use pallet_staking::StakerStatus;
pub use pallet_timestamp::Call as TimestampCall;
[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;