From d97192539fe2c4666c19dcfd393753dfeabb1d0d Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Sat, 30 Dec 2023 03:50:12 -0800 Subject: [PATCH] replace rust_decimal with bigdecimal for arbitrary precision This commit updates the version of the uniswap-sdk-core-rust library to 0.5.0 and includes bigdecimal as a dependency, replacing the previous rust_decimal. The major change involves the update in the Fraction and CurrencyAmount where BigDecimal is now used for calculations instead of Decimal from rust_decimal. The relevant changes in the code are also refactored to fit with the bigdecimal usage. The Cargo.lock file has also been updated to reflect these changes. --- Cargo.lock | 233 ++-------------------- Cargo.toml | 4 +- src/entities/fractions/currency_amount.rs | 6 +- src/entities/fractions/fraction.rs | 34 ++-- 4 files changed, 39 insertions(+), 238 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cfeba35..d8c3ffb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,17 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "ahash" -version = "0.7.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a824f2aa7e75a0c98c5a504fceb80649e9c35265d44525b5f94de4771a395cd" -dependencies = [ - "getrandom", - "once_cell", - "version_check", -] - [[package]] name = "aho-corasick" version = "1.1.2" @@ -201,6 +190,19 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "bigdecimal" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06619be423ea5bb86c95f087d5707942791a08a85530df0db2209a3ecfb8bc9" +dependencies = [ + "autocfg", + "libm", + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "bit-set" version = "0.5.3" @@ -249,58 +251,12 @@ dependencies = [ "generic-array", ] -[[package]] -name = "borsh" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26d4d6dafc1a3bb54687538972158f07b2c948bc57d5890df22c0739098b3028" -dependencies = [ - "borsh-derive", - "cfg_aliases", -] - -[[package]] -name = "borsh-derive" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf4918709cc4dd777ad2b6303ed03cb37f3ca0ccede8c1b0d28ac6db8f4710e0" -dependencies = [ - "once_cell", - "proc-macro-crate", - "proc-macro2", - "quote", - "syn 2.0.43", - "syn_derive", -] - [[package]] name = "byte-slice-cast" version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3ac9f8b63eca6fd385229b3675f6cc0dc5c8a5c8a54a59d4f52ffd670d87b0c" -[[package]] -name = "bytecheck" -version = "0.6.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b6372023ac861f6e6dc89c8344a8f398fb42aaba2b5dbc649ca0c0e9dbcb627" -dependencies = [ - "bytecheck_derive", - "ptr_meta", - "simdutf8", -] - -[[package]] -name = "bytecheck_derive" -version = "0.6.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7ec4c6f261935ad534c0c22dbef2201b45918860eb1c574b972bd213a76af61" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "byteorder" version = "1.5.0" @@ -328,12 +284,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cfg_aliases" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" - [[package]] name = "const-hex" version = "1.10.0" @@ -526,15 +476,6 @@ dependencies = [ "wasi 0.11.0+wasi-snapshot-preview1", ] -[[package]] -name = "hashbrown" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -dependencies = [ - "ahash", -] - [[package]] name = "hashbrown" version = "0.14.3" @@ -580,7 +521,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" dependencies = [ "equivalent", - "hashbrown 0.14.3", + "hashbrown", ] [[package]] @@ -690,12 +631,6 @@ dependencies = [ "libm", ] -[[package]] -name = "once_cell" -version = "1.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" - [[package]] name = "ordered-float" version = "2.10.1" @@ -828,26 +763,6 @@ dependencies = [ "unarray", ] -[[package]] -name = "ptr_meta" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" -dependencies = [ - "ptr_meta_derive", -] - -[[package]] -name = "ptr_meta_derive" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "quick-error" version = "1.2.3" @@ -993,44 +908,6 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" -[[package]] -name = "rend" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2571463863a6bd50c32f94402933f03457a3fbaf697a707c5be741e459f08fd" -dependencies = [ - "bytecheck", -] - -[[package]] -name = "rkyv" -version = "0.7.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "527a97cdfef66f65998b5f3b637c26f5a5ec09cc52a3f9932313ac645f4190f5" -dependencies = [ - "bitvec", - "bytecheck", - "bytes", - "hashbrown 0.12.3", - "ptr_meta", - "rend", - "rkyv_derive", - "seahash", - "tinyvec", - "uuid", -] - -[[package]] -name = "rkyv_derive" -version = "0.7.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5c462a1328c8e67e4d6dbad1eb0355dd43e8ab432c6e227a43657f16ade5033" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "rlp" version = "0.5.2" @@ -1084,22 +961,6 @@ dependencies = [ "time", ] -[[package]] -name = "rust_decimal" -version = "1.33.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06676aec5ccb8fc1da723cc8c0f9a46549f21ebb8753d3915c6c41db1e7f1dc4" -dependencies = [ - "arrayvec", - "borsh", - "bytes", - "num-traits", - "rand 0.8.5", - "rkyv", - "serde", - "serde_json", -] - [[package]] name = "rustc-hex" version = "2.1.0" @@ -1155,18 +1016,6 @@ dependencies = [ "wait-timeout", ] -[[package]] -name = "ryu" -version = "1.0.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" - -[[package]] -name = "seahash" -version = "4.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" - [[package]] name = "semver" version = "0.11.0" @@ -1221,17 +1070,6 @@ dependencies = [ "syn 2.0.43", ] -[[package]] -name = "serde_json" -version = "1.0.108" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" -dependencies = [ - "itoa", - "ryu", - "serde", -] - [[package]] name = "sha3" version = "0.10.8" @@ -1252,12 +1090,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "simdutf8" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" - [[package]] name = "static_assertions" version = "1.1.0" @@ -1286,18 +1118,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "syn_derive" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" -dependencies = [ - "proc-macro-error", - "proc-macro2", - "quote", - "syn 2.0.43", -] - [[package]] name = "tap" version = "1.0.1" @@ -1357,21 +1177,6 @@ dependencies = [ "crunchy", ] -[[package]] -name = "tinyvec" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" - [[package]] name = "toml_datetime" version = "0.6.3" @@ -1427,9 +1232,10 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "uniswap-sdk-core-rust" -version = "0.4.1" +version = "0.5.0" dependencies = [ "alloy-primitives", + "bigdecimal", "eth_checksum", "lazy_static", "num-bigint", @@ -1437,18 +1243,11 @@ dependencies = [ "num-rational", "num-traits", "regex", - "rust_decimal", "rustc-hex", "serde-value", "sha3", ] -[[package]] -name = "uuid" -version = "1.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560" - [[package]] name = "valuable" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 336ef65..023793c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uniswap-sdk-core-rust" -version = "0.4.1" +version = "0.5.0" edition = "2021" authors = ["malik ", "Shuhui Luo "] description = "The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange" @@ -10,6 +10,7 @@ license = "MIT" [dependencies] alloy-primitives = "0.5.4" +bigdecimal = "0.4.2" eth_checksum = "0.1.2" lazy_static = "1.4.0" num-bigint = "0.4.4" @@ -17,7 +18,6 @@ num-integer = "0.1.45" num-rational = "0.4.1" num-traits = "0.2.17" regex = "1.10.2" -rust_decimal = "1.33.1" rustc-hex = "2.1.0" serde-value = "0.7.0" sha3 = "0.10.8" diff --git a/src/entities/fractions/currency_amount.rs b/src/entities/fractions/currency_amount.rs index e6ca2da..66039bb 100644 --- a/src/entities/fractions/currency_amount.rs +++ b/src/entities/fractions/currency_amount.rs @@ -7,9 +7,9 @@ use crate::{ token::Token, }, }; +use bigdecimal::BigDecimal; use num_bigint::{BigInt, BigUint}; use num_integer::Integer; -use rust_decimal::Decimal; use std::{ops::Div, str::FromStr}; // Type alias for a currency amount using the FractionLike trait @@ -77,9 +77,9 @@ impl CurrencyAmount { // Convert the currency amount to a string with exact precision pub fn to_exact(&self) -> String { - Decimal::from_str(&self.quotient().to_str_radix(10)) + BigDecimal::from_str(&self.quotient().to_str_radix(10)) .unwrap() - .div(Decimal::from_str(&self.meta.decimal_scale.to_str_radix(10)).unwrap()) + .div(BigDecimal::from_str(&self.meta.decimal_scale.to_str_radix(10)).unwrap()) .to_string() } diff --git a/src/entities/fractions/fraction.rs b/src/entities/fractions/fraction.rs index a926455..7e92390 100644 --- a/src/entities/fractions/fraction.rs +++ b/src/entities/fractions/fraction.rs @@ -1,9 +1,10 @@ // External crate dependencies use crate::constants::Rounding; +use bigdecimal::{BigDecimal, RoundingMode}; use num_bigint::BigInt; use num_integer::Integer; -use rust_decimal::prelude::*; -use std::ops::Div; +use num_traits::Zero; +use std::{num::NonZeroU64, ops::Div, str::FromStr}; // Struct representing a fraction with metadata #[derive(Clone, PartialEq)] @@ -23,12 +24,12 @@ impl Fraction { } } -// Function to convert the custom Rounding enum to rust_decimal's RoundingStrategy -fn to_rounding_strategy(rounding: Rounding) -> RoundingStrategy { +// Function to convert the custom Rounding enum to `bigdecimal`'s `RoundingMode` +fn to_rounding_strategy(rounding: Rounding) -> RoundingMode { match rounding { - Rounding::RoundDown => RoundingStrategy::ToZero, - Rounding::RoundHalfUp => RoundingStrategy::MidpointAwayFromZero, - Rounding::RoundUp => RoundingStrategy::AwayFromZero, + Rounding::RoundDown => RoundingMode::Down, + Rounding::RoundHalfUp => RoundingMode::HalfUp, + Rounding::RoundUp => RoundingMode::Up, } } @@ -136,11 +137,11 @@ pub trait FractionTrait: Sized { self.numerator() * other.denominator() > other.numerator() * self.denominator() } - // Converts the fraction to a rust_decimal::Decimal - fn to_decimal(&self) -> Decimal { - Decimal::from_str(&self.numerator().to_str_radix(10)) + // Converts the fraction to a `bigdecimal::BigDecimal` + fn to_decimal(&self) -> BigDecimal { + BigDecimal::from_str(&self.numerator().to_str_radix(10)) .unwrap() - .div(Decimal::from_str(&self.denominator().to_str_radix(10)).unwrap()) + .div(BigDecimal::from_str(&self.denominator().to_str_radix(10)).unwrap()) } // Converts the fraction to a string with a specified number of significant digits and rounding strategy @@ -151,11 +152,12 @@ pub trait FractionTrait: Sized { ); let rounding_strategy = to_rounding_strategy(rounding); - let quotient = self - .to_decimal() - .round_sf_with_strategy(significant_digits as u32, rounding_strategy); + let quotient = self.to_decimal().with_precision_round( + NonZeroU64::new(significant_digits as u64).unwrap(), + rounding_strategy, + ); - quotient.unwrap().normalize().to_string() + quotient.normalized().to_string() } // Converts the fraction to a string with a fixed number of decimal places and rounding strategy @@ -163,7 +165,7 @@ pub trait FractionTrait: Sized { let rounding_strategy = to_rounding_strategy(rounding); let quotient = self .to_decimal() - .round_dp_with_strategy(decimal_places as u32, rounding_strategy); + .with_scale_round(decimal_places as i64, rounding_strategy); format!("{:.1$}", quotient, decimal_places as usize) }