Skip to content

Commit

Permalink
Merge pull request #59 from malik672/fix
Browse files Browse the repository at this point in the history
fix: Enhance various checks and clean up code
  • Loading branch information
malik672 authored Mar 17, 2024
2 parents f72e4c2 + cd320fa commit 4fabfa8
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 13 deletions.
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.20.0"
version = "0.20.1"
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn main() {
}
```

This example demonstrates how to create a Token instance for DAI on the Ethereum Mainnet using the token! macro.
This example demonstrates how to create a `Token` instance for DAI on the Ethereum Mainnet using the `token!` macro.

It then prints the token's address and checks if it's a native token (which it isn't, so it prints false).

Expand Down
3 changes: 1 addition & 2 deletions src/entities/fractions/currency_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,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.currency.decimals() {
if decimal_places > self.currency.decimals() {
return Err(Error::NotEqual());
}

Expand Down Expand Up @@ -204,7 +204,6 @@ mod tests {
}

#[test]
#[should_panic(expected = "DECIMALS")]
fn to_fixed_decimals_exceeds_currency_decimals() {
let amount = CurrencyAmount::from_raw_amount(TOKEN0.clone(), 1000).unwrap();
let _w = amount.to_fixed(3, Rounding::RoundDown);
Expand Down
8 changes: 3 additions & 5 deletions src/entities/fractions/fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,9 @@ pub trait FractionBase<M>: Sized {
/// strategy
fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String {
let rounding_strategy = to_rounding_strategy(rounding);
let quotient = self
.to_decimal()
.with_scale_round(decimal_places as i64, rounding_strategy);

format!("{:.1$}", quotient, decimal_places as usize)
self.to_decimal()
.with_scale_round(decimal_places as i64, rounding_strategy)
.to_string()
}

/// Helper method for converting any superclass back to a simple [`Fraction`]
Expand Down
7 changes: 3 additions & 4 deletions src/utils/sqrt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::prelude::*;
use num_traits::Signed;

/// Computes floor(sqrt(value))
///
Expand All @@ -7,10 +8,8 @@ use crate::prelude::*;
/// * `value`: the value for which to compute the square root, rounded down
///
/// returns: BigInt
#[allow(clippy::assigning_clones)]
pub fn sqrt(value: &BigInt) -> Result<BigInt, Error> {
if !value >= Zero::zero() {
if value.is_negative() {
return Err(Error::Incorrect());
}

Expand All @@ -29,7 +28,7 @@ pub fn sqrt(value: &BigInt) -> Result<BigInt, Error> {
let mut x = (value / &two) + &one;

while x < z {
z = x.clone();
z.clone_from(&x);
x = ((value / &x) + &x) / &two;
}

Expand Down

0 comments on commit 4fabfa8

Please sign in to comment.