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