referrerpolicy=no-referrer-when-downgrade

collectives_westend_runtime/
xcm_config.rs

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