diff --git a/Cargo.toml b/Cargo.toml index 9cda52c..d600b0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uniswap-sdk-core" -version = "0.20.0" +version = "0.21.0" edition = "2021" authors = ["malik ", "Shuhui Luo "] description = "The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange" @@ -8,7 +8,7 @@ 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" diff --git a/README.md b/README.md index 5d5dd4f..74834dd 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/entities/fractions/currency_amount.rs b/src/entities/fractions/currency_amount.rs index b4f56a4..0fdf47e 100644 --- a/src/entities/fractions/currency_amount.rs +++ b/src/entities/fractions/currency_amount.rs @@ -118,6 +118,11 @@ impl CurrencyAmount { 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), @@ -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" + ); } #[test] @@ -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" ); } @@ -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" ); } diff --git a/src/entities/fractions/percent.rs b/src/entities/fractions/percent.rs index c8beac6..ed5b85b 100644 --- a/src/entities/fractions/percent.rs +++ b/src/entities/fractions/percent.rs @@ -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() ); } } diff --git a/src/entities/fractions/price.rs b/src/entities/fractions/price.rs index 8868e4a..e633194 100644 --- a/src/entities/fractions/price.rs +++ b/src/entities/fractions/price.rs @@ -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" ); } @@ -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" ); } @@ -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" ); } }