Skip to content

Commit

Permalink
Merge pull request #564 from gregdhill/refactor/xcm-config
Browse files Browse the repository at this point in the history
refactor: move xcm config to separate file
  • Loading branch information
sander2 authored Apr 8, 2022
2 parents 46677dc + fa2964a commit f99456f
Show file tree
Hide file tree
Showing 9 changed files with 977 additions and 977 deletions.
268 changes: 5 additions & 263 deletions parachain/runtime/interlay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,11 @@ pub use primitives::{
};

// XCM imports
use cumulus_primitives_core::ParaId;
use orml_xcm_support::{DepositToAlternative, IsNativeConcrete, MultiCurrencyAdapter, MultiNativeAsset};
use pallet_xcm::{EnsureXcm, IsMajorityOfBody, XcmPassthrough};
use polkadot_parachain::primitives::Sibling;
use xcm::latest::prelude::*;

use xcm_builder::{
AccountId32Aliases, AllowTopLevelPaidExecutionFrom, EnsureXcmOrigin, FixedWeightBounds, LocationInverter,
NativeAsset, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, UsingComponents,
};
use xcm_executor::{Config, XcmExecutor};
use pallet_xcm::{EnsureXcm, IsMajorityOfBody};
use xcm::opaque::latest::BodyId;
use xcm_config::ParentLocation;

pub mod xcm_config;

type VaultId = primitives::VaultId<AccountId, CurrencyId>;

Expand Down Expand Up @@ -633,257 +626,6 @@ impl parachain_info::Config for Runtime {}

impl cumulus_pallet_aura_ext::Config for Runtime {}

parameter_types! {
pub const ParentLocation: MultiLocation = MultiLocation::parent();
pub const ParentNetwork: NetworkId = NetworkId::Kusama;
pub RelayChainOrigin: Origin = cumulus_pallet_xcm::Origin::Relay.into();
pub Ancestry: MultiLocation = Parachain(ParachainInfo::parachain_id().into()).into();
}

/// Means for transacting assets on this chain.
type LocationToAccountId = (
// The parent (Relay-chain) origin converts to the default `AccountId`.
ParentIsPreset<AccountId>,
// Sibling parachain origins convert to AccountId via the `ParaId::into`.
SiblingParachainConvertsVia<Sibling, AccountId>,
// Straight up local `AccountId32` origins just alias directly to `AccountId`.
AccountId32Aliases<ParentNetwork, AccountId>,
);

/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
/// biases the kind of local `Origin` it will become.
pub type XcmOriginToTransactDispatchOrigin = (
// Sovereign account converter; this attempts to derive an `AccountId` from the origin location
// using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for
// foreign chains who want to have a local sovereign account on this chain which they control.
SovereignSignedViaLocation<LocationToAccountId, Origin>,
// Native converter for Relay-chain (Parent) location; will converts to a `Relay` origin when
// recognised.
RelayChainAsNative<RelayChainOrigin, Origin>,
// Native converter for sibling Parachains; will convert to a `SiblingPara` origin when
// recognised.
SiblingParachainAsNative<cumulus_pallet_xcm::Origin, Origin>,
// Native signed account converter; this just converts an `AccountId32` origin into a normal
// `Origin::Signed` origin of the same 32-byte value.
SignedAccountId32AsNative<ParentNetwork, Origin>,
// Xcm origins can be represented natively under the Xcm pallet's Xcm origin.
XcmPassthrough<Origin>,
);

parameter_types! {
// One XCM operation is 200_000_000 weight, cross-chain transfer ~= 2x of transfer.
pub UnitWeightCost: Weight = 200_000_000;
}

pub type Barrier = (TakeWeightCredit, AllowTopLevelPaidExecutionFrom<Everything>);

parameter_types! {
pub const MaxInstructions: u32 = 100;
}

pub struct XcmConfig;

impl Config for XcmConfig {
type Call = Call;
type XcmSender = XcmRouter;
// How to withdraw and deposit an asset.
type AssetTransactor = LocalAssetTransactor;
type OriginConverter = XcmOriginToTransactDispatchOrigin;
type IsReserve = MultiNativeAsset<AbsoluteReserveProvider>;
type IsTeleporter = NativeAsset; // <- should be enough to allow teleportation
type LocationInverter = LocationInverter<Ancestry>;
type Barrier = Barrier;
type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
type Trader = UsingComponents<
IdentityFee<Balance>,
ParentLocation,
AccountId,
orml_tokens::CurrencyAdapter<Runtime, GetRelayChainCurrencyId>,
(),
>;
type ResponseHandler = PolkadotXcm;
type SubscriptionService = PolkadotXcm;
type AssetTrap = PolkadotXcm;
type AssetClaims = PolkadotXcm;
}

/// No local origins on this chain are allowed to dispatch XCM sends/executions.
pub type LocalOriginToLocation = (SignedToAccountId32<Origin, AccountId, ParentNetwork>,);

/// The means for routing XCM messages which are not for local execution into the right message
/// queues.
pub type XcmRouter = (
// Two routers - use UMP to communicate with the relay chain:
cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm>,
// ..and XCMP to communicate with the sibling chains.
XcmpQueue,
);

impl pallet_xcm::Config for Runtime {
type Event = Event;
type Call = Call;
type Origin = Origin;
type SendXcmOrigin = EnsureXcmOrigin<Origin, LocalOriginToLocation>;
type XcmRouter = XcmRouter;
type ExecuteXcmOrigin = EnsureXcmOrigin<Origin, LocalOriginToLocation>;
type XcmExecuteFilter = Nothing;
type XcmExecutor = XcmExecutor<XcmConfig>;
type XcmTeleportFilter = Everything;
type XcmReserveTransferFilter = Everything;
type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
type LocationInverter = LocationInverter<Ancestry>;
type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
}

impl cumulus_pallet_xcm::Config for Runtime {
type Event = Event;
type XcmExecutor = XcmExecutor<XcmConfig>;
}

impl cumulus_pallet_xcmp_queue::Config for Runtime {
type Event = Event;
type XcmExecutor = XcmExecutor<XcmConfig>;
type ChannelInfo = ParachainSystem;
type VersionWrapper = PolkadotXcm;
type ExecuteOverweightOrigin = EnsureRoot<AccountId>;
type ControllerOrigin = EnsureRoot<AccountId>;
type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin;
type WeightInfo = ();
}

impl cumulus_pallet_dmp_queue::Config for Runtime {
type Event = Event;
type XcmExecutor = XcmExecutor<XcmConfig>;
type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;
}

pub type LocalAssetTransactor = MultiCurrencyAdapter<
Tokens,
UnknownTokens,
IsNativeConcrete<CurrencyId, CurrencyIdConvert>,
AccountId,
LocationToAccountId,
CurrencyId,
CurrencyIdConvert,
DepositToAlternative<TreasuryAccount, Tokens, CurrencyId, AccountId, Balance>,
>;

pub use currency_id_convert::CurrencyIdConvert;

mod currency_id_convert {
use super::*;
use codec::{Decode, Encode};

fn native_currency_location(id: CurrencyId) -> MultiLocation {
MultiLocation::new(1, X2(Parachain(ParachainInfo::get().into()), GeneralKey(id.encode())))
}

pub struct CurrencyIdConvert;

impl Convert<CurrencyId, Option<MultiLocation>> for CurrencyIdConvert {
fn convert(id: CurrencyId) -> Option<MultiLocation> {
match id {
PARENT_CURRENCY_ID => Some(MultiLocation::parent()),
WRAPPED_CURRENCY_ID => Some(native_currency_location(id)),
NATIVE_CURRENCY_ID => Some(native_currency_location(id)),
_ => None,
}
}
}

impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
fn convert(location: MultiLocation) -> Option<CurrencyId> {
fn decode_currency_id(key: Vec<u8>) -> Option<CurrencyId> {
// decode the general key
if let Ok(currency_id) = CurrencyId::decode(&mut &key[..]) {
// check `currency_id` is cross-chain asset
match currency_id {
WRAPPED_CURRENCY_ID => Some(currency_id),
NATIVE_CURRENCY_ID => Some(currency_id),
_ => None,
}
} else {
None
}
}

match location {
x if x == MultiLocation::parent() => Some(PARENT_CURRENCY_ID),
MultiLocation {
parents: 1,
interior: X2(Parachain(id), GeneralKey(key)),
} if ParaId::from(id) == ParachainInfo::get() => decode_currency_id(key),
MultiLocation {
// adapt for reanchor canonical location: https://github.com/paritytech/polkadot/pull/4470
parents: 0,
interior: X1(GeneralKey(key)),
} => decode_currency_id(key),
_ => None,
}
}
}

impl Convert<MultiAsset, Option<CurrencyId>> for CurrencyIdConvert {
fn convert(asset: MultiAsset) -> Option<CurrencyId> {
if let MultiAsset {
id: Concrete(location), ..
} = asset
{
Self::convert(location)
} else {
None
}
}
}
}

parameter_types! {
pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));
pub const MaxAssetsForTransfer: usize = 2;
}

parameter_type_with_key! {
// Only used for transferring parachain tokens to other parachains using DOT as fee currency. Currently we do not support this, hence return MAX.
// See: https://github.com/open-web3-stack/open-runtime-module-library/blob/cadcc9fb10b8212f92668138fc8f83dc0c53acf5/xtokens/README.md#transfer-multiple-currencies
pub ParachainMinFee: |location: MultiLocation| -> u128 {
#[allow(clippy::match_ref_pats)] // false positive
match (location.parents, location.first_interior()) {
_ => u128::MAX,
}
};
}

pub struct AccountIdToMultiLocation;

impl Convert<AccountId, MultiLocation> for AccountIdToMultiLocation {
fn convert(account: AccountId) -> MultiLocation {
X1(AccountId32 {
network: NetworkId::Any,
id: account.into(),
})
.into()
}
}

impl orml_xtokens::Config for Runtime {
type Event = Event;
type Balance = Balance;
type CurrencyId = CurrencyId;
type CurrencyIdConvert = CurrencyIdConvert;
type AccountIdToMultiLocation = AccountIdToMultiLocation;
type SelfLocation = SelfLocation;
type XcmExecutor = XcmExecutor<XcmConfig>;
type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
type BaseXcmWeight = UnitWeightCost;
type LocationInverter = <XcmConfig as Config>::LocationInverter;
type MaxAssetsForTransfer = MaxAssetsForTransfer;
type MinXcmFee = ParachainMinFee;
type MultiLocationsFilter = Everything;
type ReserveProvider = AbsoluteReserveProvider;
}

impl orml_unknown_tokens::Config for Runtime {
type Event = Event;
}
Expand Down
Loading

0 comments on commit f99456f

Please sign in to comment.