use cfg_traits::TryConvert;
use cfg_types::{tokens::CurrencyId, EVMChainId};
use frame_support::{
parameter_types,
traits::{Everything, Get, Nothing},
};
use frame_system::EnsureRoot;
use orml_asset_registry::{AssetRegistryTrader, FixedRateAssetRegistryTrader};
use orml_traits::{location::AbsoluteReserveProvider, parameter_type_with_key};
use orml_xcm_support::MultiNativeAsset;
use pallet_xcm::XcmPassthrough;
use runtime_common::{
origins::gov::types::{EnsureRootOr, HalfOfCouncil},
transfer_filter::PreXcmTransfer,
xcm::{
AccountIdToLocation, Barrier, CanonicalNativePerSecond, FixedConversionRateProvider,
LocalOriginToLocation, ToTreasury,
},
};
use sp_core::ConstU32;
use staging_xcm::{
prelude::*,
v4::{Location, Weight as XcmWeight},
};
use staging_xcm_builder::{
ConvertedConcreteId, EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds,
FrameTransactionalProcessor, FungiblesAdapter, NoChecking, RelayChainAsNative,
SiblingParachainAsNative, SignedAccountId32AsNative, SovereignSignedViaLocation,
};
use staging_xcm_executor::{traits::JustTry, XcmExecutor};
use super::{
AccountId, Balance, OrmlAssetRegistry, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime,
RuntimeCall, RuntimeEvent, RuntimeOrigin, Tokens, XcmpQueue,
};
pub struct XcmConfig;
impl staging_xcm_executor::Config for XcmConfig {
type Aliasers = Nothing;
type AssetClaims = PolkadotXcm;
type AssetExchanger = ();
type AssetLocker = ();
type AssetTransactor = FungiblesTransactor;
type AssetTrap = PolkadotXcm;
type Barrier = Barrier<PolkadotXcm>;
type CallDispatcher = RuntimeCall;
type FeeManager = ();
type IsReserve = MultiNativeAsset<AbsoluteReserveProvider>;
type IsTeleporter = ();
type MaxAssetsIntoHolding = ConstU32<64>;
type MessageExporter = ();
type OriginConverter = XcmOriginToTransactDispatchOrigin;
type PalletInstancesInfo = crate::AllPalletsWithSystem;
type ResponseHandler = PolkadotXcm;
type RuntimeCall = RuntimeCall;
type SafeCallFilter = Everything;
type SubscriptionService = PolkadotXcm;
type Trader = Trader;
type TransactionalProcessor = FrameTransactionalProcessor;
type UniversalAliases = Nothing;
type UniversalLocation = UniversalLocation;
type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
type XcmSender = XcmRouter;
}
pub type Trader = (
FixedRateOfFungible<CanonicalNativePerSecond, ToTreasury<Runtime>>,
AssetRegistryTrader<
FixedRateAssetRegistryTrader<FixedConversionRateProvider<OrmlAssetRegistry>>,
ToTreasury<Runtime>,
>,
);
pub type FungiblesTransactor = FungiblesAdapter<
Tokens,
ConvertedConcreteId<CurrencyId, Balance, CurrencyIdConvert, JustTry>,
LocationToAccountId,
AccountId,
NoChecking,
CheckingAccount,
>;
parameter_types! {
pub const UnitWeightCost: XcmWeight = XcmWeight::from_parts(200_000_000, 0);
pub const MaxInstructions: u32 = 100;
}
impl pallet_xcm::Config for Runtime {
type AdminOrigin = EnsureRoot<AccountId>;
type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
type Currency = crate::Balances;
type CurrencyMatcher = ();
type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation<Runtime>>;
type MaxLockers = ConstU32<8>;
type MaxRemoteLockConsumers = ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation<Runtime>>;
type SovereignAccountOf = LocationToAccountId;
type TrustedLockers = ();
type UniversalLocation = UniversalLocation;
type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
type XcmExecuteFilter = Nothing;
type XcmExecutor = XcmExecutor<XcmConfig>;
type XcmReserveTransferFilter = Everything;
type XcmRouter = XcmRouter;
type XcmTeleportFilter = Everything;
const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
}
parameter_types! {
pub const RelayNetwork: NetworkId = NetworkId::Polkadot;
pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
pub Ancestry: Location = Parachain(ParachainInfo::parachain_id().into()).into();
pub CheckingAccount: AccountId = PolkadotXcm::check_account();
}
pub type CurrencyIdConvert = runtime_common::xcm::CurrencyIdConvert<Runtime>;
pub type LocationToAccountId = runtime_common::xcm::LocationToAccountId<RelayNetwork>;
pub type XcmRouter = (
cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm, ()>,
XcmpQueue,
);
const MOONBEAM_PARA_ID: u32 = 2004;
const MOONBEAM_EVM_ID: u64 = 1284;
pub struct ParaToEvm;
impl TryConvert<cfg_types::ParaId, EVMChainId> for ParaToEvm {
type Error = cfg_types::ParaId;
fn try_convert(a: cfg_types::ParaId) -> Result<EVMChainId, cfg_types::ParaId> {
match a {
MOONBEAM_PARA_ID => Ok(MOONBEAM_EVM_ID),
_ => Err(a),
}
}
}
pub type XcmOriginToTransactDispatchOrigin = (
SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,
RelayChainAsNative<RelayChainOrigin, RuntimeOrigin>,
SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,
SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
XcmPassthrough<RuntimeOrigin>,
);
parameter_types! {
pub const BaseXcmWeight: XcmWeight = XcmWeight::from_parts(100_000_000, 0);
pub const MaxAssetsForTransfer: usize = 2;
}
parameter_types! {
pub SelfLocation: Location = Location::new(1, Parachain(ParachainInfo::get().into()));
pub UniversalLocation: InteriorLocation = [
GlobalConsensus(RelayNetwork::get()),
Parachain(ParachainInfo::parachain_id().into())
].into();
}
parameter_type_with_key! {
pub ParachainMinFee: |_location: Location| -> Option<u128> {
None
};
}
impl orml_xtokens::Config for Runtime {
type AccountIdToLocation = AccountIdToLocation;
type Balance = Balance;
type BaseXcmWeight = BaseXcmWeight;
type CurrencyId = CurrencyId;
type CurrencyIdConvert = CurrencyIdConvert;
type LocationsFilter = Everything;
type MaxAssetsForTransfer = MaxAssetsForTransfer;
type MinXcmFee = ParachainMinFee;
type ReserveProvider = AbsoluteReserveProvider;
type RuntimeEvent = RuntimeEvent;
type SelfLocation = SelfLocation;
type UniversalLocation = UniversalLocation;
type Weigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
type XcmExecutor = XcmExecutor<XcmConfig>;
}
impl pallet_restricted_xtokens::Config for Runtime {
type PreTransfer = PreXcmTransfer<super::TransferAllowList, CurrencyIdConvert>;
}
impl cumulus_pallet_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type XcmExecutor = XcmExecutor<XcmConfig>;
}
impl orml_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type SovereignOrigin = EnsureRootOr<HalfOfCouncil>;
}