From 8bc99a6e8d421f55eb2e747c667d3ac771625343 Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Sat, 17 Aug 2024 14:19:03 -0700 Subject: [PATCH] chore: Update error variants and improve documentation Changed multiple occurrences of `Error::Incorrect` to `Error::Invalid` for better clarity. Updated the README with additional notes and corrected minor format issues. Also included new resources in the references section and transitioned the project version from "1.0.0-rc" to "1.0.0". --- Cargo.toml | 2 +- README.md | 18 ++++++------- src/entities/fractions/currency_amount.rs | 6 ++--- src/entities/fractions/fraction.rs | 2 +- src/entities/fractions/price.rs | 4 +-- src/error.rs | 32 +++++++++++------------ src/utils/sorted_insert.rs | 4 +-- src/utils/sqrt.rs | 2 +- 8 files changed, 34 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e961828..5ee0eb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uniswap-sdk-core" -version = "1.0.0-rc" +version = "1.0.0" edition = "2021" authors = ["malik ", "Shuhui Luo "] description = "The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange" diff --git a/README.md b/README.md index 6601a0a..8145a72 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,10 @@ **A Custom Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange.** -> **Warning** -> -> This is a custom Uniswap library +## Note on `no_std` + +By default, this library does not depend on the standard library (`std`). However, the `std` feature can be enabled to +use `thiserror` for error handling. ## Quickstart @@ -17,7 +18,7 @@ Add this to your Cargo.toml ``` [dependencies] -uniswap-sdk-core = "1.0.0-rc" +uniswap-sdk-core = "1.0.0" ``` And this to your code: @@ -36,7 +37,7 @@ the `token!` macro. // once. This can be useful if you are working with multiple parts of the library and want to avoid // having to import each dependency individually. // Import necessary preludes and types -use uniswap_sdk_core::{prelude::*,token}; +use uniswap_sdk_core::{prelude::*, token}; fn main() { // Define the chain ID, address, decimals, symbol, and name for the token @@ -85,11 +86,6 @@ with, and adjust the CHAIN_ID if you're working on a different network (e.g., a Contributions are welcome! If you find a bug or have suggestions for improvements, feel free to open an issue or submit a pull request on the [GitHub repository](https://github.com/malik672/uniswap-sdk-core-rust). -## Note on `no_std` - -By default, this library does not depend on the standard library (`std`). However, the `std` feature can be enabled to -use `thiserror` for error handling. - ## License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. @@ -103,6 +99,8 @@ provide similar functionality in the Rust programming language. - [Uniswap V3 SDK Rust](https://github.com/shuhuiluo/uniswap-v3-sdk-rs): Opinionated Rust implementation of the Uniswap V3 SDK with a focus on readability and performance +- [Uniswap V2 SDK Rust](https://github.com/shuhuiluo/uniswap-v2-sdk-rs): Opinionated Rust implementation of the Uniswap + V2 SDK with a focus on readability and performance - ... *(If you want to add project to the list, dm or open a PR)* diff --git a/src/entities/fractions/currency_amount.rs b/src/entities/fractions/currency_amount.rs index 95f1b94..64fd42e 100644 --- a/src/entities/fractions/currency_amount.rs +++ b/src/entities/fractions/currency_amount.rs @@ -81,7 +81,7 @@ impl CurrencyAmount { /// Addition of another currency amount to the current amount pub fn add(&self, other: &Self) -> Result { if !self.currency.equals(&other.currency) { - return Err(Error::NotEqual()); + return Err(Error::NotEqual); } let added = self.as_fraction() + other.as_fraction(); Self::from_fractional_amount( @@ -94,7 +94,7 @@ impl CurrencyAmount { /// Subtraction of another currency amount from the current amount pub fn subtract(&self, other: &Self) -> Result { if !self.currency.equals(&other.currency) { - return Err(Error::NotEqual()); + return Err(Error::NotEqual); } let subtracted = self.as_fraction() - other.as_fraction(); Self::from_fractional_amount( @@ -117,7 +117,7 @@ impl CurrencyAmount { /// Convert the currency amount to a string with a fixed number of decimal places pub fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> Result { if decimal_places > self.currency.decimals() { - return Err(Error::NotEqual()); + return Err(Error::NotEqual); } if decimal_places == 0 { diff --git a/src/entities/fractions/fraction.rs b/src/entities/fractions/fraction.rs index a8437c2..31e2d0a 100644 --- a/src/entities/fractions/fraction.rs +++ b/src/entities/fractions/fraction.rs @@ -115,7 +115,7 @@ pub trait FractionBase: Sized { /// strategy fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> Result { if significant_digits == 0 { - return Err(Error::Incorrect()); + return Err(Error::Invalid); } let rounding_strategy = to_rounding_strategy(rounding); let quotient = self.to_decimal().with_precision_round( diff --git a/src/entities/fractions/price.rs b/src/entities/fractions/price.rs index 5eac2bb..5e5d2d9 100644 --- a/src/entities/fractions/price.rs +++ b/src/entities/fractions/price.rs @@ -80,7 +80,7 @@ where other: &Price, ) -> Result, Error> { if !self.quote_currency.equals(&other.base_currency) { - return Err(Error::NotEqual()); + return Err(Error::NotEqual); } let fraction = self.as_fraction() * other.as_fraction(); @@ -98,7 +98,7 @@ where currency_amount: CurrencyAmount, ) -> Result, Error> { if !currency_amount.currency.equals(&self.base_currency) { - return Err(Error::NotEqual()); + return Err(Error::NotEqual); } let fraction = self.as_fraction() * currency_amount.as_fraction(); CurrencyAmount::from_fractional_amount( diff --git a/src/error.rs b/src/error.rs index 633c63a..c5e87b5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,25 +2,25 @@ #[derive(Debug, Clone, Copy)] #[cfg_attr(feature = "std", derive(thiserror::Error))] pub enum Error { - /// Triggers when the compared chain ids do not match - #[cfg_attr(feature = "std", error("Chain IDs do not match: {0} and {1}"))] + /// Triggers when the compared chain IDs do not match. + #[cfg_attr(feature = "std", error("chain IDs do not match: {0} and {1}"))] ChainIdMismatch(u64, u64), - /// Triggers when compared addresses are the same - #[cfg_attr(feature = "std", error("Addresses are equal"))] + /// Triggers when compared addresses are the same. + #[cfg_attr(feature = "std", error("addresses are equal"))] EqualAddresses, - /// Triggers when it tries to exceed the max uint + /// Triggers when it tries to exceed the max uint. #[cfg_attr(feature = "std", error("amount has exceeded MAX_UINT256"))] MaxUint, - ///Triggers when the Compared values are not equal + /// Triggers when the compared values are not equal. #[cfg_attr(feature = "std", error("not equal"))] - NotEqual(), + NotEqual, - /// Triggers when The value is incorrect - #[cfg_attr(feature = "std", error("incorrect"))] - Incorrect(), + /// Triggers when the value is invalid. + #[cfg_attr(feature = "std", error("invalid"))] + Invalid, } #[cfg(all(feature = "std", test))] @@ -31,14 +31,14 @@ mod tests { #[test] fn test_chain_id_mismatch_error() { let error = Error::ChainIdMismatch(1, 2); - assert_eq!(error.to_string(), "Chain IDs do not match: 1 and 2"); + assert_eq!(error.to_string(), "chain IDs do not match: 1 and 2"); } /// Test that `Error::EqualAddresses` displays the correct error message. #[test] fn test_equal_addresses_error() { let error = Error::EqualAddresses; - assert_eq!(error.to_string(), "Addresses are equal"); + assert_eq!(error.to_string(), "addresses are equal"); } /// Test that `Error::MaxUint` displays the correct error message. @@ -51,14 +51,14 @@ mod tests { /// Test that `Error::NotEqual` displays the correct error message. #[test] fn test_not_equal_error() { - let error = Error::NotEqual(); + let error = Error::NotEqual; assert_eq!(error.to_string(), "not equal"); } - /// Test that `Error::Incorrect` displays the correct error message. + /// Test that `Error::Invalid` displays the correct error message. #[test] fn test_incorrect_error() { - let error = Error::Incorrect(); - assert_eq!(error.to_string(), "incorrect"); + let error = Error::Invalid; + assert_eq!(error.to_string(), "invalid"); } } diff --git a/src/utils/sorted_insert.rs b/src/utils/sorted_insert.rs index 999ed16..d3e19ec 100644 --- a/src/utils/sorted_insert.rs +++ b/src/utils/sorted_insert.rs @@ -9,11 +9,11 @@ pub fn sorted_insert( comparator: fn(&T, &T) -> Ordering, ) -> Result, Error> { if max_size == 0 { - return Err(Error::Incorrect()); + return Err(Error::Invalid); } if items.len() > max_size { - return Err(Error::Incorrect()); + return Err(Error::Invalid); } let removed_item = if items.len() == max_size { diff --git a/src/utils/sqrt.rs b/src/utils/sqrt.rs index c7e1e89..c9b2f08 100644 --- a/src/utils/sqrt.rs +++ b/src/utils/sqrt.rs @@ -10,7 +10,7 @@ use num_traits::Signed; /// returns: BigInt pub fn sqrt(value: &BigInt) -> Result { if value.is_negative() { - Err(Error::Incorrect()) + Err(Error::Invalid) } else { Ok(value.sqrt()) }