Repository: paritytech/substrate
File: frame/asset-conversion/src/lib.rs (Rust)

Code before:
					amount,
					Expendable,
				)
			}
		}

		pub(crate) fn native_to_asset(amount: T::Balance) -> Result<T::AssetBalance, Error<T>> {
			T::HigherPrecisionBalance::from(amount)
				.try_into()
				.map_err(|_| Error::<T>::Overflow)
		}

		pub(crate) fn asset_to_native(amount: T::AssetBalance) -> Result<T::Balance, Error<T>> {
			T::HigherPrecisionBalance::from(amount)
				.try_into()
				.map_err(|_| Error::<T>::Overflow)
		}

		pub(crate) fn do_swap(
			sender: T::AccountId,
			amounts: &Vec<T::AssetBalance>,
			path: BoundedVec<T::MultiAssetId, T::MaxSwapPathLength>,
			send_to: T::AccountId,
			keep_alive: bool,
		) -> Result<(), DispatchError> {
			ensure!(amounts.len() > 1, Error::<T>::InvalidPath);
			if let Some([asset1, asset2]) = &path.get(0..2) {
				let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone());
				let pool_account = Self::get_pool_account(&pool_id);
				let first_amount = amounts.first().expect("Always has more than one element");

				Self::transfer(asset1, &sender, &pool_account, *first_amount, keep_alive)?;

				let mut i = 0;
				let path_len = path.len() as u32;
				for assets_pair in path.windows(2) {
					if let [asset1, asset2] = assets_pair {
						let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone());
						let pool_account = Self::get_pool_account(&pool_id);

						let amount_out =
							amounts.get((i + 1) as usize).ok_or(Error::<T>::PathError)?;

						let to = if i < path_len - 2 {
							let asset3 = path.get((i + 2) as usize).ok_or(Error::<T>::PathError)?;
							Self::get_pool_account(&Self::get_pool_id(
								asset2.clone(),
								asset3.clone(),
							))
						} else {
							send_to.clone()

Review comment (suggestion):
```suggestion
			ensure!(amounts.len() > 1, Error::<T>::CorrespondenceError);
```

Code after:
				from: from.clone(),
				to: to.clone(),
				asset: (*asset_id).clone(),
				amount,
			});
			if T::MultiAssetIdConverter::is_native(asset_id) {
				let preservation = match keep_alive {
					true => Preserve,
					false => Expendable,
				};
				let amount = Self::convert_asset_balance_to_native_balance(amount)?;
				Ok(Self::convert_native_balance_to_asset_balance(T::Currency::transfer(
					from,
					to,
					amount,
					preservation,
				)?)?)
			} else {
				T::Assets::transfer(
					T::MultiAssetIdConverter::try_convert(&asset_id)
						.map_err(|_| Error::<T>::Overflow)?,
					from,
					to,
					amount,
					Expendable,
				)
			}
		}

		/// Convert a `Balance` type to an `AssetBalance`.
		pub(crate) fn convert_native_balance_to_asset_balance(
			amount: T::Balance,
		) -> Result<T::AssetBalance, Error<T>> {
			T::HigherPrecisionBalance::from(amount)
				.try_into()
				.map_err(|_| Error::<T>::Overflow)
		}

		/// Convert an `AssetBalance` type to a `Balance`.
		pub(crate) fn convert_asset_balance_to_native_balance(
			amount: T::AssetBalance,
		) -> Result<T::Balance, Error<T>> {
			T::HigherPrecisionBalance::from(amount)
				.try_into()
				.map_err(|_| Error::<T>::Overflow)
		}

		/// Swap assets along a `path`, depositing in `send_to`.
		pub(crate) fn do_swap(
			sender: T::AccountId,
			amounts: &Vec<T::AssetBalance>,