bridge_hub_rococo_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, FeeAssetId, ParachainInfo,
19	ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeHoldReason,
20	RuntimeOrigin, TransactionByteFee, WeightToFee, XcmOverBridgeHubWestend, XcmOverRococoBulletin,
21	XcmpQueue,
22};
23
24use core::marker::PhantomData;
25use frame_support::{
26	parameter_types,
27	traits::{
28		fungible::HoldConsideration, tokens::imbalance::ResolveTo, ConstU32, Contains, Equals,
29		Everything, LinearStoragePrice, Nothing,
30	},
31};
32use frame_system::EnsureRoot;
33use pallet_collator_selection::StakingPotAccountId;
34use pallet_xcm::{AuthorizedAliasers, XcmPassthrough};
35use parachains_common::{
36	xcm_config::{
37		AllSiblingSystemParachains, ConcreteAssetFromSystem, ParentRelayOrSiblingParachains,
38		RelayOrOtherSystemParachains,
39	},
40	TREASURY_PALLET_ID,
41};
42use polkadot_parachain_primitives::primitives::Sibling;
43use polkadot_runtime_common::xcm_sender::ExponentialPrice;
44use snowbridge_runtime_common::XcmExportFeeToSibling;
45use sp_runtime::traits::AccountIdConversion;
46use testnet_parachains_constants::rococo::snowbridge::EthereumNetwork;
47use xcm::latest::{prelude::*, ROCOCO_GENESIS_HASH};
48use xcm_builder::{
49	AccountId32Aliases, AliasChildLocation, AllowExplicitUnpaidExecutionFrom,
50	AllowHrmpNotificationsFromRelayChain, AllowKnownQueryResponses, AllowSubscriptionsFrom,
51	AllowTopLevelPaidExecutionFrom, DenyRecursively, DenyReserveTransferToRelayChain, DenyThenTry,
52	DescribeAllTerminal, DescribeFamily, EnsureXcmOrigin, ExternalConsensusLocationsConverterFor,
53	FrameTransactionalProcessor, FungibleAdapter, HandleFee, HashedDescription, IsConcrete,
54	ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SendXcmFeeToAccount,
55	SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,
56	SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId,
57	UsingComponents, WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
58};
59use xcm_executor::{
60	traits::{FeeManager, FeeReason, FeeReason::Export},
61	XcmExecutor,
62};
63
64parameter_types! {
65	pub const RootLocation: Location = Location::here();
66	pub const TokenLocation: Location = Location::parent();
67	pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
68	pub RelayNetwork: NetworkId = NetworkId::ByGenesis(ROCOCO_GENESIS_HASH);
69	pub UniversalLocation: InteriorLocation =
70		[GlobalConsensus(RelayNetwork::get()), Parachain(ParachainInfo::parachain_id().into())].into();
71	pub const MaxInstructions: u32 = 100;
72	pub const MaxAssetsIntoHolding: u32 = 64;
73	pub TreasuryAccount: AccountId = TREASURY_PALLET_ID.into_account_truncating();
74	pub RelayTreasuryLocation: Location = (Parent, PalletInstance(rococo_runtime_constants::TREASURY_PALLET_ID)).into();
75	pub SiblingPeople: Location = (Parent, Parachain(rococo_runtime_constants::system_parachain::PEOPLE_ID)).into();
76}
77
78/// Type for specifying how a `Location` can be converted into an `AccountId`. This is used
79/// when determining ownership of accounts for asset transacting and when attempting to use XCM
80/// `Transact` in order to determine the dispatch Origin.
81pub type LocationToAccountId = (
82	// The parent (Relay-chain) origin converts to the parent `AccountId`.
83	ParentIsPreset<AccountId>,
84	// Sibling parachain origins convert to AccountId via the `ParaId::into`.
85	SiblingParachainConvertsVia<Sibling, AccountId>,
86	// Straight up local `AccountId32` origins just alias directly to `AccountId`.
87	AccountId32Aliases<RelayNetwork, AccountId>,
88	// Foreign locations alias into accounts according to a hash of their standard description.
89	HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
90	// Different global consensus locations sovereign accounts.
91	ExternalConsensusLocationsConverterFor<UniversalLocation, AccountId>,
92);
93
94/// Means for transacting the native currency on this chain.
95pub type FungibleTransactor = FungibleAdapter<
96	// Use this currency:
97	Balances,
98	// Use this currency when it is a fungible asset matching the given location or name:
99	IsConcrete<TokenLocation>,
100	// Do a simple punn to convert an AccountId32 Location into a native chain account ID:
101	LocationToAccountId,
102	// Our chain's account ID type (we can't get away without mentioning it explicitly):
103	AccountId,
104	// We don't track any teleports of `Balances`.
105	(),
106>;
107
108/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
109/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
110/// biases the kind of local `Origin` it will become.
111pub type XcmOriginToTransactDispatchOrigin = (
112	// Sovereign account converter; this attempts to derive an `AccountId` from the origin location
113	// using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for
114	// foreign chains who want to have a local sovereign account on this chain which they control.
115	SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,
116	// Native converter for Relay-chain (Parent) location; will convert to a `Relay` origin when
117	// recognized.
118	RelayChainAsNative<RelayChainOrigin, RuntimeOrigin>,
119	// Native converter for sibling Parachains; will convert to a `SiblingPara` origin when
120	// recognized.
121	SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,
122	// Superuser converter for the Relay-chain (Parent) location. This will allow it to issue a
123	// transaction from the Root origin.
124	ParentAsSuperuser<RuntimeOrigin>,
125	// Native signed account converter; this just converts an `AccountId32` origin into a normal
126	// `RuntimeOrigin::Signed` origin of the same 32-byte value.
127	SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
128	// Xcm origins can be represented natively under the Xcm pallet's Xcm origin.
129	XcmPassthrough<RuntimeOrigin>,
130);
131
132pub struct ParentOrParentsPlurality;
133impl Contains<Location> for ParentOrParentsPlurality {
134	fn contains(location: &Location) -> bool {
135		matches!(location.unpack(), (1, []) | (1, [Plurality { .. }]))
136	}
137}
138
139pub type Barrier = TrailingSetTopicAsId<
140	DenyThenTry<
141		DenyRecursively<DenyReserveTransferToRelayChain>,
142		(
143			// Allow local users to buy weight credit.
144			TakeWeightCredit,
145			// Expected responses are OK.
146			AllowKnownQueryResponses<PolkadotXcm>,
147			WithComputedOrigin<
148				(
149					// If the message is one that immediately attempts to pay for execution, then
150					// allow it.
151					AllowTopLevelPaidExecutionFrom<Everything>,
152					// Parent, its pluralities (i.e. governance bodies), relay treasury pallet
153					// and sibling People get free execution.
154					AllowExplicitUnpaidExecutionFrom<(
155						ParentOrParentsPlurality,
156						Equals<RelayTreasuryLocation>,
157						Equals<SiblingPeople>,
158					)>,
159					// Subscriptions for version tracking are OK.
160					AllowSubscriptionsFrom<ParentRelayOrSiblingParachains>,
161					// HRMP notifications from the relay chain are OK.
162					AllowHrmpNotificationsFromRelayChain,
163				),
164				UniversalLocation,
165				ConstU32<8>,
166			>,
167		),
168	>,
169>;
170
171/// Locations that will not be charged fees in the executor,
172/// either execution or delivery.
173/// We only waive fees for system functions, which these locations represent.
174pub type WaivedLocations = (
175	Equals<RootLocation>,
176	RelayOrOtherSystemParachains<AllSiblingSystemParachains, Runtime>,
177	Equals<RelayTreasuryLocation>,
178);
179
180/// Cases where a remote origin is accepted as trusted Teleporter for a given asset:
181/// - NativeToken with the parent Relay Chain and sibling parachains.
182pub type TrustedTeleporters = ConcreteAssetFromSystem<TokenLocation>;
183
184/// Defines origin aliasing rules for this chain.
185///
186/// - Allow any origin to alias into a child sub-location (equivalent to DescendOrigin),
187/// - Allow origins explicitly authorized by the alias target location.
188pub type TrustedAliasers = (AliasChildLocation, AuthorizedAliasers<Runtime>);
189
190pub struct XcmConfig;
191impl xcm_executor::Config for XcmConfig {
192	type RuntimeCall = RuntimeCall;
193	type XcmSender = XcmRouter;
194	type XcmEventEmitter = PolkadotXcm;
195	type AssetTransactor = FungibleTransactor;
196	type OriginConverter = XcmOriginToTransactDispatchOrigin;
197	// BridgeHub does not recognize a reserve location for any asset. Users must teleport Native
198	// token where allowed (e.g. with the Relay Chain).
199	type IsReserve = ();
200	type IsTeleporter = TrustedTeleporters;
201	type UniversalLocation = UniversalLocation;
202	type Barrier = Barrier;
203	type Weigher = WeightInfoBounds<
204		crate::weights::xcm::BridgeHubRococoXcmWeight<RuntimeCall>,
205		RuntimeCall,
206		MaxInstructions,
207	>;
208	type Trader = UsingComponents<
209		WeightToFee,
210		TokenLocation,
211		AccountId,
212		Balances,
213		ResolveTo<StakingPotAccountId<Runtime>, Balances>,
214	>;
215	type ResponseHandler = PolkadotXcm;
216	type AssetTrap = PolkadotXcm;
217	type AssetLocker = ();
218	type AssetExchanger = ();
219	type AssetClaims = PolkadotXcm;
220	type SubscriptionService = PolkadotXcm;
221	type PalletInstancesInfo = AllPalletsWithSystem;
222	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
223	type FeeManager = XcmFeeManagerFromComponentsBridgeHub<
224		WaivedLocations,
225		(
226			XcmExportFeeToSibling<
227				bp_rococo::Balance,
228				AccountId,
229				TokenLocation,
230				EthereumNetwork,
231				Self::AssetTransactor,
232				crate::EthereumOutboundQueue,
233			>,
234			SendXcmFeeToAccount<Self::AssetTransactor, TreasuryAccount>,
235		),
236	>;
237	type MessageExporter = (
238		XcmOverBridgeHubWestend,
239		XcmOverRococoBulletin,
240		crate::bridge_to_ethereum_config::SnowbridgeExporter,
241	);
242	type UniversalAliases = Nothing;
243	type CallDispatcher = RuntimeCall;
244	type SafeCallFilter = Everything;
245	type Aliasers = TrustedAliasers;
246	type TransactionalProcessor = FrameTransactionalProcessor;
247	type HrmpNewChannelOpenRequestHandler = ();
248	type HrmpChannelAcceptedHandler = ();
249	type HrmpChannelClosingHandler = ();
250	type XcmRecorder = PolkadotXcm;
251}
252
253pub type PriceForParentDelivery =
254	ExponentialPrice<FeeAssetId, BaseDeliveryFee, TransactionByteFee, ParachainSystem>;
255
256/// Converts a local signed origin into an XCM location. Forms the basis for local origins
257/// sending/executing XCMs.
258pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
259
260/// The means for routing XCM messages which are not for local execution into the right message
261/// queues.
262pub type XcmRouter = WithUniqueTopic<(
263	// Two routers - use UMP to communicate with the relay chain:
264	cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm, PriceForParentDelivery>,
265	// ..and XCMP to communicate with the sibling chains.
266	XcmpQueue,
267)>;
268
269parameter_types! {
270	pub const DepositPerItem: Balance = crate::deposit(1, 0);
271	pub const DepositPerByte: Balance = crate::deposit(0, 1);
272	pub const AuthorizeAliasHoldReason: RuntimeHoldReason = RuntimeHoldReason::PolkadotXcm(pallet_xcm::HoldReason::AuthorizeAlias);
273}
274
275impl pallet_xcm::Config for Runtime {
276	type RuntimeEvent = RuntimeEvent;
277	type XcmRouter = XcmRouter;
278	// We want to disallow users sending (arbitrary) XCMs from this chain.
279	type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, ()>;
280	// We support local origins dispatching XCM executions.
281	type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
282	type XcmExecuteFilter = Everything;
283	type XcmExecutor = XcmExecutor<XcmConfig>;
284	type XcmTeleportFilter = Everything;
285	type XcmReserveTransferFilter = Nothing; // This parachain is not meant as a reserve location.
286	type Weigher = WeightInfoBounds<
287		crate::weights::xcm::BridgeHubRococoXcmWeight<RuntimeCall>,
288		RuntimeCall,
289		MaxInstructions,
290	>;
291	type UniversalLocation = UniversalLocation;
292	type RuntimeOrigin = RuntimeOrigin;
293	type RuntimeCall = RuntimeCall;
294	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
295	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
296	type Currency = Balances;
297	type CurrencyMatcher = ();
298	type TrustedLockers = ();
299	type SovereignAccountOf = LocationToAccountId;
300	type MaxLockers = ConstU32<8>;
301	type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
302	type AdminOrigin = EnsureRoot<AccountId>;
303	type MaxRemoteLockConsumers = ConstU32<0>;
304	type RemoteLockConsumerIdentifier = ();
305	// xcm_executor::Config::Aliasers also uses pallet_xcm::AuthorizedAliasers.
306	type AuthorizedAliasConsideration = HoldConsideration<
307		AccountId,
308		Balances,
309		AuthorizeAliasHoldReason,
310		LinearStoragePrice<DepositPerItem, DepositPerByte, Balance>,
311	>;
312}
313
314impl cumulus_pallet_xcm::Config for Runtime {
315	type RuntimeEvent = RuntimeEvent;
316	type XcmExecutor = XcmExecutor<XcmConfig>;
317}
318
319pub struct XcmFeeManagerFromComponentsBridgeHub<WaivedLocations, HandleFee>(
320	PhantomData<(WaivedLocations, HandleFee)>,
321);
322impl<WaivedLocations: Contains<Location>, FeeHandler: HandleFee> FeeManager
323	for XcmFeeManagerFromComponentsBridgeHub<WaivedLocations, FeeHandler>
324{
325	fn is_waived(origin: Option<&Location>, fee_reason: FeeReason) -> bool {
326		let Some(loc) = origin else { return false };
327		if let Export { network, destination: Here } = fee_reason {
328			if network == EthereumNetwork::get().into() {
329				return false
330			}
331		}
332		WaivedLocations::contains(loc)
333	}
334
335	fn handle_fee(fee: Assets, context: Option<&XcmContext>, reason: FeeReason) {
336		FeeHandler::handle_fee(fee, context, reason);
337	}
338}