Skip to content
asset.rs 30.4 KiB
Newer Older
Francisco Aguirre's avatar
Francisco Aguirre committed

		let mixed_good = vec![(Here, 10).into(), (Here, *b"good").into()];
		let r = Assets::from_sorted_and_deduplicated(mixed_good.clone());
		assert_eq!(r, Ok(Assets(mixed_good)));

		let mixed_bad = vec![(Here, *b"bad!").into(), (Here, 10).into()];
		let r = Assets::from_sorted_and_deduplicated(mixed_bad);
		assert!(r.is_err());
	}

	#[test]
	fn reanchor_preserves_sorting() {
		use super::*;
		use alloc::vec;

		let reanchor_context: Junctions = Parachain(2000).into();
		let dest = Location::new(1, []);

		let asset_1: Asset = (Location::new(0, [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,
			(Location::new(0, [Parachain(2000), PalletInstance(50), GeneralIndex(1)]), 10).into()
		);

		let asset_2: Asset = (Location::new(1, []), 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, (Location::new(0, []), 10).into());

		let asset_3: Asset = (Location::new(1, [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, (Location::new(0, [Parachain(1000)]), 10).into());

		let mut assets: Assets = 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| Assets::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| Assets::decode(&mut enc).map(|_| ())).is_ok());
	}

	#[test]
	fn prepend_preserves_sorting() {
		use super::*;
		use alloc::vec;

		let prefix = Location::new(0, [Parachain(1000)]);

		let asset_1: Asset = (Location::new(0, [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,
			(Location::new(0, [Parachain(1000), PalletInstance(50), GeneralIndex(1)]), 10).into()
		);

		let asset_2: Asset = (Location::new(1, [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,
			(Location::new(0, [PalletInstance(50), GeneralIndex(1)]), 10).into()
		);

		let asset_3: Asset = (Location::new(2, [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,
			(Location::new(1, [PalletInstance(50), GeneralIndex(1)]), 10).into()
		);

		// `From` impl does sorting.
		let mut assets: Assets = vec![asset_1, asset_2, asset_3].into();
		// decoding respects limits and sorting
		assert!(assets.using_encoded(|mut enc| Assets::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| Assets::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: Assets =
			vec![(GeneralIndex(1), 1u128).into(); MAX_ITEMS_IN_ASSETS + 1].into();
		let encoded = lots_of_one_asset.encode();
		assert!(Assets::decode(&mut &encoded[..]).is_ok());

		// Fewer assets than the limit works
		let mut few_assets: Assets = Vec::new().into();
		for i in 0..MAX_ITEMS_IN_ASSETS {
			few_assets.push((GeneralIndex(i as u128), 1u128).into());
		}
		let encoded = few_assets.encode();
		assert!(Assets::decode(&mut &encoded[..]).is_ok());

		// Having lots of different assets will not work
		let mut too_many_different_assets: Assets = Vec::new().into();
		for i in 0..MAX_ITEMS_IN_ASSETS + 1 {
			too_many_different_assets.push((GeneralIndex(i as u128), 1u128).into());
		}
		let encoded = too_many_different_assets.encode();
		assert!(Assets::decode(&mut &encoded[..]).is_err());
	}
Francisco Aguirre's avatar
Francisco Aguirre committed
}