Skip to content

Commit

Permalink
refactor: implement currency traits using macros
Browse files Browse the repository at this point in the history
Consolidated the `Currency` trait implementations using macros for `Ether`, `Token`, and `CurrencyLike` types. This reduces code duplication and improves maintainability. The macros now handle trait method definitions for multiple types.
  • Loading branch information
shuhuiluo committed Sep 23, 2024
1 parent 618cf7f commit 629b221
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 53 deletions.
68 changes: 38 additions & 30 deletions src/entities/base_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,42 @@ pub trait BaseCurrency {
fn name(&self) -> Option<&String>;
}

impl<const IS_NATIVE: bool, M> BaseCurrency for CurrencyLike<IS_NATIVE, M> {
#[inline]
fn is_native(&self) -> bool {
IS_NATIVE
}

#[inline]
fn is_token(&self) -> bool {
!IS_NATIVE
}

#[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()
}
macro_rules! impl_base_currency {
($($currency:ty),*) => {
$(
impl<const IS_NATIVE: bool, M> BaseCurrency for $currency {
#[inline]
fn is_native(&self) -> bool {
IS_NATIVE
}

#[inline]
fn is_token(&self) -> bool {
!IS_NATIVE
}

#[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()
}
}
)*
};
}

impl_base_currency!(CurrencyLike<IS_NATIVE, M>, &CurrencyLike<IS_NATIVE, M>);
32 changes: 20 additions & 12 deletions src/entities/ether.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@ use crate::prelude::*;
/// Represents the native currency of the blockchain.
pub type Ether = CurrencyLike<true, Option<Token>>;

impl Currency for Ether {
#[inline]
fn equals(&self, other: &impl Currency) -> bool {
other.is_native() && self.chain_id() == other.chain_id()
}
macro_rules! impl_currency {
($($ether:ty),*) => {
$(
impl Currency for $ether {
#[inline]
fn equals(&self, other: &impl Currency) -> bool {
other.is_native() && self.chain_id() == other.chain_id()
}

#[inline]
fn wrapped(&self) -> &Token {
match &self.meta {
Some(weth) => weth,
None => panic!("WRAPPED"),
}
}
#[inline]
fn wrapped(&self) -> &Token {
match &self.meta {
Some(weth) => weth,
None => panic!("WRAPPED"),
}
}
}
)*
};
}

impl_currency!(Ether, &Ether);

impl Ether {
/// Creates a new instance of [`Ether`] with the specified chain ID.
#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/entities/fractions/currency_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ impl<T: Currency> CurrencyAmount<T> {

/// Wrap the currency amount if the currency is not native
#[inline]
pub fn wrapped(&self) -> Result<CurrencyAmount<Token>, Error> {
pub fn wrapped(&self) -> Result<CurrencyAmount<&Token>, Error> {
CurrencyAmount::from_fractional_amount(
self.currency.wrapped().clone(),
self.currency.wrapped(),
self.numerator().clone(),
self.denominator().clone(),
)
Expand Down
26 changes: 17 additions & 9 deletions src/entities/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,26 @@ pub struct TokenMeta {
pub sell_fee_bps: Option<BigUint>,
}

impl Currency for Token {
#[inline]
fn equals(&self, other: &impl Currency) -> bool {
other.is_token() && self.chain_id == other.chain_id() && self.address == other.address()
}
macro_rules! impl_currency {
($($token:ty),*) => {
$(
impl Currency for $token {
#[inline]
fn equals(&self, other: &impl Currency) -> bool {
other.is_token() && self.chain_id == other.chain_id() && self.address == other.address()
}

#[inline]
fn wrapped(&self) -> &Token {
self
}
#[inline]
fn wrapped(&self) -> &Token {
self
}
}
)*
};
}

impl_currency!(Token, &Token);

impl Token {
/// Creates a new [`Token`] with the given parameters.
///
Expand Down

0 comments on commit 629b221

Please sign in to comment.