referrerpolicy=no-referrer-when-downgrade

yet_another_parachain_runtime/
xcm_config.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17use crate::{
18	AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm,
19	Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue,
20};
21use frame_support::{
22	parameter_types,
23	traits::{ConstU32, Contains, Everything, Nothing},
24	weights::Weight,
25};
26use frame_system::EnsureRoot;
27use pallet_xcm::XcmPassthrough;
28use polkadot_parachain_primitives::primitives::Sibling;
29use polkadot_runtime_common::impls::ToAuthor;
30use xcm::latest::prelude::*;
31use xcm_builder::{
32	AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowTopLevelPaidExecutionFrom,
33	DenyReserveTransferToRelayChain, DenyThenTry, EnsureXcmOrigin, FixedWeightBounds,
34	FrameTransactionalProcessor, FungibleAdapter, IsConcrete, NativeAsset, ParentIsPreset,
35	RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
36	SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
37	TrailingSetTopicAsId, UsingComponents, WithComputedOrigin, WithUniqueTopic,
38};
39use xcm_executor::XcmExecutor;
40
41parameter_types! {
42	pub const RelayLocation: Location = Location::parent();
43	pub const RelayNetwork: Option<NetworkId> = None;
44	pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
45	// For the real deployment, it is recommended to set `RelayNetwork` according to the relay chain
46	// and prepend `UniversalLocation` with `GlobalConsensus(RelayNetwork::get())`.
47	pub UniversalLocation: InteriorLocation = Parachain(ParachainInfo::parachain_id().into()).into();
48}
49
50/// Type for specifying how a `Location` can be converted into an `AccountId`. This is used
51/// when determining ownership of accounts for asset transacting and when attempting to use XCM
52/// `Transact` in order to determine the dispatch Origin.
53pub type LocationToAccountId = (
54	// The parent (Relay-chain) origin converts to the parent `AccountId`.
55	ParentIsPreset<AccountId>,
56	// Sibling parachain origins convert to AccountId via the `ParaId::into`.
57	SiblingParachainConvertsVia<Sibling, AccountId>,
58	// Straight up local `AccountId32` origins just alias directly to `AccountId`.
59	AccountId32Aliases<RelayNetwork, AccountId>,
60);
61
62/// Means for transacting assets on this chain.
63pub type LocalAssetTransactor = FungibleAdapter<
64	// Use this currency:
65	Balances,
66	// Use this currency when it is a fungible asset matching the given location or name:
67	IsConcrete<RelayLocation>,
68	// Do a simple punn to convert an AccountId32 Location into a native chain account ID:
69	LocationToAccountId,
70	// Our chain's account ID type (we can't get away without mentioning it explicitly):
71	AccountId,
72	// We don't track any teleports.
73	(),
74>;
75
76/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
77/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
78/// biases the kind of local `Origin` it will become.
79pub type XcmOriginToTransactDispatchOrigin = (
80	// Sovereign account converter; this attempts to derive an `AccountId` from the origin location
81	// using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for
82	// foreign chains who want to have a local sovereign account on this chain which they control.
83	SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,
84	// Native converter for Relay-chain (Parent) location; will convert to a `Relay` origin when
85	// recognized.
86	RelayChainAsNative<RelayChainOrigin, RuntimeOrigin>,
87	// Native converter for sibling Parachains; will convert to a `SiblingPara` origin when
88	// recognized.
89	SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,
90	// Native signed account converter; this just converts an `AccountId32` origin into a normal
91	// `RuntimeOrigin::Signed` origin of the same 32-byte value.
92	SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
93	// Xcm origins can be represented natively under the Xcm pallet's Xcm origin.
94	XcmPassthrough<RuntimeOrigin>,
95);
96
97parameter_types! {
98	// One XCM operation is 1_000_000_000 weight - almost certainly a conservative estimate.
99	pub UnitWeightCost: Weight = Weight::from_parts(1_000_000_000, 64 * 1024);
100	pub const MaxInstructions: u32 = 100;
101	pub const MaxAssetsIntoHolding: u32 = 64;
102}
103
104pub struct ParentOrParentsExecutivePlurality;
105impl Contains<Location> for ParentOrParentsExecutivePlurality {
106	fn contains(location: &Location) -> bool {
107		matches!(location.unpack(), (1, []) | (1, [Plurality { id: BodyId::Executive, .. }]))
108	}
109}
110
111pub type Barrier = TrailingSetTopicAsId<
112	DenyThenTry<
113		DenyReserveTransferToRelayChain,
114		(
115			TakeWeightCredit,
116			WithComputedOrigin<
117				(
118					AllowTopLevelPaidExecutionFrom<Everything>,
119					AllowExplicitUnpaidExecutionFrom<ParentOrParentsExecutivePlurality>,
120					// ^^^ Parent and its exec plurality get free execution
121				),
122				UniversalLocation,
123				ConstU32<8>,
124			>,
125		),
126	>,
127>;
128
129pub struct XcmConfig;
130impl xcm_executor::Config for XcmConfig {
131	type RuntimeCall = RuntimeCall;
132	type XcmSender = XcmRouter;
133	type XcmEventEmitter = PolkadotXcm;
134	// How to withdraw and deposit an asset.
135	type AssetTransactor = LocalAssetTransactor;
136	type OriginConverter = XcmOriginToTransactDispatchOrigin;
137	type IsReserve = NativeAsset;
138	type IsTeleporter = (); // Teleporting is disabled.
139	type UniversalLocation = UniversalLocation;
140	type Barrier = Barrier;
141	type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
142	type Trader =
143		UsingComponents<WeightToFee, RelayLocation, AccountId, Balances, ToAuthor<Runtime>>;
144	type ResponseHandler = PolkadotXcm;
145	type AssetTrap = PolkadotXcm;
146	type AssetClaims = PolkadotXcm;
147	type SubscriptionService = PolkadotXcm;
148	type PalletInstancesInfo = AllPalletsWithSystem;
149	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
150	type AssetLocker = ();
151	type AssetExchanger = ();
152	type FeeManager = ();
153	type MessageExporter = ();
154	type UniversalAliases = Nothing;
155	type CallDispatcher = RuntimeCall;
156	type SafeCallFilter = Everything;
157	type Aliasers = Nothing;
158	type TransactionalProcessor = FrameTransactionalProcessor;
159	type HrmpNewChannelOpenRequestHandler = ();
160	type HrmpChannelAcceptedHandler = ();
161	type HrmpChannelClosingHandler = ();
162	type XcmRecorder = PolkadotXcm;
163}
164
165/// No local origins on this chain are allowed to dispatch XCM sends/executions.
166pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
167
168/// The means for routing XCM messages which are not for local execution into the right message
169/// queues.
170pub type XcmRouter = WithUniqueTopic<(
171	// Two routers - use UMP to communicate with the relay chain:
172	cumulus_primitives_utility::ParentAsUmp<ParachainSystem, (), ()>,
173	// ..and XCMP to communicate with the sibling chains.
174	XcmpQueue,
175)>;
176
177impl pallet_xcm::Config for Runtime {
178	type RuntimeEvent = RuntimeEvent;
179	type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
180	type XcmRouter = XcmRouter;
181	type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
182	type XcmExecuteFilter = Nothing;
183	// ^ Disable dispatchable execute on the XCM pallet.
184	// Needs to be `Everything` for local testing.
185	type XcmExecutor = XcmExecutor<XcmConfig>;
186	type XcmTeleportFilter = Everything;
187	type XcmReserveTransferFilter = Nothing;
188	type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
189	type UniversalLocation = UniversalLocation;
190	type RuntimeOrigin = RuntimeOrigin;
191	type RuntimeCall = RuntimeCall;
192
193	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
194	// ^ Override for AdvertisedXcmVersion default
195	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
196	type Currency = Balances;
197	type CurrencyMatcher = ();
198	type TrustedLockers = ();
199	type SovereignAccountOf = LocationToAccountId;
200	type MaxLockers = ConstU32<8>;
201	type WeightInfo = pallet_xcm::TestWeightInfo;
202	type AdminOrigin = EnsureRoot<AccountId>;
203	type MaxRemoteLockConsumers = ConstU32<0>;
204	type RemoteLockConsumerIdentifier = ();
205	type AuthorizedAliasConsideration = ();
206}
207
208impl cumulus_pallet_xcm::Config for Runtime {
209	type RuntimeEvent = RuntimeEvent;
210	type XcmExecutor = XcmExecutor<XcmConfig>;
211}