Skip to main content

ink_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, Disabled, 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	DenyRecursively, DenyReserveTransferToRelayChain, DenyThenTry, EnsureXcmOrigin,
19	FixedWeightBounds, FrameTransactionalProcessor, IsConcrete, NativeAsset, ParentIsPreset,
20	RelayChainAsNative, 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		DenyRecursively<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 XcmEventEmitter = PolkadotXcm;
123	type AssetTransactor = AssetTransactors;
124	type OriginConverter = XcmOriginToTransactDispatchOrigin;
125	type IsReserve = NativeAsset;
126	type IsTeleporter = (); // Teleporting is disabled.
127	type UniversalLocation = UniversalLocation;
128	type Barrier = Barrier;
129	type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
130	type Trader =
131		UsingComponents<WeightToFee, RelayLocation, AccountId, Balances, ToAuthor<Runtime>>;
132	type ResponseHandler = PolkadotXcm;
133	type AssetTrap = PolkadotXcm;
134	type AssetClaims = PolkadotXcm;
135	type SubscriptionService = PolkadotXcm;
136	type PalletInstancesInfo = AllPalletsWithSystem;
137	type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
138	type AssetLocker = ();
139	type AssetExchanger = ();
140	type FeeManager = ();
141	type MessageExporter = ();
142	type UniversalAliases = Nothing;
143	type CallDispatcher = RuntimeCall;
144	type SafeCallFilter = Everything;
145	type Aliasers = Nothing;
146	type TransactionalProcessor = FrameTransactionalProcessor;
147	type HrmpNewChannelOpenRequestHandler = ();
148	type HrmpChannelAcceptedHandler = ();
149	type HrmpChannelClosingHandler = ();
150	type XcmRecorder = PolkadotXcm;
151}
152
153/// No local origins on this chain are allowed to dispatch XCM sends/executions.
154pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
155
156/// The means for routing XCM messages which are not for local execution into the right message
157/// queues.
158pub type XcmRouter = WithUniqueTopic<(
159	// Two routers - use UMP to communicate with the relay chain:
160	cumulus_primitives_utility::ParentAsUmp<ParachainSystem, (), ()>,
161	// ..and XCMP to communicate with the sibling chains.
162	XcmpQueue,
163)>;
164
165#[cfg(feature = "runtime-benchmarks")]
166parameter_types! {
167	pub ReachableDest: Option<Location> = Some(Parent.into());
168}
169
170impl pallet_xcm::Config for Runtime {
171	type RuntimeEvent = RuntimeEvent;
172	type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
173	type XcmRouter = XcmRouter;
174	type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
175	type XcmExecuteFilter = Everything;
176	type XcmExecutor = XcmExecutor<XcmConfig>;
177	type XcmTeleportFilter = Everything;
178	type XcmReserveTransferFilter = Nothing;
179	type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
180	type UniversalLocation = UniversalLocation;
181	type RuntimeOrigin = RuntimeOrigin;
182	type RuntimeCall = RuntimeCall;
183
184	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
185	// ^ Override for AdvertisedXcmVersion default
186	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
187	type Currency = Balances;
188	type CurrencyMatcher = ();
189	type TrustedLockers = ();
190	type SovereignAccountOf = LocationToAccountId;
191	type MaxLockers = ConstU32<8>;
192	type WeightInfo = pallet_xcm::TestWeightInfo;
193	type AdminOrigin = EnsureRoot<AccountId>;
194	type MaxRemoteLockConsumers = ConstU32<0>;
195	type RemoteLockConsumerIdentifier = ();
196	type AuthorizedAliasConsideration = Disabled;
197}
198
199impl cumulus_pallet_xcm::Config for Runtime {
200	type RuntimeEvent = RuntimeEvent;
201	type XcmExecutor = XcmExecutor<XcmConfig>;
202}