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