From 439f0d9214d206c93bc75221c2966af1db7d4e95 Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Tue, 9 Jul 2024 11:16:12 -0700 Subject: [PATCH] refactor: Refactor currency representation and correct minor comment typos Restructured the handling of currencies in the system by consolidating all currency-related functionalities under the `Currency` trait. Eliminated the `CurrencyTrait` and the `Currency` enum, which were previously holding this responsibility across multiple code portions. Also corrected several minor errors in the comments and improved the comment descriptions across multiple files. --- Cargo.toml | 2 +- src/addresses.rs | 2 +- src/constants.rs | 6 +- src/entities/base_currency.rs | 2 +- src/entities/currency.rs | 75 +---------------------- src/entities/ether.rs | 8 +-- src/entities/fractions/currency_amount.rs | 13 ++-- src/entities/fractions/fraction.rs | 1 - src/entities/fractions/mod.rs | 2 +- src/entities/fractions/percent.rs | 1 - src/entities/fractions/price.rs | 16 ++--- src/entities/mod.rs | 6 -- src/entities/token.rs | 8 +-- src/prelude.rs | 2 +- src/utils/compute_price_impact.rs | 2 +- 15 files changed, 34 insertions(+), 112 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index afb28ff..e961828 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uniswap-sdk-core" -version = "0.26.0" +version = "1.0.0-rc" 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/src/addresses.rs b/src/addresses.rs index dfa606d..b067f4f 100644 --- a/src/addresses.rs +++ b/src/addresses.rs @@ -4,7 +4,7 @@ type AddressMap = FxHashMap; type ChainMap = FxHashMap; type ChainAddress = FxHashMap; -/// Represents the addresses of various core contracts of uniswap on a network. +/// Represents the addresses of various core contracts of Uniswap on a network. #[derive(Clone, Copy, Debug)] pub struct ChainAddresses { v3_core_factory_address: Address, diff --git a/src/constants.rs b/src/constants.rs index 2e7cbbc..414f109 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -2,7 +2,7 @@ use crate::prelude::*; use alloy_primitives::U256; use num_bigint::Sign; -/// Represents the various type of trades. +/// Represents the various types of trades. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum TradeType { /// Indicates that the trade is based on an exact input amount. @@ -12,7 +12,7 @@ pub enum TradeType { ExactOutput, } -/// Represents three various way to rounds +/// Represents three various ways to round #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Rounding { /// Rounds down to the nearest whole number. @@ -26,7 +26,7 @@ pub enum Rounding { } lazy_static! { - ///Represnts Maximum amount contained in a uint256 + /// Represents the maximum amount contained in a uint256 pub static ref MAX_UINT256: BigInt = BigInt::from_bytes_be(Sign::Plus, &U256::MAX.to_be_bytes::<32>()); } diff --git a/src/entities/base_currency.rs b/src/entities/base_currency.rs index dbda4a8..e1ff168 100644 --- a/src/entities/base_currency.rs +++ b/src/entities/base_currency.rs @@ -8,7 +8,7 @@ pub struct CurrencyLike { /// The chain ID on which this currency resides pub chain_id: ChainId, - /// represents the deciamls for the prticular currency + /// The decimals for the particular currency pub decimals: u8, /// The symbol of the currency, i.e. a short textual non-unique identifier diff --git a/src/entities/currency.rs b/src/entities/currency.rs index 7758941..2bbe426 100644 --- a/src/entities/currency.rs +++ b/src/entities/currency.rs @@ -1,16 +1,7 @@ use crate::prelude::*; -/// This enum represnets the two type of currencies i.e native and Token -#[derive(Clone, PartialEq, Debug)] -pub enum Currency { - /// Represents a native currency. - NativeCurrency(Ether), - /// Represents a token. - Token(Token), -} - /// Trait for representing a currency in the Uniswap Core SDK. -pub trait CurrencyTrait: BaseCurrency { +pub trait Currency: BaseCurrency { /// Returns whether the currency is native to the chain and must be wrapped (e.g. Ether) fn is_native(&self) -> bool; @@ -18,72 +9,12 @@ pub trait CurrencyTrait: BaseCurrency { fn address(&self) -> Address; /// Returns whether this currency is functionally equivalent to the other currency - fn equals(&self, other: &impl CurrencyTrait) -> bool; + fn equals(&self, other: &impl Currency) -> bool; - /// Returns a Token that represents the wrapped quivalent of the mative currency + /// Returns a Token that represents the wrapped equivalent of the native currency fn wrapped(&self) -> Token; } -impl CurrencyTrait for Currency { - fn is_native(&self) -> bool { - match self { - Currency::NativeCurrency(_) => true, - Currency::Token(_) => false, - } - } - - fn address(&self) -> Address { - match self { - Currency::NativeCurrency(native_currency) => native_currency.address(), - Currency::Token(token) => token.address(), - } - } - - fn equals(&self, other: &impl CurrencyTrait) -> bool { - match self { - Currency::NativeCurrency(native_currency) => native_currency.equals(other), - Currency::Token(token) => token.equals(other), - } - } - - fn wrapped(&self) -> Token { - match self { - Currency::NativeCurrency(native_currency) => native_currency.wrapped(), - Currency::Token(token) => token.clone(), - } - } -} - -impl BaseCurrency for Currency { - fn chain_id(&self) -> u64 { - match self { - Currency::NativeCurrency(native_currency) => native_currency.chain_id(), - Currency::Token(token) => token.chain_id(), - } - } - - fn decimals(&self) -> u8 { - match self { - Currency::NativeCurrency(native_currency) => native_currency.decimals(), - Currency::Token(token) => token.decimals(), - } - } - - fn symbol(&self) -> Option { - match self { - Currency::NativeCurrency(native_currency) => native_currency.symbol(), - Currency::Token(token) => token.symbol(), - } - } - - fn name(&self) -> Option { - match self { - Currency::NativeCurrency(native_currency) => native_currency.name(), - Currency::Token(token) => token.name(), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/entities/ether.rs b/src/entities/ether.rs index 745035c..f16764a 100644 --- a/src/entities/ether.rs +++ b/src/entities/ether.rs @@ -4,7 +4,7 @@ use crate::prelude::*; /// Represents the native currency of the blockchain. pub type Ether = CurrencyLike<()>; -impl CurrencyTrait for Ether { +impl Currency for Ether { /// Checks if the currency is native to the blockchain. fn is_native(&self) -> bool { true @@ -16,7 +16,7 @@ impl CurrencyTrait for Ether { } /// Checks if the currency is equal to another currency. - fn equals(&self, other: &impl CurrencyTrait) -> bool { + fn equals(&self, other: &impl Currency) -> bool { match other.is_native() { true => self.chain_id() == other.chain_id(), _ => false, @@ -66,11 +66,11 @@ mod tests { #[test] fn test_equals_returns_false_for_different_chains() { - assert!(!Ether::on_chain(1).equals(&Currency::NativeCurrency(Ether::on_chain(2)))); + assert!(!Ether::on_chain(1).equals(&Ether::on_chain(2))); } #[test] fn test_equals_returns_true_for_same_chains() { - assert!(Ether::on_chain(1).equals(&Currency::NativeCurrency(Ether::on_chain(1)))); + assert!(Ether::on_chain(1).equals(&Ether::on_chain(1))); } } diff --git a/src/entities/fractions/currency_amount.rs b/src/entities/fractions/currency_amount.rs index 0bd7c5a..95f1b94 100644 --- a/src/entities/fractions/currency_amount.rs +++ b/src/entities/fractions/currency_amount.rs @@ -6,14 +6,14 @@ pub type CurrencyAmount = FractionLike>; /// Struct representing metadata about a currency #[derive(Clone, Debug, PartialEq)] -pub struct CurrencyMeta { +pub struct CurrencyMeta { /// The currency associated with this metadata pub currency: T, /// The scale factor for the currency's decimal places pub decimal_scale: BigUint, } -impl CurrencyAmount { +impl CurrencyAmount { /// Constructor method for creating a new currency amount fn new( currency: T, @@ -151,8 +151,8 @@ mod tests { // Lazy static variables for testing currencies lazy_static! { - static ref TOKEN18: Currency = Currency::Token(token!(1, ADDRESS_ONE, 18)); - static ref TOKEN0: Currency = Currency::Token(token!(1, ADDRESS_ONE, 0)); + static ref TOKEN18: Token = token!(1, ADDRESS_ONE, 18); + static ref TOKEN0: Token = token!(1, ADDRESS_ONE, 0); } // Unit tests @@ -174,10 +174,9 @@ mod tests { #[test] fn test_ether() { let ether = Ether::on_chain(1); - let amount = - CurrencyAmount::from_raw_amount(Currency::NativeCurrency(ether.clone()), 100).unwrap(); + let amount = CurrencyAmount::from_raw_amount(ether.clone(), 100).unwrap(); assert_eq!(amount.quotient(), 100.into()); - assert!(amount.currency.equals(&Currency::NativeCurrency(ether))); + assert!(amount.currency.equals(ðer)); } #[test] diff --git a/src/entities/fractions/fraction.rs b/src/entities/fractions/fraction.rs index 535fe4b..a8437c2 100644 --- a/src/entities/fractions/fraction.rs +++ b/src/entities/fractions/fraction.rs @@ -1,4 +1,3 @@ -/// External crate dependencies use crate::prelude::*; use core::ops::{Add, Deref, Mul, Sub}; diff --git a/src/entities/fractions/mod.rs b/src/entities/fractions/mod.rs index c781e4b..57485da 100644 --- a/src/entities/fractions/mod.rs +++ b/src/entities/fractions/mod.rs @@ -4,6 +4,6 @@ pub mod currency_amount; pub mod fraction; /// A module represents a percentage. pub mod percent; -/// A module represents' a price as a ratio between two currencies, with methods for arithmetic and +/// A module represents a price as a ratio between two currencies, with methods for arithmetic and /// string conversions. pub mod price; diff --git a/src/entities/fractions/percent.rs b/src/entities/fractions/percent.rs index b11baf6..1e08dbb 100644 --- a/src/entities/fractions/percent.rs +++ b/src/entities/fractions/percent.rs @@ -1,4 +1,3 @@ -/// Importing dependencies from the same module use crate::prelude::*; lazy_static! { diff --git a/src/entities/fractions/price.rs b/src/entities/fractions/price.rs index 5fa7976..5eac2bb 100644 --- a/src/entities/fractions/price.rs +++ b/src/entities/fractions/price.rs @@ -8,8 +8,8 @@ pub type Price = FractionLike>; #[derive(Clone, Debug, PartialEq)] pub struct PriceMeta where - TBase: CurrencyTrait, - TQuote: CurrencyTrait, + TBase: Currency, + TQuote: Currency, { /// The base currency for the price pub base_currency: TBase, @@ -23,8 +23,8 @@ where impl Price where - TBase: CurrencyTrait, - TQuote: CurrencyTrait, + TBase: Currency, + TQuote: Currency, { /// Constructor for creating a new [`Price`] instance pub fn new( @@ -75,7 +75,7 @@ where /// Multiply the price by another price, returning a new price. /// The other price must have the same base currency as this price's quote currency. - pub fn multiply( + pub fn multiply( &self, other: &Price, ) -> Result, Error> { @@ -141,9 +141,9 @@ mod test { const ADDRESS_ONE: &str = "0x0000000000000000000000000000000000000001"; lazy_static! { - static ref TOKEN0: Currency = Currency::Token(token!(1, ADDRESS_ZERO, 18)); - static ref TOKEN0_6: Currency = Currency::Token(token!(1, ADDRESS_ZERO, 6)); - static ref TOKEN1: Currency = Currency::Token(token!(1, ADDRESS_ONE, 18)); + static ref TOKEN0: Token = token!(1, ADDRESS_ZERO, 18); + static ref TOKEN0_6: Token = token!(1, ADDRESS_ZERO, 6); + static ref TOKEN1: Token = token!(1, ADDRESS_ONE, 18); } #[test] diff --git a/src/entities/mod.rs b/src/entities/mod.rs index 30df1ff..fbd5c8a 100644 --- a/src/entities/mod.rs +++ b/src/entities/mod.rs @@ -1,12 +1,6 @@ -/// Module represnting base currency pub mod base_currency; -/// Module representing currency pub mod currency; -/// Module representing ether pub mod ether; -/// Module represnting fractions pub mod fractions; -/// Module represnting token pub mod token; -/// Module representing Weth9 pub mod weth9; diff --git a/src/entities/token.rs b/src/entities/token.rs index 7ff6843..016da7d 100644 --- a/src/entities/token.rs +++ b/src/entities/token.rs @@ -14,7 +14,7 @@ pub struct TokenMeta { pub sell_fee_bps: Option, } -impl CurrencyTrait for Token { +impl Currency for Token { fn is_native(&self) -> bool { false } @@ -27,10 +27,10 @@ impl CurrencyTrait for Token { /// /// # Arguments /// - /// * `other`: other token to compare + /// * `other`: another token to compare /// /// returns: bool - fn equals(&self, other: &impl CurrencyTrait) -> bool { + fn equals(&self, other: &impl Currency) -> bool { match other.is_native() { false => self.chain_id == other.chain_id() && self.address() == other.address(), _ => false, @@ -93,7 +93,7 @@ impl Token { /// /// # Arguments /// - /// * `other`: other token to compare + /// * `other`: another token to compare pub fn sorts_before(&self, other: &Token) -> Result { if self.chain_id != other.chain_id { return Err(Error::ChainIdMismatch(self.chain_id, other.chain_id)); diff --git a/src/prelude.rs b/src/prelude.rs index c687998..6eb4a72 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -3,7 +3,7 @@ pub use crate::{ constants::*, entities::{ base_currency::{BaseCurrency, CurrencyLike}, - currency::{Currency, CurrencyTrait}, + currency::Currency, ether::Ether, fractions::{ currency_amount::CurrencyAmount, diff --git a/src/utils/compute_price_impact.rs b/src/utils/compute_price_impact.rs index d7ba360..bdb4f6a 100644 --- a/src/utils/compute_price_impact.rs +++ b/src/utils/compute_price_impact.rs @@ -9,7 +9,7 @@ use crate::prelude::*; /// * `outputAmount`: the output amount of the trade /// /// returns: Percent -pub fn compute_price_impact( +pub fn compute_price_impact( mid_price: Price, input_amount: CurrencyAmount, output_amount: CurrencyAmount,