shibuya_runtime/
xcm_config.rs

1// This file is part of Astar.
2
3// Copyright (C) Stake Technologies Pte.Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later
5
6// Astar is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// Astar is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with Astar. If not, see <http://www.gnu.org/licenses/>.
18
19use super::{
20    AccountId, AllPalletsWithSystem, AssetId, Assets, Balance, Balances, DealWithFees,
21    MessageQueue, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent,
22    RuntimeOrigin, ShibuyaAssetLocationIdConverter, TreasuryAccountId, XcAssetConfig,
23    XcmWeightToFee, XcmpQueue,
24};
25use crate::weights;
26use frame_support::{
27    parameter_types,
28    traits::{ConstU32, Contains, Everything, Nothing},
29    weights::Weight,
30};
31use frame_system::EnsureRoot;
32
33// Polkadot imports
34use cumulus_primitives_core::{AggregateMessageOrigin, ParaId};
35use frame_support::traits::{Disabled, TransformOrigin};
36use parachains_common::{
37    message_queue::ParaIdToSibling, xcm_config::ParentRelayOrSiblingParachains,
38};
39use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery;
40use xcm::{latest::prelude::*, v5::ROCOCO_GENESIS_HASH};
41use xcm_builder::{
42    AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
43    AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, ConvertedConcreteId,
44    DescribeAllTerminal, DescribeFamily, EnsureXcmOrigin, FrameTransactionalProcessor,
45    FungibleAdapter, FungiblesAdapter, HashedDescription, IsConcrete, NoChecking,
46    ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative,
47    SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32,
48    SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId, UsingComponents,
49    WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
50};
51use xcm_executor::{traits::JustTry, XcmExecutor};
52
53// Astar imports
54use astar_primitives::xcm::{
55    FixedRateOfForeignAsset, ReserveAssetFilter, XcmFungibleFeeHandler, MAX_ASSETS,
56};
57
58parameter_types! {
59    pub RelayNetwork: Option<NetworkId> = Some(NetworkId::ByGenesis(ROCOCO_GENESIS_HASH));
60    pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
61    pub UniversalLocation: InteriorLocation =
62    [GlobalConsensus(RelayNetwork::get().unwrap()), Parachain(ParachainInfo::parachain_id().into())].into();
63    pub const ShibuyaLocation: Location = Here.into_location();
64    pub DummyCheckingAccount: AccountId = PolkadotXcm::check_account();
65}
66
67/// Type for specifying how a `Location` can be converted into an `AccountId`. This is used
68/// when determining ownership of accounts for asset transacting and when attempting to use XCM
69/// `Transact` in order to determine the dispatch Origin.
70pub type LocationToAccountId = (
71    // The parent (Relay-chain) origin converts to the default `AccountId`.
72    ParentIsPreset<AccountId>,
73    // Sibling parachain origins convert to AccountId via the `ParaId::into`.
74    SiblingParachainConvertsVia<polkadot_parachain::primitives::Sibling, AccountId>,
75    // Straight up local `AccountId32` origins just alias directly to `AccountId`.
76    AccountId32Aliases<RelayNetwork, AccountId>,
77    // Generates private `AccountId`s from `Location`s, in a stable & safe way.
78    // Replaces the old `Account32Hash` approach.
79    HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
80);
81
82/// Means for transacting the native currency on this chain.
83pub type CurrencyTransactor = FungibleAdapter<
84    // Use this currency:
85    Balances,
86    // Use this currency when it is a fungible asset matching the given location or name:
87    IsConcrete<ShibuyaLocation>,
88    // Convert an XCM Location into a local account id:
89    LocationToAccountId,
90    // Our chain's account ID type (we can't get away without mentioning it explicitly):
91    AccountId,
92    // We don't track any teleports of `Balances`.
93    (),
94>;
95
96/// Means for transacting assets besides the native currency on this chain.
97pub type FungiblesTransactor = FungiblesAdapter<
98    // Use this fungibles implementation:
99    Assets,
100    // Use this currency when it is a fungible asset matching the given location or name:
101    ConvertedConcreteId<AssetId, Balance, ShibuyaAssetLocationIdConverter, JustTry>,
102    // Convert an XCM Location into a local account id:
103    LocationToAccountId,
104    // Our chain's account ID type (we can't get away without mentioning it explicitly):
105    AccountId,
106    // We don't support teleport so no need to check any assets.
107    NoChecking,
108    // We don't support teleport so this is just a dummy account.
109    DummyCheckingAccount,
110>;
111
112/// Means for transacting assets on this chain.
113pub type AssetTransactors = (CurrencyTransactor, FungiblesTransactor);
114
115/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
116/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
117/// biases the kind of local `Origin` it will become.
118pub type XcmOriginToTransactDispatchOrigin = (
119    // Sovereign account converter; this attempts to derive an `AccountId` from the origin location
120    // using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for
121    // foreign chains who want to have a local sovereign account on this chain which they control.
122    SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,
123    // Native converter for Relay-chain (Parent) location; will convert to a `Relay` origin when
124    // recognised.
125    RelayChainAsNative<RelayChainOrigin, RuntimeOrigin>,
126    // Native converter for sibling Parachains; will convert to a `SiblingPara` origin when
127    // recognised.
128    SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,
129    // Superuser converter for the Relay-chain (Parent) location. This will allow it to issue a
130    // transaction from the Root origin.
131    ParentAsSuperuser<RuntimeOrigin>,
132    // Xcm origins can be represented natively under the Xcm pallet's Xcm origin.
133    pallet_xcm::XcmPassthrough<RuntimeOrigin>,
134    // Native signed account converter; this just converts an `AccountId32` origin into a normal
135    // `Origin::Signed` origin of the same 32-byte value.
136    SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
137);
138
139parameter_types! {
140    // One XCM operation is 1_000_000_000 weight - almost certainly a conservative estimate.
141    // For the PoV size, we estimate 4 kB per instruction. This will be changed when we benchmark the instructions.
142    pub UnitWeightCost: Weight = Weight::from_parts(1_000_000_000, 4 * 1024);
143    pub const MaxInstructions: u32 = 100;
144    pub const MaxAssetsIntoHolding: u32 = MAX_ASSETS as u32;
145}
146
147pub struct ParentOrParentsPlurality;
148impl Contains<Location> for ParentOrParentsPlurality {
149    fn contains(location: &Location) -> bool {
150        matches!(location.unpack(), (1, []) | (1, [Plurality { .. }]))
151    }
152}
153
154pub type XcmBarrier = TrailingSetTopicAsId<(
155    TakeWeightCredit,
156    // Expected responses are OK.
157    AllowKnownQueryResponses<PolkadotXcm>,
158    // Allow XCMs with some computed origins to pass through.
159    WithComputedOrigin<
160        (
161            // If the message is one that immediately attempts to pay for execution, then allow it.
162            AllowTopLevelPaidExecutionFrom<Everything>,
163            // Subscriptions for version tracking are OK.
164            AllowSubscriptionsFrom<ParentRelayOrSiblingParachains>,
165        ),
166        UniversalLocation,
167        ConstU32<8>,
168    >,
169    // Parent and its plurality get free execution
170    AllowUnpaidExecutionFrom<ParentOrParentsPlurality>,
171)>;
172
173// Used to handle XCM fee deposit into treasury account
174pub type ShibuyaXcmFungibleFeeHandler = XcmFungibleFeeHandler<
175    AccountId,
176    ConvertedConcreteId<AssetId, Balance, ShibuyaAssetLocationIdConverter, JustTry>,
177    Assets,
178    TreasuryAccountId,
179>;
180
181pub type Weigher =
182    WeightInfoBounds<weights::xcm::XcmWeight<Runtime, RuntimeCall>, RuntimeCall, MaxInstructions>;
183
184pub struct XcmConfig;
185impl xcm_executor::Config for XcmConfig {
186    type RuntimeCall = RuntimeCall;
187    type XcmSender = XcmRouter;
188    type AssetTransactor = AssetTransactors;
189    type OriginConverter = XcmOriginToTransactDispatchOrigin;
190    type IsReserve = ReserveAssetFilter;
191    type IsTeleporter = ();
192    type UniversalLocation = UniversalLocation;
193    type Barrier = XcmBarrier;
194    type Weigher = Weigher;
195    type Trader = (
196        UsingComponents<XcmWeightToFee, ShibuyaLocation, AccountId, Balances, DealWithFees>,
197        FixedRateOfForeignAsset<XcAssetConfig, ShibuyaXcmFungibleFeeHandler>,
198    );
199    type ResponseHandler = PolkadotXcm;
200    type AssetTrap = PolkadotXcm;
201    type AssetClaims = PolkadotXcm;
202    type SubscriptionService = PolkadotXcm;
203
204    type PalletInstancesInfo = AllPalletsWithSystem;
205    type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
206    type AssetLocker = ();
207    type AssetExchanger = ();
208    type FeeManager = ();
209    type MessageExporter = ();
210    type UniversalAliases = Nothing;
211    type CallDispatcher = RuntimeCall;
212    type SafeCallFilter = Everything;
213    type Aliasers = Nothing;
214    type TransactionalProcessor = FrameTransactionalProcessor;
215
216    type HrmpNewChannelOpenRequestHandler = ();
217    type HrmpChannelAcceptedHandler = ();
218    type HrmpChannelClosingHandler = ();
219    type XcmRecorder = PolkadotXcm;
220    type XcmEventEmitter = PolkadotXcm;
221}
222
223/// Local origins on this chain are allowed to dispatch XCM sends/executions.
224pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
225
226/// The means for routing XCM messages which are not for local execution into the right message
227/// queues.
228pub type XcmRouter = WithUniqueTopic<(
229    // Two routers - use UMP to communicate with the relay chain:
230    cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm, ()>,
231    // ..and XCMP to communicate with the sibling chains.
232    XcmpQueue,
233)>;
234
235impl pallet_xcm::Config for Runtime {
236    const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
237
238    type RuntimeEvent = RuntimeEvent;
239    type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, ()>;
240    type XcmRouter = XcmRouter;
241    type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
242    type XcmExecuteFilter = Nothing;
243    type XcmExecutor = XcmExecutor<XcmConfig>;
244    type XcmTeleportFilter = Nothing;
245    type XcmReserveTransferFilter = Everything;
246    type Weigher = Weigher;
247    type UniversalLocation = UniversalLocation;
248    type RuntimeOrigin = RuntimeOrigin;
249    type RuntimeCall = RuntimeCall;
250    type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
251    type Currency = Balances;
252    type CurrencyMatcher = ();
253    type TrustedLockers = ();
254    type SovereignAccountOf = LocationToAccountId;
255    type MaxLockers = ConstU32<0>;
256    type WeightInfo = weights::pallet_xcm::SubstrateWeight<Runtime>;
257    type MaxRemoteLockConsumers = ConstU32<0>;
258    type RemoteLockConsumerIdentifier = ();
259    type AdminOrigin = EnsureRoot<AccountId>;
260    type AuthorizedAliasConsideration = Disabled;
261}
262
263impl cumulus_pallet_xcm::Config for Runtime {
264    type RuntimeEvent = RuntimeEvent;
265    type XcmExecutor = XcmExecutor<XcmConfig>;
266}
267
268impl cumulus_pallet_xcmp_queue::Config for Runtime {
269    type RuntimeEvent = RuntimeEvent;
270    type ChannelInfo = ParachainSystem;
271    type VersionWrapper = PolkadotXcm;
272    type XcmpQueue = TransformOrigin<MessageQueue, AggregateMessageOrigin, ParaId, ParaIdToSibling>;
273    type MaxInboundSuspended = ConstU32<1_000>;
274    type MaxActiveOutboundChannels = ConstU32<128>;
275    type MaxPageSize = ConstU32<{ 128 * 1024 }>;
276    type ControllerOrigin = EnsureRoot<AccountId>;
277    type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin;
278    type PriceForSiblingDelivery = NoPriceForMessageDelivery<ParaId>;
279    type WeightInfo = cumulus_pallet_xcmp_queue::weights::SubstrateWeight<Runtime>;
280}