Skip to main content

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, PalletInfoAccess},
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	pub TreasuryLocation: Location = PalletInstance(<Treasury as PalletInfoAccess>::index() as u8).into();
62	/// The asset ID for the asset that we use to pay for message delivery fees.
63	pub FeeAssetId: AssetId = AssetId(TokenLocation::get());
64	/// The base fee for the message delivery fees.
65	pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3);
66	// Fellows pluralistic body.
67	pub const FellowsBodyId: BodyId = BodyId::Technical;
68}
69
70pub type LocationConverter = (
71	// We can convert a child parachain using the standard `AccountId` conversion.
72	ChildParachainConvertsVia<ParaId, AccountId>,
73	// We can directly alias an `AccountId32` into a local account.
74	AccountId32Aliases<ThisNetwork, AccountId>,
75	// Foreign locations alias into accounts according to a hash of their standard description.
76	HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
77);
78
79pub type LocalAssetTransactor = FungibleAdapter<
80	// Use this currency:
81	Balances,
82	// Use this currency when it is a fungible asset matching the given location or name:
83	IsConcrete<TokenLocation>,
84	// We can convert the Locations with our converter above:
85	LocationConverter,
86	// Our chain's account ID type (we can't get away without mentioning it explicitly):
87	AccountId,
88	TeleportTracking,
89>;
90
91type LocalOriginConverter = (
92	// Asset Hub can gain root on the relay chain.
93	LocationAsSuperuser<Equals<AssetHub>, RuntimeOrigin>,
94	// If the origin kind is `Sovereign`, then return a `Signed` origin with the account determined
95	// by the `LocationConverter` converter.
96	SovereignSignedViaLocation<LocationConverter, RuntimeOrigin>,
97	// If the origin kind is `Native` and the XCM origin is a child parachain, then we can express
98	// it with the special `parachains_origin::Origin` origin variant.
99	ChildParachainAsNative<parachains_origin::Origin, RuntimeOrigin>,
100	// If the origin kind is `Native` and the XCM origin is the `AccountId32` location, then it can
101	// be expressed using the `Signed` origin variant.
102	SignedAccountId32AsNative<ThisNetwork, RuntimeOrigin>,
103	// Xcm origins can be represented natively under the Xcm pallet's Xcm origin.
104	XcmPassthrough<RuntimeOrigin>,
105);
106
107pub type PriceForChildParachainDelivery =
108	ExponentialPrice<FeeAssetId, BaseDeliveryFee, TransactionByteFee, Dmp>;
109
110/// The XCM router. When we want to send an XCM message, we use this type. It amalgamates all of our
111/// individual routers.
112pub type XcmRouter = WithUniqueTopic<
113	// Only one router so far - use DMP to communicate with child parachains.
114	ChildParachainRouter<Runtime, XcmPallet, PriceForChildParachainDelivery>,
115>;
116
117parameter_types! {
118	pub AssetHub: Location = Parachain(ASSET_HUB_ID).into_location();
119	pub AssetHubNext: Location = Parachain(ASSET_HUB_NEXT_ID).into_location();
120	pub Collectives: Location = Parachain(COLLECTIVES_ID).into_location();
121	pub BridgeHub: Location = Parachain(BRIDGE_HUB_ID).into_location();
122	pub Encointer: Location = Parachain(ENCOINTER_ID).into_location();
123	pub People: Location = Parachain(PEOPLE_ID).into_location();
124	pub Broker: Location = Parachain(BROKER_ID).into_location();
125	pub Wnd: AssetFilter = Wild(AllOf { fun: WildFungible, id: AssetId(TokenLocation::get()) });
126	pub WndForAssetHub: (AssetFilter, Location) = (Wnd::get(), AssetHub::get());
127	pub WndForAssetHubNext: (AssetFilter, Location) = (Wnd::get(), AssetHubNext::get());
128	pub WndForCollectives: (AssetFilter, Location) = (Wnd::get(), Collectives::get());
129	pub WndForBridgeHub: (AssetFilter, Location) = (Wnd::get(), BridgeHub::get());
130	pub WndForEncointer: (AssetFilter, Location) = (Wnd::get(), Encointer::get());
131	pub WndForPeople: (AssetFilter, Location) = (Wnd::get(), People::get());
132	pub WndForBroker: (AssetFilter, Location) = (Wnd::get(), Broker::get());
133	pub MaxInstructions: u32 = 100;
134	pub MaxAssetsIntoHolding: u32 = 64;
135}
136
137pub type TrustedTeleporters = (
138	xcm_builder::Case<WndForAssetHub>,
139	xcm_builder::Case<WndForAssetHubNext>,
140	xcm_builder::Case<WndForCollectives>,
141	xcm_builder::Case<WndForBridgeHub>,
142	xcm_builder::Case<WndForEncointer>,
143	xcm_builder::Case<WndForPeople>,
144	xcm_builder::Case<WndForBroker>,
145);
146
147pub struct OnlyParachains;
148impl Contains<Location> for OnlyParachains {
149	fn contains(location: &Location) -> bool {
150		matches!(location.unpack(), (0, [Parachain(_)]))
151	}
152}
153
154pub struct Fellows;
155impl Contains<Location> for Fellows {
156	fn contains(location: &Location) -> bool {
157		matches!(
158			location.unpack(),
159			(0, [Parachain(COLLECTIVES_ID), Plurality { id: BodyId::Technical, .. }])
160		)
161	}
162}
163
164pub struct LocalPlurality;
165impl Contains<Location> for LocalPlurality {
166	fn contains(loc: &Location) -> bool {
167		matches!(loc.unpack(), (0, [Plurality { .. }]))
168	}
169}
170
171/// The barriers one of which must be passed for an XCM message to be executed.
172pub type Barrier = TrailingSetTopicAsId<(
173	// Weight that is paid for may be consumed.
174	TakeWeightCredit,
175	// Expected responses are OK.
176	AllowKnownQueryResponses<XcmPallet>,
177	WithComputedOrigin<
178		(
179			// If the message is one that immediately attempts to pay for execution, then allow it.
180			AllowTopLevelPaidExecutionFrom<Everything>,
181			// Subscriptions for version tracking are OK.
182			AllowSubscriptionsFrom<OnlyParachains>,
183			// Messages from system parachains or the Fellows plurality need not pay for execution.
184			AllowExplicitUnpaidExecutionFrom<(IsChildSystemParachain<ParaId>, Fellows)>,
185		),
186		UniversalLocation,
187		ConstU32<8>,
188	>,
189)>;
190
191/// Locations that will not be charged fees in the executor, neither for execution nor delivery.
192/// We only waive fees for system functions, which these locations represent.
193pub type WaivedLocations =
194	(SystemParachains, Equals<RootLocation>, LocalPlurality, Equals<TreasuryLocation>);
195
196/// We let locations alias into child locations of their own.
197/// This is a very simple aliasing rule, mimicking the behaviour of
198/// the `DescendOrigin` instruction.
199pub type Aliasers = AliasChildLocation;
200
201pub struct XcmConfig;
202impl xcm_executor::Config for XcmConfig {
203	type RuntimeCall = RuntimeCall;
204	type XcmSender = XcmRouter;
205	type XcmEventEmitter = XcmPallet;
206	type AssetTransactor = LocalAssetTransactor;
207	type OriginConverter = LocalOriginConverter;
208	type IsReserve = ();
209	type IsTeleporter = TrustedTeleporters;
210	type UniversalLocation = UniversalLocation;
211	type Barrier = Barrier;
212	type Weigher = WeightInfoBounds<
213		crate::weights::xcm::WestendXcmWeight<RuntimeCall>,
214		RuntimeCall,
215		MaxInstructions,
216	>;
217	// TODO: once DAP allocates validator/author budgets, redirect XCM execution fees to DAP
218	// satellite instead of block author (use crate::DealWithFeesSatellite as the OnUnbalanced
219	// handler).
220	type Trader =
221		UsingComponents<WeightToFee, TokenLocation, AccountId, Balances, ToAuthor<Runtime>>;
222	type ResponseHandler = XcmPallet;
223	type AssetTrap = XcmPallet;
224	type AssetLocker = ();
225	type AssetExchanger = ();
226	type SubscriptionService = XcmPallet;
227	type PalletInstancesInfo = AllPalletsWithSystem;
228	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
229	// TODO: once DAP allocates budgets, split delivery fees to DAP via a HandleFee wrapper.
230	type FeeManager = XcmFeeManagerFromComponents<
231		WaivedLocations,
232		SendXcmFeeToAccount<Self::AssetTransactor, TreasuryAccount>,
233	>;
234	type MessageExporter = ();
235	type UniversalAliases = Nothing;
236	type CallDispatcher = RuntimeCall;
237	type SafeCallFilter = Everything;
238	type Aliasers = Aliasers;
239	type TransactionalProcessor = FrameTransactionalProcessor;
240	type HrmpNewChannelOpenRequestHandler = ();
241	type HrmpChannelAcceptedHandler = ();
242	type HrmpChannelClosingHandler = ();
243	type XcmRecorder = XcmPallet;
244}
245
246parameter_types! {
247	// `GeneralAdmin` pluralistic body.
248	pub const GeneralAdminBodyId: BodyId = BodyId::Administration;
249	// StakingAdmin pluralistic body.
250	pub const StakingAdminBodyId: BodyId = BodyId::Defense;
251	// FellowshipAdmin pluralistic body.
252	pub const FellowshipAdminBodyId: BodyId = BodyId::Index(FELLOWSHIP_ADMIN_INDEX);
253	// `Treasurer` pluralistic body.
254	pub const TreasurerBodyId: BodyId = BodyId::Treasury;
255	// DDay pluralistic body.
256	pub const DDayBodyId: BodyId = BodyId::Moniker([b'd', b'd', b'a', b'y']);
257}
258
259/// Type to convert the `GeneralAdmin` origin to a Plurality `Location` value.
260pub type GeneralAdminToPlurality =
261	OriginToPluralityVoice<RuntimeOrigin, GeneralAdmin, GeneralAdminBodyId>;
262
263/// Converts a local signed origin into an XCM location. Forms the basis for local origins
264/// sending/executing XCMs.
265pub type LocalOriginToLocation = (
266	GeneralAdminToPlurality,
267	// And a usual Signed origin to be used in XCM as a corresponding AccountId32
268	SignedToAccountId32<RuntimeOrigin, AccountId, ThisNetwork>,
269);
270
271/// Type to convert the `StakingAdmin` origin to a Plurality `Location` value.
272pub type StakingAdminToPlurality =
273	OriginToPluralityVoice<RuntimeOrigin, StakingAdmin, StakingAdminBodyId>;
274
275/// Type to convert the `FellowshipAdmin` origin to a Plurality `Location` value.
276pub type FellowshipAdminToPlurality =
277	OriginToPluralityVoice<RuntimeOrigin, FellowshipAdmin, FellowshipAdminBodyId>;
278
279/// Type to convert the `Treasurer` origin to a Plurality `Location` value.
280pub type TreasurerToPlurality = OriginToPluralityVoice<RuntimeOrigin, Treasurer, TreasurerBodyId>;
281
282/// Type to convert a pallet `Origin` type value into a `Location` value which represents an
283/// interior location of this chain for a destination chain.
284pub type LocalPalletOriginToLocation = (
285	// GeneralAdmin origin to be used in XCM as a corresponding Plurality `Location` value.
286	GeneralAdminToPlurality,
287	// StakingAdmin origin to be used in XCM as a corresponding Plurality `Location` value.
288	StakingAdminToPlurality,
289	// FellowshipAdmin origin to be used in XCM as a corresponding Plurality `Location` value.
290	FellowshipAdminToPlurality,
291	// `Treasurer` origin to be used in XCM as a corresponding Plurality `Location` value.
292	TreasurerToPlurality,
293);
294
295impl pallet_xcm::Config for Runtime {
296	type RuntimeEvent = RuntimeEvent;
297	// Note that this configuration of `SendXcmOrigin` is different from the one present in
298	// production.
299	type SendXcmOrigin = xcm_builder::EnsureXcmOrigin<
300		RuntimeOrigin,
301		(LocalPalletOriginToLocation, LocalOriginToLocation),
302	>;
303	type XcmRouter = XcmRouter;
304	// Anyone can execute XCM messages locally.
305	type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
306	type XcmExecuteFilter = Everything;
307	type XcmExecutor = XcmExecutor<XcmConfig>;
308	type XcmTeleportFilter = Everything;
309	type XcmReserveTransferFilter = Everything;
310	type Weigher = WeightInfoBounds<
311		crate::weights::xcm::WestendXcmWeight<RuntimeCall>,
312		RuntimeCall,
313		MaxInstructions,
314	>;
315	type UniversalLocation = UniversalLocation;
316	type RuntimeOrigin = RuntimeOrigin;
317	type RuntimeCall = RuntimeCall;
318	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
319	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
320	type Currency = Balances;
321	type CurrencyMatcher = IsConcrete<TokenLocation>;
322	type TrustedLockers = ();
323	type SovereignAccountOf = LocationConverter;
324	type MaxLockers = ConstU32<8>;
325	type MaxRemoteLockConsumers = ConstU32<0>;
326	type RemoteLockConsumerIdentifier = ();
327	type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
328	type AdminOrigin = EnsureRoot<AccountId>;
329	// Aliasing is disabled: xcm_executor::Config::Aliasers only allows `AliasChildLocation`.
330	type AuthorizedAliasConsideration = Disabled;
331}