Skip to content

Commit 1d2acdc

Browse files
authored
refactor: clean up dependencies and re-exports (#92)
* Remove `lazy_static` from prelude and add explicit imports. `lazy_static` was removed from the common prelude module to avoid unnecessary dependencies in files that don't require it. Explicit imports of `lazy_static` were added only where it is actually used, improving clarity and reducing potential overhead. * Refactor module structure and re-export logic for clarity Reorganized module layout to improve maintainability and simplify imports. Removed standalone `prelude.rs` by integrating its functionality directly into `lib.rs`. Adjusted tests, visibility modifiers, and re-export strategy across modules for cleaner and more efficient usage. * Refactor imports and improve type safety across the codebase Replaced unused or unnecessary imports with more relevant ones to optimize dependencies. Updated type usage for better clarity and correctness, including replacing `vec![]` with `[]` for `FxHashMap::from_iter` calls. Simplified assertions and improved error handling for better maintainability. * Update README and refactor type exports in library Updated the README to improve documentation order, clarify `no_std` usage, and enhance example formatting. Refactored type exports in the library to use type aliases for better readability and maintainability.
1 parent 29e893d commit 1d2acdc

21 files changed

+84
-80
lines changed

Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
[package]
22
name = "uniswap-sdk-core"
3-
version = "3.2.0"
3+
version = "3.3.0"
44
edition = "2021"
55
authors = ["malik <aremumalik05@gmail.com>", "Shuhui Luo <twitter.com/aureliano_law>"]
66
description = "The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange"
77
license = "MIT"
88

99
[dependencies]
10-
alloy-primitives = "0.8"
10+
alloy-primitives = { version = ">=0.8.5", features = ["map-fxhash"] }
1111
bigdecimal = "0.4.5"
1212
derive_more = { version = "1.0.0", features = ["deref"] }
1313
eth_checksum = { version = "0.1.2", optional = true }
1414
lazy_static = "1.5"
1515
num-bigint = "0.4"
1616
num-integer = "0.1"
17-
num-traits = "0.2"
1817
regex = { version = "1.11", optional = true }
19-
rustc-hash = "2.0"
2018
thiserror = { version = "2", default-features = false }
2119

2220
[features]

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@
77
**A Custom Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized
88
exchange.**
99

10-
## Note on `no_std`
11-
12-
By default, this library does not depend on the standard library (`std`). However, the `std` feature can be enabled to
13-
use `thiserror` for error handling.
14-
1510
## Quickstart
1611

1712
Add this to your Cargo.toml
1813

1914
```
2015
[dependencies]
21-
uniswap-sdk-core = "3.0.0"
16+
uniswap-sdk-core = "3.3.0"
2217
```
2318

2419
And this to your code:
@@ -27,10 +22,15 @@ And this to your code:
2722
use uniswap_sdk_core::prelude::*;
2823
```
2924

25+
## Note on `no_std`
26+
27+
By default, this library does not depend on the standard library (`std`). However, the `std` feature can be enabled.
28+
3029
## Examples
3130

32-
The code below shows an example of how to create a new `Token` instance for the DAI token on the Ethereum Mainnet using
33-
the `token!` macro.
31+
<details>
32+
<summary>The code below shows an example of how to create a new `Token` instance for the DAI token on the Ethereum Mainnet using
33+
the `token!` macro.</summary>
3434

3535
```rust
3636
// The `prelude` module provides a convenient way to import a number of common dependencies at
@@ -68,6 +68,8 @@ fn main() {
6868
}
6969
```
7070

71+
</details>
72+
7173
This example demonstrates how to create a `Token` instance for DAI on the Ethereum Mainnet using the `token!` macro.
7274

7375
It then prints the token's address and checks if it's a native token (which it isn't, so it prints false).
@@ -99,8 +101,8 @@ provide similar functionality in the Rust programming language.
99101

100102
- [Uniswap V3 SDK Rust](https://github.com/shuhuiluo/uniswap-v3-sdk-rs): Opinionated Rust implementation of the Uniswap
101103
V3 SDK with a focus on readability and performance
102-
- [Uniswap V2 SDK Rust](https://github.com/shuhuiluo/uniswap-v2-sdk-rs): Opinionated Rust implementation of the Uniswap
103-
V2 SDK with a focus on readability and performance
104+
- [Uniswap V4 SDK Rust](https://github.com/shuhuiluo/uniswap-v4-sdk-rs): Opinionated Rust implementation of the Uniswap
105+
V4 SDK with a focus on readability and performance
104106
- ...
105107

106108
*(If you want to add project to the list, dm or open a PR)*

src/addresses.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::prelude::*;
2-
use alloc::vec;
2+
use alloy_primitives::address;
3+
use lazy_static::lazy_static;
34

4-
type AddressMap = FxHashMap<u64, Address>;
5+
pub type AddressMap = FxHashMap<u64, Address>;
56

67
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
78
pub struct ChainAddresses {
@@ -52,7 +53,7 @@ pub const V2_FACTORY_ADDRESS: Address = address!("5C69bEe701ef814a2B6a3EDD4B1652
5253

5354
lazy_static! {
5455
pub static ref V2_FACTORY_ADDRESSES: AddressMap = {
55-
AddressMap::from_iter(vec![
56+
AddressMap::from_iter([
5657
(ChainId::MAINNET as u64, V2_FACTORY_ADDRESS),
5758
(ChainId::GOERLI as u64, V2_FACTORY_ADDRESS),
5859
(
@@ -101,7 +102,7 @@ pub const V2_ROUTER_ADDRESS: Address = address!("7a250d5630B4cF539739dF2C5dAcb4c
101102

102103
lazy_static! {
103104
pub static ref V2_ROUTER_ADDRESSES: AddressMap = {
104-
AddressMap::from_iter(vec![
105+
AddressMap::from_iter([
105106
(ChainId::MAINNET as u64, V2_ROUTER_ADDRESS),
106107
(ChainId::GOERLI as u64, V2_ROUTER_ADDRESS),
107108
(
@@ -381,7 +382,7 @@ lazy_static! {
381382
/// for a given network. The keys in the map are the network IDs, and the values
382383
/// are the corresponding contract addresses.
383384
pub static ref CHAIN_TO_ADDRESSES_MAP: FxHashMap<u64, ChainAddresses> = {
384-
FxHashMap::from_iter(vec![
385+
FxHashMap::from_iter([
385386
(ChainId::MAINNET as u64, MAINNET_ADDRESSES),
386387
(ChainId::OPTIMISM as u64, OPTIMISM_ADDRESSES),
387388
(ChainId::ARBITRUM_ONE as u64, ARBITUM_ONE_ADDRESSES),
@@ -447,15 +448,15 @@ lazy_static! {
447448

448449
lazy_static! {
449450
/// The older V1 governance address
450-
pub static ref GOVERNANCE_ALPHA_V1_ADDRESSES: AddressMap = AddressMap::from_iter(vec![(
451+
pub static ref GOVERNANCE_ALPHA_V1_ADDRESSES: AddressMap = AddressMap::from_iter([(
451452
ChainId::MAINNET as u64,
452453
address!("C4e172459f1E7939D522503B81AFAaC1014CE6F6")
453454
)]);
454455
}
455456

456457
lazy_static! {
457458
/// The latest governor bravo that is currently admin of timelock
458-
pub static ref GOVERNANCE_BRAVO_ADDRESSES: AddressMap = AddressMap::from_iter(vec![(
459+
pub static ref GOVERNANCE_BRAVO_ADDRESSES: AddressMap = AddressMap::from_iter([(
459460
ChainId::MAINNET as u64,
460461
address!("408ED6354d4973f66138C91495F2f2FCbd8724C3")
461462
)]);
@@ -467,14 +468,14 @@ lazy_static! {
467468
}
468469

469470
lazy_static! {
470-
pub static ref MERKLE_DISTRIBUTOR_ADDRESS: AddressMap = AddressMap::from_iter(vec![(
471+
pub static ref MERKLE_DISTRIBUTOR_ADDRESS: AddressMap = AddressMap::from_iter([(
471472
ChainId::MAINNET as u64,
472473
address!("090D4613473dEE047c3f2706764f49E0821D256e"),
473474
)]);
474475
}
475476

476477
lazy_static! {
477-
pub static ref ARGENT_WALLET_DETECTOR_ADDRESS: AddressMap = AddressMap::from_iter(vec![(
478+
pub static ref ARGENT_WALLET_DETECTOR_ADDRESS: AddressMap = AddressMap::from_iter([(
478479
ChainId::MAINNET as u64,
479480
address!("eca4B0bDBf7c55E9b7925919d03CbF8Dc82537E8"),
480481
)]);
@@ -506,7 +507,7 @@ lazy_static! {
506507
}
507508

508509
lazy_static! {
509-
pub static ref SOCKS_CONTROLLER_ADDRESSES: AddressMap = AddressMap::from_iter(vec![(
510+
pub static ref SOCKS_CONTROLLER_ADDRESSES: AddressMap = AddressMap::from_iter([(
510511
ChainId::MAINNET as u64,
511512
address!("65770b5283117639760beA3F867b69b3697a91dd")
512513
)]);

src/constants.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::prelude::*;
22
use alloy_primitives::U256;
3+
use lazy_static::lazy_static;
34
use num_bigint::Sign;
45

56
/// Represents the various types of trades.

src/entities/currency.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ impl_base_currency!(Currency, &Currency);
9292
mod tests {
9393
use super::*;
9494
use crate::token;
95+
use lazy_static::lazy_static;
9596

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

src/entities/ether.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::prelude::*;
2+
use alloc::string::ToString;
23

34
/// Ether is the main usage of a 'native' currency, i.e., for Ethereum mainnet and all testnets.
45
/// Represents the native currency of the blockchain.

src/entities/fractions/currency_amount.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::prelude::*;
2+
use alloc::string::ToString;
23
use core::ops::Div;
4+
use num_integer::Integer;
35

46
/// Currency amount struct that represents a rational amount of a currency
57
pub type CurrencyAmount<T> = FractionLike<CurrencyMeta<T>>;
@@ -154,6 +156,7 @@ impl<T: BaseCurrency> CurrencyAmount<T> {
154156
mod tests {
155157
use super::*;
156158
use crate::token;
159+
use lazy_static::lazy_static;
157160

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

src/entities/fractions/fraction.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
use crate::prelude::*;
2+
use alloc::string::ToString;
23
use core::{
4+
cmp::Ordering,
35
hash::{Hash, Hasher},
6+
num::NonZeroU64,
47
ops::{Add, Div, Mul, Sub},
58
};
69
use derive_more::Deref;
10+
use num_integer::Integer;
711

812
/// Struct representing a fraction with metadata
913
#[derive(Clone, Debug, Deref)]
@@ -162,7 +166,7 @@ impl<M: Clone> FractionBase<M> for FractionLike<M> {
162166
#[inline]
163167
fn new(numerator: impl Into<BigInt>, denominator: impl Into<BigInt>, meta: M) -> Self {
164168
let denominator = denominator.into();
165-
assert!(!denominator.is_zero(), "denominator is zero");
169+
assert_ne!(denominator, BigInt::ZERO, "denominator is zero");
166170
Self {
167171
numerator: numerator.into(),
168172
denominator,

src/entities/fractions/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/// This module represents currency, including the currency type and the amount as a fraction.
21
pub mod currency_amount;
3-
/// This module contains with precise division results, avoiding floating-point arithmetic issues.
42
pub mod fraction;
5-
/// A module represents a percentage.
63
pub mod percent;
7-
/// A module represents a price as a ratio between two currencies, with methods for arithmetic and
8-
/// string conversions.
94
pub mod price;
5+
6+
pub use currency_amount::*;
7+
pub use fraction::*;
8+
pub use percent::*;
9+
pub use price::*;

src/entities/fractions/percent.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::prelude::*;
2+
use lazy_static::lazy_static;
23

34
lazy_static! {
45
static ref ONE_HUNDRED: Fraction = Fraction::new(100, 1);

src/entities/fractions/price.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ where
142142
mod test {
143143
use super::*;
144144
use crate::token;
145+
use lazy_static::lazy_static;
145146

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

src/entities/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ pub mod fractions;
55
pub mod native_currency;
66
pub mod token;
77
pub mod weth9;
8+
9+
pub use base_currency::*;
10+
pub use currency::*;
11+
pub use ether::Ether;
12+
pub use fractions::*;
13+
pub use native_currency::NativeCurrency;
14+
pub use token::*;
15+
pub use weth9::WETH9;

src/entities/token.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ macro_rules! token {
131131
($chain_id:expr, $address:literal, $decimals:expr) => {
132132
Token::new(
133133
$chain_id,
134-
address!($address),
134+
alloy_primitives::address!($address),
135135
$decimals,
136136
None,
137137
None,
@@ -153,7 +153,7 @@ macro_rules! token {
153153
($chain_id:expr, $address:literal, $decimals:expr, $symbol:expr) => {
154154
Token::new(
155155
$chain_id,
156-
address!($address),
156+
alloy_primitives::address!($address),
157157
$decimals,
158158
Some($symbol.to_string()),
159159
None,
@@ -175,7 +175,7 @@ macro_rules! token {
175175
($chain_id:expr, $address:literal, $decimals:expr, $symbol:expr, $name:expr) => {
176176
Token::new(
177177
$chain_id,
178-
address!($address),
178+
alloy_primitives::address!($address),
179179
$decimals,
180180
Some($symbol.to_string()),
181181
Some($name.to_string()),

src/entities/weth9.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{prelude::*, token};
2-
use alloc::vec;
2+
use alloc::string::ToString;
33

44
/// Represents the WETH9 contract and provides information about WETH tokens on different Ethereum
55
/// chains.
@@ -31,7 +31,7 @@ impl WETH9 {
3131
#[inline]
3232
#[must_use]
3333
pub fn new() -> Self {
34-
let tokens = FxHashMap::from_iter(vec![
34+
let tokens = FxHashMap::from_iter([
3535
(1, Self::on_chain(1).unwrap()),
3636
(3, Self::on_chain(3).unwrap()),
3737
(4, Self::on_chain(4).unwrap()),

src/examples/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub mod token_example;
1+
mod token_example;

src/examples/token_example.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
#![cfg(test)]
2-
3-
// Import the Token struct from the Uniswap SDK-Core
41
use crate::entities::token::Token;
52

63
/// This function demonstrates basic operations with the Token struct from the Uniswap SDK-Core.
7-
pub fn main() {
4+
#[test]
5+
fn main() {
86
// Create a new Token instance for DAI
97
let dai_token = Token::new(
108
1, // Assuming chain_id is 1 for Ethereum mainnet

src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,25 @@ pub mod entities;
3636
/// This module defines custom error types that are used throughout the SDK to
3737
/// handle various error conditions.
3838
pub mod error;
39+
/// Contains utility functions and helpers used across the Uniswap SDK Core.
40+
pub mod utils;
41+
3942
/// Contains commonly used items from the Uniswap SDK Core.
4043
///
4144
/// This module re-exports items that are commonly used together,
4245
/// making it easier to import them in other parts of your application.
43-
pub mod prelude;
44-
/// Contains utility functions and helpers used across the Uniswap SDK Core.
45-
pub mod utils;
46+
pub mod prelude {
47+
pub use crate::{addresses::*, chains::*, constants::*, entities::*, error::Error, utils::*};
48+
49+
pub use alloc::{string::String, vec::Vec};
50+
pub use alloy_primitives::{map::rustc_hash::FxHashMap, Address, Bytes, B256, U256};
51+
52+
pub type BigInt = num_bigint::BigInt;
53+
pub type BigUint = num_bigint::BigUint;
54+
pub type BigDecimal = bigdecimal::BigDecimal;
55+
pub type RoundingMode = bigdecimal::RoundingMode;
56+
}
4657

4758
/// Contains examples of how Uniswap sdk core can be used
48-
pub mod examples;
59+
#[cfg(test)]
60+
mod examples;

src/prelude.rs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/utils/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@ pub mod compute_zksync_create2_address;
33
pub mod sorted_insert;
44
pub mod sqrt;
55

6+
pub use compute_price_impact::compute_price_impact;
7+
pub use compute_zksync_create2_address::compute_zksync_create2_address;
8+
pub use sorted_insert::sorted_insert;
9+
pub use sqrt::sqrt;
10+
611
#[cfg(feature = "validate_parse_address")]
712
pub mod validate_and_parse_address;

0 commit comments

Comments
 (0)