1
// Copyright (C) Parity Technologies (UK) Ltd.
2
// This file is part of Polkadot.
3

            
4
// Substrate is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Substrate is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! Cross-Consensus Message format asset data structures.
18
//!
19
//! This encompasses four types for representing assets:
20
//! - `MultiAsset`: A description of a single asset, either an instance of a non-fungible or some
21
//!   amount of a fungible.
22
//! - `MultiAssets`: A collection of `MultiAsset`s. These are stored in a `Vec` and sorted with
23
//!   fungibles first.
24
//! - `Wild`: A single asset wildcard, this can either be "all" assets, or all assets of a specific
25
//!   kind.
26
//! - `MultiAssetFilter`: A combination of `Wild` and `MultiAssets` designed for efficiently
27
//!   filtering an XCM holding account.
28

            
29
use super::{InteriorMultiLocation, MultiLocation};
30
use crate::{
31
	v2::{
32
		AssetId as OldAssetId, AssetInstance as OldAssetInstance, Fungibility as OldFungibility,
33
		MultiAsset as OldMultiAsset, MultiAssetFilter as OldMultiAssetFilter,
34
		MultiAssets as OldMultiAssets, WildFungibility as OldWildFungibility,
35
		WildMultiAsset as OldWildMultiAsset,
36
	},
37
	v4::{
38
		Asset as NewMultiAsset, AssetFilter as NewMultiAssetFilter, AssetId as NewAssetId,
39
		AssetInstance as NewAssetInstance, Assets as NewMultiAssets, Fungibility as NewFungibility,
40
		WildAsset as NewWildMultiAsset, WildFungibility as NewWildFungibility,
41
	},
42
};
43
use alloc::{vec, vec::Vec};
44
use bounded_collections::{BoundedVec, ConstU32};
45
use codec::{self as codec, Decode, Encode, MaxEncodedLen};
46
use core::cmp::Ordering;
47
use scale_info::TypeInfo;
48

            
49
/// A general identifier for an instance of a non-fungible asset class.
50
#[derive(
51
	Copy,
52
	Clone,
53
	Eq,
54
	PartialEq,
55
	Ord,
56
	PartialOrd,
57
	Encode,
58
15
	Decode,
59
	Debug,
60
	TypeInfo,
61
	MaxEncodedLen,
62
	serde::Serialize,
63
	serde::Deserialize,
64
)]
65
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
66
#[scale_info(replace_segment("staging_xcm", "xcm"))]
67
pub enum AssetInstance {
68
1128
	/// Undefined - used if the non-fungible asset class has only one instance.
69
1128
	Undefined,
70

            
71
363
	/// A compact index. Technically this could be greater than `u128`, but this implementation
72
	/// supports only values up to `2**128 - 1`.
73
363
	Index(#[codec(compact)] u128),
74

            
75
18
	/// A 4-byte fixed-length datum.
76
18
	Array4([u8; 4]),
77

            
78
39
	/// An 8-byte fixed-length datum.
79
39
	Array8([u8; 8]),
80

            
81
24
	/// A 16-byte fixed-length datum.
82
24
	Array16([u8; 16]),
83

            
84
21
	/// A 32-byte fixed-length datum.
85
21
	Array32([u8; 32]),
86
}
87

            
88
impl TryFrom<OldAssetInstance> for AssetInstance {
89
	type Error = ();
90
524
	fn try_from(value: OldAssetInstance) -> Result<Self, Self::Error> {
91
		use OldAssetInstance::*;
92
524
		Ok(match value {
93
168
			Undefined => Self::Undefined,
94
88
			Index(n) => Self::Index(n),
95
22
			Array4(n) => Self::Array4(n),
96
28
			Array8(n) => Self::Array8(n),
97
62
			Array16(n) => Self::Array16(n),
98
32
			Array32(n) => Self::Array32(n),
99
124
			Blob(_) => return Err(()),
100
		})
101
524
	}
102
}
103

            
104
impl TryFrom<NewAssetInstance> for AssetInstance {
105
	type Error = ();
106
	fn try_from(value: NewAssetInstance) -> Result<Self, Self::Error> {
107
		use NewAssetInstance::*;
108
		Ok(match value {
109
			Undefined => Self::Undefined,
110
			Index(n) => Self::Index(n),
111
			Array4(n) => Self::Array4(n),
112
			Array8(n) => Self::Array8(n),
113
			Array16(n) => Self::Array16(n),
114
			Array32(n) => Self::Array32(n),
115
		})
116
	}
117
}
118

            
119
impl From<()> for AssetInstance {
120
	fn from(_: ()) -> Self {
121
		Self::Undefined
122
	}
123
}
124

            
125
impl From<[u8; 4]> for AssetInstance {
126
	fn from(x: [u8; 4]) -> Self {
127
		Self::Array4(x)
128
	}
129
}
130

            
131
impl From<[u8; 8]> for AssetInstance {
132
	fn from(x: [u8; 8]) -> Self {
133
		Self::Array8(x)
134
	}
135
}
136

            
137
impl From<[u8; 16]> for AssetInstance {
138
	fn from(x: [u8; 16]) -> Self {
139
		Self::Array16(x)
140
	}
141
}
142

            
143
impl From<[u8; 32]> for AssetInstance {
144
	fn from(x: [u8; 32]) -> Self {
145
		Self::Array32(x)
146
	}
147
}
148

            
149
impl From<u8> for AssetInstance {
150
	fn from(x: u8) -> Self {
151
		Self::Index(x as u128)
152
	}
153
}
154

            
155
impl From<u16> for AssetInstance {
156
	fn from(x: u16) -> Self {
157
		Self::Index(x as u128)
158
	}
159
}
160

            
161
impl From<u32> for AssetInstance {
162
	fn from(x: u32) -> Self {
163
		Self::Index(x as u128)
164
	}
165
}
166

            
167
impl From<u64> for AssetInstance {
168
	fn from(x: u64) -> Self {
169
		Self::Index(x as u128)
170
	}
171
}
172

            
173
impl TryFrom<AssetInstance> for () {
174
	type Error = ();
175
	fn try_from(x: AssetInstance) -> Result<Self, ()> {
176
		match x {
177
			AssetInstance::Undefined => Ok(()),
178
			_ => Err(()),
179
		}
180
	}
181
}
182

            
183
impl TryFrom<AssetInstance> for [u8; 4] {
184
	type Error = ();
185
	fn try_from(x: AssetInstance) -> Result<Self, ()> {
186
		match x {
187
			AssetInstance::Array4(x) => Ok(x),
188
			_ => Err(()),
189
		}
190
	}
191
}
192

            
193
impl TryFrom<AssetInstance> for [u8; 8] {
194
	type Error = ();
195
	fn try_from(x: AssetInstance) -> Result<Self, ()> {
196
		match x {
197
			AssetInstance::Array8(x) => Ok(x),
198
			_ => Err(()),
199
		}
200
	}
201
}
202

            
203
impl TryFrom<AssetInstance> for [u8; 16] {
204
	type Error = ();
205
	fn try_from(x: AssetInstance) -> Result<Self, ()> {
206
		match x {
207
			AssetInstance::Array16(x) => Ok(x),
208
			_ => Err(()),
209
		}
210
	}
211
}
212

            
213
impl TryFrom<AssetInstance> for [u8; 32] {
214
	type Error = ();
215
	fn try_from(x: AssetInstance) -> Result<Self, ()> {
216
		match x {
217
			AssetInstance::Array32(x) => Ok(x),
218
			_ => Err(()),
219
		}
220
	}
221
}
222

            
223
impl TryFrom<AssetInstance> for u8 {
224
	type Error = ();
225
	fn try_from(x: AssetInstance) -> Result<Self, ()> {
226
		match x {
227
			AssetInstance::Index(x) => x.try_into().map_err(|_| ()),
228
			_ => Err(()),
229
		}
230
	}
231
}
232

            
233
impl TryFrom<AssetInstance> for u16 {
234
	type Error = ();
235
	fn try_from(x: AssetInstance) -> Result<Self, ()> {
236
		match x {
237
			AssetInstance::Index(x) => x.try_into().map_err(|_| ()),
238
			_ => Err(()),
239
		}
240
	}
241
}
242

            
243
impl TryFrom<AssetInstance> for u32 {
244
	type Error = ();
245
	fn try_from(x: AssetInstance) -> Result<Self, ()> {
246
		match x {
247
			AssetInstance::Index(x) => x.try_into().map_err(|_| ()),
248
			_ => Err(()),
249
		}
250
	}
251
}
252

            
253
impl TryFrom<AssetInstance> for u64 {
254
	type Error = ();
255
	fn try_from(x: AssetInstance) -> Result<Self, ()> {
256
		match x {
257
			AssetInstance::Index(x) => x.try_into().map_err(|_| ()),
258
			_ => Err(()),
259
		}
260
	}
261
}
262

            
263
impl TryFrom<AssetInstance> for u128 {
264
	type Error = ();
265
	fn try_from(x: AssetInstance) -> Result<Self, ()> {
266
		match x {
267
			AssetInstance::Index(x) => Ok(x),
268
			_ => Err(()),
269
		}
270
	}
271
}
272

            
273
/// Classification of whether an asset is fungible or not, along with a mandatory amount or
274
/// instance.
275
#[derive(
276
	Clone,
277
	Eq,
278
	PartialEq,
279
	Ord,
280
	PartialOrd,
281
	Debug,
282
	Encode,
283
	TypeInfo,
284
	MaxEncodedLen,
285
	serde::Serialize,
286
	serde::Deserialize,
287
)]
288
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
289
#[scale_info(replace_segment("staging_xcm", "xcm"))]
290
pub enum Fungibility {
291
	/// A fungible asset; we record a number of units, as a `u128` in the inner item.
292
	Fungible(#[codec(compact)] u128),
293
	/// A non-fungible asset. We record the instance identifier in the inner item. Only one asset
294
	/// of each instance identifier may ever be in existence at once.
295
	NonFungible(AssetInstance),
296
}
297

            
298
69
#[derive(Decode)]
299
enum UncheckedFungibility {
300
1881
	Fungible(#[codec(compact)] u128),
301
1608
	NonFungible(AssetInstance),
302
}
303

            
304
impl Decode for Fungibility {
305
3558
	fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
306
3558
		match UncheckedFungibility::decode(input)? {
307
1848
			UncheckedFungibility::Fungible(a) if a != 0 => Ok(Self::Fungible(a)),
308
1572
			UncheckedFungibility::NonFungible(i) => Ok(Self::NonFungible(i)),
309
			UncheckedFungibility::Fungible(_) =>
310
168
				Err("Fungible asset of zero amount is not allowed".into()),
311
		}
312
3558
	}
313
}
314

            
315
impl Fungibility {
316
	pub fn is_kind(&self, w: WildFungibility) -> bool {
317
		use Fungibility::*;
318
		use WildFungibility::{Fungible as WildFungible, NonFungible as WildNonFungible};
319
		matches!((self, w), (Fungible(_), WildFungible) | (NonFungible(_), WildNonFungible))
320
	}
321
}
322

            
323
impl From<i32> for Fungibility {
324
	fn from(amount: i32) -> Fungibility {
325
		debug_assert_ne!(amount, 0);
326
		Fungibility::Fungible(amount as u128)
327
	}
328
}
329

            
330
impl From<u128> for Fungibility {
331
	fn from(amount: u128) -> Fungibility {
332
		debug_assert_ne!(amount, 0);
333
		Fungibility::Fungible(amount)
334
	}
335
}
336

            
337
impl<T: Into<AssetInstance>> From<T> for Fungibility {
338
	fn from(instance: T) -> Fungibility {
339
		Fungibility::NonFungible(instance.into())
340
	}
341
}
342

            
343
impl TryFrom<OldFungibility> for Fungibility {
344
	type Error = ();
345
3660
	fn try_from(value: OldFungibility) -> Result<Self, Self::Error> {
346
		use OldFungibility::*;
347
3660
		Ok(match value {
348
3136
			Fungible(n) => Self::Fungible(n),
349
524
			NonFungible(i) => Self::NonFungible(i.try_into()?),
350
		})
351
3660
	}
352
}
353

            
354
impl TryFrom<NewFungibility> for Fungibility {
355
	type Error = ();
356
	fn try_from(value: NewFungibility) -> Result<Self, Self::Error> {
357
		use NewFungibility::*;
358
		Ok(match value {
359
			Fungible(n) => Self::Fungible(n),
360
			NonFungible(i) => Self::NonFungible(i.try_into()?),
361
		})
362
	}
363
}
364

            
365
/// Classification of whether an asset is fungible or not.
366
#[derive(
367
	Copy,
368
	Clone,
369
	Eq,
370
	PartialEq,
371
	Ord,
372
	PartialOrd,
373
	Debug,
374
	Encode,
375
18
	Decode,
376
	TypeInfo,
377
	MaxEncodedLen,
378
	serde::Serialize,
379
	serde::Deserialize,
380
)]
381
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
382
#[scale_info(replace_segment("staging_xcm", "xcm"))]
383
pub enum WildFungibility {
384
1155
	/// The asset is fungible.
385
1155
	Fungible,
386
186
	/// The asset is not fungible.
387
186
	NonFungible,
388
}
389

            
390
impl TryFrom<OldWildFungibility> for WildFungibility {
391
	type Error = ();
392
588
	fn try_from(value: OldWildFungibility) -> Result<Self, Self::Error> {
393
		use OldWildFungibility::*;
394
588
		Ok(match value {
395
558
			Fungible => Self::Fungible,
396
30
			NonFungible => Self::NonFungible,
397
		})
398
588
	}
399
}
400

            
401
impl TryFrom<NewWildFungibility> for WildFungibility {
402
	type Error = ();
403
	fn try_from(value: NewWildFungibility) -> Result<Self, Self::Error> {
404
		use NewWildFungibility::*;
405
		Ok(match value {
406
			Fungible => Self::Fungible,
407
			NonFungible => Self::NonFungible,
408
		})
409
	}
410
}
411

            
412
/// Classification of an asset being concrete or abstract.
413
#[derive(
414
	Copy,
415
	Clone,
416
	Eq,
417
	PartialEq,
418
	Ord,
419
	PartialOrd,
420
	Debug,
421
	Encode,
422
237
	Decode,
423
	TypeInfo,
424
	MaxEncodedLen,
425
	serde::Serialize,
426
	serde::Deserialize,
427
)]
428
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
429
#[scale_info(replace_segment("staging_xcm", "xcm"))]
430
pub enum AssetId {
431
3591
	/// A specific location identifying an asset.
432
3591
	Concrete(MultiLocation),
433
1512
	/// An abstract location; this is a name which may mean different specific locations on
434
	/// different chains at different times.
435
1512
	Abstract([u8; 32]),
436
}
437

            
438
impl<T: Into<MultiLocation>> From<T> for AssetId {
439
	fn from(x: T) -> Self {
440
		Self::Concrete(x.into())
441
	}
442
}
443

            
444
impl From<[u8; 32]> for AssetId {
445
	fn from(x: [u8; 32]) -> Self {
446
		Self::Abstract(x)
447
	}
448
}
449

            
450
impl TryFrom<OldAssetId> for AssetId {
451
	type Error = ();
452
4796
	fn try_from(old: OldAssetId) -> Result<Self, ()> {
453
		use OldAssetId::*;
454
1104
		Ok(match old {
455
3692
			Concrete(l) => Self::Concrete(l.try_into()?),
456
1104
			Abstract(v) if v.len() <= 32 => {
457
574
				let mut r = [0u8; 32];
458
574
				r[..v.len()].copy_from_slice(&v[..]);
459
574
				Self::Abstract(r)
460
			},
461
530
			_ => return Err(()),
462
		})
463
4796
	}
464
}
465

            
466
impl TryFrom<NewAssetId> for AssetId {
467
	type Error = ();
468
	fn try_from(new: NewAssetId) -> Result<Self, Self::Error> {
469
		Ok(Self::Concrete(new.0.try_into()?))
470
	}
471
}
472

            
473
impl AssetId {
474
	/// Prepend a `MultiLocation` to a concrete asset, giving it a new root location.
475
	pub fn prepend_with(&mut self, prepend: &MultiLocation) -> Result<(), ()> {
476
		if let AssetId::Concrete(ref mut l) = self {
477
			l.prepend_with(*prepend).map_err(|_| ())?;
478
		}
479
		Ok(())
480
	}
481

            
482
	/// Mutate the asset to represent the same value from the perspective of a new `target`
483
	/// location. The local chain's location is provided in `context`.
484
	pub fn reanchor(
485
		&mut self,
486
		target: &MultiLocation,
487
		context: InteriorMultiLocation,
488
	) -> Result<(), ()> {
489
		if let AssetId::Concrete(ref mut l) = self {
490
			l.reanchor(target, context)?;
491
		}
492
		Ok(())
493
	}
494

            
495
	/// Use the value of `self` along with a `fun` fungibility specifier to create the corresponding
496
	/// `MultiAsset` value.
497
	pub fn into_multiasset(self, fun: Fungibility) -> MultiAsset {
498
		MultiAsset { fun, id: self }
499
	}
500

            
501
	/// Use the value of `self` along with a `fun` fungibility specifier to create the corresponding
502
	/// `WildMultiAsset` wildcard (`AllOf`) value.
503
	pub fn into_wild(self, fun: WildFungibility) -> WildMultiAsset {
504
		WildMultiAsset::AllOf { fun, id: self }
505
	}
506
}
507

            
508
/// Either an amount of a single fungible asset, or a single well-identified non-fungible asset.
509
#[derive(
510
	Clone,
511
	Eq,
512
	PartialEq,
513
	Debug,
514
	Encode,
515
	Decode,
516
	TypeInfo,
517
	MaxEncodedLen,
518
	serde::Serialize,
519
	serde::Deserialize,
520
)]
521
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
522
#[scale_info(replace_segment("staging_xcm", "xcm"))]
523
pub struct MultiAsset {
524
	/// The overall asset identity (aka *class*, in the case of a non-fungible).
525
	pub id: AssetId,
526
	/// The fungibility of the asset, which contains either the amount (in the case of a fungible
527
	/// asset) or the *instance ID*, the secondary asset identifier.
528
	pub fun: Fungibility,
529
}
530

            
531
impl PartialOrd for MultiAsset {
532
72
	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
533
72
		Some(self.cmp(other))
534
72
	}
535
}
536

            
537
impl Ord for MultiAsset {
538
72
	fn cmp(&self, other: &Self) -> Ordering {
539
72
		match (&self.fun, &other.fun) {
540
18
			(Fungibility::Fungible(..), Fungibility::NonFungible(..)) => Ordering::Less,
541
6
			(Fungibility::NonFungible(..), Fungibility::Fungible(..)) => Ordering::Greater,
542
48
			_ => (&self.id, &self.fun).cmp(&(&other.id, &other.fun)),
543
		}
544
72
	}
545
}
546

            
547
impl<A: Into<AssetId>, B: Into<Fungibility>> From<(A, B)> for MultiAsset {
548
	fn from((id, fun): (A, B)) -> MultiAsset {
549
		MultiAsset { fun: fun.into(), id: id.into() }
550
	}
551
}
552

            
553
impl MultiAsset {
554
	pub fn is_fungible(&self, maybe_id: Option<AssetId>) -> bool {
555
		use Fungibility::*;
556
		matches!(self.fun, Fungible(..)) && maybe_id.map_or(true, |i| i == self.id)
557
	}
558

            
559
52
	pub fn is_non_fungible(&self, maybe_id: Option<AssetId>) -> bool {
560
		use Fungibility::*;
561
52
		matches!(self.fun, NonFungible(..)) && maybe_id.map_or(true, |i| i == self.id)
562
52
	}
563

            
564
	/// Prepend a `MultiLocation` to a concrete asset, giving it a new root location.
565
	pub fn prepend_with(&mut self, prepend: &MultiLocation) -> Result<(), ()> {
566
		self.id.prepend_with(prepend)
567
	}
568

            
569
	/// Mutate the location of the asset identifier if concrete, giving it the same location
570
	/// relative to a `target` context. The local context is provided as `context`.
571
	pub fn reanchor(
572
		&mut self,
573
		target: &MultiLocation,
574
		context: InteriorMultiLocation,
575
	) -> Result<(), ()> {
576
		self.id.reanchor(target, context)
577
	}
578

            
579
	/// Mutate the location of the asset identifier if concrete, giving it the same location
580
	/// relative to a `target` context. The local context is provided as `context`.
581
	pub fn reanchored(
582
		mut self,
583
		target: &MultiLocation,
584
		context: InteriorMultiLocation,
585
	) -> Result<Self, ()> {
586
		self.id.reanchor(target, context)?;
587
		Ok(self)
588
	}
589

            
590
	/// Returns true if `self` is a super-set of the given `inner` asset.
591
	pub fn contains(&self, inner: &MultiAsset) -> bool {
592
		use Fungibility::*;
593
		if self.id == inner.id {
594
			match (&self.fun, &inner.fun) {
595
				(Fungible(a), Fungible(i)) if a >= i => return true,
596
				(NonFungible(a), NonFungible(i)) if a == i => return true,
597
				_ => (),
598
			}
599
		}
600
		false
601
	}
602
}
603

            
604
impl TryFrom<OldMultiAsset> for MultiAsset {
605
	type Error = ();
606
3880
	fn try_from(old: OldMultiAsset) -> Result<Self, ()> {
607
3880
		Ok(Self { id: old.id.try_into()?, fun: old.fun.try_into()? })
608
3880
	}
609
}
610

            
611
impl TryFrom<NewMultiAsset> for MultiAsset {
612
	type Error = ();
613
	fn try_from(new: NewMultiAsset) -> Result<Self, Self::Error> {
614
		Ok(Self { id: new.id.try_into()?, fun: new.fun.try_into()? })
615
	}
616
}
617

            
618
/// A `Vec` of `MultiAsset`s.
619
///
620
/// There are a number of invariants which the construction and mutation functions must ensure are
621
/// maintained in order to maintain polynomial time complexity during iteration:
622
/// - It may contain no items of duplicate asset class;
623
/// - All items must be ordered;
624
/// - The number of items should grow no larger than `MAX_ITEMS_IN_MULTIASSETS`.
625
#[derive(
626
	Clone,
627
	Eq,
628
	PartialEq,
629
	Ord,
630
	PartialOrd,
631
	Debug,
632
	Encode,
633
	TypeInfo,
634
	Default,
635
	serde::Serialize,
636
	serde::Deserialize,
637
)]
638
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
639
#[scale_info(replace_segment("staging_xcm", "xcm"))]
640
pub struct MultiAssets(Vec<MultiAsset>);
641

            
642
/// Maximum number of items in a single `MultiAssets` value that can be decoded.
643
pub const MAX_ITEMS_IN_MULTIASSETS: usize = 20;
644

            
645
impl MaxEncodedLen for MultiAssets {
646
	fn max_encoded_len() -> usize {
647
		MultiAsset::max_encoded_len() * MAX_ITEMS_IN_MULTIASSETS
648
	}
649
}
650

            
651
impl Decode for MultiAssets {
652
213654
	fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
653
212055
		let bounded_instructions =
654
213654
			BoundedVec::<MultiAsset, ConstU32<{ MAX_ITEMS_IN_MULTIASSETS as u32 }>>::decode(input)?;
655
212055
		Self::from_sorted_and_deduplicated(bounded_instructions.into_inner())
656
212055
			.map_err(|()| "Out of order".into())
657
213654
	}
658
}
659

            
660
impl TryFrom<OldMultiAssets> for MultiAssets {
661
	type Error = ();
662
61090
	fn try_from(old: OldMultiAssets) -> Result<Self, ()> {
663
61090
		let v = old
664
61090
			.drain()
665
61090
			.into_iter()
666
61090
			.map(MultiAsset::try_from)
667
61090
			.collect::<Result<Vec<_>, ()>>()?;
668
60850
		Ok(MultiAssets(v))
669
61090
	}
670
}
671

            
672
impl TryFrom<NewMultiAssets> for MultiAssets {
673
	type Error = ();
674
32
	fn try_from(new: NewMultiAssets) -> Result<Self, Self::Error> {
675
32
		let v = new
676
32
			.into_inner()
677
32
			.into_iter()
678
32
			.map(MultiAsset::try_from)
679
32
			.collect::<Result<Vec<_>, ()>>()?;
680
32
		Ok(MultiAssets(v))
681
32
	}
682
}
683

            
684
impl From<Vec<MultiAsset>> for MultiAssets {
685
	fn from(mut assets: Vec<MultiAsset>) -> Self {
686
		let mut res = Vec::with_capacity(assets.len());
687
		if !assets.is_empty() {
688
			assets.sort();
689
			let mut iter = assets.into_iter();
690
			if let Some(first) = iter.next() {
691
				let last = iter.fold(first, |a, b| -> MultiAsset {
692
					match (a, b) {
693
						(
694
							MultiAsset { fun: Fungibility::Fungible(a_amount), id: a_id },
695
							MultiAsset { fun: Fungibility::Fungible(b_amount), id: b_id },
696
						) if a_id == b_id => MultiAsset {
697
							id: a_id,
698
							fun: Fungibility::Fungible(a_amount.saturating_add(b_amount)),
699
						},
700
						(
701
							MultiAsset { fun: Fungibility::NonFungible(a_instance), id: a_id },
702
							MultiAsset { fun: Fungibility::NonFungible(b_instance), id: b_id },
703
						) if a_id == b_id && a_instance == b_instance =>
704
							MultiAsset { fun: Fungibility::NonFungible(a_instance), id: a_id },
705
						(to_push, to_remember) => {
706
							res.push(to_push);
707
							to_remember
708
						},
709
					}
710
				});
711
				res.push(last);
712
			}
713
		}
714
		Self(res)
715
	}
716
}
717

            
718
impl<T: Into<MultiAsset>> From<T> for MultiAssets {
719
	fn from(x: T) -> Self {
720
		Self(vec![x.into()])
721
	}
722
}
723

            
724
impl MultiAssets {
725
	/// A new (empty) value.
726
	pub fn new() -> Self {
727
		Self(Vec::new())
728
	}
729

            
730
	/// Create a new instance of `MultiAssets` from a `Vec<MultiAsset>` whose contents are sorted
731
	/// and which contain no duplicates.
732
	///
733
	/// Returns `Ok` if the operation succeeds and `Err` if `r` is out of order or had duplicates.
734
	/// If you can't guarantee that `r` is sorted and deduplicated, then use
735
	/// `From::<Vec<MultiAsset>>::from` which is infallible.
736
141370
	pub fn from_sorted_and_deduplicated(r: Vec<MultiAsset>) -> Result<Self, ()> {
737
141370
		if r.is_empty() {
738
140294
			return Ok(Self(Vec::new()))
739
1076
		}
740
1076
		r.iter().skip(1).try_fold(&r[0], |a, b| -> Result<&MultiAsset, ()> {
741
190
			if a.id < b.id || a < b && (a.is_non_fungible(None) || b.is_non_fungible(None)) {
742
144
				Ok(b)
743
			} else {
744
46
				Err(())
745
			}
746
1076
		})?;
747
1030
		Ok(Self(r))
748
141370
	}
749

            
750
	/// Create a new instance of `MultiAssets` from a `Vec<MultiAsset>` whose contents are sorted
751
	/// and which contain no duplicates.
752
	///
753
	/// In release mode, this skips any checks to ensure that `r` is correct, making it a
754
	/// negligible-cost operation. Generally though you should avoid using it unless you have a
755
	/// strict proof that `r` is valid.
756
	#[cfg(test)]
757
	pub fn from_sorted_and_deduplicated_skip_checks(r: Vec<MultiAsset>) -> Self {
758
		Self::from_sorted_and_deduplicated(r).expect("Invalid input r is not sorted/deduped")
759
	}
760
	/// Create a new instance of `MultiAssets` from a `Vec<MultiAsset>` whose contents are sorted
761
	/// and which contain no duplicates.
762
	///
763
	/// In release mode, this skips any checks to ensure that `r` is correct, making it a
764
	/// negligible-cost operation. Generally though you should avoid using it unless you have a
765
	/// strict proof that `r` is valid.
766
	///
767
	/// In test mode, this checks anyway and panics on fail.
768
	#[cfg(not(test))]
769
	pub fn from_sorted_and_deduplicated_skip_checks(r: Vec<MultiAsset>) -> Self {
770
		Self(r)
771
	}
772

            
773
	/// Add some asset onto the list, saturating. This is quite a laborious operation since it
774
	/// maintains the ordering.
775
	pub fn push(&mut self, a: MultiAsset) {
776
		for asset in self.0.iter_mut().filter(|x| x.id == a.id) {
777
			match (&a.fun, &mut asset.fun) {
778
				(Fungibility::Fungible(amount), Fungibility::Fungible(balance)) => {
779
					*balance = balance.saturating_add(*amount);
780
					return
781
				},
782
				(Fungibility::NonFungible(inst1), Fungibility::NonFungible(inst2))
783
					if inst1 == inst2 =>
784
					return,
785
				_ => (),
786
			}
787
		}
788
		self.0.push(a);
789
		self.0.sort();
790
	}
791

            
792
	/// Returns `true` if this definitely represents no asset.
793
	pub fn is_none(&self) -> bool {
794
		self.0.is_empty()
795
	}
796

            
797
	/// Returns true if `self` is a super-set of the given `inner` asset.
798
	pub fn contains(&self, inner: &MultiAsset) -> bool {
799
		self.0.iter().any(|i| i.contains(inner))
800
	}
801

            
802
	/// Consume `self` and return the inner vec.
803
	#[deprecated = "Use `into_inner()` instead"]
804
	pub fn drain(self) -> Vec<MultiAsset> {
805
		self.0
806
	}
807

            
808
	/// Consume `self` and return the inner vec.
809
97774
	pub fn into_inner(self) -> Vec<MultiAsset> {
810
97774
		self.0
811
97774
	}
812

            
813
	/// Return a reference to the inner vec.
814
	pub fn inner(&self) -> &Vec<MultiAsset> {
815
		&self.0
816
	}
817

            
818
	/// Return the number of distinct asset instances contained.
819
	pub fn len(&self) -> usize {
820
		self.0.len()
821
	}
822

            
823
	/// Prepend a `MultiLocation` to any concrete asset items, giving it a new root location.
824
	pub fn prepend_with(&mut self, prefix: &MultiLocation) -> Result<(), ()> {
825
		self.0.iter_mut().try_for_each(|i| i.prepend_with(prefix))?;
826
		self.0.sort();
827
		Ok(())
828
	}
829

            
830
	/// Mutate the location of the asset identifier if concrete, giving it the same location
831
	/// relative to a `target` context. The local context is provided as `context`.
832
	///
833
	/// This will also re-sort the inner assets to preserve ordering guarantees.
834
	pub fn reanchor(
835
		&mut self,
836
		target: &MultiLocation,
837
		context: InteriorMultiLocation,
838
	) -> Result<(), ()> {
839
		self.0.iter_mut().try_for_each(|i| i.reanchor(target, context))?;
840
		self.0.sort();
841
		Ok(())
842
	}
843

            
844
	/// Return a reference to an item at a specific index or `None` if it doesn't exist.
845
	pub fn get(&self, index: usize) -> Option<&MultiAsset> {
846
		self.0.get(index)
847
	}
848
}
849

            
850
/// A wildcard representing a set of assets.
851
#[derive(
852
	Clone,
853
	Eq,
854
	PartialEq,
855
	Ord,
856
	PartialOrd,
857
	Debug,
858
	Encode,
859
18
	Decode,
860
	TypeInfo,
861
	MaxEncodedLen,
862
	serde::Serialize,
863
	serde::Deserialize,
864
)]
865
#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
866
#[scale_info(replace_segment("staging_xcm", "xcm"))]
867
pub enum WildMultiAsset {
868
1599
	/// All assets in Holding.
869
1599
	All,
870
1323
	/// All assets in Holding of a given fungibility and ID.
871
1323
	AllOf { id: AssetId, fun: WildFungibility },
872
30
	/// All assets in Holding, up to `u32` individual assets (different instances of non-fungibles
873
	/// are separate assets).
874
30
	AllCounted(#[codec(compact)] u32),
875
60
	/// All assets in Holding of a given fungibility and ID up to `count` individual assets
876
	/// (different instances of non-fungibles are separate assets).
877
	AllOfCounted {
878
12
		id: AssetId,
879
6
		fun: WildFungibility,
880
3
		#[codec(compact)]
881
3
		count: u32,
882
60
	},
883
}
884

            
885
impl TryFrom<OldWildMultiAsset> for WildMultiAsset {
886
	type Error = ();
887
1370
	fn try_from(old: OldWildMultiAsset) -> Result<WildMultiAsset, ()> {
888
		use OldWildMultiAsset::*;
889
1370
		Ok(match old {
890
664
			AllOf { id, fun } => Self::AllOf { id: id.try_into()?, fun: fun.try_into()? },
891
706
			All => Self::All,
892
		})
893
1370
	}
894
}
895

            
896
impl TryFrom<NewWildMultiAsset> for WildMultiAsset {
897
	type Error = ();
898
	fn try_from(new: NewWildMultiAsset) -> Result<Self, ()> {
899
		use NewWildMultiAsset::*;
900
		Ok(match new {
901
			AllOf { id, fun } => Self::AllOf { id: id.try_into()?, fun: fun.try_into()? },
902
			AllOfCounted { id, fun, count } =>
903
				Self::AllOfCounted { id: id.try_into()?, fun: fun.try_into()?, count },
904
			All => Self::All,
905
			AllCounted(count) => Self::AllCounted(count),
906
		})
907
	}
908
}
909

            
910
impl TryFrom<(OldWildMultiAsset, u32)> for WildMultiAsset {
911
	type Error = ();
912
656
	fn try_from(old: (OldWildMultiAsset, u32)) -> Result<WildMultiAsset, ()> {
913
		use OldWildMultiAsset::*;
914
656
		let count = old.1;
915
656
		Ok(match old.0 {
916
252
			AllOf { id, fun } =>
917
252
				Self::AllOfCounted { id: id.try_into()?, fun: fun.try_into()?, count },
918
404
			All => Self::AllCounted(count),
919
		})
920
656
	}
921
}
922

            
923
impl WildMultiAsset {
924
	/// Returns true if `self` is a super-set of the given `inner` asset.
925
	pub fn contains(&self, inner: &MultiAsset) -> bool {
926
		use WildMultiAsset::*;
927
		match self {
928
			AllOfCounted { count: 0, .. } | AllCounted(0) => false,
929
			AllOf { fun, id } | AllOfCounted { id, fun, .. } =>
930
				inner.fun.is_kind(*fun) && &inner.id == id,
931
			All | AllCounted(_) => true,
932
		}
933
	}
934

            
935
	/// Returns true if the wild element of `self` matches `inner`.
936
	///
937
	/// Note that for `Counted` variants of wildcards, then it will disregard the count except for
938
	/// always returning `false` when equal to 0.
939
	#[deprecated = "Use `contains` instead"]
940
	pub fn matches(&self, inner: &MultiAsset) -> bool {
941
		self.contains(inner)
942
	}
943

            
944
	/// Mutate the asset to represent the same value from the perspective of a new `target`
945
	/// location. The local chain's location is provided in `context`.
946
	pub fn reanchor(
947
		&mut self,
948
		target: &MultiLocation,
949
		context: InteriorMultiLocation,
950
	) -> Result<(), ()> {
951
		use WildMultiAsset::*;
952
		match self {
953
			AllOf { ref mut id, .. } | AllOfCounted { ref mut id, .. } =>
954
				id.reanchor(target, context),
955
			All | AllCounted(_) => Ok(()),
956
		}
957
	}
958

            
959
	/// Maximum count of assets allowed to match, if any.
960
	pub fn count(&self) -> Option<u32> {
961
		use WildMultiAsset::*;
962
		match self {
963
			AllOfCounted { count, .. } | AllCounted(count) => Some(*count),
964
			All | AllOf { .. } => None,
965
		}
966
	}
967

            
968
	/// Explicit limit on number of assets allowed to match, if any.
969
	pub fn limit(&self) -> Option<u32> {
970
		self.count()
971
	}
972

            
973
	/// Consume self and return the equivalent version but counted and with the `count` set to the
974
	/// given parameter.
975
	pub fn counted(self, count: u32) -> Self {
976
		use WildMultiAsset::*;
977
		match self {
978
			AllOfCounted { fun, id, .. } | AllOf { fun, id } => AllOfCounted { fun, id, count },
979
			All | AllCounted(_) => AllCounted(count),
980
		}
981
	}
982
}
983

            
984
impl<A: Into<AssetId>, B: Into<WildFungibility>> From<(A, B)> for WildMultiAsset {
985
	fn from((id, fun): (A, B)) -> WildMultiAsset {
986
		WildMultiAsset::AllOf { fun: fun.into(), id: id.into() }
987
	}
988
}
989

            
990
/// `MultiAsset` collection, defined either by a number of `MultiAssets` or a single wildcard.
991
#[derive(
992
	Clone,
993
	Eq,
994
	PartialEq,
995
	Ord,
996
	PartialOrd,
997
	Debug,
998
	Encode,
999
99
	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 {
7323
	/// Specify the filter as being everything contained by the given `MultiAssets` inner.
7323
	Definite(MultiAssets),
3030
	/// Specify the filter as the given `WildMultiAsset` wildcard.
3030
	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 {
	/// Returns true if `inner` would be matched by `self`.
	///
	/// Note that for `Counted` variants of wildcards, then it will disregard the count except for
	/// always returning `false` when equal to 0.
	pub fn matches(&self, inner: &MultiAsset) -> bool {
		match self {
			MultiAssetFilter::Definite(ref assets) => assets.contains(inner),
			MultiAssetFilter::Wild(ref wild) => wild.contains(inner),
		}
	}
	/// Mutate the location of the asset identifier if concrete, giving it the same location
	/// relative to a `target` context. The local context is provided as `context`.
	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),
		}
	}
	/// Maximum count of assets it is possible to match, if known.
	pub fn count(&self) -> Option<u32> {
		use MultiAssetFilter::*;
		match self {
			Definite(x) => Some(x.len() as u32),
			Wild(x) => x.count(),
		}
	}
	/// Explicit limit placed on the number of items, if any.
	pub fn limit(&self) -> Option<u32> {
		use MultiAssetFilter::*;
		match self {
			Definite(_) => None,
			Wild(x) => x.limit(),
		}
	}
}
impl TryFrom<OldMultiAssetFilter> for MultiAssetFilter {
	type Error = ();
3178
	fn try_from(old: OldMultiAssetFilter) -> Result<MultiAssetFilter, ()> {
3178
		Ok(match old {
1808
			OldMultiAssetFilter::Definite(x) => Self::Definite(x.try_into()?),
1370
			OldMultiAssetFilter::Wild(x) => Self::Wild(x.try_into()?),
		})
3178
	}
}
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 = ();
1640
	fn try_from(old: (OldMultiAssetFilter, u32)) -> Result<MultiAssetFilter, ()> {
1640
		let count = old.1;
984
		Ok(match old.0 {
984
			OldMultiAssetFilter::Definite(x) if count >= x.len() as u32 =>
980
				Self::Definite(x.try_into()?),
656
			OldMultiAssetFilter::Wild(x) => Self::Wild((x, count).try_into()?),
4
			_ => return Err(()),
		})
1640
	}
}
#[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());
		// decoding respects limits and sorting
		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]);
		// decoding respects limits and sorting
		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());
		// changes interior X2->X3
		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());
		// changes parent
		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());
		// changes parent
		assert_eq!(
			asset_3_prepended,
			(MultiLocation::new(1, X2(PalletInstance(50), GeneralIndex(1))), 10).into()
		);
		// `From` impl does sorting.
		let mut assets: MultiAssets = vec![asset_1, asset_2, asset_3].into();
		// decoding respects limits and sorting
		assert!(assets
			.using_encoded(|mut enc| MultiAssets::decode(&mut enc).map(|_| ()))
			.is_ok());
		// let's do `prepend_with`
		assert!(assets.prepend_with(&prefix).is_ok());
		assert_eq!(assets.0, vec![asset_2_prepended, asset_1_prepended, asset_3_prepended]);
		// decoding respects limits and sorting
		assert!(assets
			.using_encoded(|mut enc| MultiAssets::decode(&mut enc).map(|_| ()))
			.is_ok());
	}
	#[test]
	fn decoding_respects_limit() {
		use super::*;
		// Having lots of one asset will work since they are deduplicated
		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());
		// Fewer assets than the limit works
		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());
		// Having lots of different assets will not work
		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());
	}
}