diff --git a/src/entities/fractions/fraction.rs b/src/entities/fractions/fraction.rs index bf762f4..ccc7646 100644 --- a/src/entities/fractions/fraction.rs +++ b/src/entities/fractions/fraction.rs @@ -345,15 +345,15 @@ mod tests { #[test] fn test_multiply() { assert_eq!( - (Fraction::new(1, 10) * Fraction::new(4, 12)), + Fraction::new(1, 10) * Fraction::new(4, 12), Fraction::new(4, 120) ); assert_eq!( - (Fraction::new(1, 3) * Fraction::new(4, 12)), + Fraction::new(1, 3) * Fraction::new(4, 12), Fraction::new(4, 36) ); assert_eq!( - (Fraction::new(5, 12) * Fraction::new(4, 12)), + Fraction::new(5, 12) * Fraction::new(4, 12), Fraction::new(20, 144) ); } diff --git a/src/entities/fractions/percent.rs b/src/entities/fractions/percent.rs index ceb0f5f..b11baf6 100644 --- a/src/entities/fractions/percent.rs +++ b/src/entities/fractions/percent.rs @@ -47,11 +47,11 @@ mod tests { #[test] fn test_add() { assert_eq!( - (Percent::new(1, 100) + Percent::new(2, 100)), + Percent::new(1, 100) + Percent::new(2, 100), Percent::new(3, 100) ); assert_eq!( - (Percent::new(1, 25) + Percent::new(2, 100)), + Percent::new(1, 25) + Percent::new(2, 100), Percent::new(150, 2500) ); } @@ -59,11 +59,11 @@ mod tests { #[test] fn test_subtract() { assert_eq!( - (Percent::new(1, 100) - Percent::new(2, 100)), + Percent::new(1, 100) - Percent::new(2, 100), Percent::new(-1, 100) ); assert_eq!( - (Percent::new(1, 25) - Percent::new(2, 100)), + Percent::new(1, 25) - Percent::new(2, 100), Percent::new(50, 2500) ); } @@ -71,11 +71,11 @@ mod tests { #[test] fn test_multiply() { assert_eq!( - (Percent::new(1, 100) * Percent::new(2, 100)), + Percent::new(1, 100) * Percent::new(2, 100), Percent::new(2, 10000) ); assert_eq!( - (Percent::new(1, 25) * Percent::new(2, 100)), + Percent::new(1, 25) * Percent::new(2, 100), Percent::new(2, 2500) ); } @@ -83,11 +83,11 @@ mod tests { #[test] fn test_divide() { assert_eq!( - (Percent::new(1, 100) / Percent::new(2, 100)), + Percent::new(1, 100) / Percent::new(2, 100), Percent::new(100, 200) ); assert_eq!( - (Percent::new(1, 25) / Percent::new(2, 100)), + Percent::new(1, 25) / Percent::new(2, 100), Percent::new(100, 50) ); } diff --git a/src/utils/compute_price_impact.rs b/src/utils/compute_price_impact.rs index 2861309..d7ba360 100644 --- a/src/utils/compute_price_impact.rs +++ b/src/utils/compute_price_impact.rs @@ -14,18 +14,14 @@ pub fn compute_price_impact( input_amount: CurrencyAmount, output_amount: CurrencyAmount, ) -> Result { - let quoted_output_amount = mid_price.quote(input_amount); + let quoted_output_amount = mid_price.quote(input_amount)?; // calculate price impact := (exactQuote - outputAmount) / exactQuote - let price_impact = match quoted_output_amount { - Ok(quoted_output_amount) => quoted_output_amount - .subtract(&output_amount)? - .divide("ed_output_amount), - Err(e) => Err(e), - }; - let price_impact_clone = price_impact?; + let price_impact = quoted_output_amount + .subtract(&output_amount)? + .divide("ed_output_amount)?; Ok(Percent::new( - price_impact_clone.numerator(), - price_impact_clone.denominator(), + price_impact.numerator(), + price_impact.denominator(), )) } @@ -65,14 +61,14 @@ mod tests { ); //is negative for more output - assert!( + assert_eq!( compute_price_impact( Price::new(token.clone(), token_1.clone(), 10, 100), CurrencyAmount::from_raw_amount(token.clone(), 10).unwrap(), CurrencyAmount::from_raw_amount(token_1.clone(), 200).unwrap() ) - .unwrap() - == Percent::new(-10000, 10000) + .unwrap(), + Percent::new(-10000, 10000) ) } } diff --git a/src/utils/sqrt.rs b/src/utils/sqrt.rs index 48eeebf..c7e1e89 100644 --- a/src/utils/sqrt.rs +++ b/src/utils/sqrt.rs @@ -10,29 +10,10 @@ use num_traits::Signed; /// returns: BigInt pub fn sqrt(value: &BigInt) -> Result { if value.is_negative() { - return Err(Error::Incorrect()); + Err(Error::Incorrect()) + } else { + Ok(value.sqrt()) } - - // If the value is less than or equal to MAX_SAFE_INTEGER, - // we can safely convert it to an i64 and use the primitive sqrt function. - if let Some(safe_value) = value.to_i64() { - return Ok(((safe_value as f64).sqrt().floor() as i64) - .to_bigint() - .unwrap()); - } - - // Otherwise, we use the Babylonian method to calculate the square root. - let two = 2.to_bigint().unwrap(); - let one = 1.to_bigint().unwrap(); - let mut z = value.clone(); - let mut x = (value / &two) + &one; - - while x < z { - z.clone_from(&x); - x = ((value / &x) + &x) / &two; - } - - Ok(z) } #[cfg(test)]