use sp_runtime::traits::MaybeEquivalence;
use sp_std::marker::PhantomData;
use xcm::v3::Location;
use xcm_executor::traits::ConvertLocation;
pub struct AsAssetType<AssetId, AssetType, AssetIdInfoGetter>(
PhantomData<(AssetId, AssetType, AssetIdInfoGetter)>,
);
impl<AssetId, AssetType, AssetIdInfoGetter> MaybeEquivalence<Location, AssetId>
for AsAssetType<AssetId, AssetType, AssetIdInfoGetter>
where
AssetId: Clone,
AssetType: From<Location> + Into<Option<Location>> + Clone,
AssetIdInfoGetter: AssetTypeGetter<AssetId, AssetType>,
{
fn convert(id: &Location) -> Option<AssetId> {
AssetIdInfoGetter::get_asset_id(id.clone().into())
}
fn convert_back(what: &AssetId) -> Option<Location> {
AssetIdInfoGetter::get_asset_type(what.clone()).and_then(Into::into)
}
}
impl<AssetId, AssetType, AssetIdInfoGetter> MaybeEquivalence<xcm::v4::Location, AssetId>
for AsAssetType<AssetId, AssetType, AssetIdInfoGetter>
where
AssetId: Clone,
AssetType: From<Location> + Into<Option<Location>> + Clone,
AssetIdInfoGetter: AssetTypeGetter<AssetId, AssetType>,
{
fn convert(id: &xcm::v4::Location) -> Option<AssetId> {
let v3_location =
xcm_builder::WithLatestLocationConverter::<xcm::v3::Location>::convert(id)?;
AssetIdInfoGetter::get_asset_id(v3_location.clone().into())
}
fn convert_back(what: &AssetId) -> Option<xcm::v4::Location> {
let v3_location: Location =
AssetIdInfoGetter::get_asset_type(what.clone()).and_then(Into::into)?;
xcm_builder::WithLatestLocationConverter::convert_back(&v3_location)
}
}
impl<AssetId, AssetType, AssetIdInfoGetter> ConvertLocation<AssetId>
for AsAssetType<AssetId, AssetType, AssetIdInfoGetter>
where
AssetId: Clone,
AssetType: From<Location> + Into<Option<Location>> + Clone,
AssetIdInfoGetter: AssetTypeGetter<AssetId, AssetType>,
{
fn convert_location(id: &xcm::v4::Location) -> Option<AssetId> {
let v3_location =
xcm_builder::WithLatestLocationConverter::<xcm::v3::Location>::convert(id)?;
AssetIdInfoGetter::get_asset_id(v3_location.clone().into())
}
}
pub trait AssetTypeGetter<AssetId, AssetType> {
fn get_asset_type(asset_id: AssetId) -> Option<AssetType>;
fn get_asset_id(asset_type: AssetType) -> Option<AssetId>;
#[cfg(feature = "runtime-benchmarks")]
fn set_asset_type_asset_id(asset_type: AssetType, asset_id: AssetId);
}
pub trait AccountIdToCurrencyId<Account, CurrencyId> {
fn account_to_currency_id(account: Account) -> Option<CurrencyId>;
}