referrerpolicy=no-referrer-when-downgrade

coretime_westend_runtime/
xcm_config.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17use super::{
18	AccountId, AllPalletsWithSystem, Balance, Balances, BaseDeliveryFee, Broker, FeeAssetId,
19	ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent,
20	RuntimeHoldReason, RuntimeOrigin, TransactionByteFee, WeightToFee, XcmpQueue,
21};
22use frame_support::{
23	pallet_prelude::PalletInfoAccess,
24	parameter_types,
25	traits::{
26		fungible::HoldConsideration, tokens::imbalance::ResolveTo, ConstU32, Contains, Equals,
27		Everything, LinearStoragePrice, Nothing,
28	},
29};
30use frame_system::EnsureRoot;
31use pallet_collator_selection::StakingPotAccountId;
32use pallet_xcm::{AuthorizedAliasers, XcmPassthrough};
33use parachains_common::{
34	xcm_config::{
35		AliasAccountId32FromSiblingSystemChain, AllSiblingSystemParachains,
36		ConcreteAssetFromSystem, ParentRelayOrSiblingParachains, RelayOrOtherSystemParachains,
37	},
38	TREASURY_PALLET_ID,
39};
40use polkadot_parachain_primitives::primitives::Sibling;
41use polkadot_runtime_common::xcm_sender::ExponentialPrice;
42use sp_runtime::traits::AccountIdConversion;
43use testnet_parachains_constants::westend::locations::AssetHubLocation;
44use westend_runtime_constants::system_parachain::COLLECTIVES_ID;
45use xcm::latest::{prelude::*, WESTEND_GENESIS_HASH};
46use xcm_builder::{
47	AccountId32Aliases, AliasChildLocation, AliasOriginRootUsingFilter,
48	AllowExplicitUnpaidExecutionFrom, AllowHrmpNotificationsFromRelayChain,
49	AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom,
50	DenyRecursively, DenyReserveTransferToRelayChain, DenyThenTry, DescribeAllTerminal,
51	DescribeFamily, EnsureXcmOrigin, FrameTransactionalProcessor, FungibleAdapter,
52	HashedDescription, IsConcrete, LocationAsSuperuser, NonFungibleAdapter, ParentAsSuperuser,
53	ParentIsPreset, RelayChainAsNative, SendXcmFeeToAccount, SiblingParachainAsNative,
54	SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32,
55	SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId, UsingComponents,
56	WeightInfoBounds, WithComputedOrigin, WithUniqueTopic, XcmFeeManagerFromComponents,
57};
58use xcm_executor::XcmExecutor;
59
60// Re-export
61pub use testnet_parachains_constants::westend::locations::GovernanceLocation;
62
63parameter_types! {
64	pub const RootLocation: Location = Location::here();
65	pub const TokenRelayLocation: Location = Location::parent();
66	pub const RelayNetwork: Option<NetworkId> = Some(NetworkId::ByGenesis(WESTEND_GENESIS_HASH));
67	pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
68	pub UniversalLocation: InteriorLocation =
69		[GlobalConsensus(RelayNetwork::get().unwrap()), Parachain(ParachainInfo::parachain_id().into())].into();
70	pub BrokerPalletLocation: Location =
71		PalletInstance(<Broker as PalletInfoAccess>::index() as u8).into();
72	pub const MaxInstructions: u32 = 100;
73	pub const MaxAssetsIntoHolding: u32 = 64;
74	pub FellowshipLocation: Location = Location::new(1, Parachain(COLLECTIVES_ID));
75}
76
77/// Type for specifying how a `Location` can be converted into an `AccountId`. This is used
78/// when determining ownership of accounts for asset transacting and when attempting to use XCM
79/// `Transact` in order to determine the dispatch Origin.
80pub type LocationToAccountId = (
81	// The parent (Relay-chain) origin converts to the parent `AccountId`.
82	ParentIsPreset<AccountId>,
83	// Sibling parachain origins convert to AccountId via the `ParaId::into`.
84	SiblingParachainConvertsVia<Sibling, AccountId>,
85	// Straight up local `AccountId32` origins just alias directly to `AccountId`.
86	AccountId32Aliases<RelayNetwork, AccountId>,
87	// Foreign locations alias into accounts according to a hash of their standard description.
88	HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
89);
90
91/// Means for transacting the native currency on this chain.
92pub type FungibleTransactor = FungibleAdapter<
93	// Use this currency:
94	Balances,
95	// Use this currency when it is a fungible asset matching the given location or name:
96	IsConcrete<TokenRelayLocation>,
97	// Do a simple punn to convert an `AccountId32` `Location` into a native chain
98	// `AccountId`:
99	LocationToAccountId,
100	// Our chain's `AccountId` type (we can't get away without mentioning it explicitly):
101	AccountId,
102	// We don't track any teleports of `Balances`.
103	(),
104>;
105
106/// Means for transacting coretime regions on this chain.
107pub type RegionTransactor = NonFungibleAdapter<
108	// Use this non-fungible implementation:
109	Broker,
110	// This adapter will handle coretime regions from the broker pallet.
111	IsConcrete<BrokerPalletLocation>,
112	// Convert an XCM Location into a local account id:
113	LocationToAccountId,
114	// Our chain's account ID type (we can't get away without mentioning it explicitly):
115	AccountId,
116	// We don't track any teleports.
117	(),
118>;
119
120/// Means for transacting assets on this chain.
121pub type AssetTransactors = (FungibleTransactor, RegionTransactor);
122
123/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
124/// ready for dispatching a transaction with XCM's `Transact`. There is an `OriginKind` that can
125/// bias the kind of local `Origin` it will become.
126pub type XcmOriginToTransactDispatchOrigin = (
127	// Governance location can gain root.
128	LocationAsSuperuser<Equals<GovernanceLocation>, RuntimeOrigin>,
129	// Sovereign account converter; this attempts to derive an `AccountId` from the origin location
130	// using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for
131	// foreign chains who want to have a local sovereign account on this chain that they control.
132	SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,
133	// Native converter for Relay-chain (Parent) location; will convert to a `Relay` origin when
134	// recognized.
135	RelayChainAsNative<RelayChainOrigin, RuntimeOrigin>,
136	// Native converter for sibling Parachains; will convert to a `SiblingPara` origin when
137	// recognized.
138	SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,
139	// Superuser converter for the Relay-chain (Parent) location. This will allow it to issue a
140	// transaction from the Root origin.
141	ParentAsSuperuser<RuntimeOrigin>,
142	// Native signed account converter; this just converts an `AccountId32` origin into a normal
143	// `RuntimeOrigin::Signed` origin of the same 32-byte value.
144	SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
145	// XCM origins can be represented natively under the XCM pallet's `Xcm` origin.
146	XcmPassthrough<RuntimeOrigin>,
147);
148
149pub struct ParentOrParentsPlurality;
150impl Contains<Location> for ParentOrParentsPlurality {
151	fn contains(location: &Location) -> bool {
152		matches!(location.unpack(), (1, []) | (1, [Plurality { .. }]))
153	}
154}
155
156pub struct FellowsPlurality;
157impl Contains<Location> for FellowsPlurality {
158	fn contains(location: &Location) -> bool {
159		matches!(
160			location.unpack(),
161			(1, [Parachain(COLLECTIVES_ID), Plurality { id: BodyId::Technical, .. }])
162		)
163	}
164}
165
166pub type Barrier = TrailingSetTopicAsId<
167	DenyThenTry<
168		DenyRecursively<DenyReserveTransferToRelayChain>,
169		(
170			// Allow local users to buy weight credit.
171			TakeWeightCredit,
172			// Expected responses are OK.
173			AllowKnownQueryResponses<PolkadotXcm>,
174			WithComputedOrigin<
175				(
176					// If the message is one that immediately attempts to pay for execution, then
177					// allow it.
178					AllowTopLevelPaidExecutionFrom<Everything>,
179					// Parent, its pluralities (i.e. governance bodies), and the Fellows plurality
180					// get free execution.
181					AllowExplicitUnpaidExecutionFrom<(
182						ParentOrParentsPlurality,
183						FellowsPlurality,
184						Equals<GovernanceLocation>,
185					)>,
186					// Subscriptions for version tracking are OK.
187					AllowSubscriptionsFrom<ParentRelayOrSiblingParachains>,
188					// HRMP notifications from the relay chain are OK.
189					AllowHrmpNotificationsFromRelayChain,
190				),
191				UniversalLocation,
192				ConstU32<8>,
193			>,
194		),
195	>,
196>;
197
198parameter_types! {
199	pub TreasuryAccount: AccountId = TREASURY_PALLET_ID.into_account_truncating();
200	pub RelayTreasuryLocation: Location = (Parent, PalletInstance(westend_runtime_constants::TREASURY_PALLET_ID)).into();
201}
202
203/// Locations that will not be charged fees in the executor, neither for execution nor delivery.
204/// We only waive fees for system functions, which these locations represent.
205pub type WaivedLocations = (
206	Equals<RootLocation>,
207	RelayOrOtherSystemParachains<AllSiblingSystemParachains, Runtime>,
208	Equals<RelayTreasuryLocation>,
209);
210
211/// Cases where a remote origin is accepted as trusted Teleporter for a given asset:
212/// - WND with the parent Relay Chain and sibling parachains.
213pub type TrustedTeleporters = ConcreteAssetFromSystem<TokenRelayLocation>;
214
215/// Defines origin aliasing rules for this chain.
216///
217/// - Allow any origin to alias into a child sub-location (equivalent to DescendOrigin),
218/// - Allow same accounts to alias into each other across system chains,
219/// - Allow AssetHub root to alias into anything,
220/// - Allow origins explicitly authorized to alias into target location.
221pub type TrustedAliasers = (
222	AliasChildLocation,
223	AliasAccountId32FromSiblingSystemChain,
224	AliasOriginRootUsingFilter<AssetHubLocation, Everything>,
225	AuthorizedAliasers<Runtime>,
226);
227
228pub struct XcmConfig;
229impl xcm_executor::Config for XcmConfig {
230	type RuntimeCall = RuntimeCall;
231	type XcmSender = XcmRouter;
232	type XcmEventEmitter = PolkadotXcm;
233	type AssetTransactor = AssetTransactors;
234	type OriginConverter = XcmOriginToTransactDispatchOrigin;
235	// Coretime chain does not recognize a reserve location for any asset. Users must teleport ROC
236	// where allowed (e.g. with the Relay Chain).
237	type IsReserve = ();
238	type IsTeleporter = TrustedTeleporters;
239	type UniversalLocation = UniversalLocation;
240	type Barrier = Barrier;
241	type Weigher = WeightInfoBounds<
242		crate::weights::xcm::CoretimeWestendXcmWeight<RuntimeCall>,
243		RuntimeCall,
244		MaxInstructions,
245	>;
246	type Trader = UsingComponents<
247		WeightToFee,
248		TokenRelayLocation,
249		AccountId,
250		Balances,
251		ResolveTo<StakingPotAccountId<Runtime>, Balances>,
252	>;
253	type ResponseHandler = PolkadotXcm;
254	type AssetTrap = PolkadotXcm;
255	type AssetClaims = PolkadotXcm;
256	type SubscriptionService = PolkadotXcm;
257	type PalletInstancesInfo = AllPalletsWithSystem;
258	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
259	type AssetLocker = ();
260	type AssetExchanger = ();
261	type FeeManager = XcmFeeManagerFromComponents<
262		WaivedLocations,
263		SendXcmFeeToAccount<Self::AssetTransactor, TreasuryAccount>,
264	>;
265	type MessageExporter = ();
266	type UniversalAliases = Nothing;
267	type CallDispatcher = RuntimeCall;
268	type SafeCallFilter = Everything;
269	type Aliasers = TrustedAliasers;
270	type TransactionalProcessor = FrameTransactionalProcessor;
271	type HrmpNewChannelOpenRequestHandler = ();
272	type HrmpChannelAcceptedHandler = ();
273	type HrmpChannelClosingHandler = ();
274	type XcmRecorder = PolkadotXcm;
275}
276
277/// Converts a local signed origin into an XCM location. Forms the basis for local origins
278/// sending/executing XCMs.
279pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
280
281pub type PriceForParentDelivery =
282	ExponentialPrice<FeeAssetId, BaseDeliveryFee, TransactionByteFee, ParachainSystem>;
283
284/// The means for routing XCM messages which are not for local execution into the right message
285/// queues.
286pub type XcmRouter = WithUniqueTopic<(
287	// Two routers - use UMP to communicate with the relay chain:
288	cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm, ()>,
289	// ..and XCMP to communicate with the sibling chains.
290	XcmpQueue,
291)>;
292
293parameter_types! {
294	pub const DepositPerItem: Balance = crate::deposit(1, 0);
295	pub const DepositPerByte: Balance = crate::deposit(0, 1);
296	pub const AuthorizeAliasHoldReason: RuntimeHoldReason = RuntimeHoldReason::PolkadotXcm(pallet_xcm::HoldReason::AuthorizeAlias);
297}
298
299impl pallet_xcm::Config for Runtime {
300	type RuntimeEvent = RuntimeEvent;
301	// We want to disallow users sending (arbitrary) XCM programs from this chain.
302	type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, ()>;
303	type XcmRouter = XcmRouter;
304	// We support local origins dispatching XCM executions.
305	type ExecuteXcmOrigin = 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::CoretimeWestendXcmWeight<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 = ();
322	type TrustedLockers = ();
323	type SovereignAccountOf = LocationToAccountId;
324	type MaxLockers = ConstU32<8>;
325	type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
326	type AdminOrigin = EnsureRoot<AccountId>;
327	type MaxRemoteLockConsumers = ConstU32<0>;
328	type RemoteLockConsumerIdentifier = ();
329	// xcm_executor::Config::Aliasers also uses pallet_xcm::AuthorizedAliasers.
330	type AuthorizedAliasConsideration = HoldConsideration<
331		AccountId,
332		Balances,
333		AuthorizeAliasHoldReason,
334		LinearStoragePrice<DepositPerItem, DepositPerByte, Balance>,
335	>;
336}
337
338impl cumulus_pallet_xcm::Config for Runtime {
339	type RuntimeEvent = RuntimeEvent;
340	type XcmExecutor = XcmExecutor<XcmConfig>;
341}