1234567891011121314151617use frame_support::{18 traits::{Everything, Get},19 weights::Weight,20 parameter_types,21};22use frame_system::EnsureRoot;23use pallet_xcm::XcmPassthrough;24use polkadot_parachain::primitives::Sibling;25use xcm::v1::{Junction::*, MultiLocation, NetworkId};26use xcm::latest::prelude::*;27use xcm_builder::{28 AccountId32Aliases, EnsureXcmOrigin, FixedWeightBounds, LocationInverter, ParentAsSuperuser,29 RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,30 SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, ParentIsPreset,31};32use xcm_executor::{Config, XcmExecutor, traits::ShouldExecute};33use sp_std::{marker::PhantomData, vec::Vec};34use crate::{35 Runtime, Call, Event, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, XcmpQueue,36 xcm_config::Barrier,37};3839use up_common::types::AccountId;4041#[cfg(feature = "foreign-assets")]42pub mod foreignassets;4344#[cfg(not(feature = "foreign-assets"))]45pub mod nativeassets;4647#[cfg(feature = "foreign-assets")]48pub use foreignassets as xcm_assets;4950#[cfg(not(feature = "foreign-assets"))]51pub use nativeassets as xcm_assets;5253use xcm_assets::{AssetTransactors, IsReserve, Trader};5455parameter_types! {56 pub const RelayLocation: MultiLocation = MultiLocation::parent();57 pub const RelayNetwork: NetworkId = NetworkId::Polkadot;58 pub RelayOrigin: Origin = cumulus_pallet_xcm::Origin::Relay.into();59 pub Ancestry: MultiLocation = Parachain(ParachainInfo::parachain_id().into()).into();60 pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));6162 63 pub UnitWeightCost: Weight = 1_000_000;64 pub const MaxInstructions: u32 = 100;65}6667686970pub type LocationToAccountId = (71 72 ParentIsPreset<AccountId>,73 74 SiblingParachainConvertsVia<Sibling, AccountId>,75 76 AccountId32Aliases<RelayNetwork, AccountId>,77);787980pub type LocalOriginToLocation = (SignedToAccountId32<Origin, AccountId, RelayNetwork>,);81828384pub type XcmRouter = (85 86 cumulus_primitives_utility::ParentAsUmp<ParachainSystem, ()>,87 88 XcmpQueue,89);9091929394pub type XcmOriginToTransactDispatchOrigin = (95 96 97 98 SovereignSignedViaLocation<LocationToAccountId, Origin>,99 100 101 RelayChainAsNative<RelayOrigin, Origin>,102 103 104 SiblingParachainAsNative<cumulus_pallet_xcm::Origin, Origin>,105 106 107 ParentAsSuperuser<Origin>,108 109 110 SignedAccountId32AsNative<RelayNetwork, Origin>,111 112 XcmPassthrough<Origin>,113);114115pub trait TryPass {116 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()>;117}118119#[impl_trait_for_tuples::impl_for_tuples(30)]120impl TryPass for Tuple {121 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {122 for_tuples!( #(123 Tuple::try_pass(origin, message)?;124 )* );125126 Ok(())127 }128}129130pub struct DenyTransact;131impl TryPass for DenyTransact {132 fn try_pass<Call>(_origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {133 let transact_inst = message134 .0135 .iter()136 .find(|inst| matches![inst, Instruction::Transact { .. }]);137138 match transact_inst {139 Some(_) => {140 log::warn!(141 target: "xcm::barrier",142 "transact XCM rejected"143 );144145 Err(())146 }147 None => Ok(()),148 }149 }150}151152153154pub struct DenyThenTry<Deny, Allow>(PhantomData<Deny>, PhantomData<Allow>)155where156 Deny: TryPass,157 Allow: ShouldExecute;158159impl<Deny, Allow> ShouldExecute for DenyThenTry<Deny, Allow>160where161 Deny: TryPass,162 Allow: ShouldExecute,163{164 fn should_execute<Call>(165 origin: &MultiLocation,166 message: &mut Xcm<Call>,167 max_weight: Weight,168 weight_credit: &mut Weight,169 ) -> Result<(), ()> {170 Deny::try_pass(origin, message)?;171 Allow::should_execute(origin, message, max_weight, weight_credit)172 }173}174175176pub struct DenyExchangeWithUnknownLocation<T>(PhantomData<T>);177impl<T: Get<Vec<MultiLocation>>> TryPass for DenyExchangeWithUnknownLocation<T> {178 fn try_pass<Call>(origin: &MultiLocation, message: &mut Xcm<Call>) -> Result<(), ()> {179 let allowed_locations = T::get();180181 182 let mut allowed = allowed_locations.contains(origin);183184 message.0.iter().for_each(|inst| match inst {185 DepositReserveAsset { dest: dst, .. } => {186 allowed |= allowed_locations.contains(dst);187 }188 TransferReserveAsset { dest: dst, .. } => {189 allowed |= allowed_locations.contains(dst);190 }191 _ => {}192 });193194 if allowed {195 return Ok(());196 }197198 log::warn!(199 target: "xcm::barrier",200 "Unexpected deposit or transfer location"201 );202 203 Err(())204 }205}206207pub type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;208209pub struct XcmConfig<T>(PhantomData<T>);210impl<T> Config for XcmConfig<T>211where212 T: pallet_configuration::Config,213{214 type Call = Call;215 type XcmSender = XcmRouter;216 217 type AssetTransactor = AssetTransactors;218 type OriginConverter = XcmOriginToTransactDispatchOrigin;219 type IsReserve = IsReserve;220 type IsTeleporter = (); 221 type LocationInverter = LocationInverter<Ancestry>;222 type Barrier = Barrier;223 type Weigher = Weigher;224 type Trader = Trader<T>;225 type ResponseHandler = (); 226 type SubscriptionService = PolkadotXcm;227228 type AssetTrap = PolkadotXcm;229 type AssetClaims = PolkadotXcm;230}231232impl pallet_xcm::Config for Runtime {233 type Event = Event;234 type SendXcmOrigin = EnsureXcmOrigin<Origin, LocalOriginToLocation>;235 type XcmRouter = XcmRouter;236 type ExecuteXcmOrigin = EnsureXcmOrigin<Origin, LocalOriginToLocation>;237 type XcmExecuteFilter = Everything;238 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;239 type XcmTeleportFilter = Everything;240 type XcmReserveTransferFilter = Everything;241 type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;242 type LocationInverter = LocationInverter<Ancestry>;243 type Origin = Origin;244 type Call = Call;245 const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;246 type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;247}248249impl cumulus_pallet_xcm::Config for Runtime {250 type Event = Event;251 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;252}253254impl cumulus_pallet_xcmp_queue::Config for Runtime {255 type WeightInfo = ();256 type Event = Event;257 type XcmExecutor = XcmExecutor<XcmConfig<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}264265impl cumulus_pallet_dmp_queue::Config for Runtime {266 type Event = Event;267 type XcmExecutor = XcmExecutor<XcmConfig<Self>>;268 type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;269}