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, FellowshipAdmin,
21	GeneralAdmin, ParaId, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, StakingAdmin,
22	TransactionByteFee, Treasury, WeightToFee, XcmPallet,
23};
24use crate::governance::pallet_custom_origins::Treasurer;
25use frame_support::{
26	parameter_types,
27	traits::{Contains, Disabled, Equals, Everything, Nothing},
28};
29use frame_system::EnsureRoot;
30use pallet_xcm::XcmPassthrough;
31use polkadot_runtime_common::{
32	xcm_sender::{ChildParachainRouter, ExponentialPrice},
33	ToAuthor,
34};
35use sp_core::ConstU32;
36use westend_runtime_constants::{
37	currency::CENTS, system_parachain::*, xcm::body::FELLOWSHIP_ADMIN_INDEX,
38};
39use xcm::latest::{prelude::*, WESTEND_GENESIS_HASH};
40use xcm_builder::{
41	AccountId32Aliases, AliasChildLocation, AllowExplicitUnpaidExecutionFrom,
42	AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom,
43	ChildParachainAsNative, ChildParachainConvertsVia, DescribeAllTerminal, DescribeFamily,
44	FrameTransactionalProcessor, FungibleAdapter, HashedDescription, IsChildSystemParachain,
45	IsConcrete, LocationAsSuperuser, MintLocation, OriginToPluralityVoice, SendXcmFeeToAccount,
46	SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
47	TrailingSetTopicAsId, UsingComponents, WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
48	XcmFeeManagerFromComponents,
49};
50use xcm_executor::XcmExecutor;
51
52parameter_types! {
53	pub const TokenLocation: Location = Here.into_location();
54	pub const RootLocation: Location = Location::here();
55	pub const ThisNetwork: NetworkId = ByGenesis(WESTEND_GENESIS_HASH);
56	pub UniversalLocation: InteriorLocation = [GlobalConsensus(ThisNetwork::get())].into();
57	pub CheckAccount: AccountId = XcmPallet::check_account();
58	/// Westend does not have mint authority anymore after the Asset Hub migration.
59	pub TeleportTracking: Option<(AccountId, MintLocation)> = None;
60	pub TreasuryAccount: AccountId = Treasury::account_id();
61	/// The asset ID for the asset that we use to pay for message delivery fees.
62	pub FeeAssetId: AssetId = AssetId(TokenLocation::get());
63	/// The base fee for the message delivery fees.
64	pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3);
65	// Fellows pluralistic body.
66	pub const FellowsBodyId: BodyId = BodyId::Technical;
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 = (SystemParachains, Equals<RootLocation>, LocalPlurality);
193
194/// We let locations alias into child locations of their own.
195/// This is a very simple aliasing rule, mimicking the behaviour of
196/// the `DescendOrigin` instruction.
197pub type Aliasers = AliasChildLocation;
198
199pub struct XcmConfig;
200impl xcm_executor::Config for XcmConfig {
201	type RuntimeCall = RuntimeCall;
202	type XcmSender = XcmRouter;
203	type XcmEventEmitter = XcmPallet;
204	type AssetTransactor = LocalAssetTransactor;
205	type OriginConverter = LocalOriginConverter;
206	type IsReserve = ();
207	type IsTeleporter = TrustedTeleporters;
208	type UniversalLocation = UniversalLocation;
209	type Barrier = Barrier;
210	type Weigher = WeightInfoBounds<
211		crate::weights::xcm::WestendXcmWeight<RuntimeCall>,
212		RuntimeCall,
213		MaxInstructions,
214	>;
215	type Trader =
216		UsingComponents<WeightToFee, TokenLocation, AccountId, Balances, ToAuthor<Runtime>>;
217	type ResponseHandler = XcmPallet;
218	type AssetTrap = XcmPallet;
219	type AssetLocker = ();
220	type AssetExchanger = ();
221	type AssetClaims = XcmPallet;
222	type SubscriptionService = XcmPallet;
223	type PalletInstancesInfo = AllPalletsWithSystem;
224	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
225	type FeeManager = XcmFeeManagerFromComponents<
226		WaivedLocations,
227		SendXcmFeeToAccount<Self::AssetTransactor, TreasuryAccount>,
228	>;
229	type MessageExporter = ();
230	type UniversalAliases = Nothing;
231	type CallDispatcher = RuntimeCall;
232	type SafeCallFilter = Everything;
233	type Aliasers = Aliasers;
234	type TransactionalProcessor = FrameTransactionalProcessor;
235	type HrmpNewChannelOpenRequestHandler = ();
236	type HrmpChannelAcceptedHandler = ();
237	type HrmpChannelClosingHandler = ();
238	type XcmRecorder = XcmPallet;
239}
240
241parameter_types! {
242	// `GeneralAdmin` pluralistic body.
243	pub const GeneralAdminBodyId: BodyId = BodyId::Administration;
244	// StakingAdmin pluralistic body.
245	pub const StakingAdminBodyId: BodyId = BodyId::Defense;
246	// FellowshipAdmin pluralistic body.
247	pub const FellowshipAdminBodyId: BodyId = BodyId::Index(FELLOWSHIP_ADMIN_INDEX);
248	// `Treasurer` pluralistic body.
249	pub const TreasurerBodyId: BodyId = BodyId::Treasury;
250	// DDay pluralistic body.
251	pub const DDayBodyId: BodyId = BodyId::Moniker([b'd', b'd', b'a', b'y']);
252}
253
254/// Type to convert the `GeneralAdmin` origin to a Plurality `Location` value.
255pub type GeneralAdminToPlurality =
256	OriginToPluralityVoice<RuntimeOrigin, GeneralAdmin, GeneralAdminBodyId>;
257
258/// Converts a local signed origin into an XCM location. Forms the basis for local origins
259/// sending/executing XCMs.
260pub type LocalOriginToLocation = (
261	GeneralAdminToPlurality,
262	// And a usual Signed origin to be used in XCM as a corresponding AccountId32
263	SignedToAccountId32<RuntimeOrigin, AccountId, ThisNetwork>,
264);
265
266/// Type to convert the `StakingAdmin` origin to a Plurality `Location` value.
267pub type StakingAdminToPlurality =
268	OriginToPluralityVoice<RuntimeOrigin, StakingAdmin, StakingAdminBodyId>;
269
270/// Type to convert the `FellowshipAdmin` origin to a Plurality `Location` value.
271pub type FellowshipAdminToPlurality =
272	OriginToPluralityVoice<RuntimeOrigin, FellowshipAdmin, FellowshipAdminBodyId>;
273
274/// Type to convert the `Treasurer` origin to a Plurality `Location` value.
275pub type TreasurerToPlurality = OriginToPluralityVoice<RuntimeOrigin, Treasurer, TreasurerBodyId>;
276
277/// Type to convert a pallet `Origin` type value into a `Location` value which represents an
278/// interior location of this chain for a destination chain.
279pub type LocalPalletOriginToLocation = (
280	// GeneralAdmin origin to be used in XCM as a corresponding Plurality `Location` value.
281	GeneralAdminToPlurality,
282	// StakingAdmin origin to be used in XCM as a corresponding Plurality `Location` value.
283	StakingAdminToPlurality,
284	// FellowshipAdmin origin to be used in XCM as a corresponding Plurality `Location` value.
285	FellowshipAdminToPlurality,
286	// `Treasurer` origin to be used in XCM as a corresponding Plurality `Location` value.
287	TreasurerToPlurality,
288);
289
290impl pallet_xcm::Config for Runtime {
291	type RuntimeEvent = RuntimeEvent;
292	// Note that this configuration of `SendXcmOrigin` is different from the one present in
293	// production.
294	type SendXcmOrigin = xcm_builder::EnsureXcmOrigin<
295		RuntimeOrigin,
296		(LocalPalletOriginToLocation, LocalOriginToLocation),
297	>;
298	type XcmRouter = XcmRouter;
299	// Anyone can execute XCM messages locally.
300	type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
301	type XcmExecuteFilter = Everything;
302	type XcmExecutor = XcmExecutor<XcmConfig>;
303	type XcmTeleportFilter = Everything;
304	type XcmReserveTransferFilter = Everything;
305	type Weigher = WeightInfoBounds<
306		crate::weights::xcm::WestendXcmWeight<RuntimeCall>,
307		RuntimeCall,
308		MaxInstructions,
309	>;
310	type UniversalLocation = UniversalLocation;
311	type RuntimeOrigin = RuntimeOrigin;
312	type RuntimeCall = RuntimeCall;
313	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
314	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
315	type Currency = Balances;
316	type CurrencyMatcher = IsConcrete<TokenLocation>;
317	type TrustedLockers = ();
318	type SovereignAccountOf = LocationConverter;
319	type MaxLockers = ConstU32<8>;
320	type MaxRemoteLockConsumers = ConstU32<0>;
321	type RemoteLockConsumerIdentifier = ();
322	type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
323	type AdminOrigin = EnsureRoot<AccountId>;
324	// Aliasing is disabled: xcm_executor::Config::Aliasers only allows `AliasChildLocation`.
325	type AuthorizedAliasConsideration = Disabled;
326}