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

restrict types #13

Merged
merged 2 commits into from
Dec 25, 2023
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
818 changes: 816 additions & 2 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[package]
name = "uniswap-sdk-core-rust"
version = "0.2.0"
version = "0.3.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
alloy-primitives = "0.5.3"
eth_checksum = "0.1.2"
lazy_static = "1.4.0"
num-bigint = "0.4.4"
Expand Down
2 changes: 1 addition & 1 deletion src/entities/base_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub trait BaseCurrency: Clone {
fn chain_id(&self) -> u32;

/// The decimals used in representing currency amounts
fn decimals(&self) -> u32;
fn decimals(&self) -> u8;

/// The symbol of the currency, i.e. a short textual non-unique identifier
fn symbol(&self) -> Option<String>;
Expand Down
7 changes: 4 additions & 3 deletions src/entities/currency.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{base_currency::BaseCurrency, ether::Ether, token::Token};
use alloy_primitives::Address;

#[derive(Clone, PartialEq)]
pub enum Currency {
Expand All @@ -10,7 +11,7 @@ pub trait CurrencyTrait: BaseCurrency {
/// Returns whether the currency is native to the chain and must be wrapped (e.g. Ether)
fn is_native(&self) -> bool;

fn address(&self) -> String;
fn address(&self) -> Address;
}

impl CurrencyTrait for Currency {
Expand All @@ -21,7 +22,7 @@ impl CurrencyTrait for Currency {
}
}

fn address(&self) -> String {
fn address(&self) -> Address {
match self {
Currency::NativeCurrency(native_currency) => native_currency.address(),
Currency::Token(token) => token.address(),
Expand All @@ -37,7 +38,7 @@ impl BaseCurrency for Currency {
}
}

fn decimals(&self) -> u32 {
fn decimals(&self) -> u8 {
match self {
Currency::NativeCurrency(native_currency) => native_currency.decimals(),
Currency::Token(token) => token.decimals(),
Expand Down
10 changes: 5 additions & 5 deletions src/entities/ether.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{base_currency::BaseCurrency, currency::CurrencyTrait, token::Token};
use crate::entities::weth9::WETH9;
use super::{base_currency::BaseCurrency, currency::CurrencyTrait, token::Token, weth9::WETH9};
use alloy_primitives::Address;
use lazy_static::lazy_static;
use std::{collections::HashMap, sync::Mutex};

Expand All @@ -11,7 +11,7 @@ lazy_static! {
#[derive(Clone, PartialEq)]
pub struct Ether {
pub chain_id: u32,
pub decimals: u32,
pub decimals: u8,
pub symbol: Option<String>,
pub name: Option<String>,
}
Expand All @@ -21,7 +21,7 @@ impl CurrencyTrait for Ether {
true
}

fn address(&self) -> String {
fn address(&self) -> Address {
self.wrapped().address()
}
}
Expand Down Expand Up @@ -54,7 +54,7 @@ impl BaseCurrency for Ether {
self.chain_id
}

fn decimals(&self) -> u32 {
fn decimals(&self) -> u8 {
self.decimals
}

Expand Down
6 changes: 3 additions & 3 deletions src/entities/fractions/currency_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<T: CurrencyTrait> CurrencyAmount<T> {
denominator,
meta: CurrencyMeta {
currency,
decimal_scale: BigUint::from(10u64).pow(exponent),
decimal_scale: BigUint::from(10u64).pow(exponent as u32),
},
}
}
Expand Down Expand Up @@ -156,7 +156,7 @@ impl<T: CurrencyTrait> FractionTrait<CurrencyMeta<T>> for CurrencyAmount<T> {
)
}

fn to_significant(&self, significant_digits: u32, rounding: Rounding) -> String {
fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> String {
self.as_fraction()
.divide(&Fraction::new(
self.meta.decimal_scale.to_bigint().unwrap(),
Expand All @@ -166,7 +166,7 @@ impl<T: CurrencyTrait> FractionTrait<CurrencyMeta<T>> for CurrencyAmount<T> {
.to_significant(significant_digits, rounding)
}

fn to_fixed(&self, decimal_places: u32, rounding: Rounding) -> String {
fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String {
assert!(decimal_places <= self.meta.currency.decimals(), "DECIMALS");
self.as_fraction()
.divide(&Fraction::new(
Expand Down
8 changes: 4 additions & 4 deletions src/entities/fractions/fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub trait FractionTrait<M>: Sized {
.div(Decimal::from_str(&self.denominator().to_str_radix(10)).unwrap())
}

fn to_significant(&self, significant_digits: u32, rounding: Rounding) -> String {
fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> String {
assert!(
significant_digits > 0,
"Significant digits must be positive."
Expand All @@ -125,16 +125,16 @@ pub trait FractionTrait<M>: Sized {
let rounding_strategy = to_rounding_strategy(rounding);
let quotient = self
.to_decimal()
.round_sf_with_strategy(significant_digits, rounding_strategy);
.round_sf_with_strategy(significant_digits as u32, rounding_strategy);

quotient.unwrap().normalize().to_string()
}

fn to_fixed(&self, decimal_places: u32, rounding: Rounding) -> String {
fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String {
let rounding_strategy = to_rounding_strategy(rounding);
let quotient = self
.to_decimal()
.round_dp_with_strategy(decimal_places, rounding_strategy);
.round_dp_with_strategy(decimal_places as u32, rounding_strategy);

format!("{:.1$}", quotient, decimal_places as usize)
}
Expand Down
4 changes: 2 additions & 2 deletions src/entities/fractions/percent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ impl FractionTrait<()> for Percent {
&self.denominator
}

fn to_significant(&self, significant_digits: u32, rounding: Rounding) -> String {
fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> String {
self.as_fraction()
.multiply(&ONE_HUNDRED)
.to_significant(significant_digits, rounding)
}

fn to_fixed(&self, decimal_places: u32, rounding: Rounding) -> String {
fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String {
self.as_fraction()
.multiply(&ONE_HUNDRED)
.to_fixed(decimal_places, rounding)
Expand Down
8 changes: 4 additions & 4 deletions src/entities/fractions/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ where
&self.denominator
}

fn to_significant(&self, significant_digits: u32, rounding: Rounding) -> String {
fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> String {
self.adjusted_for_decimals()
.to_significant(significant_digits, rounding)
}

fn to_fixed(&self, decimal_places: u32, rounding: Rounding) -> String {
fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String {
self.adjusted_for_decimals()
.to_fixed(decimal_places, rounding)
}
Expand All @@ -84,8 +84,8 @@ where
numerator: impl Into<BigInt>,
) -> Self {
let scalar = Fraction::new(
BigInt::from(10).pow(base_currency.decimals()),
BigInt::from(10).pow(quote_currency.decimals()),
BigInt::from(10).pow(base_currency.decimals() as u32),
BigInt::from(10).pow(quote_currency.decimals() as u32),
(),
);
Self {
Expand Down
Loading