1234567891011121314151617use frame_support::{18 traits::{Everything, Nothing, Get, ConstU32},19 parameter_types,20};21use frame_system::EnsureRoot;22use pallet_xcm::XcmPassthrough;23use polkadot_parachain::primitives::Sibling;24use xcm::latest::{prelude::*, Weight, MultiLocation};25use xcm::v3::Instruction;26use xcm_builder::{27 AccountId32Aliases, EnsureXcmOrigin, FixedWeightBounds, ParentAsSuperuser, RelayChainAsNative,28 SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,29 SignedToAccountId32, SovereignSignedViaLocation, ParentIsPreset,30};31use xcm_executor::{XcmExecutor, traits::ShouldExecute};32use sp_std::{marker::PhantomData, vec::Vec};33use crate::{34 Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, ParachainInfo, ParachainSystem, PolkadotXcm,35 XcmpQueue, xcm_barrier::Barrier, RelayNetwork, AllPalletsWithSystem, Balances,36};3738use up_common::types::AccountId;3940#[cfg(feature = "foreign-assets")]41pub mod foreignassets;4243#[cfg(not(feature = "foreign-assets"))]44pub mod nativeassets;4546#[cfg(feature = "foreign-assets")]47pub use foreignassets as xcm_assets;4849#[cfg(not(feature = "foreign-assets"))]50pub use nativeassets as xcm_assets;5152use xcm_assets::{AssetTransactor, IsReserve, Trader};5354parameter_types! {55 pub const RelayLocation: MultiLocation = MultiLocation::parent();56 pub RelayOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();57 pub UniversalLocation: InteriorMultiLocation = X2(GlobalConsensus(RelayNetwork::get()), Parachain(ParachainInfo::get().into()));58 pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));5960 61 pub UnitWeightCost: Weight = Weight::from_parts(1_000_000, 1000); 62 pub const MaxInstructions: u32 = 100;63}6465666768pub type LocationToAccountId = (69 70 ParentIsPreset<AccountId>,71 72 SiblingParachainConvertsVia<Sibling, AccountId>,73 74 AccountId32Aliases<RelayNetwork, AccountId>,75);767778pub type LocalOriginToLocation = (SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>,);79808182pub type XcmRouter = (83 84 cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm, ()>,85 86 XcmpQueue,87);8889909192pub type XcmOriginToTransactDispatchOrigin = (93 94 95 96 SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,97 98 99 RelayChainAsNative<RelayOrigin, RuntimeOrigin>,100 101 102 SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,103 104 105 ParentAsSuperuser<RuntimeOrigin>,106 107 108 SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,109 110 XcmPassthrough<RuntimeOrigin>,111);112113pub trait TryPass {114 fn try_pass<Call>(origin: &MultiLocation, message: &mut [Instruction<Call>]) -> Result<(), ()>;115}116117#[impl_trait_for_tuples::impl_for_tuples(30)]118impl TryPass for Tuple {119 fn try_pass<Call>(origin: &MultiLocation, message: &mut [Instruction<Call>]) -> Result<(), ()> {120 for_tuples!( #(121 Tuple::try_pass(origin, message)?;122 )* );123124 Ok(())125 }126}127128129130pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)131where132 Deny: TryPass,133 Allow: ShouldExecute;134135impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>136where137 Deny: TryPass,138 Allow: ShouldExecute,139{140 fn should_execute<Call>(141 origin: &MultiLocation,142 message: &mut [Instruction<Call>],143 max_weight: Weight,144 weight_credit: &mut Weight,145 ) -> Result<(), ()> {146 Deny::try_pass(origin, message)?;147 Allow::should_execute(origin, message, max_weight, weight_credit)148 }149}150151152pub struct DenyExchangeWithUnknownLocation<T>(PhantomData<T>);153impl<T: Get<Vec<MultiLocation>>> TryPass for DenyExchangeWithUnknownLocation<T> {154 fn try_pass<Call>(origin: &MultiLocation, message: &mut [Instruction<Call>]) -> Result<(), ()> {155 let allowed_locations = T::get();156157 158 let mut allowed = allowed_locations.contains(origin);159160 message.iter().for_each(|inst| match inst {161 DepositReserveAsset { dest: dst, .. }162 | TransferReserveAsset { dest: dst, .. }163 | InitiateReserveWithdraw { reserve: dst, .. } => {164 allowed |= allowed_locations.contains(&dst);165 }166 167 _ => {}168 });169170 if allowed {171 return Ok(());172 }173174 log::warn!(175 target: "xcm::barrier",176 "Unexpected deposit or transfer location"177 );178 179 Err(())180 }181}182183pub type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;184185pub struct XcmExecutorConfig<T>(PhantomData<T>);186impl<T> xcm_executor::Config for XcmExecutorConfig<T>187where188 T: pallet_configuration::Config,189{190 type RuntimeCall = RuntimeCall;191 type XcmSender = XcmRouter;192 193 type AssetTransactor = AssetTransactor;194 type OriginConverter = XcmOriginToTransactDispatchOrigin;195 type IsReserve = IsReserve;196 type IsTeleporter = (); 197 type UniversalLocation = UniversalLocation;198 type Barrier = Barrier;199 type Weigher = Weigher;200 type Trader = Trader<T>;201 type ResponseHandler = PolkadotXcm;202 type SubscriptionService = PolkadotXcm;203 type PalletInstancesInfo = AllPalletsWithSystem;204 type MaxAssetsIntoHolding = ConstU32<8>;205206 type AssetTrap = PolkadotXcm;207 type AssetClaims = PolkadotXcm;208 type AssetLocker = ();209 type AssetExchanger = ();210 type FeeManager = ();211 type MessageExporter = ();212 type UniversalAliases = Nothing;213 type CallDispatcher = RuntimeCall;214215 216 type SafeCallFilter = Nothing;217}218219#[cfg(feature = "runtime-benchmarks")]220parameter_types! {221 pub ReachableDest: Option<MultiLocation> = Some(Parent.into());222}223224impl pallet_xcm::Config for Runtime {225 type RuntimeEvent = RuntimeEvent;226 type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, ()>;227 type XcmRouter = XcmRouter;228 type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;229 type XcmExecuteFilter = Everything;230 type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;231 type XcmTeleportFilter = Everything;232 type XcmReserveTransferFilter = Everything;233 type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;234 type RuntimeOrigin = RuntimeOrigin;235 type RuntimeCall = RuntimeCall;236 const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;237 type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;238 type UniversalLocation = UniversalLocation;239 type Currency = Balances;240 type CurrencyMatcher = ();241 type TrustedLockers = ();242 type SovereignAccountOf = LocationToAccountId;243 type MaxLockers = ConstU32<8>;244 type WeightInfo = crate::weights::xcm::SubstrateWeight<Runtime>;245 #[cfg(feature = "runtime-benchmarks")]246 type ReachableDest = ReachableDest;247}248249impl cumulus_pallet_xcm::Config for Runtime {250 type RuntimeEvent = RuntimeEvent;251 type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;252}253254impl cumulus_pallet_xcmp_queue::Config for Runtime {255 type WeightInfo = ();256 type RuntimeEvent = RuntimeEvent;257 type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;258 type ChannelInfo = ParachainSystem;259 type VersionWrapper = ();260 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;261 type ControllerOrigin = EnsureRoot<AccountId>;262 type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin;263 type PriceForSiblingDelivery = ();264}265266impl cumulus_pallet_dmp_queue::Config for Runtime {267 type RuntimeEvent = RuntimeEvent;268 type XcmExecutor = XcmExecutor<XcmExecutorConfig<Self>>;269 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;270}