Skip to content

Commit 5e4f81b

Browse files
authored
fix(no-std): update dependencies and features and replace FxHashMap with HashMap (#107)
Updated dependencies to use non-default features and introduced `default = []` in features. Replaced `FxHashMap` with standard `HashMap` for better compatibility. Refactored documentation and examples for clarity, aligning the library's setup with best practices for no-std support.
1 parent f4c4d03 commit 5e4f81b

File tree

6 files changed

+57
-62
lines changed

6 files changed

+57
-62
lines changed

Cargo.toml

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
[package]
22
name = "uniswap-sdk-core"
3-
version = "3.4.0"
3+
version = "3.5.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 = { version = ">=0.8.5", features = ["map-fxhash"] }
11-
bigdecimal = "0.4.7"
12-
derive_more = { version = "1.0.0", features = ["deref"] }
10+
alloy-primitives = { version = ">=0.8.5", default-features = false, features = ["map-fxhash"] }
11+
bigdecimal = { version = "0.4.7", default-features = false }
12+
derive_more = { version = "2", default-features = false, features = ["deref", "from"] }
1313
eth_checksum = { version = "0.1.2", optional = true }
1414
lazy_static = "1.5"
15-
num-bigint = "0.4"
16-
num-integer = "0.1"
15+
num-bigint = { version = "0.4", default-features = false }
16+
num-integer = { version = "0.1", default-features = false }
1717
regex = { version = "1.11", optional = true }
1818
thiserror = { version = "2", default-features = false }
1919

2020
[features]
21-
std = ["thiserror/std"]
21+
default = []
22+
std = ["alloy-primitives/std", "bigdecimal/std", "derive_more/std", "thiserror/std"]
2223
validate_parse_address = ["eth_checksum", "regex"]
23-
24-
[lib]
25-
doctest = true

README.md

+35-38
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ exchange.**
1111

1212
Add this to your Cargo.toml
1313

14-
```
14+
```toml
1515
[dependencies]
16-
uniswap-sdk-core = "3.3.0"
16+
uniswap-sdk-core = "3.5.0"
1717
```
1818

1919
And this to your code:
2020

21-
```
21+
```rust
2222
use uniswap_sdk_core::prelude::*;
2323
```
2424

@@ -28,48 +28,43 @@ By default, this library does not depend on the standard library (`std`). Howeve
2828

2929
## Examples
3030

31+
The code below shows an example of how to create a new `Token` instance for the DAI token on the Ethereum Mainnet using
32+
the `token!` macro.
33+
3134
<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>
35+
<summary>Click to expand</summary>
3436

3537
```rust
36-
// The `prelude` module provides a convenient way to import a number of common dependencies at
37-
// once. This can be useful if you are working with multiple parts of the library and want to avoid
38-
// having to import each dependency individually.
39-
// Import necessary preludes and types
38+
// Import necessary preludes and token macro
4039
use uniswap_sdk_core::{prelude::*, token};
4140

42-
fn main() {
43-
// Define the chain ID, address, decimals, symbol, and name for the token
44-
const CHAIN_ID: u64 = 1; // Ethereum Mainnet
45-
const TOKEN_ADDRESS: &str = "0x6B175474E89094C44Da98b954EedeAC495271d0F"; // DAI Token Address
46-
const DECIMALS: u8 = 18;
47-
const SYMBOL: &str = "DAI";
48-
const NAME: &str = "Dai Stablecoin";
49-
50-
// Use the `token!` macro to create a new `Token` instance
51-
let dai_token = token!(CHAIN_ID, TOKEN_ADDRESS, DECIMALS, SYMBOL, NAME);
52-
53-
// Example usage of the `Token` methods
54-
println!("Token Address: {}", dai_token.address());
55-
println!("Is Native: {}", dai_token.is_native());
56-
57-
// Example of comparing two tokens
58-
let another_dai_token = token!(CHAIN_ID, TOKEN_ADDRESS, DECIMALS, SYMBOL, NAME);
59-
println!("Are the tokens equal? {}", dai_token.equals(&another_dai_token));
60-
61-
// Example of sorting tokens
62-
let another_token = token!(CHAIN_ID, "0000000000000000000000000000000000000002", DECIMALS, "ETH", "Ethereum");
63-
match dai_token.sorts_before(&another_token) {
64-
Ok(true) => println!("DAI sorts before ETH"),
65-
Ok(false) => println!("DAI does not sort before ETH"),
66-
Err(e) => println!("Error comparing tokens: {:?}", e),
67-
}
41+
// Define the chain ID, address, decimals, symbol, and name for the token
42+
const CHAIN_ID: u64 = 1; // Ethereum Mainnet
43+
const TOKEN_ADDRESS: &str = "0x6B175474E89094C44Da98b954EedeAC495271d0F"; // DAI Token Address
44+
const DECIMALS: u8 = 18;
45+
const SYMBOL: &str = "DAI";
46+
const NAME: &str = "Dai Stablecoin";
47+
48+
// Use the `token!` macro to create a new `Token` instance
49+
let dai_token = token!(CHAIN_ID, TOKEN_ADDRESS, DECIMALS, SYMBOL, NAME);
50+
51+
// Example usage of the `Token` methods
52+
println!("Token Address: {}", dai_token.address());
53+
println!("Is Native: {}", dai_token.is_native());
54+
55+
// Example of comparing two tokens
56+
let another_dai_token = token!(CHAIN_ID, TOKEN_ADDRESS, DECIMALS, SYMBOL, NAME);
57+
println!("Are the tokens equal? {}", dai_token.equals(&another_dai_token));
58+
59+
// Example of sorting tokens
60+
let another_token = token!(CHAIN_ID, "0000000000000000000000000000000000000002", DECIMALS, "ETH", "Ethereum");
61+
match dai_token.sorts_before( & another_token) {
62+
Ok(true) => println ! ("DAI sorts before ETH"),
63+
Ok(false) => println ! ("DAI does not sort before ETH"),
64+
Err(e) => println ! ("Error comparing tokens: {:?}", e),
6865
}
6966
```
7067

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

7570
It then prints the token's address and checks if it's a native token (which it isn't, so it prints false).
@@ -83,14 +78,16 @@ assuming the addresses are correctly set up for this comparison.
8378
Remember to replace "0x6B175474E89094C44Da98b954EedeAC495271d0F" with the actual address of the DAI token you're working
8479
with, and adjust the CHAIN_ID if you're working on a different network (e.g., a testnet).
8580

81+
</details>
82+
8683
## Contribution
8784

8885
Contributions are welcome! If you find a bug or have suggestions for improvements, feel free to open an issue or submit
8986
a pull request on the [GitHub repository](https://github.com/malik672/uniswap-sdk-core-rust).
9087

9188
## License
9289

93-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
90+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
9491

9592
## Acknowledgments
9693

src/addresses.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::prelude::*;
22
use alloy_primitives::address;
33
use lazy_static::lazy_static;
44

5-
pub type AddressMap = FxHashMap<u64, Address>;
5+
pub type AddressMap = HashMap<u64, Address>;
66

77
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
88
pub struct ChainAddresses {
@@ -381,8 +381,8 @@ lazy_static! {
381381
/// This map is used to look up the addresses of various Uniswap contracts
382382
/// for a given network. The keys in the map are the network IDs, and the values
383383
/// are the corresponding contract addresses.
384-
pub static ref CHAIN_TO_ADDRESSES_MAP: FxHashMap<u64, ChainAddresses> = {
385-
FxHashMap::from_iter([
384+
pub static ref CHAIN_TO_ADDRESSES_MAP: HashMap<u64, ChainAddresses> = {
385+
HashMap::from_iter([
386386
(ChainId::MAINNET as u64, MAINNET_ADDRESSES),
387387
(ChainId::OPTIMISM as u64, OPTIMISM_ADDRESSES),
388388
(ChainId::ARBITRUM_ONE as u64, ARBITUM_ONE_ADDRESSES),

src/entities/weth9.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use alloc::string::ToString;
66
#[derive(Clone, PartialEq, Debug)]
77
pub struct WETH9 {
88
/// A mapping of chain IDs to corresponding WETH tokens.
9-
tokens: FxHashMap<u64, Token>,
9+
tokens: HashMap<u64, Token>,
1010
}
1111

1212
/// Default implementation for [`WETH9`], creating an instance with predefined WETH tokens on
@@ -31,7 +31,7 @@ impl WETH9 {
3131
#[inline]
3232
#[must_use]
3333
pub fn new() -> Self {
34-
let tokens = FxHashMap::from_iter([
34+
let tokens = HashMap::from_iter([
3535
(1, Self::on_chain(1).unwrap()),
3636
(3, Self::on_chain(3).unwrap()),
3737
(4, Self::on_chain(4).unwrap()),

src/lib.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
//! # uniswap-sdk-core
2-
//!
3-
//! The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap
4-
//! decentralized exchange.
5-
6-
#![cfg_attr(not(any(feature = "std", test)), no_std)]
1+
#![doc = include_str!("../README.md")]
2+
#![cfg_attr(not(feature = "std"), no_std)]
73
#![warn(
84
missing_copy_implementations,
95
missing_debug_implementations,
@@ -46,8 +42,11 @@ pub mod utils;
4642
pub mod prelude {
4743
pub use crate::{addresses::*, chains::*, constants::*, entities::*, error::Error, utils::*};
4844

49-
pub use alloc::{string::String, vec::Vec};
50-
pub use alloy_primitives::{map::rustc_hash::FxHashMap, Address, Bytes, B256, U256};
45+
pub use alloc::{
46+
string::{String, ToString},
47+
vec::Vec,
48+
};
49+
pub use alloy_primitives::{map::HashMap, Address, Bytes, B256, U256};
5150

5251
pub type BigInt = num_bigint::BigInt;
5352
pub type BigUint = num_bigint::BigUint;
@@ -56,5 +55,5 @@ pub mod prelude {
5655
}
5756

5857
/// Contains examples of how Uniswap sdk core can be used
59-
#[cfg(test)]
58+
#[cfg(all(feature = "std", test))]
6059
mod examples;

src/utils/sorted_insert.rs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub fn sorted_insert<T: Clone>(
3737
#[cfg(test)]
3838
mod tests {
3939
use super::*;
40+
use alloc::vec;
4041

4142
fn cmp(a: &i32, b: &i32) -> Ordering {
4243
a.cmp(b)

0 commit comments

Comments
 (0)