Skip to content

Commit 167db5e

Browse files
authored
chore: Update package versions and adjust test assertions (#67)
* 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.
1 parent db82793 commit 167db5e

File tree

6 files changed

+35
-58
lines changed

6 files changed

+35
-58
lines changed

Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
[package]
22
name = "uniswap-sdk-core"
3-
version = "0.24.0"
3+
version = "0.25.0"
44
edition = "2021"
55
authors = ["malik <aremumalik05@gmail.com>", "Shuhui Luo <twitter.com/aureliano_law>"]
66
description = "The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange"
77
license = "MIT"
88

99
[dependencies]
1010
alloy-primitives = "0.7"
11-
bigdecimal = "=0.4.2"
11+
bigdecimal = "0.4.5"
1212
eth_checksum = { version = "0.1.2", optional = true }
13-
lazy_static = "1.4"
14-
num-bigint = "0.4.4"
15-
num-integer = "0.1.45"
16-
num-traits = "0.2.17"
13+
lazy_static = "1.5"
14+
num-bigint = "0.4"
15+
num-integer = "0.1"
16+
num-traits = "0.2"
1717
regex = { version = "1.10", optional = true }
18-
rustc-hash = "2.0.0"
18+
rustc-hash = "2.0"
1919
thiserror = "1.0"
2020

2121
[features]

src/entities/fractions/fraction.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,15 @@ mod tests {
345345
#[test]
346346
fn test_multiply() {
347347
assert_eq!(
348-
(Fraction::new(1, 10) * Fraction::new(4, 12)),
348+
Fraction::new(1, 10) * Fraction::new(4, 12),
349349
Fraction::new(4, 120)
350350
);
351351
assert_eq!(
352-
(Fraction::new(1, 3) * Fraction::new(4, 12)),
352+
Fraction::new(1, 3) * Fraction::new(4, 12),
353353
Fraction::new(4, 36)
354354
);
355355
assert_eq!(
356-
(Fraction::new(5, 12) * Fraction::new(4, 12)),
356+
Fraction::new(5, 12) * Fraction::new(4, 12),
357357
Fraction::new(20, 144)
358358
);
359359
}

src/entities/fractions/percent.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,47 +47,47 @@ mod tests {
4747
#[test]
4848
fn test_add() {
4949
assert_eq!(
50-
(Percent::new(1, 100) + Percent::new(2, 100)),
50+
Percent::new(1, 100) + Percent::new(2, 100),
5151
Percent::new(3, 100)
5252
);
5353
assert_eq!(
54-
(Percent::new(1, 25) + Percent::new(2, 100)),
54+
Percent::new(1, 25) + Percent::new(2, 100),
5555
Percent::new(150, 2500)
5656
);
5757
}
5858

5959
#[test]
6060
fn test_subtract() {
6161
assert_eq!(
62-
(Percent::new(1, 100) - Percent::new(2, 100)),
62+
Percent::new(1, 100) - Percent::new(2, 100),
6363
Percent::new(-1, 100)
6464
);
6565
assert_eq!(
66-
(Percent::new(1, 25) - Percent::new(2, 100)),
66+
Percent::new(1, 25) - Percent::new(2, 100),
6767
Percent::new(50, 2500)
6868
);
6969
}
7070

7171
#[test]
7272
fn test_multiply() {
7373
assert_eq!(
74-
(Percent::new(1, 100) * Percent::new(2, 100)),
74+
Percent::new(1, 100) * Percent::new(2, 100),
7575
Percent::new(2, 10000)
7676
);
7777
assert_eq!(
78-
(Percent::new(1, 25) * Percent::new(2, 100)),
78+
Percent::new(1, 25) * Percent::new(2, 100),
7979
Percent::new(2, 2500)
8080
);
8181
}
8282

8383
#[test]
8484
fn test_divide() {
8585
assert_eq!(
86-
(Percent::new(1, 100) / Percent::new(2, 100)),
86+
Percent::new(1, 100) / Percent::new(2, 100),
8787
Percent::new(100, 200)
8888
);
8989
assert_eq!(
90-
(Percent::new(1, 25) / Percent::new(2, 100)),
90+
Percent::new(1, 25) / Percent::new(2, 100),
9191
Percent::new(100, 50)
9292
);
9393
}

src/entities/fractions/price.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ mod test {
174174
#[test]
175175
fn test_quote_returns_correct_value() {
176176
let price = Price::new(TOKEN0.clone(), TOKEN1.clone(), 1, 5);
177-
assert!(
177+
assert_eq!(
178178
price
179179
.quote(CurrencyAmount::from_raw_amount(TOKEN0.clone(), 10).unwrap())
180-
.unwrap()
181-
== CurrencyAmount::from_raw_amount(TOKEN1.clone(), 50).unwrap()
180+
.unwrap(),
181+
CurrencyAmount::from_raw_amount(TOKEN1.clone(), 50).unwrap()
182182
);
183183
}
184184

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

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

src/utils/compute_price_impact.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@ pub fn compute_price_impact<TBase: CurrencyTrait, TQuote: CurrencyTrait>(
1414
input_amount: CurrencyAmount<TBase>,
1515
output_amount: CurrencyAmount<TQuote>,
1616
) -> Result<Percent, Error> {
17-
let quoted_output_amount = mid_price.quote(input_amount);
17+
let quoted_output_amount = mid_price.quote(input_amount)?;
1818
// calculate price impact := (exactQuote - outputAmount) / exactQuote
19-
let price_impact = match quoted_output_amount {
20-
Ok(quoted_output_amount) => quoted_output_amount
21-
.subtract(&output_amount)?
22-
.divide(&quoted_output_amount),
23-
Err(e) => Err(e),
24-
};
25-
let price_impact_clone = price_impact?;
19+
let price_impact = quoted_output_amount
20+
.subtract(&output_amount)?
21+
.divide(&quoted_output_amount)?;
2622
Ok(Percent::new(
27-
price_impact_clone.numerator(),
28-
price_impact_clone.denominator(),
23+
price_impact.numerator(),
24+
price_impact.denominator(),
2925
))
3026
}
3127

@@ -65,14 +61,14 @@ mod tests {
6561
);
6662

6763
//is negative for more output
68-
assert!(
64+
assert_eq!(
6965
compute_price_impact(
7066
Price::new(token.clone(), token_1.clone(), 10, 100),
7167
CurrencyAmount::from_raw_amount(token.clone(), 10).unwrap(),
7268
CurrencyAmount::from_raw_amount(token_1.clone(), 200).unwrap()
7369
)
74-
.unwrap()
75-
== Percent::new(-10000, 10000)
70+
.unwrap(),
71+
Percent::new(-10000, 10000)
7672
)
7773
}
7874
}

src/utils/sqrt.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,10 @@ use num_traits::Signed;
1010
/// returns: BigInt
1111
pub fn sqrt(value: &BigInt) -> Result<BigInt, Error> {
1212
if value.is_negative() {
13-
return Err(Error::Incorrect());
13+
Err(Error::Incorrect())
14+
} else {
15+
Ok(value.sqrt())
1416
}
15-
16-
// If the value is less than or equal to MAX_SAFE_INTEGER,
17-
// we can safely convert it to an i64 and use the primitive sqrt function.
18-
if let Some(safe_value) = value.to_i64() {
19-
return Ok(((safe_value as f64).sqrt().floor() as i64)
20-
.to_bigint()
21-
.unwrap());
22-
}
23-
24-
// Otherwise, we use the Babylonian method to calculate the square root.
25-
let two = 2.to_bigint().unwrap();
26-
let one = 1.to_bigint().unwrap();
27-
let mut z = value.clone();
28-
let mut x = (value / &two) + &one;
29-
30-
while x < z {
31-
z.clone_from(&x);
32-
x = ((value / &x) + &x) / &two;
33-
}
34-
35-
Ok(z)
3617
}
3718

3819
#[cfg(test)]

0 commit comments

Comments
 (0)