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, 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 Collectives: Location = Parachain(COLLECTIVES_ID).into_location();
115	pub BridgeHub: Location = Parachain(BRIDGE_HUB_ID).into_location();
116	pub Encointer: Location = Parachain(ENCOINTER_ID).into_location();
117	pub People: Location = Parachain(PEOPLE_ID).into_location();
118	pub Broker: Location = Parachain(BROKER_ID).into_location();
119	pub Wnd: AssetFilter = Wild(AllOf { fun: WildFungible, id: AssetId(TokenLocation::get()) });
120	pub WndForAssetHub: (AssetFilter, Location) = (Wnd::get(), AssetHub::get());
121	pub WndForCollectives: (AssetFilter, Location) = (Wnd::get(), Collectives::get());
122	pub WndForBridgeHub: (AssetFilter, Location) = (Wnd::get(), BridgeHub::get());
123	pub WndForEncointer: (AssetFilter, Location) = (Wnd::get(), Encointer::get());
124	pub WndForPeople: (AssetFilter, Location) = (Wnd::get(), People::get());
125	pub WndForBroker: (AssetFilter, Location) = (Wnd::get(), Broker::get());
126	pub MaxInstructions: u32 = 100;
127	pub MaxAssetsIntoHolding: u32 = 64;
128}
129
130pub type TrustedTeleporters = (
131	xcm_builder::Case<WndForAssetHub>,
132	xcm_builder::Case<WndForCollectives>,
133	xcm_builder::Case<WndForBridgeHub>,
134	xcm_builder::Case<WndForEncointer>,
135	xcm_builder::Case<WndForPeople>,
136	xcm_builder::Case<WndForBroker>,
137);
138
139pub struct OnlyParachains;
140impl Contains<Location> for OnlyParachains {
141	fn contains(location: &Location) -> bool {
142		matches!(location.unpack(), (0, [Parachain(_)]))
143	}
144}
145
146pub struct Fellows;
147impl Contains<Location> for Fellows {
148	fn contains(location: &Location) -> bool {
149		matches!(
150			location.unpack(),
151			(0, [Parachain(COLLECTIVES_ID), Plurality { id: BodyId::Technical, .. }])
152		)
153	}
154}
155
156pub struct LocalPlurality;
157impl Contains<Location> for LocalPlurality {
158	fn contains(loc: &Location) -> bool {
159		matches!(loc.unpack(), (0, [Plurality { .. }]))
160	}
161}
162
163/// The barriers one of which must be passed for an XCM message to be executed.
164pub type Barrier = TrailingSetTopicAsId<(
165	// Weight that is paid for may be consumed.
166	TakeWeightCredit,
167	// Expected responses are OK.
168	AllowKnownQueryResponses<XcmPallet>,
169	WithComputedOrigin<
170		(
171			// If the message is one that immediately attempts to pay for execution, then allow it.
172			AllowTopLevelPaidExecutionFrom<Everything>,
173			// Subscriptions for version tracking are OK.
174			AllowSubscriptionsFrom<OnlyParachains>,
175			// Messages from system parachains or the Fellows plurality need not pay for execution.
176			AllowExplicitUnpaidExecutionFrom<(IsChildSystemParachain<ParaId>, Fellows)>,
177		),
178		UniversalLocation,
179		ConstU32<8>,
180	>,
181)>;
182
183/// Locations that will not be charged fees in the executor, neither for execution nor delivery.
184/// We only waive fees for system functions, which these locations represent.
185pub type WaivedLocations = (SystemParachains, Equals<RootLocation>, LocalPlurality);
186
187/// We let locations alias into child locations of their own.
188/// This is a very simple aliasing rule, mimicking the behaviour of
189/// the `DescendOrigin` instruction.
190pub type Aliasers = AliasChildLocation;
191
192pub struct XcmConfig;
193impl xcm_executor::Config for XcmConfig {
194	type RuntimeCall = RuntimeCall;
195	type XcmSender = XcmRouter;
196	type AssetTransactor = LocalAssetTransactor;
197	type OriginConverter = LocalOriginConverter;
198	type IsReserve = ();
199	type IsTeleporter = TrustedTeleporters;
200	type UniversalLocation = UniversalLocation;
201	type Barrier = Barrier;
202	type Weigher = WeightInfoBounds<
203		crate::weights::xcm::WestendXcmWeight<RuntimeCall>,
204		RuntimeCall,
205		MaxInstructions,
206	>;
207	type Trader =
208		UsingComponents<WeightToFee, TokenLocation, AccountId, Balances, ToAuthor<Runtime>>;
209	type ResponseHandler = XcmPallet;
210	type AssetTrap = XcmPallet;
211	type AssetLocker = ();
212	type AssetExchanger = ();
213	type AssetClaims = XcmPallet;
214	type SubscriptionService = XcmPallet;
215	type PalletInstancesInfo = AllPalletsWithSystem;
216	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
217	type FeeManager = XcmFeeManagerFromComponents<
218		WaivedLocations,
219		SendXcmFeeToAccount<Self::AssetTransactor, TreasuryAccount>,
220	>;
221	type MessageExporter = ();
222	type UniversalAliases = Nothing;
223	type CallDispatcher = RuntimeCall;
224	type SafeCallFilter = Everything;
225	type Aliasers = Aliasers;
226	type TransactionalProcessor = FrameTransactionalProcessor;
227	type HrmpNewChannelOpenRequestHandler = ();
228	type HrmpChannelAcceptedHandler = ();
229	type HrmpChannelClosingHandler = ();
230	type XcmRecorder = XcmPallet;
231}
232
233parameter_types! {
234	// `GeneralAdmin` pluralistic body.
235	pub const GeneralAdminBodyId: BodyId = BodyId::Administration;
236	// StakingAdmin pluralistic body.
237	pub const StakingAdminBodyId: BodyId = BodyId::Defense;
238	// FellowshipAdmin pluralistic body.
239	pub const FellowshipAdminBodyId: BodyId = BodyId::Index(FELLOWSHIP_ADMIN_INDEX);
240	// `Treasurer` pluralistic body.
241	pub const TreasurerBodyId: BodyId = BodyId::Treasury;
242}
243
244/// Type to convert the `GeneralAdmin` origin to a Plurality `Location` value.
245pub type GeneralAdminToPlurality =
246	OriginToPluralityVoice<RuntimeOrigin, GeneralAdmin, GeneralAdminBodyId>;
247
248/// location of this chain.
249pub type LocalOriginToLocation = (
250	GeneralAdminToPlurality,
251	// And a usual Signed origin to be used in XCM as a corresponding AccountId32
252	SignedToAccountId32<RuntimeOrigin, AccountId, ThisNetwork>,
253);
254
255/// Type to convert the `StakingAdmin` origin to a Plurality `Location` value.
256pub type StakingAdminToPlurality =
257	OriginToPluralityVoice<RuntimeOrigin, StakingAdmin, StakingAdminBodyId>;
258
259/// Type to convert the `FellowshipAdmin` origin to a Plurality `Location` value.
260pub type FellowshipAdminToPlurality =
261	OriginToPluralityVoice<RuntimeOrigin, FellowshipAdmin, FellowshipAdminBodyId>;
262
263/// Type to convert the `Treasurer` origin to a Plurality `Location` value.
264pub type TreasurerToPlurality = OriginToPluralityVoice<RuntimeOrigin, Treasurer, TreasurerBodyId>;
265
266/// Type to convert a pallet `Origin` type value into a `Location` value which represents an
267/// interior location of this chain for a destination chain.
268pub type LocalPalletOriginToLocation = (
269	// GeneralAdmin origin to be used in XCM as a corresponding Plurality `Location` value.
270	GeneralAdminToPlurality,
271	// StakingAdmin origin to be used in XCM as a corresponding Plurality `Location` value.
272	StakingAdminToPlurality,
273	// FellowshipAdmin origin to be used in XCM as a corresponding Plurality `Location` value.
274	FellowshipAdminToPlurality,
275	// `Treasurer` origin to be used in XCM as a corresponding Plurality `Location` value.
276	TreasurerToPlurality,
277);
278
279impl pallet_xcm::Config for Runtime {
280	type RuntimeEvent = RuntimeEvent;
281	// Note that this configuration of `SendXcmOrigin` is different from the one present in
282	// production.
283	type SendXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
284	type XcmRouter = XcmRouter;
285	// Anyone can execute XCM messages locally.
286	type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
287	type XcmExecuteFilter = Everything;
288	type XcmExecutor = XcmExecutor<XcmConfig>;
289	type XcmTeleportFilter = Everything;
290	type XcmReserveTransferFilter = Everything;
291	type Weigher = WeightInfoBounds<
292		crate::weights::xcm::WestendXcmWeight<RuntimeCall>,
293		RuntimeCall,
294		MaxInstructions,
295	>;
296	type UniversalLocation = UniversalLocation;
297	type RuntimeOrigin = RuntimeOrigin;
298	type RuntimeCall = RuntimeCall;
299	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
300	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
301	type Currency = Balances;
302	type CurrencyMatcher = IsConcrete<TokenLocation>;
303	type TrustedLockers = ();
304	type SovereignAccountOf = LocationConverter;
305	type MaxLockers = ConstU32<8>;
306	type MaxRemoteLockConsumers = ConstU32<0>;
307	type RemoteLockConsumerIdentifier = ();
308	type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
309	type AdminOrigin = EnsureRoot<AccountId>;
310}