referrerpolicy=no-referrer-when-downgrade

westend_runtime/
xcm_config.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! XCM configurations for Westend.
18
19use super::{
20	parachains_origin, AccountId, AllPalletsWithSystem, Balances, Dmp, ParaId, Runtime,
21	RuntimeCall, RuntimeEvent, RuntimeOrigin, TransactionByteFee, WeightToFee, XcmPallet,
22};
23use frame_support::{
24	parameter_types,
25	traits::{Contains, Disabled, Equals, Everything, Nothing},
26};
27use frame_system::EnsureRoot;
28use pallet_xcm::XcmPassthrough;
29use polkadot_runtime_common::{
30	xcm_sender::{ChildParachainRouter, ExponentialPrice},
31	ToAuthor,
32};
33use sp_core::ConstU32;
34use westend_runtime_constants::{currency::CENTS, system_parachain::*};
35use xcm::latest::{prelude::*, WESTEND_GENESIS_HASH};
36use xcm_builder::{
37	AccountId32Aliases, AliasChildLocation, AllowExplicitUnpaidExecutionFrom,
38	AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom,
39	ChildParachainAsNative, ChildParachainConvertsVia, DescribeAllTerminal, DescribeFamily,
40	FrameTransactionalProcessor, FungibleAdapter, HashedDescription, IsChildSystemParachain,
41	IsConcrete, LocationAsSuperuser, MintLocation, SendXcmFeeToAccount, SignedAccountId32AsNative,
42	SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId,
43	UsingComponents, WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
44	XcmFeeManagerFromComponents,
45};
46use xcm_executor::XcmExecutor;
47
48parameter_types! {
49	pub const TokenLocation: Location = Here.into_location();
50	pub const RootLocation: Location = Location::here();
51	pub const ThisNetwork: NetworkId = ByGenesis(WESTEND_GENESIS_HASH);
52	pub UniversalLocation: InteriorLocation = [GlobalConsensus(ThisNetwork::get())].into();
53	pub CheckAccount: AccountId = XcmPallet::check_account();
54	/// Westend does not have mint authority anymore after the Asset Hub migration.
55	pub TeleportTracking: Option<(AccountId, MintLocation)> = None;
56	pub AccumulateAccount: AccountId = pallet_accumulate_and_forward::Pallet::<Runtime>::accumulation_account();
57	/// The asset ID for the asset that we use to pay for message delivery fees.
58	pub FeeAssetId: AssetId = AssetId(TokenLocation::get());
59	/// The base fee for the message delivery fees.
60	pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3);
61	// Fellows pluralistic body.
62	pub const FellowsBodyId: BodyId = BodyId::Technical;
63	/// The accumulation account location on this chain.
64	pub AccumulateForwardLocation: Location = {
65		AccountId32 { network: None, id: AccumulateAccount::get().into() }.into()
66	};
67}
68
69pub type LocationConverter = (
70	// We can convert a child parachain using the standard `AccountId` conversion.
71	ChildParachainConvertsVia<ParaId, AccountId>,
72	// We can directly alias an `AccountId32` into a local account.
73	AccountId32Aliases<ThisNetwork, AccountId>,
74	// Foreign locations alias into accounts according to a hash of their standard description.
75	HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
76);
77
78pub type LocalAssetTransactor = FungibleAdapter<
79	// Use this currency:
80	Balances,
81	// Use this currency when it is a fungible asset matching the given location or name:
82	IsConcrete<TokenLocation>,
83	// We can convert the Locations with our converter above:
84	LocationConverter,
85	// Our chain's account ID type (we can't get away without mentioning it explicitly):
86	AccountId,
87	TeleportTracking,
88>;
89
90type LocalOriginConverter = (
91	// Asset Hub can gain root on the relay chain.
92	LocationAsSuperuser<Equals<AssetHub>, RuntimeOrigin>,
93	// If the origin kind is `Sovereign`, then return a `Signed` origin with the account determined
94	// by the `LocationConverter` converter.
95	SovereignSignedViaLocation<LocationConverter, RuntimeOrigin>,
96	// If the origin kind is `Native` and the XCM origin is a child parachain, then we can express
97	// it with the special `parachains_origin::Origin` origin variant.
98	ChildParachainAsNative<parachains_origin::Origin, RuntimeOrigin>,
99	// If the origin kind is `Native` and the XCM origin is the `AccountId32` location, then it can
100	// be expressed using the `Signed` origin variant.
101	SignedAccountId32AsNative<ThisNetwork, RuntimeOrigin>,
102	// Xcm origins can be represented natively under the Xcm pallet's Xcm origin.
103	XcmPassthrough<RuntimeOrigin>,
104);
105
106pub type PriceForChildParachainDelivery =
107	ExponentialPrice<FeeAssetId, BaseDeliveryFee, TransactionByteFee, Dmp>;
108
109/// The XCM router. When we want to send an XCM message, we use this type. It amalgamates all of our
110/// individual routers.
111pub type XcmRouter = WithUniqueTopic<
112	// Only one router so far - use DMP to communicate with child parachains.
113	ChildParachainRouter<Runtime, XcmPallet, PriceForChildParachainDelivery>,
114>;
115
116parameter_types! {
117	pub AssetHub: Location = Parachain(ASSET_HUB_ID).into_location();
118	pub AssetHubNext: Location = Parachain(ASSET_HUB_NEXT_ID).into_location();
119	pub Collectives: Location = Parachain(COLLECTIVES_ID).into_location();
120	pub BridgeHub: Location = Parachain(BRIDGE_HUB_ID).into_location();
121	pub Encointer: Location = Parachain(ENCOINTER_ID).into_location();
122	pub People: Location = Parachain(PEOPLE_ID).into_location();
123	pub Broker: Location = Parachain(BROKER_ID).into_location();
124	pub Wnd: AssetFilter = Wild(AllOf { fun: WildFungible, id: AssetId(TokenLocation::get()) });
125	pub WndForAssetHub: (AssetFilter, Location) = (Wnd::get(), AssetHub::get());
126	pub WndForAssetHubNext: (AssetFilter, Location) = (Wnd::get(), AssetHubNext::get());
127	pub WndForCollectives: (AssetFilter, Location) = (Wnd::get(), Collectives::get());
128	pub WndForBridgeHub: (AssetFilter, Location) = (Wnd::get(), BridgeHub::get());
129	pub WndForEncointer: (AssetFilter, Location) = (Wnd::get(), Encointer::get());
130	pub WndForPeople: (AssetFilter, Location) = (Wnd::get(), People::get());
131	pub WndForBroker: (AssetFilter, Location) = (Wnd::get(), Broker::get());
132	pub MaxInstructions: u32 = 100;
133	pub MaxAssetsIntoHolding: u32 = 64;
134}
135
136pub type TrustedTeleporters = (
137	xcm_builder::Case<WndForAssetHub>,
138	xcm_builder::Case<WndForAssetHubNext>,
139	xcm_builder::Case<WndForCollectives>,
140	xcm_builder::Case<WndForBridgeHub>,
141	xcm_builder::Case<WndForEncointer>,
142	xcm_builder::Case<WndForPeople>,
143	xcm_builder::Case<WndForBroker>,
144);
145
146pub struct OnlyParachains;
147impl Contains<Location> for OnlyParachains {
148	fn contains(location: &Location) -> bool {
149		matches!(location.unpack(), (0, [Parachain(_)]))
150	}
151}
152
153pub struct Fellows;
154impl Contains<Location> for Fellows {
155	fn contains(location: &Location) -> bool {
156		matches!(
157			location.unpack(),
158			(0, [Parachain(COLLECTIVES_ID), Plurality { id: BodyId::Technical, .. }])
159		)
160	}
161}
162
163pub struct LocalPlurality;
164impl Contains<Location> for LocalPlurality {
165	fn contains(loc: &Location) -> bool {
166		matches!(loc.unpack(), (0, [Plurality { .. }]))
167	}
168}
169
170/// The barriers one of which must be passed for an XCM message to be executed.
171pub type Barrier = TrailingSetTopicAsId<(
172	// Weight that is paid for may be consumed.
173	TakeWeightCredit,
174	// Expected responses are OK.
175	AllowKnownQueryResponses<XcmPallet>,
176	WithComputedOrigin<
177		(
178			// If the message is one that immediately attempts to pay for execution, then allow it.
179			AllowTopLevelPaidExecutionFrom<Everything>,
180			// Subscriptions for version tracking are OK.
181			AllowSubscriptionsFrom<OnlyParachains>,
182			// Messages from system parachains or the Fellows plurality need not pay for execution.
183			AllowExplicitUnpaidExecutionFrom<(IsChildSystemParachain<ParaId>, Fellows)>,
184		),
185		UniversalLocation,
186		ConstU32<8>,
187	>,
188)>;
189
190/// Locations that will not be charged fees in the executor, neither for execution nor delivery.
191/// We only waive fees for system functions, which these locations represent.
192pub type WaivedLocations =
193	(SystemParachains, Equals<RootLocation>, LocalPlurality, Equals<AccumulateForwardLocation>);
194
195/// We let locations alias into child locations of their own.
196/// This is a very simple aliasing rule, mimicking the behaviour of
197/// the `DescendOrigin` instruction.
198pub type Aliasers = AliasChildLocation;
199
200pub struct XcmConfig;
201impl xcm_executor::Config for XcmConfig {
202	type RuntimeCall = RuntimeCall;
203	type XcmSender = XcmRouter;
204	type XcmEventEmitter = XcmPallet;
205	type AssetTransactor = LocalAssetTransactor;
206	type OriginConverter = LocalOriginConverter;
207	type IsReserve = ();
208	type IsTeleporter = TrustedTeleporters;
209	type UniversalLocation = UniversalLocation;
210	type Barrier = Barrier;
211	type Weigher = WeightInfoBounds<
212		crate::weights::xcm::WestendXcmWeight<RuntimeCall>,
213		RuntimeCall,
214		MaxInstructions,
215	>;
216	// TODO: once DAP allocates validator/author budgets, redirect XCM execution fees to the
217	// accumulation account instead of block author (use AccumulateForward as the OnUnbalanced
218	// handler).
219	type Trader =
220		UsingComponents<WeightToFee, TokenLocation, AccountId, Balances, ToAuthor<Runtime>>;
221	type ResponseHandler = XcmPallet;
222	type AssetTrap = XcmPallet;
223	type AssetLocker = ();
224	type AssetExchanger = ();
225	type SubscriptionService = XcmPallet;
226	type PalletInstancesInfo = AllPalletsWithSystem;
227	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
228	type FeeManager = XcmFeeManagerFromComponents<
229		WaivedLocations,
230		SendXcmFeeToAccount<Self::AssetTransactor, AccumulateAccount>,
231	>;
232	type MessageExporter = ();
233	type UniversalAliases = Nothing;
234	type CallDispatcher = RuntimeCall;
235	type SafeCallFilter = Everything;
236	type Aliasers = Aliasers;
237	type TransactionalProcessor = FrameTransactionalProcessor;
238	type HrmpNewChannelOpenRequestHandler = ();
239	type HrmpChannelAcceptedHandler = ();
240	type HrmpChannelClosingHandler = ();
241	type XcmRecorder = XcmPallet;
242}
243
244/// Converts a local signed origin into an XCM location. Forms the basis for local origins
245/// sending/executing XCMs.
246pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, ThisNetwork>;
247
248impl pallet_xcm::Config for Runtime {
249	type RuntimeEvent = RuntimeEvent;
250	type SendXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
251	type XcmRouter = XcmRouter;
252	// Anyone can execute XCM messages locally.
253	type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
254	type XcmExecuteFilter = Everything;
255	type XcmExecutor = XcmExecutor<XcmConfig>;
256	type XcmTeleportFilter = Everything;
257	type XcmReserveTransferFilter = Everything;
258	type Weigher = WeightInfoBounds<
259		crate::weights::xcm::WestendXcmWeight<RuntimeCall>,
260		RuntimeCall,
261		MaxInstructions,
262	>;
263	type UniversalLocation = UniversalLocation;
264	type RuntimeOrigin = RuntimeOrigin;
265	type RuntimeCall = RuntimeCall;
266	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
267	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
268	type Currency = Balances;
269	type CurrencyMatcher = IsConcrete<TokenLocation>;
270	type TrustedLockers = ();
271	type SovereignAccountOf = LocationConverter;
272	type MaxLockers = ConstU32<8>;
273	type MaxRemoteLockConsumers = ConstU32<0>;
274	type RemoteLockConsumerIdentifier = ();
275	type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
276	type AdminOrigin = EnsureRoot<AccountId>;
277	// Aliasing is disabled: xcm_executor::Config::Aliasers only allows `AliasChildLocation`.
278	type AuthorizedAliasConsideration = Disabled;
279}