Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #25 from jsidorenko/js-improve-u128-support
Browse files Browse the repository at this point in the history
Refactor conversion into u128
  • Loading branch information
jsidorenko authored Dec 16, 2022
2 parents 9c8bd5e + a4704ee commit 027c7a5
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions frame/dex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ pub mod pallet {
PalletId,
};
use frame_system::pallet_prelude::*;
use sp_runtime::{
helpers_128bit::multiply_by_rational_with_rounding,
traits::{AccountIdConversion, AtLeast32BitUnsigned, Hash, IntegerSquareRoot, One, Zero},
Rounding,
use sp_runtime::traits::{
AccountIdConversion, AtLeast32BitUnsigned, Hash, IntegerSquareRoot, One, Zero,
};

#[pallet::pallet]
Expand Down Expand Up @@ -191,6 +189,8 @@ pub mod pallet {
PoolNotFound,
/// An overflow happened.
Overflow,
/// This balance type can't be converted into u128.
UnsupportedBalanceType,
/// Insufficient amount provided for the first token in the pair.
InsufficientAmountParam1,
/// Insufficient amount provided for the second token in the pair.
Expand Down Expand Up @@ -607,8 +607,10 @@ pub mod pallet {
amount1: &AssetBalanceOf<T>,
amount2: &AssetBalanceOf<T>,
) -> Result<AssetBalanceOf<T>, Error<T>> {
let amount1 = u128::try_from(*amount1).map_err(|_| Error::<T>::Overflow)?;
let amount2 = u128::try_from(*amount2).map_err(|_| Error::<T>::Overflow)?;
let amount1 =
u128::try_from(*amount1).map_err(|_| Error::<T>::UnsupportedBalanceType)?;
let amount2 =
u128::try_from(*amount2).map_err(|_| Error::<T>::UnsupportedBalanceType)?;

let result = amount1
.checked_mul(amount2)
Expand All @@ -625,14 +627,15 @@ pub mod pallet {
b: &AssetBalanceOf<T>,
c: &AssetBalanceOf<T>,
) -> Result<AssetBalanceOf<T>, Error<T>> {
// amount * reserve2 / reserve1
let result = multiply_by_rational_with_rounding(
(*a).into(),
(*b).into(),
(*c).into(),
Rounding::Down,
)
.ok_or(Error::<T>::Overflow)?;
let a = u128::try_from(*a).map_err(|_| Error::<T>::UnsupportedBalanceType)?;
let b = u128::try_from(*b).map_err(|_| Error::<T>::UnsupportedBalanceType)?;
let c = u128::try_from(*c).map_err(|_| Error::<T>::UnsupportedBalanceType)?;

let result = a
.checked_mul(b)
.ok_or(Error::<T>::Overflow)?
.checked_div(c)
.ok_or(Error::<T>::Overflow)?;

result.try_into().map_err(|_| Error::<T>::Overflow)
}
Expand All @@ -646,15 +649,17 @@ pub mod pallet {
reserve_in: &AssetBalanceOf<T>,
reserve_out: &AssetBalanceOf<T>,
) -> Result<AssetBalanceOf<T>, Error<T>> {
let amount_in = u128::try_from(*amount_in).map_err(|_| Error::<T>::Overflow)?;
let reserve_in = u128::try_from(*reserve_in).map_err(|_| Error::<T>::Overflow)?;
let reserve_out = u128::try_from(*reserve_out).map_err(|_| Error::<T>::Overflow)?;
let amount_in =
u128::try_from(*amount_in).map_err(|_| Error::<T>::UnsupportedBalanceType)?;
let reserve_in =
u128::try_from(*reserve_in).map_err(|_| Error::<T>::UnsupportedBalanceType)?;
let reserve_out =
u128::try_from(*reserve_out).map_err(|_| Error::<T>::UnsupportedBalanceType)?;

if reserve_in.is_zero() || reserve_out.is_zero() {
return Err(Error::<T>::ZeroLiquidity.into())
}

// TODO: could use Permill type
let amount_in_with_fee = amount_in
.checked_mul(1000u128 - (T::Fee::get() as u128))
.ok_or(Error::<T>::Overflow)?;
Expand Down Expand Up @@ -682,9 +687,12 @@ pub mod pallet {
reserve_in: &AssetBalanceOf<T>,
reserve_out: &AssetBalanceOf<T>,
) -> Result<AssetBalanceOf<T>, Error<T>> {
let amount_out = u128::try_from(*amount_out).map_err(|_| Error::<T>::Overflow)?;
let reserve_in = u128::try_from(*reserve_in).map_err(|_| Error::<T>::Overflow)?;
let reserve_out = u128::try_from(*reserve_out).map_err(|_| Error::<T>::Overflow)?;
let amount_out =
u128::try_from(*amount_out).map_err(|_| Error::<T>::UnsupportedBalanceType)?;
let reserve_in =
u128::try_from(*reserve_in).map_err(|_| Error::<T>::UnsupportedBalanceType)?;
let reserve_out =
u128::try_from(*reserve_out).map_err(|_| Error::<T>::UnsupportedBalanceType)?;

if reserve_in.is_zero() || reserve_out.is_zero() {
return Err(Error::<T>::ZeroLiquidity.into())
Expand Down

0 comments on commit 027c7a5

Please sign in to comment.