yet_another_teyrchain_runtime/
xcm_config.rs

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