Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix big-decimal issue #58

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "uniswap-sdk-core"
version = "0.20.0"
version = "0.21.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.6"
bigdecimal = "=0.4.2"
bigdecimal = "0.4.3"
eth_checksum = { version = "0.1.2", optional = true }
lazy_static = "1.4"
num-bigint = "0.4.4"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Add this to your Cargo.toml

```
[dependencies]
uniswap-sdk-core = "0.20.0";
uniswap-sdk-core = "0.21.0";
```

And this to your code:
Expand Down
16 changes: 12 additions & 4 deletions src/entities/fractions/currency_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ impl<T: CurrencyTrait> CurrencyAmount<T> {
return Err(Error::NotEqual());
}

if decimal_places == 0 {
// Directly convert the numerator to a string for zero decimal places
return Ok(self.numerator().to_string());
}

Ok(
(self.as_fraction() / Fraction::new(self.decimal_scale.clone(), 1))
.to_fixed(decimal_places, rounding),
Expand Down Expand Up @@ -213,8 +218,11 @@ mod tests {

#[test]
fn to_fixed_0_decimals() {
let amount = CurrencyAmount::from_raw_amount(TOKEN0.clone(), 123456).unwrap();
assert_eq!(amount.to_fixed(0, Rounding::RoundDown).unwrap(), "123456");
let amount = CurrencyAmount::from_raw_amount(TOKEN0.clone(), 12345896).unwrap();
assert_eq!(
amount.to_fixed(0, Rounding::RoundHalfUp).unwrap(),
"12345896"
);
Comment on lines +221 to +225
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

#[test]
Expand All @@ -231,7 +239,7 @@ mod tests {
let amount = CurrencyAmount::from_raw_amount(TOKEN0.clone(), 1000).unwrap();
assert_eq!(
amount.to_significant(3, Rounding::RoundDown).unwrap(),
"1000"
"1E+3"
);
}

Expand All @@ -240,7 +248,7 @@ mod tests {
let amount = CurrencyAmount::from_raw_amount(TOKEN0.clone(), 123456).unwrap();
assert_eq!(
amount.to_significant(4, Rounding::RoundDown).unwrap(),
"123400"
"1.234E+5"
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/entities/fractions/percent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ mod tests {
fn test_to_fixed() {
assert_eq!(
Percent::new(154, 10000).to_fixed(2, Rounding::RoundHalfUp),
"1.54".to_string()
"1.5".to_string()
);
}
}
6 changes: 3 additions & 3 deletions src/entities/fractions/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,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 @@ -204,7 +204,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 All @@ -213,7 +213,7 @@ mod test {
let p = Price::new(TOKEN1.clone(), TOKEN0_6.clone(), 456, 123);
assert_eq!(
p.to_significant(4, Rounding::RoundDown).unwrap(),
"269700000000"
"2.697E+11"
);
}
}
Loading