use super::{InteriorMultiLocation, MultiLocation};
use crate::{
v2::{
AssetId as OldAssetId, AssetInstance as OldAssetInstance, Fungibility as OldFungibility,
MultiAsset as OldMultiAsset, MultiAssetFilter as OldMultiAssetFilter,
MultiAssets as OldMultiAssets, WildFungibility as OldWildFungibility,
WildMultiAsset as OldWildMultiAsset,
},
v4::{
Asset as NewMultiAsset, AssetFilter as NewMultiAssetFilter, AssetId as NewAssetId,
AssetInstance as NewAssetInstance, Assets as NewMultiAssets, Fungibility as NewFungibility,
WildAsset as NewWildMultiAsset, WildFungibility as NewWildFungibility,
},
};
use alloc::{vec, vec::Vec};
use bounded_collections::{BoundedVec, ConstU32};
use codec::{self as codec, Decode, Encode, MaxEncodedLen};
use core::cmp::Ordering;
use scale_info::TypeInfo;
#[derive(
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Encode,
Decode,
Debug,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum AssetInstance {
Undefined,
Index(#[codec(compact)] u128),
Array4([u8; 4]),
Array8([u8; 8]),
Array16([u8; 16]),
Array32([u8; 32]),
}
impl TryFrom<OldAssetInstance> for AssetInstance {
type Error = ();
fn try_from(value: OldAssetInstance) -> Result<Self, Self::Error> {
use OldAssetInstance::*;
Ok(match value {
Undefined => Self::Undefined,
Index(n) => Self::Index(n),
Array4(n) => Self::Array4(n),
Array8(n) => Self::Array8(n),
Array16(n) => Self::Array16(n),
Array32(n) => Self::Array32(n),
Blob(_) => return Err(()),
})
}
}
impl TryFrom<NewAssetInstance> for AssetInstance {
type Error = ();
fn try_from(value: NewAssetInstance) -> Result<Self, Self::Error> {
use NewAssetInstance::*;
Ok(match value {
Undefined => Self::Undefined,
Index(n) => Self::Index(n),
Array4(n) => Self::Array4(n),
Array8(n) => Self::Array8(n),
Array16(n) => Self::Array16(n),
Array32(n) => Self::Array32(n),
})
}
}
impl From<()> for AssetInstance {
fn from(_: ()) -> Self {
Self::Undefined
}
}
impl From<[u8; 4]> for AssetInstance {
fn from(x: [u8; 4]) -> Self {
Self::Array4(x)
}
}
impl From<[u8; 8]> for AssetInstance {
fn from(x: [u8; 8]) -> Self {
Self::Array8(x)
}
}
impl From<[u8; 16]> for AssetInstance {
fn from(x: [u8; 16]) -> Self {
Self::Array16(x)
}
}
impl From<[u8; 32]> for AssetInstance {
fn from(x: [u8; 32]) -> Self {
Self::Array32(x)
}
}
impl From<u8> for AssetInstance {
fn from(x: u8) -> Self {
Self::Index(x as u128)
}
}
impl From<u16> for AssetInstance {
fn from(x: u16) -> Self {
Self::Index(x as u128)
}
}
impl From<u32> for AssetInstance {
fn from(x: u32) -> Self {
Self::Index(x as u128)
}
}
impl From<u64> for AssetInstance {
fn from(x: u64) -> Self {
Self::Index(x as u128)
}
}
impl TryFrom<AssetInstance> for () {
type Error = ();
fn try_from(x: AssetInstance) -> Result<Self, ()> {
match x {
AssetInstance::Undefined => Ok(()),
_ => Err(()),
}
}
}
impl TryFrom<AssetInstance> for [u8; 4] {
type Error = ();
fn try_from(x: AssetInstance) -> Result<Self, ()> {
match x {
AssetInstance::Array4(x) => Ok(x),
_ => Err(()),
}
}
}
impl TryFrom<AssetInstance> for [u8; 8] {
type Error = ();
fn try_from(x: AssetInstance) -> Result<Self, ()> {
match x {
AssetInstance::Array8(x) => Ok(x),
_ => Err(()),
}
}
}
impl TryFrom<AssetInstance> for [u8; 16] {
type Error = ();
fn try_from(x: AssetInstance) -> Result<Self, ()> {
match x {
AssetInstance::Array16(x) => Ok(x),
_ => Err(()),
}
}
}
impl TryFrom<AssetInstance> for [u8; 32] {
type Error = ();
fn try_from(x: AssetInstance) -> Result<Self, ()> {
match x {
AssetInstance::Array32(x) => Ok(x),
_ => Err(()),
}
}
}
impl TryFrom<AssetInstance> for u8 {
type Error = ();
fn try_from(x: AssetInstance) -> Result<Self, ()> {
match x {
AssetInstance::Index(x) => x.try_into().map_err(|_| ()),
_ => Err(()),
}
}
}
impl TryFrom<AssetInstance> for u16 {
type Error = ();
fn try_from(x: AssetInstance) -> Result<Self, ()> {
match x {
AssetInstance::Index(x) => x.try_into().map_err(|_| ()),
_ => Err(()),
}
}
}
impl TryFrom<AssetInstance> for u32 {
type Error = ();
fn try_from(x: AssetInstance) -> Result<Self, ()> {
match x {
AssetInstance::Index(x) => x.try_into().map_err(|_| ()),
_ => Err(()),
}
}
}
impl TryFrom<AssetInstance> for u64 {
type Error = ();
fn try_from(x: AssetInstance) -> Result<Self, ()> {
match x {
AssetInstance::Index(x) => x.try_into().map_err(|_| ()),
_ => Err(()),
}
}
}
impl TryFrom<AssetInstance> for u128 {
type Error = ();
fn try_from(x: AssetInstance) -> Result<Self, ()> {
match x {
AssetInstance::Index(x) => Ok(x),
_ => Err(()),
}
}
}
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum Fungibility {
Fungible(#[codec(compact)] u128),
NonFungible(AssetInstance),
}
#[derive(Decode)]
enum UncheckedFungibility {
Fungible(#[codec(compact)] u128),
NonFungible(AssetInstance),
}
impl Decode for Fungibility {
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
match UncheckedFungibility::decode(input)? {
UncheckedFungibility::Fungible(a) if a != 0 => Ok(Self::Fungible(a)),
UncheckedFungibility::NonFungible(i) => Ok(Self::NonFungible(i)),
UncheckedFungibility::Fungible(_) =>
Err("Fungible asset of zero amount is not allowed".into()),
}
}
}
impl Fungibility {
pub fn is_kind(&self, w: WildFungibility) -> bool {
use Fungibility::*;
use WildFungibility::{Fungible as WildFungible, NonFungible as WildNonFungible};
matches!((self, w), (Fungible(_), WildFungible) | (NonFungible(_), WildNonFungible))
}
}
impl From<i32> for Fungibility {
fn from(amount: i32) -> Fungibility {
debug_assert_ne!(amount, 0);
Fungibility::Fungible(amount as u128)
}
}
impl From<u128> for Fungibility {
fn from(amount: u128) -> Fungibility {
debug_assert_ne!(amount, 0);
Fungibility::Fungible(amount)
}
}
impl<T: Into<AssetInstance>> From<T> for Fungibility {
fn from(instance: T) -> Fungibility {
Fungibility::NonFungible(instance.into())
}
}
impl TryFrom<OldFungibility> for Fungibility {
type Error = ();
fn try_from(value: OldFungibility) -> Result<Self, Self::Error> {
use OldFungibility::*;
Ok(match value {
Fungible(n) => Self::Fungible(n),
NonFungible(i) => Self::NonFungible(i.try_into()?),
})
}
}
impl TryFrom<NewFungibility> for Fungibility {
type Error = ();
fn try_from(value: NewFungibility) -> Result<Self, Self::Error> {
use NewFungibility::*;
Ok(match value {
Fungible(n) => Self::Fungible(n),
NonFungible(i) => Self::NonFungible(i.try_into()?),
})
}
}
#[derive(
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum WildFungibility {
Fungible,
NonFungible,
}
impl TryFrom<OldWildFungibility> for WildFungibility {
type Error = ();
fn try_from(value: OldWildFungibility) -> Result<Self, Self::Error> {
use OldWildFungibility::*;
Ok(match value {
Fungible => Self::Fungible,
NonFungible => Self::NonFungible,
})
}
}
impl TryFrom<NewWildFungibility> for WildFungibility {
type Error = ();
fn try_from(value: NewWildFungibility) -> Result<Self, Self::Error> {
use NewWildFungibility::*;
Ok(match value {
Fungible => Self::Fungible,
NonFungible => Self::NonFungible,
})
}
}
#[derive(
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum AssetId {
Concrete(MultiLocation),
Abstract([u8; 32]),
}
impl<T: Into<MultiLocation>> From<T> for AssetId {
fn from(x: T) -> Self {
Self::Concrete(x.into())
}
}
impl From<[u8; 32]> for AssetId {
fn from(x: [u8; 32]) -> Self {
Self::Abstract(x)
}
}
impl TryFrom<OldAssetId> for AssetId {
type Error = ();
fn try_from(old: OldAssetId) -> Result<Self, ()> {
use OldAssetId::*;
Ok(match old {
Concrete(l) => Self::Concrete(l.try_into()?),
Abstract(v) if v.len() <= 32 => {
let mut r = [0u8; 32];
r[..v.len()].copy_from_slice(&v[..]);
Self::Abstract(r)
},
_ => return Err(()),
})
}
}
impl TryFrom<NewAssetId> for AssetId {
type Error = ();
fn try_from(new: NewAssetId) -> Result<Self, Self::Error> {
Ok(Self::Concrete(new.0.try_into()?))
}
}
impl AssetId {
pub fn prepend_with(&mut self, prepend: &MultiLocation) -> Result<(), ()> {
if let AssetId::Concrete(ref mut l) = self {
l.prepend_with(*prepend).map_err(|_| ())?;
}
Ok(())
}
pub fn reanchor(
&mut self,
target: &MultiLocation,
context: InteriorMultiLocation,
) -> Result<(), ()> {
if let AssetId::Concrete(ref mut l) = self {
l.reanchor(target, context)?;
}
Ok(())
}
pub fn into_multiasset(self, fun: Fungibility) -> MultiAsset {
MultiAsset { fun, id: self }
}
pub fn into_wild(self, fun: WildFungibility) -> WildMultiAsset {
WildMultiAsset::AllOf { fun, id: self }
}
}
#[derive(
Clone,
Eq,
PartialEq,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub struct MultiAsset {
pub id: AssetId,
pub fun: Fungibility,
}
impl PartialOrd for MultiAsset {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for MultiAsset {
fn cmp(&self, other: &Self) -> Ordering {
match (&self.fun, &other.fun) {
(Fungibility::Fungible(..), Fungibility::NonFungible(..)) => Ordering::Less,
(Fungibility::NonFungible(..), Fungibility::Fungible(..)) => Ordering::Greater,
_ => (&self.id, &self.fun).cmp(&(&other.id, &other.fun)),
}
}
}
impl<A: Into<AssetId>, B: Into<Fungibility>> From<(A, B)> for MultiAsset {
fn from((id, fun): (A, B)) -> MultiAsset {
MultiAsset { fun: fun.into(), id: id.into() }
}
}
impl MultiAsset {
pub fn is_fungible(&self, maybe_id: Option<AssetId>) -> bool {
use Fungibility::*;
matches!(self.fun, Fungible(..)) && maybe_id.map_or(true, |i| i == self.id)
}
pub fn is_non_fungible(&self, maybe_id: Option<AssetId>) -> bool {
use Fungibility::*;
matches!(self.fun, NonFungible(..)) && maybe_id.map_or(true, |i| i == self.id)
}
pub fn prepend_with(&mut self, prepend: &MultiLocation) -> Result<(), ()> {
self.id.prepend_with(prepend)
}
pub fn reanchor(
&mut self,
target: &MultiLocation,
context: InteriorMultiLocation,
) -> Result<(), ()> {
self.id.reanchor(target, context)
}
pub fn reanchored(
mut self,
target: &MultiLocation,
context: InteriorMultiLocation,
) -> Result<Self, ()> {
self.id.reanchor(target, context)?;
Ok(self)
}
pub fn contains(&self, inner: &MultiAsset) -> bool {
use Fungibility::*;
if self.id == inner.id {
match (&self.fun, &inner.fun) {
(Fungible(a), Fungible(i)) if a >= i => return true,
(NonFungible(a), NonFungible(i)) if a == i => return true,
_ => (),
}
}
false
}
}
impl TryFrom<OldMultiAsset> for MultiAsset {
type Error = ();
fn try_from(old: OldMultiAsset) -> Result<Self, ()> {
Ok(Self { id: old.id.try_into()?, fun: old.fun.try_into()? })
}
}
impl TryFrom<NewMultiAsset> for MultiAsset {
type Error = ();
fn try_from(new: NewMultiAsset) -> Result<Self, Self::Error> {
Ok(Self { id: new.id.try_into()?, fun: new.fun.try_into()? })
}
}
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
TypeInfo,
Default,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub struct MultiAssets(Vec<MultiAsset>);
pub const MAX_ITEMS_IN_MULTIASSETS: usize = 20;
impl MaxEncodedLen for MultiAssets {
fn max_encoded_len() -> usize {
MultiAsset::max_encoded_len() * MAX_ITEMS_IN_MULTIASSETS
}
}
impl Decode for MultiAssets {
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
let bounded_instructions =
BoundedVec::<MultiAsset, ConstU32<{ MAX_ITEMS_IN_MULTIASSETS as u32 }>>::decode(input)?;
Self::from_sorted_and_deduplicated(bounded_instructions.into_inner())
.map_err(|()| "Out of order".into())
}
}
impl TryFrom<OldMultiAssets> for MultiAssets {
type Error = ();
fn try_from(old: OldMultiAssets) -> Result<Self, ()> {
let v = old
.drain()
.into_iter()
.map(MultiAsset::try_from)
.collect::<Result<Vec<_>, ()>>()?;
Ok(MultiAssets(v))
}
}
impl TryFrom<NewMultiAssets> for MultiAssets {
type Error = ();
fn try_from(new: NewMultiAssets) -> Result<Self, Self::Error> {
let v = new
.into_inner()
.into_iter()
.map(MultiAsset::try_from)
.collect::<Result<Vec<_>, ()>>()?;
Ok(MultiAssets(v))
}
}
impl From<Vec<MultiAsset>> for MultiAssets {
fn from(mut assets: Vec<MultiAsset>) -> Self {
let mut res = Vec::with_capacity(assets.len());
if !assets.is_empty() {
assets.sort();
let mut iter = assets.into_iter();
if let Some(first) = iter.next() {
let last = iter.fold(first, |a, b| -> MultiAsset {
match (a, b) {
(
MultiAsset { fun: Fungibility::Fungible(a_amount), id: a_id },
MultiAsset { fun: Fungibility::Fungible(b_amount), id: b_id },
) if a_id == b_id => MultiAsset {
id: a_id,
fun: Fungibility::Fungible(a_amount.saturating_add(b_amount)),
},
(
MultiAsset { fun: Fungibility::NonFungible(a_instance), id: a_id },
MultiAsset { fun: Fungibility::NonFungible(b_instance), id: b_id },
) if a_id == b_id && a_instance == b_instance =>
MultiAsset { fun: Fungibility::NonFungible(a_instance), id: a_id },
(to_push, to_remember) => {
res.push(to_push);
to_remember
},
}
});
res.push(last);
}
}
Self(res)
}
}
impl<T: Into<MultiAsset>> From<T> for MultiAssets {
fn from(x: T) -> Self {
Self(vec![x.into()])
}
}
impl MultiAssets {
pub fn new() -> Self {
Self(Vec::new())
}
pub fn from_sorted_and_deduplicated(r: Vec<MultiAsset>) -> Result<Self, ()> {
if r.is_empty() {
return Ok(Self(Vec::new()))
}
r.iter().skip(1).try_fold(&r[0], |a, b| -> Result<&MultiAsset, ()> {
if a.id < b.id || a < b && (a.is_non_fungible(None) || b.is_non_fungible(None)) {
Ok(b)
} else {
Err(())
}
})?;
Ok(Self(r))
}
#[cfg(test)]
pub fn from_sorted_and_deduplicated_skip_checks(r: Vec<MultiAsset>) -> Self {
Self::from_sorted_and_deduplicated(r).expect("Invalid input r is not sorted/deduped")
}
#[cfg(not(test))]
pub fn from_sorted_and_deduplicated_skip_checks(r: Vec<MultiAsset>) -> Self {
Self(r)
}
pub fn push(&mut self, a: MultiAsset) {
for asset in self.0.iter_mut().filter(|x| x.id == a.id) {
match (&a.fun, &mut asset.fun) {
(Fungibility::Fungible(amount), Fungibility::Fungible(balance)) => {
*balance = balance.saturating_add(*amount);
return
},
(Fungibility::NonFungible(inst1), Fungibility::NonFungible(inst2))
if inst1 == inst2 =>
return,
_ => (),
}
}
self.0.push(a);
self.0.sort();
}
pub fn is_none(&self) -> bool {
self.0.is_empty()
}
pub fn contains(&self, inner: &MultiAsset) -> bool {
self.0.iter().any(|i| i.contains(inner))
}
#[deprecated = "Use `into_inner()` instead"]
pub fn drain(self) -> Vec<MultiAsset> {
self.0
}
pub fn into_inner(self) -> Vec<MultiAsset> {
self.0
}
pub fn inner(&self) -> &Vec<MultiAsset> {
&self.0
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn prepend_with(&mut self, prefix: &MultiLocation) -> Result<(), ()> {
self.0.iter_mut().try_for_each(|i| i.prepend_with(prefix))?;
self.0.sort();
Ok(())
}
pub fn reanchor(
&mut self,
target: &MultiLocation,
context: InteriorMultiLocation,
) -> Result<(), ()> {
self.0.iter_mut().try_for_each(|i| i.reanchor(target, context))?;
self.0.sort();
Ok(())
}
pub fn get(&self, index: usize) -> Option<&MultiAsset> {
self.0.get(index)
}
}
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum WildMultiAsset {
All,
AllOf { id: AssetId, fun: WildFungibility },
AllCounted(#[codec(compact)] u32),
AllOfCounted {
id: AssetId,
fun: WildFungibility,
#[codec(compact)]
count: u32,
},
}
impl TryFrom<OldWildMultiAsset> for WildMultiAsset {
type Error = ();
fn try_from(old: OldWildMultiAsset) -> Result<WildMultiAsset, ()> {
use OldWildMultiAsset::*;
Ok(match old {
AllOf { id, fun } => Self::AllOf { id: id.try_into()?, fun: fun.try_into()? },
All => Self::All,
})
}
}
impl TryFrom<NewWildMultiAsset> for WildMultiAsset {
type Error = ();
fn try_from(new: NewWildMultiAsset) -> Result<Self, ()> {
use NewWildMultiAsset::*;
Ok(match new {
AllOf { id, fun } => Self::AllOf { id: id.try_into()?, fun: fun.try_into()? },
AllOfCounted { id, fun, count } =>
Self::AllOfCounted { id: id.try_into()?, fun: fun.try_into()?, count },
All => Self::All,
AllCounted(count) => Self::AllCounted(count),
})
}
}
impl TryFrom<(OldWildMultiAsset, u32)> for WildMultiAsset {
type Error = ();
fn try_from(old: (OldWildMultiAsset, u32)) -> Result<WildMultiAsset, ()> {
use OldWildMultiAsset::*;
let count = old.1;
Ok(match old.0 {
AllOf { id, fun } =>
Self::AllOfCounted { id: id.try_into()?, fun: fun.try_into()?, count },
All => Self::AllCounted(count),
})
}
}
impl WildMultiAsset {
pub fn contains(&self, inner: &MultiAsset) -> bool {
use WildMultiAsset::*;
match self {
AllOfCounted { count: 0, .. } | AllCounted(0) => false,
AllOf { fun, id } | AllOfCounted { id, fun, .. } =>
inner.fun.is_kind(*fun) && &inner.id == id,
All | AllCounted(_) => true,
}
}
#[deprecated = "Use `contains` instead"]
pub fn matches(&self, inner: &MultiAsset) -> bool {
self.contains(inner)
}
pub fn reanchor(
&mut self,
target: &MultiLocation,
context: InteriorMultiLocation,
) -> Result<(), ()> {
use WildMultiAsset::*;
match self {
AllOf { ref mut id, .. } | AllOfCounted { ref mut id, .. } =>
id.reanchor(target, context),
All | AllCounted(_) => Ok(()),
}
}
pub fn count(&self) -> Option<u32> {
use WildMultiAsset::*;
match self {
AllOfCounted { count, .. } | AllCounted(count) => Some(*count),
All | AllOf { .. } => None,
}
}
pub fn limit(&self) -> Option<u32> {
self.count()
}
pub fn counted(self, count: u32) -> Self {
use WildMultiAsset::*;
match self {
AllOfCounted { fun, id, .. } | AllOf { fun, id } => AllOfCounted { fun, id, count },
All | AllCounted(_) => AllCounted(count),
}
}
}
impl<A: Into<AssetId>, B: Into<WildFungibility>> From<(A, B)> for WildMultiAsset {
fn from((id, fun): (A, B)) -> WildMultiAsset {
WildMultiAsset::AllOf { fun: fun.into(), id: id.into() }
}
}
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
serde::Serialize,
serde::Deserialize,
)]
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
#[scale_info(replace_segment("staging_xcm", "xcm"))]
pub enum MultiAssetFilter {
Definite(MultiAssets),
Wild(WildMultiAsset),
}
impl<T: Into<WildMultiAsset>> From<T> for MultiAssetFilter {
fn from(x: T) -> Self {
Self::Wild(x.into())
}
}
impl From<MultiAsset> for MultiAssetFilter {
fn from(x: MultiAsset) -> Self {
Self::Definite(vec![x].into())
}
}
impl From<Vec<MultiAsset>> for MultiAssetFilter {
fn from(x: Vec<MultiAsset>) -> Self {
Self::Definite(x.into())
}
}
impl From<MultiAssets> for MultiAssetFilter {
fn from(x: MultiAssets) -> Self {
Self::Definite(x)
}
}
impl MultiAssetFilter {
pub fn matches(&self, inner: &MultiAsset) -> bool {
match self {
MultiAssetFilter::Definite(ref assets) => assets.contains(inner),
MultiAssetFilter::Wild(ref wild) => wild.contains(inner),
}
}
pub fn reanchor(
&mut self,
target: &MultiLocation,
context: InteriorMultiLocation,
) -> Result<(), ()> {
match self {
MultiAssetFilter::Definite(ref mut assets) => assets.reanchor(target, context),
MultiAssetFilter::Wild(ref mut wild) => wild.reanchor(target, context),
}
}
pub fn count(&self) -> Option<u32> {
use MultiAssetFilter::*;
match self {
Definite(x) => Some(x.len() as u32),
Wild(x) => x.count(),
}
}
pub fn limit(&self) -> Option<u32> {
use MultiAssetFilter::*;
match self {
Definite(_) => None,
Wild(x) => x.limit(),
}
}
}
impl TryFrom<OldMultiAssetFilter> for MultiAssetFilter {
type Error = ();
fn try_from(old: OldMultiAssetFilter) -> Result<MultiAssetFilter, ()> {
Ok(match old {
OldMultiAssetFilter::Definite(x) => Self::Definite(x.try_into()?),
OldMultiAssetFilter::Wild(x) => Self::Wild(x.try_into()?),
})
}
}
impl TryFrom<NewMultiAssetFilter> for MultiAssetFilter {
type Error = ();
fn try_from(new: NewMultiAssetFilter) -> Result<MultiAssetFilter, Self::Error> {
use NewMultiAssetFilter::*;
Ok(match new {
Definite(x) => Self::Definite(x.try_into()?),
Wild(x) => Self::Wild(x.try_into()?),
})
}
}
impl TryFrom<(OldMultiAssetFilter, u32)> for MultiAssetFilter {
type Error = ();
fn try_from(old: (OldMultiAssetFilter, u32)) -> Result<MultiAssetFilter, ()> {
let count = old.1;
Ok(match old.0 {
OldMultiAssetFilter::Definite(x) if count >= x.len() as u32 =>
Self::Definite(x.try_into()?),
OldMultiAssetFilter::Wild(x) => Self::Wild((x, count).try_into()?),
_ => return Err(()),
})
}
}
#[cfg(test)]
mod tests {
use super::super::prelude::*;
#[test]
fn conversion_works() {
let _: MultiAssets = (Here, 1u128).into();
}
#[test]
fn from_sorted_and_deduplicated_works() {
use super::*;
use alloc::vec;
let empty = vec![];
let r = MultiAssets::from_sorted_and_deduplicated(empty);
assert_eq!(r, Ok(MultiAssets(vec![])));
let dup_fun = vec![(Here, 100).into(), (Here, 10).into()];
let r = MultiAssets::from_sorted_and_deduplicated(dup_fun);
assert!(r.is_err());
let dup_nft = vec![(Here, *b"notgood!").into(), (Here, *b"notgood!").into()];
let r = MultiAssets::from_sorted_and_deduplicated(dup_nft);
assert!(r.is_err());
let good_fun = vec![(Here, 10).into(), (Parent, 10).into()];
let r = MultiAssets::from_sorted_and_deduplicated(good_fun.clone());
assert_eq!(r, Ok(MultiAssets(good_fun)));
let bad_fun = vec![(Parent, 10).into(), (Here, 10).into()];
let r = MultiAssets::from_sorted_and_deduplicated(bad_fun);
assert!(r.is_err());
let good_abstract_fun = vec![(Here, 100).into(), ([0u8; 32], 10).into()];
let r = MultiAssets::from_sorted_and_deduplicated(good_abstract_fun.clone());
assert_eq!(r, Ok(MultiAssets(good_abstract_fun)));
let bad_abstract_fun = vec![([0u8; 32], 10).into(), (Here, 10).into()];
let r = MultiAssets::from_sorted_and_deduplicated(bad_abstract_fun);
assert!(r.is_err());
let good_nft = vec![(Here, ()).into(), (Here, *b"good").into()];
let r = MultiAssets::from_sorted_and_deduplicated(good_nft.clone());
assert_eq!(r, Ok(MultiAssets(good_nft)));
let bad_nft = vec![(Here, *b"bad!").into(), (Here, ()).into()];
let r = MultiAssets::from_sorted_and_deduplicated(bad_nft);
assert!(r.is_err());
let good_abstract_nft = vec![(Here, ()).into(), ([0u8; 32], ()).into()];
let r = MultiAssets::from_sorted_and_deduplicated(good_abstract_nft.clone());
assert_eq!(r, Ok(MultiAssets(good_abstract_nft)));
let bad_abstract_nft = vec![([0u8; 32], ()).into(), (Here, ()).into()];
let r = MultiAssets::from_sorted_and_deduplicated(bad_abstract_nft);
assert!(r.is_err());
let mixed_good = vec![(Here, 10).into(), (Here, *b"good").into()];
let r = MultiAssets::from_sorted_and_deduplicated(mixed_good.clone());
assert_eq!(r, Ok(MultiAssets(mixed_good)));
let mixed_bad = vec![(Here, *b"bad!").into(), (Here, 10).into()];
let r = MultiAssets::from_sorted_and_deduplicated(mixed_bad);
assert!(r.is_err());
}
#[test]
fn reanchor_preserves_sorting() {
use super::*;
use alloc::vec;
let reanchor_context = X1(Parachain(2000));
let dest = MultiLocation::new(1, Here);
let asset_1: MultiAsset =
(MultiLocation::new(0, X2(PalletInstance(50), GeneralIndex(1))), 10).into();
let mut asset_1_reanchored = asset_1.clone();
assert!(asset_1_reanchored.reanchor(&dest, reanchor_context).is_ok());
assert_eq!(
asset_1_reanchored,
(MultiLocation::new(0, X3(Parachain(2000), PalletInstance(50), GeneralIndex(1))), 10)
.into()
);
let asset_2: MultiAsset = (MultiLocation::new(1, Here), 10).into();
let mut asset_2_reanchored = asset_2.clone();
assert!(asset_2_reanchored.reanchor(&dest, reanchor_context).is_ok());
assert_eq!(asset_2_reanchored, (MultiLocation::new(0, Here), 10).into());
let asset_3: MultiAsset = (MultiLocation::new(1, X1(Parachain(1000))), 10).into();
let mut asset_3_reanchored = asset_3.clone();
assert!(asset_3_reanchored.reanchor(&dest, reanchor_context).is_ok());
assert_eq!(asset_3_reanchored, (MultiLocation::new(0, X1(Parachain(1000))), 10).into());
let mut assets: MultiAssets =
vec![asset_1.clone(), asset_2.clone(), asset_3.clone()].into();
assert_eq!(assets.clone(), vec![asset_1.clone(), asset_2.clone(), asset_3.clone()].into());
assert!(assets
.using_encoded(|mut enc| MultiAssets::decode(&mut enc).map(|_| ()))
.is_ok());
assert!(assets.reanchor(&dest, reanchor_context).is_ok());
assert_eq!(assets.0, vec![asset_2_reanchored, asset_3_reanchored, asset_1_reanchored]);
assert!(assets
.using_encoded(|mut enc| MultiAssets::decode(&mut enc).map(|_| ()))
.is_ok());
}
#[test]
fn prepend_preserves_sorting() {
use super::*;
use alloc::vec;
let prefix = MultiLocation::new(0, X1(Parachain(1000)));
let asset_1: MultiAsset =
(MultiLocation::new(0, X2(PalletInstance(50), GeneralIndex(1))), 10).into();
let mut asset_1_prepended = asset_1.clone();
assert!(asset_1_prepended.prepend_with(&prefix).is_ok());
assert_eq!(
asset_1_prepended,
(MultiLocation::new(0, X3(Parachain(1000), PalletInstance(50), GeneralIndex(1))), 10)
.into()
);
let asset_2: MultiAsset =
(MultiLocation::new(1, X2(PalletInstance(50), GeneralIndex(1))), 10).into();
let mut asset_2_prepended = asset_2.clone();
assert!(asset_2_prepended.prepend_with(&prefix).is_ok());
assert_eq!(
asset_2_prepended,
(MultiLocation::new(0, X2(PalletInstance(50), GeneralIndex(1))), 10).into()
);
let asset_3: MultiAsset =
(MultiLocation::new(2, X2(PalletInstance(50), GeneralIndex(1))), 10).into();
let mut asset_3_prepended = asset_3.clone();
assert!(asset_3_prepended.prepend_with(&prefix).is_ok());
assert_eq!(
asset_3_prepended,
(MultiLocation::new(1, X2(PalletInstance(50), GeneralIndex(1))), 10).into()
);
let mut assets: MultiAssets = vec![asset_1, asset_2, asset_3].into();
assert!(assets
.using_encoded(|mut enc| MultiAssets::decode(&mut enc).map(|_| ()))
.is_ok());
assert!(assets.prepend_with(&prefix).is_ok());
assert_eq!(assets.0, vec![asset_2_prepended, asset_1_prepended, asset_3_prepended]);
assert!(assets
.using_encoded(|mut enc| MultiAssets::decode(&mut enc).map(|_| ()))
.is_ok());
}
#[test]
fn decoding_respects_limit() {
use super::*;
let lots_of_one_asset: MultiAssets =
vec![(GeneralIndex(1), 1u128).into(); MAX_ITEMS_IN_MULTIASSETS + 1].into();
let encoded = lots_of_one_asset.encode();
assert!(MultiAssets::decode(&mut &encoded[..]).is_ok());
let mut few_assets: MultiAssets = Vec::new().into();
for i in 0..MAX_ITEMS_IN_MULTIASSETS {
few_assets.push((GeneralIndex(i as u128), 1u128).into());
}
let encoded = few_assets.encode();
assert!(MultiAssets::decode(&mut &encoded[..]).is_ok());
let mut too_many_different_assets: MultiAssets = Vec::new().into();
for i in 0..MAX_ITEMS_IN_MULTIASSETS + 1 {
too_many_different_assets.push((GeneralIndex(i as u128), 1u128).into());
}
let encoded = too_many_different_assets.encode();
assert!(MultiAssets::decode(&mut &encoded[..]).is_err());
}
}