pallet_contracts_mock_network/
parachain.rs1mod contracts_config;
20use crate::{
21 mocks::msg_queue::pallet as mock_msg_queue,
22 primitives::{AccountId, AssetIdForAssets, Balance},
23};
24use core::marker::PhantomData;
25use frame_support::{
26 construct_runtime, derive_impl, parameter_types,
27 traits::{
28 AsEnsureOriginWithArg, Contains, ContainsPair, Disabled, Everything, EverythingBut, Nothing,
29 },
30 weights::{
31 constants::{WEIGHT_PROOF_SIZE_PER_MB, WEIGHT_REF_TIME_PER_SECOND},
32 Weight,
33 },
34};
35use frame_system::{EnsureRoot, EnsureSigned};
36use pallet_xcm::XcmPassthrough;
37use sp_core::{ConstU32, ConstU64, H256};
38use sp_runtime::traits::{Get, IdentityLookup, MaybeEquivalence, TryConvertInto};
39
40use xcm::latest::prelude::*;
41use xcm_builder::{
42 AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowTopLevelPaidExecutionFrom,
43 ConvertedConcreteId, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds,
44 FrameTransactionalProcessor, FungibleAdapter, FungiblesAdapter, IsConcrete, NativeAsset,
45 NoChecking, ParentAsSuperuser, ParentIsPreset, SignedAccountId32AsNative, SignedToAccountId32,
46 SovereignSignedViaLocation, WithComputedOrigin,
47};
48use xcm_executor::{Config, XcmExecutor};
49
50pub type SovereignAccountOf =
51 (AccountId32Aliases<RelayNetwork, AccountId>, ParentIsPreset<AccountId>);
52
53#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
54impl frame_system::Config for Runtime {
55 type RuntimeOrigin = RuntimeOrigin;
56 type RuntimeCall = RuntimeCall;
57 type Nonce = u64;
58 type Block = Block;
59 type Hash = H256;
60 type Hashing = ::sp_runtime::traits::BlakeTwo256;
61 type AccountId = AccountId;
62 type Lookup = IdentityLookup<Self::AccountId>;
63 type RuntimeEvent = RuntimeEvent;
64 type BlockWeights = ();
65 type BlockLength = ();
66 type Version = ();
67 type PalletInfo = PalletInfo;
68 type AccountData = pallet_balances::AccountData<Balance>;
69 type OnNewAccount = ();
70 type OnKilledAccount = ();
71 type DbWeight = ();
72 type BaseCallFilter = Everything;
73 type SystemWeightInfo = ();
74 type SS58Prefix = ();
75 type OnSetCode = ();
76 type MaxConsumers = ConstU32<16>;
77}
78
79parameter_types! {
80 pub ExistentialDeposit: Balance = 1;
81 pub const MaxLocks: u32 = 50;
82 pub const MaxReserves: u32 = 50;
83}
84
85impl pallet_balances::Config for Runtime {
86 type AccountStore = System;
87 type Balance = Balance;
88 type DustRemoval = ();
89 type ExistentialDeposit = ExistentialDeposit;
90 type FreezeIdentifier = ();
91 type MaxFreezes = ConstU32<0>;
92 type MaxLocks = MaxLocks;
93 type MaxReserves = MaxReserves;
94 type ReserveIdentifier = [u8; 8];
95 type RuntimeEvent = RuntimeEvent;
96 type RuntimeHoldReason = RuntimeHoldReason;
97 type RuntimeFreezeReason = RuntimeFreezeReason;
98 type WeightInfo = ();
99 type DoneSlashHandler = ();
100}
101
102parameter_types! {
103 pub const AssetDeposit: u128 = 1_000_000;
104 pub const MetadataDepositBase: u128 = 1_000_000;
105 pub const MetadataDepositPerByte: u128 = 100_000;
106 pub const AssetAccountDeposit: u128 = 1_000_000;
107 pub const ApprovalDeposit: u128 = 1_000_000;
108 pub const AssetsStringLimit: u32 = 50;
109 pub const RemoveItemsLimit: u32 = 50;
110}
111
112impl pallet_assets::Config for Runtime {
113 type RuntimeEvent = RuntimeEvent;
114 type Balance = Balance;
115 type AssetId = AssetIdForAssets;
116 type ReserveData = ();
117 type Currency = Balances;
118 type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId>>;
119 type ForceOrigin = EnsureRoot<AccountId>;
120 type AssetDeposit = AssetDeposit;
121 type MetadataDepositBase = MetadataDepositBase;
122 type MetadataDepositPerByte = MetadataDepositPerByte;
123 type AssetAccountDeposit = AssetAccountDeposit;
124 type ApprovalDeposit = ApprovalDeposit;
125 type StringLimit = AssetsStringLimit;
126 type Holder = ();
127 type Freezer = ();
128 type Extra = ();
129 type WeightInfo = ();
130 type RemoveItemsLimit = RemoveItemsLimit;
131 type AssetIdParameter = AssetIdForAssets;
132 type CallbackHandle = ();
133 type AssetIdAllocator = ();
134 #[cfg(feature = "runtime-benchmarks")]
135 type BenchmarkHelper = ();
136}
137
138parameter_types! {
139 pub const ReservedXcmpWeight: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_div(4), 0);
140 pub const ReservedDmpWeight: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_div(4), 0);
141}
142
143parameter_types! {
144 pub const KsmLocation: Location = Location::parent();
145 pub const TokenLocation: Location = Here.into_location();
146 pub const RelayNetwork: NetworkId = ByGenesis([0; 32]);
147 pub UniversalLocation: InteriorLocation = [GlobalConsensus(RelayNetwork::get()), Parachain(MsgQueue::parachain_id().into())].into();
148}
149
150pub type XcmOriginToCallOrigin = (
151 SovereignSignedViaLocation<SovereignAccountOf, RuntimeOrigin>,
152 ParentAsSuperuser<RuntimeOrigin>,
153 SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
154 XcmPassthrough<RuntimeOrigin>,
155);
156
157parameter_types! {
158 pub const XcmInstructionWeight: Weight = Weight::from_parts(1_000, 1_000);
159 pub TokensPerSecondPerMegabyte: (AssetId, u128, u128) = (AssetId(Parent.into()), 1_000_000_000_000, 1024 * 1024);
160 pub const MaxInstructions: u32 = 100;
161 pub const MaxAssetsIntoHolding: u32 = 64;
162 pub ForeignPrefix: Location = (Parent,).into();
163 pub CheckingAccount: AccountId = PolkadotXcm::check_account();
164 pub TrustedLockPairs: (Location, AssetFilter) =
165 (Parent.into(), Wild(AllOf { id: AssetId(Parent.into()), fun: WildFungible }));
166}
167
168pub fn estimate_message_fee(number_of_instructions: u64) -> u128 {
169 let weight = estimate_weight(number_of_instructions);
170
171 estimate_fee_for_weight(weight)
172}
173
174pub fn estimate_weight(number_of_instructions: u64) -> Weight {
175 XcmInstructionWeight::get().saturating_mul(number_of_instructions)
176}
177
178pub fn estimate_fee_for_weight(weight: Weight) -> u128 {
179 let (_, units_per_second, units_per_mb) = TokensPerSecondPerMegabyte::get();
180
181 units_per_second * (weight.ref_time() as u128) / (WEIGHT_REF_TIME_PER_SECOND as u128) +
182 units_per_mb * (weight.proof_size() as u128) / (WEIGHT_PROOF_SIZE_PER_MB as u128)
183}
184
185pub type LocalBalancesTransactor =
186 FungibleAdapter<Balances, IsConcrete<TokenLocation>, SovereignAccountOf, AccountId, ()>;
187
188pub struct FromLocationToAsset<Location, AssetId>(PhantomData<(Location, AssetId)>);
189impl MaybeEquivalence<Location, AssetIdForAssets>
190 for FromLocationToAsset<Location, AssetIdForAssets>
191{
192 fn convert(value: &Location) -> Option<AssetIdForAssets> {
193 match value.unpack() {
194 (1, []) => Some(0 as AssetIdForAssets),
195 (1, [Parachain(para_id)]) => Some(*para_id as AssetIdForAssets),
196 _ => None,
197 }
198 }
199
200 fn convert_back(_id: &AssetIdForAssets) -> Option<Location> {
201 None
202 }
203}
204
205pub type ForeignAssetsTransactor = FungiblesAdapter<
206 Assets,
207 ConvertedConcreteId<
208 AssetIdForAssets,
209 Balance,
210 FromLocationToAsset<Location, AssetIdForAssets>,
211 TryConvertInto,
212 >,
213 SovereignAccountOf,
214 AccountId,
215 NoChecking,
216 CheckingAccount,
217>;
218
219pub type AssetTransactors = (LocalBalancesTransactor, ForeignAssetsTransactor);
221
222pub struct ParentRelay;
223impl Contains<Location> for ParentRelay {
224 fn contains(location: &Location) -> bool {
225 location.contains_parents_only(1)
226 }
227}
228pub struct ThisParachain;
229impl Contains<Location> for ThisParachain {
230 fn contains(location: &Location) -> bool {
231 matches!(location.unpack(), (0, [Junction::AccountId32 { .. }]))
232 }
233}
234
235pub type XcmRouter = crate::ParachainXcmRouter<MsgQueue>;
236
237pub type Barrier = (
238 xcm_builder::AllowUnpaidExecutionFrom<ThisParachain>,
239 WithComputedOrigin<
240 (AllowExplicitUnpaidExecutionFrom<ParentRelay>, AllowTopLevelPaidExecutionFrom<Everything>),
241 UniversalLocation,
242 ConstU32<1>,
243 >,
244);
245
246parameter_types! {
247 pub NftCollectionOne: AssetFilter
248 = Wild(AllOf { fun: WildNonFungible, id: AssetId((Parent, GeneralIndex(1)).into()) });
249 pub NftCollectionOneForRelay: (AssetFilter, Location)
250 = (NftCollectionOne::get(), Parent.into());
251 pub RelayNativeAsset: AssetFilter = Wild(AllOf { fun: WildFungible, id: AssetId((Parent, Here).into()) });
252 pub RelayNativeAssetForRelay: (AssetFilter, Location) = (RelayNativeAsset::get(), Parent.into());
253}
254pub type TrustedTeleporters =
255 (xcm_builder::Case<NftCollectionOneForRelay>, xcm_builder::Case<RelayNativeAssetForRelay>);
256pub type TrustedReserves = EverythingBut<xcm_builder::Case<NftCollectionOneForRelay>>;
257
258pub struct XcmConfig;
259impl Config for XcmConfig {
260 type RuntimeCall = RuntimeCall;
261 type XcmSender = XcmRouter;
262 type XcmEventEmitter = PolkadotXcm;
263 type AssetTransactor = AssetTransactors;
264 type OriginConverter = XcmOriginToCallOrigin;
265 type IsReserve = (NativeAsset, TrustedReserves);
266 type IsTeleporter = TrustedTeleporters;
267 type UniversalLocation = UniversalLocation;
268 type Barrier = Barrier;
269 type Weigher = FixedWeightBounds<XcmInstructionWeight, RuntimeCall, MaxInstructions>;
270 type Trader = FixedRateOfFungible<TokensPerSecondPerMegabyte, ()>;
271 type ResponseHandler = PolkadotXcm;
272 type AssetTrap = PolkadotXcm;
273 type AssetLocker = PolkadotXcm;
274 type AssetExchanger = ();
275 type SubscriptionService = PolkadotXcm;
276 type PalletInstancesInfo = AllPalletsWithSystem;
277 type FeeManager = ();
278 type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
279 type MessageExporter = ();
280 type UniversalAliases = Nothing;
281 type CallDispatcher = RuntimeCall;
282 type SafeCallFilter = Everything;
283 type Aliasers = Nothing;
284 type TransactionalProcessor = FrameTransactionalProcessor;
285 type HrmpNewChannelOpenRequestHandler = ();
286 type HrmpChannelAcceptedHandler = ();
287 type HrmpChannelClosingHandler = ();
288 type XcmRecorder = PolkadotXcm;
289}
290
291impl mock_msg_queue::Config for Runtime {
292 type RuntimeEvent = RuntimeEvent;
293 type XcmExecutor = XcmExecutor<XcmConfig>;
294}
295
296pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
297
298pub struct TrustedLockerCase<T>(PhantomData<T>);
299impl<T: Get<(Location, AssetFilter)>> ContainsPair<Location, Asset> for TrustedLockerCase<T> {
300 fn contains(origin: &Location, asset: &Asset) -> bool {
301 let (o, a) = T::get();
302 a.matches(asset) && &o == origin
303 }
304}
305
306impl pallet_xcm::Config for Runtime {
307 type RuntimeEvent = RuntimeEvent;
308 type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
309 type XcmRouter = XcmRouter;
310 type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
311 type XcmExecuteFilter = Everything;
312 type XcmExecutor = XcmExecutor<XcmConfig>;
313 type XcmTeleportFilter = Nothing;
314 type XcmReserveTransferFilter = Everything;
315 type Weigher = FixedWeightBounds<XcmInstructionWeight, RuntimeCall, MaxInstructions>;
316 type UniversalLocation = UniversalLocation;
317 type RuntimeOrigin = RuntimeOrigin;
318 type RuntimeCall = RuntimeCall;
319 const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
320 type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
321 type Currency = Balances;
322 type CurrencyMatcher = IsConcrete<TokenLocation>;
323 type TrustedLockers = TrustedLockerCase<TrustedLockPairs>;
324 type SovereignAccountOf = SovereignAccountOf;
325 type MaxLockers = ConstU32<8>;
326 type MaxRemoteLockConsumers = ConstU32<0>;
327 type RemoteLockConsumerIdentifier = ();
328 type WeightInfo = pallet_xcm::TestWeightInfo;
329 type AdminOrigin = EnsureRoot<AccountId>;
330 type AuthorizedAliasConsideration = Disabled;
332}
333
334type Block = frame_system::mocking::MockBlock<Runtime>;
335
336impl pallet_timestamp::Config for Runtime {
337 type Moment = u64;
338 type OnTimestampSet = ();
339 type MinimumPeriod = ConstU64<1>;
340 type WeightInfo = ();
341}
342
343construct_runtime!(
344 pub enum Runtime
345 {
346 System: frame_system,
347 Balances: pallet_balances,
348 Timestamp: pallet_timestamp,
349 MsgQueue: mock_msg_queue,
350 PolkadotXcm: pallet_xcm,
351 Contracts: pallet_contracts,
352 Assets: pallet_assets,
353 }
354);