Skip to content

Commit

Permalink
Refactor token creation with new macro for brevity
Browse files Browse the repository at this point in the history
A new macro `token!` is introduced to streamline the creation of Token instances in test cases and actual logic. This change simplifies the previous verbose syntax, making the codebase more concise. It also improves readability and maintainability.
  • Loading branch information
shuhuiluo committed Jan 1, 2024
1 parent 020c5b0 commit f5d7cad
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 311 deletions.
17 changes: 4 additions & 13 deletions src/entities/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,14 @@ impl BaseCurrency for Currency {
#[cfg(test)]
mod tests {
use super::*;
use crate::token;

const ADDRESS_ZERO: &str = "0x0000000000000000000000000000000000000000";
const ADDRESS_ONE: &str = "0x0000000000000000000000000000000000000001";

lazy_static! {
static ref TOKEN0: Token =
Token::new(1, ADDRESS_ZERO.to_string(), 18, None, None, None, None,);
static ref TOKEN1: Token =
Token::new(1, ADDRESS_ONE.to_string(), 18, None, None, None, None,);
static ref TOKEN0: Token = token!(1, ADDRESS_ZERO, 18);
static ref TOKEN1: Token = token!(1, ADDRESS_ONE, 18);
}

#[test]
Expand All @@ -121,14 +120,6 @@ mod tests {

#[test]
fn equals_token0_is_equal_to_another_token0() {
assert!(TOKEN0.equals(&Token::new(
1,
ADDRESS_ZERO.to_owned(),
18,
Some("symbol".to_owned()),
Some("name".to_owned()),
None,
None
)));
assert!(TOKEN0.equals(&token!(1, ADDRESS_ZERO, 18, "symbol", "name")));
}
}
21 changes: 3 additions & 18 deletions src/entities/fractions/currency_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,30 +129,15 @@ impl CurrencyAmount<Token> {
#[cfg(test)]
mod tests {
use super::*;
use crate::token;

// Constants for testing
const ADDRESS_ONE: &str = "0x0000000000000000000000000000000000000001";

// Lazy static variables for testing currencies
lazy_static! {
static ref TOKEN18: Currency = Currency::Token(Token::new(
1,
ADDRESS_ONE.to_string(),
18,
None,
None,
None,
None,
));
static ref TOKEN0: Currency = Currency::Token(Token::new(
1,
ADDRESS_ONE.to_string(),
0,
None,
None,
None,
None,
));
static ref TOKEN18: Currency = Currency::Token(token!(1, ADDRESS_ONE, 18));
static ref TOKEN0: Currency = Currency::Token(token!(1, ADDRESS_ONE, 0));
}

// Unit tests
Expand Down
31 changes: 4 additions & 27 deletions src/entities/fractions/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,38 +126,15 @@ where
#[cfg(test)]
mod test {
use super::*;
use crate::token;

const ADDRESS_ZERO: &str = "0x0000000000000000000000000000000000000000";
const ADDRESS_ONE: &str = "0x0000000000000000000000000000000000000001";

lazy_static! {
static ref TOKEN0: Currency = Currency::Token(Token::new(
1,
ADDRESS_ZERO.to_string(),
18,
None,
None,
None,
None,
));
static ref TOKEN0_6: Currency = Currency::Token(Token::new(
1,
ADDRESS_ZERO.to_string(),
6,
None,
None,
None,
None,
));
static ref TOKEN1: Currency = Currency::Token(Token::new(
1,
ADDRESS_ONE.to_string(),
18,
None,
None,
None,
None,
));
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));
}

#[test]
Expand Down
Loading

0 comments on commit f5d7cad

Please sign in to comment.