referrerpolicy=no-referrer-when-downgrade

pallet_staking_async_rc_runtime/
xcm_config.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Substrate.
3
4// Substrate 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// Substrate 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 Substrate.  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, Balance, RuntimeHoldReason};
25use frame_support::{
26	parameter_types,
27	traits::{
28		fungible::HoldConsideration, Contains, Equals, Everything, LinearStoragePrice, Nothing,
29	},
30};
31use frame_system::EnsureRoot;
32use pallet_staking_async_rc_runtime_constants::{
33	currency::CENTS, system_parachain::*, xcm::body::FELLOWSHIP_ADMIN_INDEX,
34};
35use pallet_xcm::XcmPassthrough;
36use polkadot_runtime_common::{
37	xcm_sender::{ChildParachainRouter, ExponentialPrice},
38	ToAuthor,
39};
40use sp_core::ConstU32;
41use xcm::latest::{prelude::*, WESTEND_GENESIS_HASH};
42use xcm_builder::{
43	AccountId32Aliases, AliasChildLocation, AllowExplicitUnpaidExecutionFrom,
44	AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom,
45	ChildParachainAsNative, ChildParachainConvertsVia, DescribeAllTerminal, DescribeFamily,
46	FrameTransactionalProcessor, FungibleAdapter, HashedDescription, IsChildSystemParachain,
47	IsConcrete, MintLocation, OriginToPluralityVoice, SendXcmFeeToAccount,
48	SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
49	TrailingSetTopicAsId, UsingComponents, WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
50	XcmFeeManagerFromComponents,
51};
52use xcm_executor::XcmExecutor;
53
54parameter_types! {
55	pub const TokenLocation: Location = Here.into_location();
56	pub const RootLocation: Location = Location::here();
57	pub const ThisNetwork: NetworkId = ByGenesis(WESTEND_GENESIS_HASH);
58	pub UniversalLocation: InteriorLocation = [GlobalConsensus(ThisNetwork::get())].into();
59	pub CheckAccount: AccountId = XcmPallet::check_account();
60	pub LocalCheckAccount: (AccountId, MintLocation) = (CheckAccount::get(), MintLocation::Local);
61	pub TreasuryAccount: AccountId = Treasury::account_id();
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	/// Westend does not have mint authority anymore after the Asset Hub migration.
67	pub TeleportTracking: Option<(AccountId, MintLocation)> = None;
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	// Teleport tracking
89	TeleportTracking,
90>;
91
92type LocalOriginConverter = (
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 Collectives: Location = Parachain(COLLECTIVES_ID).into_location();
119	pub BridgeHub: Location = Parachain(BRIDGE_HUB_ID).into_location();
120	pub Encointer: Location = Parachain(ENCOINTER_ID).into_location();
121	pub People: Location = Parachain(PEOPLE_ID).into_location();
122	pub Broker: Location = Parachain(BROKER_ID).into_location();
123	pub Wnd: AssetFilter = Wild(AllOf { fun: WildFungible, id: AssetId(TokenLocation::get()) });
124	pub WndForAssetHub: (AssetFilter, Location) = (Wnd::get(), AssetHub::get());
125	pub WndForCollectives: (AssetFilter, Location) = (Wnd::get(), Collectives::get());
126	pub WndForBridgeHub: (AssetFilter, Location) = (Wnd::get(), BridgeHub::get());
127	pub WndForEncointer: (AssetFilter, Location) = (Wnd::get(), Encointer::get());
128	pub WndForPeople: (AssetFilter, Location) = (Wnd::get(), People::get());
129	pub WndForBroker: (AssetFilter, Location) = (Wnd::get(), Broker::get());
130	pub MaxInstructions: u32 = 100;
131	pub MaxAssetsIntoHolding: u32 = 64;
132}
133
134pub type TrustedTeleporters = (
135	xcm_builder::Case<WndForAssetHub>,
136	xcm_builder::Case<WndForCollectives>,
137	xcm_builder::Case<WndForBridgeHub>,
138	xcm_builder::Case<WndForEncointer>,
139	xcm_builder::Case<WndForPeople>,
140	xcm_builder::Case<WndForBroker>,
141);
142
143pub struct OnlyParachains;
144impl Contains<Location> for OnlyParachains {
145	fn contains(location: &Location) -> bool {
146		matches!(location.unpack(), (0, [Parachain(_)]))
147	}
148}
149
150pub struct Fellows;
151impl Contains<Location> for Fellows {
152	fn contains(location: &Location) -> bool {
153		matches!(
154			location.unpack(),
155			(0, [Parachain(COLLECTIVES_ID), Plurality { id: BodyId::Technical, .. }])
156		)
157	}
158}
159
160pub struct LocalPlurality;
161impl Contains<Location> for LocalPlurality {
162	fn contains(loc: &Location) -> bool {
163		matches!(loc.unpack(), (0, [Plurality { .. }]))
164	}
165}
166
167/// The barriers one of which must be passed for an XCM message to be executed.
168pub type Barrier = TrailingSetTopicAsId<(
169	// Weight that is paid for may be consumed.
170	TakeWeightCredit,
171	// Expected responses are OK.
172	AllowKnownQueryResponses<XcmPallet>,
173	WithComputedOrigin<
174		(
175			// If the message is one that immediately attempts to pay for execution, then allow it.
176			AllowTopLevelPaidExecutionFrom<Everything>,
177			// Subscriptions for version tracking are OK.
178			AllowSubscriptionsFrom<OnlyParachains>,
179			// Messages from system parachains or the Fellows plurality need not pay for execution.
180			AllowExplicitUnpaidExecutionFrom<(IsChildSystemParachain<ParaId>, Fellows)>,
181		),
182		UniversalLocation,
183		ConstU32<8>,
184	>,
185)>;
186
187/// Locations that will not be charged fees in the executor, neither for execution nor delivery.
188/// We only waive fees for system functions, which these locations represent.
189pub type WaivedLocations = (SystemParachains, Equals<RootLocation>, LocalPlurality);
190
191/// We let locations alias into child locations of their own.
192/// This is a very simple aliasing rule, mimicking the behaviour of
193/// the `DescendOrigin` instruction.
194pub type Aliasers = AliasChildLocation;
195
196pub struct XcmConfig;
197impl xcm_executor::Config for XcmConfig {
198	type RuntimeCall = RuntimeCall;
199	type XcmSender = XcmRouter;
200	type AssetTransactor = LocalAssetTransactor;
201	type OriginConverter = LocalOriginConverter;
202	type IsReserve = ();
203	type XcmEventEmitter = XcmPallet;
204	type IsTeleporter = TrustedTeleporters;
205	type UniversalLocation = UniversalLocation;
206	type Barrier = Barrier;
207	type Weigher = WeightInfoBounds<
208		crate::weights::xcm::WestendXcmWeight<RuntimeCall>,
209		RuntimeCall,
210		MaxInstructions,
211	>;
212	type Trader =
213		UsingComponents<WeightToFee, TokenLocation, AccountId, Balances, ToAuthor<Runtime>>;
214	type ResponseHandler = XcmPallet;
215	type AssetTrap = XcmPallet;
216	type AssetLocker = ();
217	type AssetExchanger = ();
218	type AssetClaims = XcmPallet;
219	type SubscriptionService = XcmPallet;
220	type PalletInstancesInfo = AllPalletsWithSystem;
221	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
222	type FeeManager = XcmFeeManagerFromComponents<
223		WaivedLocations,
224		SendXcmFeeToAccount<Self::AssetTransactor, TreasuryAccount>,
225	>;
226	type MessageExporter = ();
227	type UniversalAliases = Nothing;
228	type CallDispatcher = RuntimeCall;
229	type SafeCallFilter = Everything;
230	type Aliasers = Aliasers;
231	type TransactionalProcessor = FrameTransactionalProcessor;
232	type HrmpNewChannelOpenRequestHandler = ();
233	type HrmpChannelAcceptedHandler = ();
234	type HrmpChannelClosingHandler = ();
235	type XcmRecorder = XcmPallet;
236}
237
238parameter_types! {
239	// `GeneralAdmin` pluralistic body.
240	pub const GeneralAdminBodyId: BodyId = BodyId::Administration;
241	// StakingAdmin pluralistic body.
242	pub const StakingAdminBodyId: BodyId = BodyId::Defense;
243	// FellowshipAdmin pluralistic body.
244	pub const FellowshipAdminBodyId: BodyId = BodyId::Index(FELLOWSHIP_ADMIN_INDEX);
245	// `Treasurer` pluralistic body.
246	pub const TreasurerBodyId: BodyId = BodyId::Treasury;
247
248	pub const DepositPerItem: Balance = crate::deposit(1, 0);
249	pub const DepositPerByte: Balance = crate::deposit(0, 1);
250	pub const AuthorizeAliasHoldReason: RuntimeHoldReason = RuntimeHoldReason::XcmPallet(pallet_xcm::HoldReason::AuthorizeAlias);
251}
252
253/// Type to convert the `GeneralAdmin` origin to a Plurality `Location` value.
254pub type GeneralAdminToPlurality =
255	OriginToPluralityVoice<RuntimeOrigin, GeneralAdmin, GeneralAdminBodyId>;
256
257/// location of this chain.
258pub type LocalOriginToLocation = (
259	GeneralAdminToPlurality,
260	// And a usual Signed origin to be used in XCM as a corresponding AccountId32
261	SignedToAccountId32<RuntimeOrigin, AccountId, ThisNetwork>,
262);
263
264/// Type to convert the `StakingAdmin` origin to a Plurality `Location` value.
265pub type StakingAdminToPlurality =
266	OriginToPluralityVoice<RuntimeOrigin, StakingAdmin, StakingAdminBodyId>;
267
268/// Type to convert the `FellowshipAdmin` origin to a Plurality `Location` value.
269pub type FellowshipAdminToPlurality =
270	OriginToPluralityVoice<RuntimeOrigin, FellowshipAdmin, FellowshipAdminBodyId>;
271
272/// Type to convert the `Treasurer` origin to a Plurality `Location` value.
273pub type TreasurerToPlurality = OriginToPluralityVoice<RuntimeOrigin, Treasurer, TreasurerBodyId>;
274
275/// Type to convert a pallet `Origin` type value into a `Location` value which represents an
276/// interior location of this chain for a destination chain.
277pub type LocalPalletOriginToLocation = (
278	// GeneralAdmin origin to be used in XCM as a corresponding Plurality `Location` value.
279	GeneralAdminToPlurality,
280	// StakingAdmin origin to be used in XCM as a corresponding Plurality `Location` value.
281	StakingAdminToPlurality,
282	// FellowshipAdmin origin to be used in XCM as a corresponding Plurality `Location` value.
283	FellowshipAdminToPlurality,
284	// `Treasurer` origin to be used in XCM as a corresponding Plurality `Location` value.
285	TreasurerToPlurality,
286);
287
288impl pallet_xcm::Config for Runtime {
289	type RuntimeEvent = RuntimeEvent;
290	// Note that this configuration of `SendXcmOrigin` is different from the one present in
291	// production.
292	type SendXcmOrigin = xcm_builder::EnsureXcmOrigin<
293		RuntimeOrigin,
294		(LocalPalletOriginToLocation, LocalOriginToLocation),
295	>;
296	type XcmRouter = XcmRouter;
297	// Anyone can execute XCM messages locally.
298	type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
299	type XcmExecuteFilter = Everything;
300	type XcmExecutor = XcmExecutor<XcmConfig>;
301	type XcmTeleportFilter = Everything;
302	type XcmReserveTransferFilter = Everything;
303	type Weigher = WeightInfoBounds<
304		crate::weights::xcm::WestendXcmWeight<RuntimeCall>,
305		RuntimeCall,
306		MaxInstructions,
307	>;
308	type UniversalLocation = UniversalLocation;
309	type RuntimeOrigin = RuntimeOrigin;
310	type RuntimeCall = RuntimeCall;
311	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
312	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
313	type Currency = Balances;
314	type CurrencyMatcher = IsConcrete<TokenLocation>;
315	type TrustedLockers = ();
316	type SovereignAccountOf = LocationConverter;
317	type MaxLockers = ConstU32<8>;
318	type MaxRemoteLockConsumers = ConstU32<0>;
319	type RemoteLockConsumerIdentifier = ();
320	type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
321	type AdminOrigin = EnsureRoot<AccountId>;
322	type AuthorizedAliasConsideration = HoldConsideration<
323		AccountId,
324		Balances,
325		AuthorizeAliasHoldReason,
326		LinearStoragePrice<DepositPerItem, DepositPerByte, Balance>,
327	>;
328}