Skip to content

Commit

Permalink
chore: Update package versions and adjust test assertions (#67)
Browse files Browse the repository at this point in the history
* chore: Update package versions and adjust test assertions

The package versions in Cargo.toml have been updated including 'bigdecimal', 'lazy_static', 'num-traits' and few others. In the same commit, the assertion methodology in the test cases for the 'Price' entity has been changed to increase precision. Also, the expected results for 'to_significant' tests have been replaced with new values in scientific notation.

* refactor: Improve efficiency and readability (#68)

* 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 db82793 commit 167db5e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 58 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
[package]
name = "uniswap-sdk-core"
version = "0.24.0"
version = "0.25.0"
edition = "2021"
authors = ["malik <[email protected]>", "Shuhui Luo <twitter.com/aureliano_law>"]
description = "The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange"
license = "MIT"

[dependencies]
alloy-primitives = "0.7"
bigdecimal = "=0.4.2"
bigdecimal = "0.4.5"
eth_checksum = { version = "0.1.2", optional = true }
lazy_static = "1.4"
num-bigint = "0.4.4"
num-integer = "0.1.45"
num-traits = "0.2.17"
lazy_static = "1.5"
num-bigint = "0.4"
num-integer = "0.1"
num-traits = "0.2"
regex = { version = "1.10", optional = true }
rustc-hash = "2.0.0"
rustc-hash = "2.0"
thiserror = "1.0"

[features]
Expand Down
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
10 changes: 5 additions & 5 deletions src/entities/fractions/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ mod test {
#[test]
fn test_quote_returns_correct_value() {
let price = Price::new(TOKEN0.clone(), TOKEN1.clone(), 1, 5);
assert!(
assert_eq!(
price
.quote(CurrencyAmount::from_raw_amount(TOKEN0.clone(), 10).unwrap())
.unwrap()
== CurrencyAmount::from_raw_amount(TOKEN1.clone(), 50).unwrap()
.unwrap(),
CurrencyAmount::from_raw_amount(TOKEN1.clone(), 50).unwrap()
);
}

Expand All @@ -199,7 +199,7 @@ mod test {
let p = Price::new(TOKEN0_6.clone(), TOKEN1.clone(), 123, 456);
assert_eq!(
p.to_significant(4, Rounding::RoundDown).unwrap(),
"0.000000000003707"
"3.707E-12"
);
}

Expand All @@ -208,7 +208,7 @@ mod test {
let p = Price::new(TOKEN0_6.clone(), TOKEN1.clone(), 456, 123);
assert_eq!(
p.to_significant(4, Rounding::RoundDown).unwrap(),
"0.0000000000002697"
"2.697E-13"
);
}

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 167db5e

Please sign in to comment.