contracts_parachain_runtime/
xcm_config.rs

1use super::{
2	AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm,
3	Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue,
4};
5use frame_support::{
6	parameter_types,
7	traits::{ConstU32, Contains, Everything, Nothing},
8	weights::Weight,
9};
10use frame_system::EnsureRoot;
11use pallet_xcm::XcmPassthrough;
12use polkadot_runtime_common::impls::ToAuthor;
13use xcm::latest::prelude::*;
14#[allow(deprecated)]
15use xcm_builder::CurrencyAdapter;
16use xcm_builder::{
17	AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowTopLevelPaidExecutionFrom,
18	DenyReserveTransferToRelayChain, DenyThenTry, EnsureXcmOrigin, FixedWeightBounds,
19	FrameTransactionalProcessor, IsConcrete, NativeAsset, ParentIsPreset, RelayChainAsNative,
20	SiblingParachainAsNative, SignedAccountId32AsNative, SignedToAccountId32,
21	SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId, UsingComponents,
22	WithComputedOrigin, WithUniqueTopic,
23};
24use xcm_executor::XcmExecutor;
25
26parameter_types! {
27	pub const TokenLocation: Location = Here.into_location();
28	pub const RelayLocation: Location = Location::parent();
29	pub const RelayNetwork: Option<NetworkId> = None;
30	pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
31	pub UniversalLocation: InteriorLocation = Parachain(ParachainInfo::parachain_id().into()).into();
32}
33
34/// Type for specifying how a `Location` can be converted into an `AccountId`. This is used
35/// when determining ownership of accounts for asset transacting and when attempting to use XCM
36/// `Transact` in order to determine the dispatch Origin.
37pub type LocationToAccountId = (
38	// Straight up local `AccountId32` origins just alias directly to `AccountId`.
39	AccountId32Aliases<RelayNetwork, AccountId>,
40	// The parent (Relay-chain) origin converts to the parent `AccountId`.
41	ParentIsPreset<AccountId>,
42);
43
44/// Means for transacting assets on this chain.
45#[allow(deprecated)]
46pub type LocalAssetTransactor = CurrencyAdapter<
47	// Use this currency:
48	Balances,
49	// Use this currency when it is a fungible asset matching the given location or name:
50	IsConcrete<RelayLocation>,
51	// Do a simple punn to convert an AccountId32 Location into a native chain account ID:
52	LocationToAccountId,
53	// Our chain's account ID type (we can't get away without mentioning it explicitly):
54	AccountId,
55	// We don't track any teleports.
56	(),
57>;
58
59#[allow(deprecated)]
60pub type LocalBalancesTransactor =
61	CurrencyAdapter<Balances, IsConcrete<TokenLocation>, LocationToAccountId, AccountId, ()>;
62
63pub type AssetTransactors = (LocalBalancesTransactor, LocalAssetTransactor);
64
65/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
66/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
67/// biases the kind of local `Origin` it will become.
68pub type XcmOriginToTransactDispatchOrigin = (
69	// Sovereign account converter; this attempts to derive an `AccountId` from the origin location
70	// using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for
71	// foreign chains who want to have a local sovereign account on this chain which they control.
72	SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,
73	// Native converter for Relay-chain (Parent) location; will convert to a `Relay` origin when
74	// recognized.
75	RelayChainAsNative<RelayChainOrigin, RuntimeOrigin>,
76	// Native converter for sibling Parachains; will convert to a `SiblingPara` origin when
77	// recognized.
78	SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,
79	// Native signed account converter; this just converts an `AccountId32` origin into a normal
80	// `RuntimeOrigin::Signed` origin of the same 32-byte value.
81	SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
82	// Xcm origins can be represented natively under the Xcm pallet's Xcm origin.
83	XcmPassthrough<RuntimeOrigin>,
84);
85
86parameter_types! {
87	// One XCM operation is 1_000_000_000 weight - almost certainly a conservative estimate.
88	pub UnitWeightCost: Weight = Weight::from_parts(1_000_000_000, 64 * 1024);
89	pub const MaxInstructions: u32 = 100;
90	pub const MaxAssetsIntoHolding: u32 = 64;
91}
92
93pub struct ParentOrParentsPlurality;
94impl Contains<Location> for ParentOrParentsPlurality {
95	fn contains(location: &Location) -> bool {
96		matches!(location.unpack(), (1, []) | (1, [Plurality { .. }]))
97	}
98}
99
100pub type Barrier = TrailingSetTopicAsId<
101	DenyThenTry<
102		DenyReserveTransferToRelayChain,
103		(
104			TakeWeightCredit,
105			WithComputedOrigin<
106				(
107					AllowTopLevelPaidExecutionFrom<Everything>,
108					AllowExplicitUnpaidExecutionFrom<ParentOrParentsPlurality>,
109					// ^^^ Parent and its exec plurality get free execution
110				),
111				UniversalLocation,
112				ConstU32<8>,
113			>,
114		),
115	>,
116>;
117
118pub struct XcmConfig;
119impl xcm_executor::Config for XcmConfig {
120	type RuntimeCall = RuntimeCall;
121	type XcmSender = XcmRouter;
122	type AssetTransactor = AssetTransactors;
123	type OriginConverter = XcmOriginToTransactDispatchOrigin;
124	type IsReserve = NativeAsset;
125	type IsTeleporter = (); // Teleporting is disabled.
126	type UniversalLocation = UniversalLocation;
127	type Barrier = Barrier;
128	type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
129	type Trader =
130		UsingComponents<WeightToFee, RelayLocation, AccountId, Balances, ToAuthor<Runtime>>;
131	type ResponseHandler = PolkadotXcm;
132	type AssetTrap = PolkadotXcm;
133	type AssetClaims = PolkadotXcm;
134	type SubscriptionService = PolkadotXcm;
135	type PalletInstancesInfo = AllPalletsWithSystem;
136	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
137	type AssetLocker = ();
138	type AssetExchanger = ();
139	type FeeManager = ();
140	type MessageExporter = ();
141	type UniversalAliases = Nothing;
142	type CallDispatcher = RuntimeCall;
143	type SafeCallFilter = Everything;
144	type Aliasers = Nothing;
145	type TransactionalProcessor = FrameTransactionalProcessor;
146	type HrmpNewChannelOpenRequestHandler = ();
147	type HrmpChannelAcceptedHandler = ();
148	type HrmpChannelClosingHandler = ();
149	type XcmRecorder = PolkadotXcm;
150}
151
152/// No local origins on this chain are allowed to dispatch XCM sends/executions.
153pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
154
155/// The means for routing XCM messages which are not for local execution into the right message
156/// queues.
157pub type XcmRouter = WithUniqueTopic<(
158	// Two routers - use UMP to communicate with the relay chain:
159	cumulus_primitives_utility::ParentAsUmp<ParachainSystem, (), ()>,
160	// ..and XCMP to communicate with the sibling chains.
161	XcmpQueue,
162)>;
163
164#[cfg(feature = "runtime-benchmarks")]
165parameter_types! {
166	pub ReachableDest: Option<Location> = Some(Parent.into());
167}
168
169impl pallet_xcm::Config for Runtime {
170	type RuntimeEvent = RuntimeEvent;
171	type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
172	type XcmRouter = XcmRouter;
173	type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
174	type XcmExecuteFilter = Everything;
175	type XcmExecutor = XcmExecutor<XcmConfig>;
176	type XcmTeleportFilter = Everything;
177	type XcmReserveTransferFilter = Nothing;
178	type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
179	type UniversalLocation = UniversalLocation;
180	type RuntimeOrigin = RuntimeOrigin;
181	type RuntimeCall = RuntimeCall;
182
183	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
184	// ^ Override for AdvertisedXcmVersion default
185	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
186	type Currency = Balances;
187	type CurrencyMatcher = ();
188	type TrustedLockers = ();
189	type SovereignAccountOf = LocationToAccountId;
190	type MaxLockers = ConstU32<8>;
191	type WeightInfo = pallet_xcm::TestWeightInfo;
192	type AdminOrigin = EnsureRoot<AccountId>;
193	type MaxRemoteLockConsumers = ConstU32<0>;
194	type RemoteLockConsumerIdentifier = ();
195}
196
197impl cumulus_pallet_xcm::Config for Runtime {
198	type RuntimeEvent = RuntimeEvent;
199	type XcmExecutor = XcmExecutor<XcmConfig>;
200}