referrerpolicy=no-referrer-when-downgrade

xcm_docs/cookbook/relay_token_transactor/parachain/
xcm_config.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3// This file is part of Polkadot.
4
5// Polkadot is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9
10// Polkadot is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14
15// You should have received a copy of the GNU General Public License
16// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
17
18//! # XCM Configuration
19
20use frame::{
21	deps::frame_system,
22	runtime::prelude::*,
23	traits::{Disabled, Everything, Nothing},
24};
25use xcm::latest::prelude::*;
26use xcm_builder::{
27	AccountId32Aliases, DescribeAllTerminal, DescribeFamily, EnsureXcmOrigin,
28	FrameTransactionalProcessor, FungibleAdapter, HashedDescription, IsConcrete,
29	SignedToAccountId32,
30};
31use xcm_executor::XcmExecutor;
32
33use super::{AccountId, Balances, MessageQueue, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin};
34
35parameter_types! {
36	pub RelayLocation: Location = Location::parent();
37	pub ThisNetwork: NetworkId = NetworkId::Polkadot;
38}
39
40pub type LocationToAccountId = (
41	HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
42	AccountId32Aliases<ThisNetwork, AccountId>,
43);
44
45/// Configuration related to asset transactors
46#[docify::export]
47mod asset_transactor {
48	use super::*;
49
50	parameter_types! {
51		pub ParentRelayLocation: Location = Location::parent();
52	}
53
54	/// AssetTransactor for handling the relay chain token
55	pub type FungibleTransactor = FungibleAdapter<
56		// Use this implementation of the `fungible::*` traits.
57		// `Balances` is the name given to the balances pallet in this particular recipe.
58		// Any implementation of the traits would suffice.
59		Balances,
60		// This transactor deals with the native token of the Relay Chain.
61		// This token is referenced by the Location of the Relay Chain relative to this chain
62		// -- Location::parent().
63		IsConcrete<ParentRelayLocation>,
64		// How to convert an XCM Location into a local account id.
65		// This is also something that's configured in the XCM executor.
66		LocationToAccountId,
67		// The type for account ids, only needed because `fungible` is generic over it.
68		AccountId,
69		// Not tracking teleports.
70		// This recipe only uses reserve asset transfers to handle the Relay Chain token.
71		(),
72	>;
73
74	/// Actual configuration item that'll be set in the XCM config.
75	/// A tuple could be used here to have multiple transactors, each (potentially) handling
76	/// different assets.
77	/// In this recipe, we only have one.
78	pub type AssetTransactor = FungibleTransactor;
79}
80
81/// Configuration related to token reserves
82#[docify::export]
83mod is_reserve {
84	use super::*;
85
86	parameter_types! {
87		/// Reserves are specified using a pair `(AssetFilter, Location)`.
88		/// Each pair means that the specified Location is a reserve for all the assets in AssetsFilter.
89		/// Here, we are specifying that the Relay Chain is the reserve location for its native token.
90		pub RelayTokenForRelay: (AssetFilter, Location) =
91		  (Wild(AllOf { id: AssetId(Parent.into()), fun: WildFungible }), Parent.into());
92	}
93
94	/// The wrapper type xcm_builder::Case is needed in order to use this in the configuration.
95	pub type IsReserve = xcm_builder::Case<RelayTokenForRelay>;
96}
97
98mod weigher {
99	use super::*;
100	use xcm_builder::FixedWeightBounds;
101
102	parameter_types! {
103		pub const WeightPerInstruction: Weight = Weight::from_parts(1, 1);
104		pub const MaxInstructions: u32 = 100;
105	}
106
107	pub type Weigher = FixedWeightBounds<WeightPerInstruction, RuntimeCall, MaxInstructions>;
108}
109
110parameter_types! {
111	pub UniversalLocation: InteriorLocation = [GlobalConsensus(NetworkId::Polkadot), Parachain(2222)].into();
112}
113
114pub struct XcmConfig;
115impl xcm_executor::Config for XcmConfig {
116	type RuntimeCall = RuntimeCall;
117	type XcmSender = ();
118	type XcmEventEmitter = ();
119	type AssetTransactor = asset_transactor::AssetTransactor;
120	type OriginConverter = ();
121	// The declaration of which Locations are reserves for which Assets.
122	type IsReserve = is_reserve::IsReserve;
123	type IsTeleporter = ();
124	type UniversalLocation = UniversalLocation;
125	// This is not safe, you should use `xcm_builder::AllowTopLevelPaidExecutionFrom<T>` in a
126	// production chain
127	type Barrier = xcm_builder::AllowUnpaidExecutionFrom<Everything>;
128	type Weigher = weigher::Weigher;
129	type Trader = ();
130	type ResponseHandler = ();
131	type AssetTrap = ();
132	type AssetLocker = ();
133	type AssetExchanger = ();
134	type AssetClaims = ();
135	type SubscriptionService = ();
136	type PalletInstancesInfo = ();
137	type FeeManager = ();
138	type MaxAssetsIntoHolding = frame::traits::ConstU32<1>;
139	type MessageExporter = ();
140	type UniversalAliases = Nothing;
141	type CallDispatcher = RuntimeCall;
142	type SafeCallFilter = Everything;
143	type Aliasers = Nothing;
144	type TransactionalProcessor = FrameTransactionalProcessor;
145	type HrmpNewChannelOpenRequestHandler = ();
146	type HrmpChannelAcceptedHandler = ();
147	type HrmpChannelClosingHandler = ();
148	type XcmRecorder = ();
149}
150
151/// Converts a local signed origin into an XCM location. Forms the basis for local origins
152/// sending/executing XCMs.
153pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, ThisNetwork>;
154
155impl pallet_xcm::Config for Runtime {
156	// We turn off sending for these tests
157	type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, ()>;
158	type XcmRouter = super::super::network::ParachainXcmRouter<MessageQueue>; // Provided by xcm-simulator
159																		   // Anyone can execute XCM programs
160	type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
161	// We execute any type of program
162	type XcmExecuteFilter = Everything;
163	// How we execute programs
164	type XcmExecutor = XcmExecutor<XcmConfig>;
165	// We don't allow teleports
166	type XcmTeleportFilter = Nothing;
167	// We allow all reserve transfers
168	type XcmReserveTransferFilter = Everything;
169	// Same weigher executor uses to weigh XCM programs
170	type Weigher = weigher::Weigher;
171	// Same universal location
172	type UniversalLocation = UniversalLocation;
173	// No version discovery needed
174	const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 0;
175	type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
176	type AdminOrigin = frame_system::EnsureRoot<AccountId>;
177	// No locking
178	type TrustedLockers = ();
179	type MaxLockers = frame::traits::ConstU32<0>;
180	type MaxRemoteLockConsumers = frame::traits::ConstU32<0>;
181	type RemoteLockConsumerIdentifier = ();
182	// How to turn locations into accounts
183	type SovereignAccountOf = LocationToAccountId;
184	// A currency to pay for things and its matcher, we are using the relay token
185	type Currency = Balances;
186	type CurrencyMatcher = IsConcrete<RelayLocation>;
187	// Pallet benchmarks, no need for this recipe
188	type WeightInfo = pallet_xcm::TestWeightInfo;
189	// Runtime types
190	type RuntimeOrigin = RuntimeOrigin;
191	type RuntimeCall = RuntimeCall;
192	type RuntimeEvent = RuntimeEvent;
193	// Aliasing is disabled: xcm_executor::Config::Aliasers is set to `Nothing`.
194	type AuthorizedAliasConsideration = Disabled;
195}