referrerpolicy=no-referrer-when-downgrade

parachain_template_runtime/configs/
xcm_config.rs

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