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

Code before:
				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()
						};

						let reserve = Self::get_balance(&pool_account, asset2)?;
						let reserve_left = reserve.saturating_sub(*amount_out);
						Self::validate_minimal_amount(reserve_left, asset2)
							.map_err(|_| Error::<T>::ReserveLeftLessThanMinimal)?;

						Self::transfer(asset2, &pool_account, &to, *amount_out, true)?;
					}
					i.saturating_inc();
				}
				Self::deposit_event(Event::SwapExecuted {
					who: sender,
					send_to,
					path,
					amount_in: *first_amount,
					amount_out: *amounts.last().expect("Always has more than 1 element"),
				});
			} else {
				return Err(Error::<T>::InvalidPath.into())
			}
			Ok(())
		}

		/// The account ID of the pool.
		///
		/// This actually does computation. If you need to keep using it, then make sure you cache
		/// the value and only call this once.
		pub fn get_pool_account(pool_id: &PoolIdOf<T>) -> T::AccountId {
			let encoded_pool_id = sp_io::hashing::blake2_256(&Encode::encode(pool_id)[..]);


Review comment (suggestion):
Instead of using if .. else we can simply write here:
`let [asset1, asset2] = &path.get(0..2).ok_or(Error::<T>::CorrespondenceError)?;` (or Error::<T>:: InvalidPath)

Code after:
			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>,
			path: BoundedVec<T::MultiAssetId, T::MaxSwapPathLength>,
			send_to: T::AccountId,
			keep_alive: bool,
		) -> Result<(), DispatchError> {
			ensure!(amounts.len() > 1, Error::<T>::CorrespondenceError);
			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);
				// amounts should always contain a corresponding element to path.
				let first_amount = amounts.first().ok_or(Error::<T>::CorrespondenceError)?;

				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>::CorrespondenceError)?;

						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()