use codec::{Decode, Encode};
use scale_info::TypeInfo;
use xcm::VersionedLocation;
use xcm_executor::traits::ConvertLocation;
sp_api::decl_runtime_apis! {
pub trait LocationToAccountApi<AccountId> where AccountId: Decode {
fn convert_location(location: VersionedLocation) -> Result<AccountId, Error>;
}
}
#[derive(Copy, Clone, Encode, Decode, Eq, PartialEq, Debug, TypeInfo)]
pub enum Error {
#[codec(index = 0)]
Unsupported,
#[codec(index = 1)]
VersionedConversionFailed,
}
pub struct LocationToAccountHelper<AccountId, Conversion>(
core::marker::PhantomData<(AccountId, Conversion)>,
);
impl<AccountId: Decode, Conversion: ConvertLocation<AccountId>>
LocationToAccountHelper<AccountId, Conversion>
{
pub fn convert_location(location: VersionedLocation) -> Result<AccountId, Error> {
let location = location.try_into().map_err(|_| Error::VersionedConversionFailed)?;
Conversion::convert_location(&location).ok_or(Error::Unsupported)
}
}