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

chore: Update error variants and improve documentation #76

Merged
merged 1 commit into from
Aug 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-sdk-core"
version = "1.0.0-rc"
version = "1.0.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"
Expand Down
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
**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

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:
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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)*
6 changes: 3 additions & 3 deletions src/entities/fractions/currency_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<T: Currency> CurrencyAmount<T> {
/// Addition of another currency amount to the current amount
pub fn add(&self, other: &Self) -> Result<Self, Error> {
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(
Expand All @@ -94,7 +94,7 @@ impl<T: Currency> CurrencyAmount<T> {
/// Subtraction of another currency amount from the current amount
pub fn subtract(&self, other: &Self) -> Result<Self, Error> {
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(
Expand All @@ -117,7 +117,7 @@ impl<T: Currency> CurrencyAmount<T> {
/// 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<String, Error> {
if decimal_places > self.currency.decimals() {
return Err(Error::NotEqual());
return Err(Error::NotEqual);
}

if decimal_places == 0 {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/fractions/fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub trait FractionBase<M>: Sized {
/// strategy
fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> Result<String, Error> {
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(
Expand Down
4 changes: 2 additions & 2 deletions src/entities/fractions/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ where
other: &Price<TQuote, TOtherQuote>,
) -> Result<Price<TBase, TOtherQuote>, 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();
Expand All @@ -98,7 +98,7 @@ where
currency_amount: CurrencyAmount<TBase>,
) -> Result<CurrencyAmount<TQuote>, 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(
Expand Down
32 changes: 16 additions & 16 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand All @@ -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.
Expand All @@ -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");
}
}
4 changes: 2 additions & 2 deletions src/utils/sorted_insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pub fn sorted_insert<T: Clone>(
comparator: fn(&T, &T) -> Ordering,
) -> Result<Option<T>, 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 {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/sqrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use num_traits::Signed;
/// returns: BigInt
pub fn sqrt(value: &BigInt) -> Result<BigInt, Error> {
if value.is_negative() {
Err(Error::Incorrect())
Err(Error::Invalid)
} else {
Ok(value.sqrt())
}
Expand Down