Skip to content

Commit

Permalink
Merge pull request #38 from malik672/performance
Browse files Browse the repository at this point in the history
Performance
  • Loading branch information
malik672 authored Feb 7, 2024
2 parents aa85078 + bb73474 commit 0bf2853
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 = "0.12.5"
version = "0.13.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
2 changes: 1 addition & 1 deletion src/addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn construct_same_address_map(address: Address, additional_networks: &[Chain
}

lazy_static! {
#[derive(Copy, Clone)]
#[derive(Debug, Clone, Copy)]
pub static ref UNI_ADDRESSES: AddressMap = construct_same_address_map(
address!("1f9840a85d5aF5bf1D1762F925BDADdC4201F984"),
&[
Expand Down
2 changes: 1 addition & 1 deletion src/chains.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Copy, Clone)]
#[derive(Debug, Clone, Copy)]
pub enum ChainId {
MAINNET = 1,
GOERLI = 5,
Expand Down
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub enum TradeType {
ExactOutput,
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub enum Rounding {
RoundDown,
RoundHalfUp,
Expand Down
10 changes: 3 additions & 7 deletions src/entities/fractions/currency_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<T: CurrencyTrait> CurrencyAmount<T> {
// Addition of another currency amount to the current amount
pub fn add(&self, other: &Self) -> Result<Self, Error> {
if !self.meta.currency.equals(&other.meta.currency) {
return Err(Error::NotEqual("CURRENCY: not equal".to_owned()));
return Err(Error::NotEqual());
}
let added = self.as_fraction() + other.as_fraction();
Self::from_fractional_amount(
Expand All @@ -95,7 +95,7 @@ impl<T: CurrencyTrait> CurrencyAmount<T> {
// Subtraction of another currency amount from the current amount
pub fn subtract(&self, other: &Self) -> Result<Self, Error> {
if !self.meta.currency.equals(&other.meta.currency) {
return Err(Error::NotEqual("CURRENCY: not equal".to_owned()));
return Err(Error::NotEqual());
}
let subtracted = self.as_fraction() - other.as_fraction();
Self::from_fractional_amount(
Expand All @@ -118,11 +118,7 @@ impl<T: CurrencyTrait> 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.meta.currency.decimals() {
return Err(Error::NotEqual(format!(
"{} should be less than or equal to {}",
decimal_places,
self.meta.currency.decimals()
)));
return Err(Error::NotEqual());
}

Ok(
Expand Down
4 changes: 1 addition & 3 deletions src/entities/fractions/fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ pub trait FractionBase<M>: Sized {
// Converts the fraction to a string with a specified number of significant digits and rounding strategy
fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> Result<String, Error> {
if significant_digits == 0 {
return Err(Error::Incorrect(
"significant dgits should always be greater than zero".to_string(),
));
return Err(Error::Incorrect());
}
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 @@ -76,7 +76,7 @@ where
other: &Price<TQuote, TOtherQuote>,
) -> Result<Price<TBase, TOtherQuote>, Error> {
if !self.meta.quote_currency.equals(&other.meta.base_currency) {
return Err(Error::NotEqual("the comparison are not equal".to_owned()));
return Err(Error::NotEqual());
}

let fraction = self.as_fraction() * other.as_fraction();
Expand All @@ -98,7 +98,7 @@ where
.currency
.equals(&self.meta.base_currency)
{
return Err(Error::NotEqual("the comparison are not equal".to_owned()));
return Err(Error::NotEqual());
}
let fraction = self.as_fraction() * currency_amount.as_fraction();
CurrencyAmount::from_fractional_amount(
Expand Down
8 changes: 4 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ pub enum Error {
#[error("amount has exceeded MAX_UINT256")]
MaxUint,

#[error("not equal: {0}")]
NotEqual(String),
#[error("not equal")]
NotEqual(),

//Custo
#[error("incorrect: {0}")]
Incorrect(String),
#[error("incorrect")]
Incorrect(),
}
12 changes: 4 additions & 8 deletions src/utils/sorted_insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +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(
"max_size can't be equal to zero".to_string(),
));
return Err(Error::Incorrect());
}

if items.len() > max_size {
return Err(Error::Incorrect(
"array length cannot exceed max_size".to_string(),
));
return Err(Error::Incorrect());
}

let removed_item = if items.len() == max_size {
Expand Down Expand Up @@ -51,14 +47,14 @@ mod tests {
}

#[test]
#[should_panic(expected = "max_size can't be equal to zero")]
#[should_panic]
fn test_max_size_zero() {
let mut arr = Vec::new();
sorted_insert(&mut arr, 1, 0, cmp).unwrap();
}

#[test]
#[should_panic(expected = "array length cannot exceed max_size")]
#[should_panic]
fn test_length_greater_than_max_size() {
let mut arr = vec![1, 2];
let _w = sorted_insert(&mut arr, 1, 1, cmp).unwrap();
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 crate::prelude::*;
///
pub fn sqrt(value: &BigInt) -> Result<BigInt, Error> {
if !value >= Zero::zero() {
return Err(Error::Incorrect("value cannot be negative".to_string()));
return Err(Error::Incorrect());
}

// If the value is less than or equal to MAX_SAFE_INTEGER,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/validate_and_parse_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use regex::Regex;
///
/// * If the input string satisfies the condition of starting with `0x` and being 42 characters long with only hexadecimal characters after `0x`, returns `Ok(ethereum_address.to_string())`.
/// * Otherwise, returns an error message in the form of `Err(format!("{} is not a valid Ethereum address.", ethereum_address))`.
pub fn check_valid_ethereum_address(ethereum_address: &str) -> Result<String, String> {
pub fn check_valid_ethereum_address(ethereum_address: &str) -> Result<&str, String> {
let valid_address_regex = Regex::new(r"^0x[0-9a-fA-F]{40}$").unwrap();
if valid_address_regex.is_match(ethereum_address) {
Ok(ethereum_address.to_string())
Ok(ethereum_address)
} else {
Err(format!(
"{} is not a valid Ethereum address.",
Expand Down

0 comments on commit 0bf2853

Please sign in to comment.