Skip to content

Commit

Permalink
Merge pull request #19 from malik672/prelude
Browse files Browse the repository at this point in the history
Introduce Custom Prelude for Improved Code Organization and Convenience
  • Loading branch information
malik672 authored Dec 30, 2023
2 parents f52d1f7 + 373a958 commit 332dd0f
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 60 deletions.
5 changes: 1 addition & 4 deletions src/addresses.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use super::chains::{ChainId, SUPPORTED_CHAINS};
use lazy_static::lazy_static;
use std::collections::HashMap;

use crate::prelude::*;
type AddressMap = HashMap<u32, String>;
type ChainMap = HashMap<u32, ChainAddresses>;
type ChainAddress = HashMap<u32, String>;
Expand Down
4 changes: 1 addition & 3 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use lazy_static::lazy_static;
use num_bigint::BigInt;
use num_traits::Num;
use crate::prelude::*;

pub enum TradeType {
ExactInput,
Expand Down
4 changes: 2 additions & 2 deletions src/entities/currency.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{base_currency::BaseCurrency, ether::Ether, token::Token};
use alloy_primitives::Address;
use crate::prelude::*;


#[derive(Clone, PartialEq)]
pub enum Currency {
Expand Down
11 changes: 1 addition & 10 deletions src/entities/ether.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
use super::{
base_currency::{BaseCurrency, CurrencyLike},
currency::CurrencyTrait,
token::Token,
weth9::WETH9,
};
use alloy_primitives::Address;
use lazy_static::lazy_static;
use std::{collections::HashMap, sync::Mutex};

use crate::prelude::*;
// Lazy static cache for Ether instances
lazy_static! {
static ref ETHER_CACHE: Mutex<HashMap<u32, Ether>> = Mutex::new(HashMap::new());
Expand Down
13 changes: 1 addition & 12 deletions src/entities/fractions/currency_amount.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
// External crate dependencies
use crate::{
constants::{Rounding, MAX_UINT256},
entities::{
currency::CurrencyTrait,
fractions::fraction::{Fraction, FractionLike, FractionTrait},
token::Token,
},
};
use bigdecimal::BigDecimal;
use num_bigint::{BigInt, BigUint};
use num_integer::Integer;
use std::{ops::Div, str::FromStr};
use crate::prelude::*;

// Type alias for a currency amount using the FractionLike trait
pub type CurrencyAmount<T> = FractionLike<CurrencyMeta<T>>;
Expand Down
7 changes: 1 addition & 6 deletions src/entities/fractions/fraction.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
// External crate dependencies
use crate::constants::Rounding;
use bigdecimal::{BigDecimal, RoundingMode};
use num_bigint::BigInt;
use num_integer::Integer;
use num_traits::Zero;
use std::{num::NonZeroU64, ops::Div, str::FromStr};
use crate::prelude::*;

// Struct representing a fraction with metadata
#[derive(Clone, PartialEq)]
Expand Down
5 changes: 1 addition & 4 deletions src/entities/fractions/percent.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// Importing dependencies from the same module
use super::fraction::{Fraction, FractionLike, FractionTrait};
use crate::constants::Rounding;
use lazy_static::lazy_static;
use num_bigint::BigInt;
use crate::prelude::*;

// Lazily initialized constant representing the fraction 100/1
lazy_static! {
Expand Down
12 changes: 1 addition & 11 deletions src/entities/fractions/price.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
// External crate dependencies
use crate::{
constants::Rounding,
entities::{
currency::CurrencyTrait,
fractions::{
currency_amount::CurrencyAmount,
fraction::{Fraction, FractionLike, FractionTrait},
},
},
};
use num_bigint::BigInt;
use crate::prelude::*;

// Type alias for a Price, a Fraction with metadata PriceMeta
pub type Price<TBase, TQuote> = FractionLike<PriceMeta<TBase, TQuote>>;
Expand Down
4 changes: 1 addition & 3 deletions src/entities/token.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use super::{base_currency::CurrencyLike, currency::CurrencyTrait};
use alloy_primitives::Address;
use num_bigint::BigUint;
use crate::prelude::*;

/// Represents an ERC20 token with a unique address and some metadata.
pub type Token = CurrencyLike<TokenMeta>;
Expand Down
3 changes: 1 addition & 2 deletions src/entities/weth9.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::token::Token;
use std::collections::HashMap;
use crate::prelude::*;

/// `WETH9` represents the WETH9 contract and provides information about WETH tokens on different Ethereum chains.
pub struct WETH9 {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod chains;
pub mod constants;
pub mod entities;
pub mod utils;
pub mod prelude;
24 changes: 24 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pub use super::chains::{ChainId, SUPPORTED_CHAINS};
pub use crate::{
constants::{Rounding, MAX_UINT256},
entities::{
base_currency::{BaseCurrency, CurrencyLike},
currency::CurrencyTrait,
ether::Ether,
fractions::{
currency_amount::CurrencyAmount,
fraction::{Fraction, FractionLike, FractionTrait},
},
token::Token,
weth9::WETH9,
},
};
pub use alloy_primitives::Address;
pub use bigdecimal::{BigDecimal, RoundingMode};
pub use lazy_static::lazy_static;
pub use num_bigint::{BigInt, BigUint, ToBigInt, ToBigUint};
pub use num_integer::Integer;
pub use num_traits::{Num, ToPrimitive, Zero};
pub use std::{
cmp::Ordering, collections::HashMap, num::NonZeroU64, ops::Div, str::FromStr, sync::Mutex,
};
2 changes: 1 addition & 1 deletion src/utils/sorted_insert.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::cmp::Ordering;
use crate::prelude::*;

/// Given an array of items sorted by `comparator`, insert an item into its sort index and constrain the size to
/// `maxSize` by removing the last item
Expand Down
3 changes: 1 addition & 2 deletions src/utils/sqrt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use num_bigint::{BigInt, ToBigInt};
use num_traits::{ToPrimitive, Zero};
use crate::prelude::*;

/// Computes floor(sqrt(value))
///
Expand Down

0 comments on commit 332dd0f

Please sign in to comment.