coretime_rococo_runtime/
xcm_config.rs

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