Skip to main content

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	// TODO: once DAP allocates collator budgets, redirect XCM execution fees to DAP satellite
247	// instead of StakingPot (use crate::DealWithFeesSatellite as the OnUnbalanced handler).
248	type Trader = UsingComponents<
249		WeightToFee,
250		TokenRelayLocation,
251		AccountId,
252		Balances,
253		ResolveTo<StakingPotAccountId<Runtime>, Balances>,
254	>;
255	type ResponseHandler = PolkadotXcm;
256	type AssetTrap = PolkadotXcm;
257	type SubscriptionService = PolkadotXcm;
258	type PalletInstancesInfo = AllPalletsWithSystem;
259	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
260	type AssetLocker = ();
261	type AssetExchanger = ();
262	// TODO: once DAP allocates budgets, split delivery fees to DAP via a HandleFee wrapper.
263	type FeeManager = XcmFeeManagerFromComponents<
264		WaivedLocations,
265		SendXcmFeeToAccount<Self::AssetTransactor, TreasuryAccount>,
266	>;
267	type MessageExporter = ();
268	type UniversalAliases = Nothing;
269	type CallDispatcher = RuntimeCall;
270	type SafeCallFilter = Everything;
271	type Aliasers = TrustedAliasers;
272	type TransactionalProcessor = FrameTransactionalProcessor;
273	type HrmpNewChannelOpenRequestHandler = ();
274	type HrmpChannelAcceptedHandler = ();
275	type HrmpChannelClosingHandler = ();
276	type XcmRecorder = PolkadotXcm;
277}
278
279/// Converts a local signed origin into an XCM location. Forms the basis for local origins
280/// sending/executing XCMs.
281pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
282
283pub type PriceForParentDelivery =
284	ExponentialPrice<FeeAssetId, BaseDeliveryFee, TransactionByteFee, ParachainSystem>;
285
286/// The means for routing XCM messages which are not for local execution into the right message
287/// queues.
288pub type XcmRouter = WithUniqueTopic<(
289	// Two routers - use UMP to communicate with the relay chain:
290	cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm, PriceForParentDelivery>,
291	// ..and XCMP to communicate with the sibling chains.
292	XcmpQueue,
293)>;
294
295parameter_types! {
296	pub const DepositPerItem: Balance = crate::deposit(1, 0);
297	pub const DepositPerByte: Balance = crate::deposit(0, 1);
298	pub const AuthorizeAliasHoldReason: RuntimeHoldReason = RuntimeHoldReason::PolkadotXcm(pallet_xcm::HoldReason::AuthorizeAlias);
299}
300
301impl pallet_xcm::Config for Runtime {
302	type RuntimeEvent = RuntimeEvent;
303	// We want to disallow users sending (arbitrary) XCM programs from this chain.
304	type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, ()>;
305	type XcmRouter = XcmRouter;
306	// We support local origins dispatching XCM executions.
307	type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
308	type XcmExecuteFilter = Everything;
309	type XcmExecutor = XcmExecutor<XcmConfig>;
310	type XcmTeleportFilter = Everything;
311	type XcmReserveTransferFilter = Everything;
312	type Weigher = WeightInfoBounds<
313		crate::weights::xcm::CoretimeWestendXcmWeight<RuntimeCall>,
314		RuntimeCall,
315		MaxInstructions,
316	>;
317	type UniversalLocation = UniversalLocation;
318	type RuntimeOrigin = RuntimeOrigin;
319	type RuntimeCall = RuntimeCall;
320	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
321	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
322	type Currency = Balances;
323	type CurrencyMatcher = ();
324	type TrustedLockers = ();
325	type SovereignAccountOf = LocationToAccountId;
326	type MaxLockers = ConstU32<8>;
327	type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
328	type AdminOrigin = EnsureRoot<AccountId>;
329	type MaxRemoteLockConsumers = ConstU32<0>;
330	type RemoteLockConsumerIdentifier = ();
331	// xcm_executor::Config::Aliasers also uses pallet_xcm::AuthorizedAliasers.
332	type AuthorizedAliasConsideration = HoldConsideration<
333		AccountId,
334		Balances,
335		AuthorizeAliasHoldReason,
336		LinearStoragePrice<DepositPerItem, DepositPerByte, Balance>,
337	>;
338}
339
340impl cumulus_pallet_xcm::Config for Runtime {
341	type RuntimeEvent = RuntimeEvent;
342	type XcmExecutor = XcmExecutor<XcmConfig>;
343}