Skip to content

Commit

Permalink
refactor: Improve efficiency and readability (#68)
Browse files Browse the repository at this point in the history
* Refactor code for fraction and percent tests

The brackets around the arithmetic operations in the fraction.rs and percent.rs test functions have been removed to improve readability. Also, in compute_price_impact.rs tests, the 'assert!' function has been replaced with 'assert_eq!' to enhance precision and clarity of the tests.

* Refactor `compute_price_impact` function

Simplify the `compute_price_impact` function by eliminating the need for manual error handling and using the question mark operator instead. This change also eliminates the need for clone of price impact, making the function more efficient and cleaner.

* Refactor `sqrt` function for `BigInt`

The square root function for BigInt has been simplified. The previous implementation used a combination of primitive sqrt for smaller values and Babylonian method for larger ones. Now, the function uses the in-built sqrt method regardless of the input size, simplifying the code and reducing potential errors.
  • Loading branch information
shuhuiluo authored Jul 9, 2024
1 parent 77d853d commit 44aed2c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 46 deletions.
6 changes: 3 additions & 3 deletions src/entities/fractions/fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand Down
16 changes: 8 additions & 8 deletions src/entities/fractions/percent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,47 @@ 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)
);
}

#[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)
);
}

#[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)
);
}

#[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)
);
}
Expand Down
22 changes: 9 additions & 13 deletions src/utils/compute_price_impact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@ pub fn compute_price_impact<TBase: CurrencyTrait, TQuote: CurrencyTrait>(
input_amount: CurrencyAmount<TBase>,
output_amount: CurrencyAmount<TQuote>,
) -> Result<Percent, Error> {
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(&quoted_output_amount),
Err(e) => Err(e),
};
let price_impact_clone = price_impact?;
let price_impact = quoted_output_amount
.subtract(&output_amount)?
.divide(&quoted_output_amount)?;
Ok(Percent::new(
price_impact_clone.numerator(),
price_impact_clone.denominator(),
price_impact.numerator(),
price_impact.denominator(),
))
}

Expand Down Expand Up @@ -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)
)
}
}
25 changes: 3 additions & 22 deletions src/utils/sqrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,10 @@ use num_traits::Signed;
/// returns: BigInt
pub fn sqrt(value: &BigInt) -> Result<BigInt, Error> {
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)]
Expand Down

0 comments on commit 44aed2c

Please sign in to comment.