diff --git a/src/entities/base_currency.rs b/src/entities/base_currency.rs index f5efa01..84b21a5 100644 --- a/src/entities/base_currency.rs +++ b/src/entities/base_currency.rs @@ -38,18 +38,22 @@ pub trait BaseCurrency: Clone { } impl BaseCurrency for CurrencyLike { + #[inline] fn chain_id(&self) -> ChainId { self.chain_id } + #[inline] fn decimals(&self) -> u8 { self.decimals } + #[inline] fn symbol(&self) -> Option<&String> { self.symbol.as_ref() } + #[inline] fn name(&self) -> Option<&String> { self.name.as_ref() } @@ -59,6 +63,7 @@ impl BaseCurrency for CurrencyLike { impl Deref for CurrencyLike { type Target = M; + #[inline] fn deref(&self) -> &Self::Target { &self.meta } diff --git a/src/entities/ether.rs b/src/entities/ether.rs index f16764a..65eda07 100644 --- a/src/entities/ether.rs +++ b/src/entities/ether.rs @@ -6,16 +6,19 @@ pub type Ether = CurrencyLike<()>; impl Currency for Ether { /// Checks if the currency is native to the blockchain. + #[inline] fn is_native(&self) -> bool { true } /// Retrieves the address associated with the currency. + #[inline] fn address(&self) -> Address { self.wrapped().address() } /// Checks if the currency is equal to another currency. + #[inline] fn equals(&self, other: &impl Currency) -> bool { match other.is_native() { true => self.chain_id() == other.chain_id(), @@ -24,6 +27,7 @@ impl Currency for Ether { } /// Returns the wrapped token representation of the currency. + #[inline] fn wrapped(&self) -> Token { match WETH9::default().get(self.chain_id()) { Some(weth9) => weth9.clone(), @@ -34,6 +38,7 @@ impl Currency for Ether { impl Ether { /// Creates a new instance of [`Ether`] with the specified chain ID. + #[inline] pub fn new(chain_id: u64) -> Self { Self { chain_id, @@ -45,6 +50,7 @@ impl Ether { } /// Retrieves or creates an [`Ether`] instance for the specified chain ID. + #[inline] pub fn on_chain(chain_id: u64) -> Self { Self::new(chain_id) } diff --git a/src/entities/fractions/currency_amount.rs b/src/entities/fractions/currency_amount.rs index b2eff80..74ab040 100644 --- a/src/entities/fractions/currency_amount.rs +++ b/src/entities/fractions/currency_amount.rs @@ -15,6 +15,7 @@ pub struct CurrencyMeta { impl CurrencyAmount { /// Constructor method for creating a new currency amount + #[inline] fn new( currency: T, numerator: impl Into, @@ -38,11 +39,13 @@ impl CurrencyAmount { } /// Returns a new currency amount instance from the unitless amount of token (raw amount) + #[inline] pub fn from_raw_amount(currency: T, raw_amount: impl Into) -> Result { Self::new(currency, raw_amount, 1) } /// Construct a currency amount with a denominator that is not equal to 0 + #[inline] pub fn from_fractional_amount( currency: T, numerator: impl Into, @@ -52,6 +55,7 @@ impl CurrencyAmount { } /// Multiplication of currency amount by another fractional amount + #[inline] pub fn multiply(&self, other: &impl FractionBase) -> Result { let multiplied = self.as_fraction() * other.as_fraction(); Self::from_fractional_amount( @@ -62,6 +66,7 @@ impl CurrencyAmount { } /// Division of currency amount by another fractional amount + #[inline] pub fn divide(&self, other: &impl FractionBase) -> Result { let divided = self.as_fraction() / other.as_fraction(); Self::from_fractional_amount( @@ -72,6 +77,7 @@ impl CurrencyAmount { } /// Convert the currency amount to a string with exact precision + #[inline] pub fn to_exact(&self) -> String { BigDecimal::from(self.quotient()) .div(BigDecimal::from(BigInt::from(self.decimal_scale.clone()))) @@ -79,6 +85,7 @@ impl CurrencyAmount { } /// Addition of another currency amount to the current amount + #[inline] pub fn add(&self, other: &Self) -> Result { if !self.currency.equals(&other.currency) { return Err(Error::NotEqual); @@ -88,6 +95,7 @@ impl CurrencyAmount { } /// Subtraction of another currency amount from the current amount + #[inline] pub fn subtract(&self, other: &Self) -> Result { if !self.currency.equals(&other.currency) { return Err(Error::NotEqual); @@ -101,6 +109,7 @@ impl CurrencyAmount { } /// Convert the currency amount to a string with a specified number of significant digits + #[inline] pub fn to_significant( &self, significant_digits: u8, @@ -111,6 +120,7 @@ impl CurrencyAmount { } /// Convert the currency amount to a string with a fixed number of decimal places + #[inline] pub fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> Result { if decimal_places > self.currency.decimals() { return Err(Error::NotEqual); @@ -128,6 +138,7 @@ impl CurrencyAmount { } /// Wrap the currency amount if the currency is not native + #[inline] pub fn wrapped(&self) -> Result, Error> { CurrencyAmount::from_fractional_amount( self.currency.wrapped(), diff --git a/src/entities/fractions/fraction.rs b/src/entities/fractions/fraction.rs index 5b1dce4..0a1f809 100644 --- a/src/entities/fractions/fraction.rs +++ b/src/entities/fractions/fraction.rs @@ -11,6 +11,7 @@ pub struct FractionLike { } impl Default for FractionLike { + #[inline] fn default() -> Self { Self { numerator: BigInt::from(0), @@ -24,6 +25,7 @@ impl Default for FractionLike { impl Deref for FractionLike { type Target = M; + #[inline] fn deref(&self) -> &Self::Target { &self.meta } @@ -47,12 +49,14 @@ impl Fraction { /// # Panics /// /// This function will panic if the denominator is zero. + #[inline] pub fn new(numerator: impl Into, denominator: impl Into) -> Self { FractionBase::new(numerator, denominator, ()) } } /// Function to convert the custom Rounding enum to [`bigdecimal`]'s [`RoundingMode`] +#[inline] const fn to_rounding_strategy(rounding: Rounding) -> RoundingMode { match rounding { Rounding::RoundDown => RoundingMode::Down, @@ -88,11 +92,13 @@ pub trait FractionBase: Sized { fn denominator(&self) -> &BigInt; /// Returns the floor division quotient of the fraction + #[inline] fn quotient(&self) -> BigInt { self.numerator().div_floor(self.denominator()) } /// Returns the remainder after floor division as a new fraction + #[inline] fn remainder(&self) -> Self { Self::new( self.numerator() % self.denominator(), @@ -102,6 +108,7 @@ pub trait FractionBase: Sized { } /// Returns the inverted fraction + #[inline] fn invert(&self) -> Self { Self::new( self.denominator().clone(), @@ -111,12 +118,14 @@ pub trait FractionBase: Sized { } /// Converts the fraction to a [`BigDecimal`] + #[inline] fn to_decimal(&self) -> BigDecimal { BigDecimal::from(self.numerator().clone()).div(BigDecimal::from(self.denominator().clone())) } /// Converts the fraction to a string with a specified number of significant digits and rounding /// strategy + #[inline] fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> Result { if significant_digits == 0 { return Err(Error::Invalid); @@ -132,6 +141,7 @@ pub trait FractionBase: Sized { /// Converts the fraction to a string with a fixed number of decimal places and rounding /// strategy + #[inline] fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String { let rounding_strategy = to_rounding_strategy(rounding); self.to_decimal() @@ -140,6 +150,7 @@ pub trait FractionBase: Sized { } /// Helper method for converting any superclass back to a simple [`Fraction`] + #[inline] fn as_fraction(&self) -> Fraction { Fraction::new(self.numerator().clone(), self.denominator().clone()) } @@ -149,6 +160,7 @@ impl FractionTrait for FractionLike {} impl FractionBase for FractionLike { /// Constructor for creating a new [`FractionLike`] with metadata + #[inline] fn new(numerator: impl Into, denominator: impl Into, meta: M) -> Self { let denominator = denominator.into(); // Ensure the denominator is not zero @@ -163,16 +175,19 @@ impl FractionBase for FractionLike { } /// Accessor method for retrieving metadata + #[inline] fn meta(&self) -> &M { &self.meta } /// Accessor method for retrieving the numerator + #[inline] fn numerator(&self) -> &BigInt { &self.numerator } /// Accessor method for retrieving the denominator + #[inline] fn denominator(&self) -> &BigInt { &self.denominator } @@ -183,6 +198,7 @@ where M: Clone + PartialEq, { /// Checks if the current fraction is equal to another fraction + #[inline] fn eq(&self, other: &Self) -> bool { self.numerator() * other.denominator() == other.numerator() * self.denominator() && self.meta() == other.meta() @@ -195,6 +211,7 @@ impl Ord for FractionLike where M: Clone + PartialEq, { + #[inline] fn cmp(&self, other: &Self) -> Ordering { (self.numerator() * other.denominator()).cmp(&(other.numerator() * self.denominator())) } @@ -204,6 +221,7 @@ impl PartialOrd for FractionLike where M: Clone + PartialEq, { + #[inline] fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } @@ -212,6 +230,7 @@ where impl Add for FractionLike { type Output = Self; + #[inline] fn add(self, other: Self) -> Self::Output { if self.denominator == other.denominator { FractionBase::new( @@ -232,6 +251,7 @@ impl Add for FractionLike { impl Add<&Self> for FractionLike { type Output = Self; + #[inline] fn add(self, other: &Self) -> Self::Output { if self.denominator == other.denominator { FractionBase::new( @@ -252,6 +272,7 @@ impl Add<&Self> for FractionLike { impl Sub for FractionLike { type Output = Self; + #[inline] fn sub(self, other: Self) -> Self::Output { if self.denominator == other.denominator { FractionBase::new( @@ -272,6 +293,7 @@ impl Sub for FractionLike { impl Sub<&Self> for FractionLike { type Output = Self; + #[inline] fn sub(self, other: &Self) -> Self::Output { if self.denominator == other.denominator { FractionBase::new( @@ -292,6 +314,7 @@ impl Sub<&Self> for FractionLike { impl Mul for FractionLike { type Output = Self; + #[inline] fn mul(self, other: Self) -> Self::Output { FractionBase::new( self.numerator * other.numerator, @@ -304,6 +327,7 @@ impl Mul for FractionLike { impl Mul<&Self> for FractionLike { type Output = Self; + #[inline] fn mul(self, other: &Self) -> Self::Output { FractionBase::new( self.numerator * &other.numerator, @@ -317,6 +341,7 @@ impl Div for FractionLike { type Output = Self; /// There's little to no possibility of an error, so unwrap can be used + #[inline] fn div(self, other: Self) -> Self::Output { FractionBase::new( self.numerator * other.denominator, @@ -330,6 +355,7 @@ impl Div<&Self> for FractionLike { type Output = Self; /// There's little to no possibility of an error, so unwrap can be used + #[inline] fn div(self, other: &Self) -> Self::Output { FractionBase::new( self.numerator * &other.denominator, diff --git a/src/entities/fractions/percent.rs b/src/entities/fractions/percent.rs index 1e08dbb..7394e36 100644 --- a/src/entities/fractions/percent.rs +++ b/src/entities/fractions/percent.rs @@ -13,12 +13,14 @@ pub type Percent = FractionLike; impl Percent { /// Constructor for creating a new [`Percent`] instance + #[inline] pub fn new(numerator: impl Into, denominator: impl Into) -> Self { FractionBase::new(numerator, denominator, IsPercent) } /// Converts the [`Percent`] to a string with a specified number of significant digits and /// rounding strategy + #[inline] pub fn to_significant( &self, significant_digits: u8, @@ -32,6 +34,7 @@ impl Percent { /// Converts the [`Percent`] to a string with a fixed number of decimal places and rounding /// strategy + #[inline] pub fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String { // Convert the Percent to a simple Fraction, multiply by 100, and then call to_fixed on the // result diff --git a/src/entities/fractions/price.rs b/src/entities/fractions/price.rs index bc965b1..ffbb12c 100644 --- a/src/entities/fractions/price.rs +++ b/src/entities/fractions/price.rs @@ -27,6 +27,7 @@ where TQuote: Currency, { /// Constructor for creating a new [`Price`] instance + #[inline] pub fn new( base_currency: TBase, quote_currency: TQuote, @@ -50,6 +51,7 @@ where } /// Create a [`Price`] instance from currency amounts of the base and quote currencies + #[inline] pub fn from_currency_amounts( base_amount: CurrencyAmount, quote_amount: CurrencyAmount, @@ -64,6 +66,7 @@ where } /// Flip the price, switching the base and quote currency + #[inline] pub fn invert(&self) -> Price { Price::new( self.quote_currency.clone(), @@ -75,6 +78,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. + #[inline] pub fn multiply( &self, other: &Price, @@ -93,6 +97,7 @@ where } /// Return the amount of quote currency corresponding to a given amount of the base currency + #[inline] pub fn quote( &self, currency_amount: CurrencyAmount, @@ -109,12 +114,14 @@ where } /// Get the value scaled by decimals for formatting + #[inline] pub fn adjusted_for_decimals(&self) -> Fraction { self.as_fraction() * self.scalar.clone() } /// Converts the adjusted price to a string with a specified number of significant digits and /// rounding strategy + #[inline] pub fn to_significant( &self, significant_digits: u8, @@ -126,6 +133,7 @@ where /// Converts the adjusted price to a string with a fixed number of decimal places and rounding /// strategy + #[inline] pub fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String { self.adjusted_for_decimals() .to_fixed(decimal_places, rounding) diff --git a/src/entities/token.rs b/src/entities/token.rs index 016da7d..fb24cda 100644 --- a/src/entities/token.rs +++ b/src/entities/token.rs @@ -15,10 +15,12 @@ pub struct TokenMeta { } impl Currency for Token { + #[inline] fn is_native(&self) -> bool { false } + #[inline] fn address(&self) -> Address { self.address } @@ -30,6 +32,7 @@ impl Currency for Token { /// * `other`: another token to compare /// /// returns: bool + #[inline] fn equals(&self, other: &impl Currency) -> bool { match other.is_native() { false => self.chain_id == other.chain_id() && self.address() == other.address(), @@ -37,6 +40,7 @@ impl Currency for Token { } } + #[inline] /// Return this token, which does not need to be wrapped fn wrapped(&self) -> Token { self.clone() @@ -63,6 +67,7 @@ impl Token { /// # Panics /// /// Panics if `chain_id` is 0. + #[inline] pub const fn new( chain_id: u64, address: Address, @@ -94,6 +99,7 @@ impl Token { /// # Arguments /// /// * `other`: another token to compare + #[inline] 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)); @@ -102,7 +108,7 @@ impl Token { if self.address() == other.address() { return Err(Error::EqualAddresses); } - Ok(self.address().lt(&other.address())) + Ok(self.address() < other.address()) } } diff --git a/src/entities/weth9.rs b/src/entities/weth9.rs index 9712d71..68d2205 100644 --- a/src/entities/weth9.rs +++ b/src/entities/weth9.rs @@ -171,6 +171,7 @@ impl WETH9 { /// * `chain_id`: The chain ID for which to retrieve the WETH token. /// /// Returns: `Some(Token)` if the token exists, `None` otherwise. + #[inline] pub fn get(&self, chain_id: u64) -> Option<&Token> { self.tokens.get(&chain_id) } diff --git a/src/utils/compute_price_impact.rs b/src/utils/compute_price_impact.rs index ca5d1db..8210388 100644 --- a/src/utils/compute_price_impact.rs +++ b/src/utils/compute_price_impact.rs @@ -9,6 +9,7 @@ use crate::prelude::*; /// * `outputAmount`: the output amount of the trade /// /// returns: Percent +#[inline] pub fn compute_price_impact( mid_price: Price, input_amount: CurrencyAmount,