Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mustermeiszer committed Aug 29, 2024
1 parent 07e3fb4 commit a3fb413
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
27 changes: 15 additions & 12 deletions libs/types/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ use orml_traits::asset_registry;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_core::{bounded::BoundedBTreeSet, bounded_vec::BoundedVec};
use sp_runtime::{traits::Get, DispatchError, TokenError};

use crate::{xcm::XcmMetadata, EVMChainId};

pub const MAX_ASSET_STRING_LIMIT: u32 = 128;

pub const MAX_LOCAL_VARIANTS: u32 = 10;

frame_support::parameter_types! {
#[derive(TypeInfo, Eq, PartialEq, Debug, Clone, Copy )]
pub const AssetStringLimit: u32 = MAX_ASSET_STRING_LIMIT;

#[derive(TypeInfo, Eq, PartialEq, Debug, Clone, Copy )]
pub const VariantsLimit: u32 = MAX_LOCAL_VARIANTS;
}

pub type AssetMetadata = asset_registry::AssetMetadata<Balance, CustomMetadata, AssetStringLimit>;
Expand Down Expand Up @@ -109,12 +115,12 @@ impl From<LocalAssetId> for CurrencyId {
}
}

impl TryFrom<CurrencyId> for LocalAssetId {
impl TryFrom<&CurrencyId> for LocalAssetId {
type Error = ();

fn try_from(value: CurrencyId) -> Result<Self, Self::Error> {
fn try_from(value: &CurrencyId) -> Result<Self, Self::Error> {
if let CurrencyId::LocalAsset(local) = value {
Ok(local)
Ok(*local)
} else {
Err(())
}
Expand Down Expand Up @@ -275,7 +281,8 @@ pub struct CustomMetadata {
/// Whether an asset has a local representation. Usually, this means that we
/// are receiving the same asset from multiple domains and unify the asset
/// under a common local representation.
pub local_representation: Option<LocalAssetId>,
/// Can be multiple
pub local_representations: Option<BoundedBTreeSet<LocalAssetId, VariantsLimit>>,
}

#[cfg(feature = "std")]
Expand Down Expand Up @@ -375,17 +382,13 @@ where
>,
{
fn is_local_representation_of(&self, variant_currency: &Self) -> Result<bool, DispatchError> {
let meta_local = AssetInspect::metadata(self).ok_or(DispatchError::CannotLookup)?;
let variant_local_id = LocalAssetId::try_from(variant_currency)
.map_err(DispatchError::Other("Not a local variant"))?;
let meta_variant =
AssetInspect::metadata(variant_currency).ok_or(DispatchError::CannotLookup)?;

if let Some(local) = meta_variant.additional.local_representation {
frame_support::ensure!(
meta_local.decimals == meta_variant.decimals,
DispatchError::Other("Mismatching decimals")
);

Ok(self == &local.into())
if let Some(local) = meta_variant.additional.local_representations {
Ok(local.iter().any(|local_id| local_id == &variant_local_id))
} else {
Ok(false)
}
Expand Down
23 changes: 11 additions & 12 deletions pallets/token-mux/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,20 +343,19 @@ pub mod pallet {
.map(|_| ())
}

pub(crate) fn try_local(currency: &T::CurrencyId) -> Result<T::CurrencyId, DispatchError> {
let meta_variant =
T::AssetRegistry::metadata(currency).ok_or(Error::<T>::MetadataNotFound)?;

let local: T::CurrencyId = T::LocalAssetId::from(
meta_variant
.additional
.local_representation
.ok_or(Error::<T>::NoLocalRepresentation)?,
)
.into();
pub(crate) fn try_local(
variant: &T::CurrencyId,
local: &T::CurrencyId,
) -> Result<T::CurrencyId, DispatchError> {
ensure!(
local.is_local_representation_of(variant)?,
Error::<T>::NoLocalRepresentation
);

let meta_variant =
T::AssetRegistry::metadata(variant).ok_or(Error::<T>::MetadataNotFound)?;
let meta_local =
T::AssetRegistry::metadata(&local).ok_or(Error::<T>::MetadataNotFound)?;
T::AssetRegistry::metadata(local).ok_or(Error::<T>::MetadataNotFound)?;

// NOTE: We could also think about making conversion between local
// representations and variants but I fear that we then have problems with
Expand Down

0 comments on commit a3fb413

Please sign in to comment.