Skip to content

Commit

Permalink
[asset_conversion] adapt paritytech/polkadot-sdk#2031
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur committed Feb 6, 2024
1 parent 7d03f8a commit ded3fad
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 105 deletions.
73 changes: 40 additions & 33 deletions system-parachains/asset-hubs/asset-hub-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub mod xcm_config;

use assets_common::{
foreign_creators::ForeignCreators,
local_and_foreign_assets::{LocalAndForeignAssets, MultiLocationConverter},
local_and_foreign_assets::{LocalFromLeft, TargetFromLeft},
matching::FromSiblingParachain,
AssetIdForTrustBackedAssetsConvert, MultiLocationForAssetId,
};
Expand All @@ -56,8 +56,9 @@ use frame_support::{
genesis_builder_helper::{build_config, create_default_config},
ord_parameter_types, parameter_types,
traits::{
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, EitherOfDiverse,
Equals, InstanceFilter, TransformOrigin,
fungible, fungibles, tokens::imbalance::ResolveAssetTo, AsEnsureOriginWithArg, ConstBool,
ConstU128, ConstU32, ConstU64, ConstU8, EitherOfDiverse, Equals, InstanceFilter,
TransformOrigin,
},
weights::{ConstantMultiplier, Weight},
BoundedVec, PalletId,
Expand All @@ -80,8 +81,9 @@ use system_parachains_constants::{
};
use xcm::opaque::v3::MultiLocation;
use xcm_config::{
FellowshipLocation, ForeignAssetsConvertedConcreteId, GovernanceLocation, KsmLocation,
PoolAssetsConvertedConcreteId, TrustBackedAssetsConvertedConcreteId,
FellowshipLocation, ForeignAssetsConvertedConcreteId, ForeignCreatorsSovereignAccountOf,
GovernanceLocation, KsmLocation, PoolAssetsConvertedConcreteId,
TrustBackedAssetsConvertedConcreteId, TrustBackedAssetsPalletLocation,
};

#[cfg(any(feature = "std", test))]
Expand All @@ -92,10 +94,6 @@ use pallet_xcm::{EnsureXcm, IsVoiceOfBody};
use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
use xcm::prelude::*;

use crate::xcm_config::{
ForeignCreatorsSovereignAccountOf, LocalAndForeignAssetsMultiLocationMatcher,
TrustBackedAssetsPalletLocation,
};
use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};

impl_opaque_keys! {
Expand Down Expand Up @@ -293,8 +291,6 @@ impl pallet_assets::Config<TrustBackedAssetsInstance> for Runtime {

parameter_types! {
pub const AssetConversionPalletId: PalletId = PalletId(*b"py/ascon");
pub const AllowMultiAssetPools: bool = false;
// should be non-zero if AllowMultiAssetPools is true, otherwise can be zero
pub const LiquidityWithdrawalFee: Permill = Permill::from_percent(0);
}

Expand Down Expand Up @@ -329,35 +325,50 @@ impl pallet_assets::Config<PoolAssetsInstance> for Runtime {
type BenchmarkHelper = ();
}

/// Union fungibles implementation for `Assets`` and `ForeignAssets`.
pub type LocalAndForeignAssets = fungibles::UnionOf<
Assets,
ForeignAssets,
LocalFromLeft<
AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>,
AssetIdForTrustBackedAssets,
>,
MultiLocation,
AccountId,
>;

impl pallet_asset_conversion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type HigherPrecisionBalance = sp_core::U256;
type Currency = Balances;
type AssetId = MultiLocation;
type Assets = LocalAndForeignAssets<
Assets,
AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>,
ForeignAssets,
type AssetKind = MultiLocation;
type Assets = fungible::UnionOf<
Balances,
LocalAndForeignAssets,
TargetFromLeft<KsmLocation>,
Self::AssetKind,
Self::AccountId,
>;
type PoolAssets = PoolAssets;
type PoolId = (Self::AssetKind, Self::AssetKind);
type PoolLocator =
pallet_asset_conversion::WithFirstAsset<KsmLocation, AccountId, Self::AssetKind>;
type PoolAssetId = u32;
type PoolAssets = PoolAssets;
type PoolSetupFee = ConstU128<0>; // Asset class deposit fees are sufficient to prevent spam
type PoolSetupFeeReceiver = AssetConversionOrigin;
// should be non-zero if `AllowMultiAssetPools` is true, otherwise can be zero.
type PoolSetupFeeAsset = KsmLocation;
type PoolSetupFeeTarget = ResolveAssetTo<AssetConversionOrigin, Self::Assets>;
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
type LPFee = ConstU32<3>;
type PalletId = AssetConversionPalletId;
type AllowMultiAssetPools = AllowMultiAssetPools;
type MaxSwapPathLength = ConstU32<4>;
type MultiAssetId = MultiLocation;
type MultiAssetIdConverter =
MultiLocationConverter<KsmLocation, LocalAndForeignAssetsMultiLocationMatcher>;
type MintMinLiquidity = ConstU128<100>;
type WeightInfo = weights::pallet_asset_conversion::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper =
crate::xcm_config::BenchmarkMultiLocationConverter<parachain_info::Pallet<Runtime>>;
type BenchmarkHelper = assets_common::benchmarks::AssetPairFactory<
KsmLocation,
parachain_info::Pallet<Runtime>,
xcm_config::AssetsPalletIndex,
>;
}

parameter_types! {
Expand Down Expand Up @@ -762,12 +773,8 @@ impl pallet_collator_selection::Config for Runtime {

impl pallet_asset_conversion_tx_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Fungibles = LocalAndForeignAssets<
Assets,
AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>,
ForeignAssets,
>;
type OnChargeAssetTransaction = AssetConversionAdapter<Balances, AssetConversion>;
type Fungibles = LocalAndForeignAssets;
type OnChargeAssetTransaction = AssetConversionAdapter<Balances, AssetConversion, KsmLocation>;
}

parameter_types! {
Expand Down Expand Up @@ -1127,7 +1134,7 @@ impl_runtime_apis! {
}

fn get_reserves(asset1: MultiLocation, asset2: MultiLocation) -> Option<(Balance, Balance)> {
AssetConversion::get_reserves(&asset1, &asset2).ok()
AssetConversion::get_reserves(asset1, asset2).ok()
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 3 additions & 53 deletions system-parachains/asset-hubs/asset-hub-kusama/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ use super::{
TrustBackedAssetsInstance, WeightToFee, XcmpQueue,
};
use crate::{ForeignAssets, ForeignAssetsInstance};
use assets_common::{
local_and_foreign_assets::MatchesLocalAndForeignAssetsMultiLocation,
matching::{FromSiblingParachain, IsForeignConcreteAsset},
};
use assets_common::matching::{FromSiblingParachain, IsForeignConcreteAsset};
use frame_support::{
match_types, parameter_types,
traits::{ConstU32, Contains, Equals, Everything, Nothing, PalletInfoAccess},
Expand Down Expand Up @@ -58,18 +55,15 @@ use xcm_builder::{
};
use xcm_executor::{traits::WithOriginFilter, XcmExecutor};

#[cfg(feature = "runtime-benchmarks")]
use {cumulus_primitives_core::ParaId, sp_core::Get};

parameter_types! {
pub const KsmLocation: MultiLocation = MultiLocation::parent();
pub const RelayNetwork: Option<NetworkId> = Some(NetworkId::Kusama);
pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
pub UniversalLocation: InteriorMultiLocation =
X2(GlobalConsensus(RelayNetwork::get().unwrap()), Parachain(ParachainInfo::parachain_id().into()));
pub UniversalLocationNetworkId: NetworkId = UniversalLocation::get().global_consensus().unwrap();
pub TrustBackedAssetsPalletLocation: MultiLocation =
PalletInstance(<Assets as PalletInfoAccess>::index() as u8).into();
pub AssetsPalletIndex: u32 = <Assets as PalletInfoAccess>::index() as u32;
pub TrustBackedAssetsPalletLocation: MultiLocation = PalletInstance(AssetsPalletIndex::get() as u8).into();
pub ForeignAssetsPalletLocation: MultiLocation =
PalletInstance(<ForeignAssets as PalletInfoAccess>::index() as u8).into();
pub PoolAssetsPalletLocation: MultiLocation =
Expand Down Expand Up @@ -189,24 +183,6 @@ pub type PoolFungiblesTransactor = FungiblesAdapter<
pub type AssetTransactors =
(CurrencyTransactor, FungiblesTransactor, ForeignFungiblesTransactor, PoolFungiblesTransactor);

/// Simple `MultiLocation` matcher for Local and Foreign asset `MultiLocation`.
pub struct LocalAndForeignAssetsMultiLocationMatcher;
impl MatchesLocalAndForeignAssetsMultiLocation for LocalAndForeignAssetsMultiLocationMatcher {
fn is_local(location: &MultiLocation) -> bool {
use assets_common::fungible_conversion::MatchesMultiLocation;
TrustBackedAssetsConvertedConcreteId::contains(location)
}
fn is_foreign(location: &MultiLocation) -> bool {
use assets_common::fungible_conversion::MatchesMultiLocation;
ForeignAssetsConvertedConcreteId::contains(location)
}
}
impl Contains<MultiLocation> for LocalAndForeignAssetsMultiLocationMatcher {
fn contains(location: &MultiLocation) -> bool {
Self::is_local(location) || Self::is_foreign(location)
}
}

/// 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.
Expand Down Expand Up @@ -688,32 +664,6 @@ impl pallet_assets::BenchmarkHelper<MultiLocation> for XcmBenchmarkHelper {
}
}

#[cfg(feature = "runtime-benchmarks")]
pub struct BenchmarkMultiLocationConverter<SelfParaId> {
_phantom: sp_std::marker::PhantomData<SelfParaId>,
}

#[cfg(feature = "runtime-benchmarks")]
impl<SelfParaId> pallet_asset_conversion::BenchmarkHelper<MultiLocation, MultiLocation>
for BenchmarkMultiLocationConverter<SelfParaId>
where
SelfParaId: Get<ParaId>,
{
fn asset_id(asset_id: u32) -> MultiLocation {
MultiLocation {
parents: 1,
interior: X3(
Parachain(SelfParaId::get().into()),
PalletInstance(<Assets as PalletInfoAccess>::index() as u8),
GeneralIndex(asset_id.into()),
),
}
}
fn multiasset_id(asset_id: u32) -> MultiLocation {
Self::asset_id(asset_id)
}
}

/// All configuration related to bridging
pub mod bridging {
use super::*;
Expand Down
6 changes: 3 additions & 3 deletions system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ use system_parachains_constants::{
SLOT_DURATION,
};
use xcm_config::{
DotLocation, FellowshipLocation, ForeignAssetsConvertedConcreteId, GovernanceLocation,
TrustBackedAssetsConvertedConcreteId, XcmOriginToTransactDispatchOrigin,
DotLocation, FellowshipLocation, ForeignAssetsConvertedConcreteId,
ForeignCreatorsSovereignAccountOf, GovernanceLocation, TrustBackedAssetsConvertedConcreteId,
XcmOriginToTransactDispatchOrigin,
};

#[cfg(any(feature = "std", test))]
Expand All @@ -125,7 +126,6 @@ use pallet_xcm::{EnsureXcm, IsVoiceOfBody};
use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
use xcm::prelude::*;

use crate::xcm_config::ForeignCreatorsSovereignAccountOf;
use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};

impl_opaque_keys! {
Expand Down

0 comments on commit ded3fad

Please sign in to comment.