Skip to content

Commit

Permalink
fix(no-std): update dependencies and features and replace FxHashMap
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
shuhuiluo authored Feb 5, 2025
1 parent f4c4d03 commit 5e4f81b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 62 deletions.
18 changes: 8 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
[package]
name = "uniswap-sdk-core"
version = "3.4.0"
version = "3.5.0"
edition = "2021"
authors = ["malik <aremumalik05@gmail.com>", "Shuhui Luo <twitter.com/aureliano_law>"]
description = "The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange"
license = "MIT"

[dependencies]
alloy-primitives = { version = ">=0.8.5", features = ["map-fxhash"] }
bigdecimal = "0.4.7"
derive_more = { version = "1.0.0", features = ["deref"] }
alloy-primitives = { version = ">=0.8.5", default-features = false, features = ["map-fxhash"] }
bigdecimal = { version = "0.4.7", default-features = false }
derive_more = { version = "2", default-features = false, features = ["deref", "from"] }
eth_checksum = { version = "0.1.2", optional = true }
lazy_static = "1.5"
num-bigint = "0.4"
num-integer = "0.1"
num-bigint = { version = "0.4", default-features = false }
num-integer = { version = "0.1", default-features = false }
regex = { version = "1.11", optional = true }
thiserror = { version = "2", default-features = false }

[features]
std = ["thiserror/std"]
default = []
std = ["alloy-primitives/std", "bigdecimal/std", "derive_more/std", "thiserror/std"]
validate_parse_address = ["eth_checksum", "regex"]

[lib]
doctest = true
73 changes: 35 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ exchange.**

Add this to your Cargo.toml

```
```toml
[dependencies]
uniswap-sdk-core = "3.3.0"
uniswap-sdk-core = "3.5.0"
```

And this to your code:

```
```rust
use uniswap_sdk_core::prelude::*;
```

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

## Examples

The code below shows an example of how to create a new `Token` instance for the DAI token on the Ethereum Mainnet using
the `token!` macro.

<details>
<summary>The code below shows an example of how to create a new `Token` instance for the DAI token on the Ethereum Mainnet using
the `token!` macro.</summary>
<summary>Click to expand</summary>

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

fn main() {
// Define the chain ID, address, decimals, symbol, and name for the token
const CHAIN_ID: u64 = 1; // Ethereum Mainnet
const TOKEN_ADDRESS: &str = "0x6B175474E89094C44Da98b954EedeAC495271d0F"; // DAI Token Address
const DECIMALS: u8 = 18;
const SYMBOL: &str = "DAI";
const NAME: &str = "Dai Stablecoin";

// Use the `token!` macro to create a new `Token` instance
let dai_token = token!(CHAIN_ID, TOKEN_ADDRESS, DECIMALS, SYMBOL, NAME);

// Example usage of the `Token` methods
println!("Token Address: {}", dai_token.address());
println!("Is Native: {}", dai_token.is_native());

// Example of comparing two tokens
let another_dai_token = token!(CHAIN_ID, TOKEN_ADDRESS, DECIMALS, SYMBOL, NAME);
println!("Are the tokens equal? {}", dai_token.equals(&another_dai_token));

// Example of sorting tokens
let another_token = token!(CHAIN_ID, "0000000000000000000000000000000000000002", DECIMALS, "ETH", "Ethereum");
match dai_token.sorts_before(&another_token) {
Ok(true) => println!("DAI sorts before ETH"),
Ok(false) => println!("DAI does not sort before ETH"),
Err(e) => println!("Error comparing tokens: {:?}", e),
}
// Define the chain ID, address, decimals, symbol, and name for the token
const CHAIN_ID: u64 = 1; // Ethereum Mainnet
const TOKEN_ADDRESS: &str = "0x6B175474E89094C44Da98b954EedeAC495271d0F"; // DAI Token Address
const DECIMALS: u8 = 18;
const SYMBOL: &str = "DAI";
const NAME: &str = "Dai Stablecoin";

// Use the `token!` macro to create a new `Token` instance
let dai_token = token!(CHAIN_ID, TOKEN_ADDRESS, DECIMALS, SYMBOL, NAME);

// Example usage of the `Token` methods
println!("Token Address: {}", dai_token.address());
println!("Is Native: {}", dai_token.is_native());

// Example of comparing two tokens
let another_dai_token = token!(CHAIN_ID, TOKEN_ADDRESS, DECIMALS, SYMBOL, NAME);
println!("Are the tokens equal? {}", dai_token.equals(&another_dai_token));

// Example of sorting tokens
let another_token = token!(CHAIN_ID, "0000000000000000000000000000000000000002", DECIMALS, "ETH", "Ethereum");
match dai_token.sorts_before( & another_token) {
Ok(true) => println ! ("DAI sorts before ETH"),
Ok(false) => println ! ("DAI does not sort before ETH"),
Err(e) => println ! ("Error comparing tokens: {:?}", e),
}
```

</details>

This example demonstrates how to create a `Token` instance for DAI on the Ethereum Mainnet using the `token!` macro.

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

</details>

## Contribution

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

## License

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

## Acknowledgments

Expand Down
6 changes: 3 additions & 3 deletions src/addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::prelude::*;
use alloy_primitives::address;
use lazy_static::lazy_static;

pub type AddressMap = FxHashMap<u64, Address>;
pub type AddressMap = HashMap<u64, Address>;

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct ChainAddresses {
Expand Down Expand Up @@ -381,8 +381,8 @@ lazy_static! {
/// This map is used to look up the addresses of various Uniswap contracts
/// for a given network. The keys in the map are the network IDs, and the values
/// are the corresponding contract addresses.
pub static ref CHAIN_TO_ADDRESSES_MAP: FxHashMap<u64, ChainAddresses> = {
FxHashMap::from_iter([
pub static ref CHAIN_TO_ADDRESSES_MAP: HashMap<u64, ChainAddresses> = {
HashMap::from_iter([
(ChainId::MAINNET as u64, MAINNET_ADDRESSES),
(ChainId::OPTIMISM as u64, OPTIMISM_ADDRESSES),
(ChainId::ARBITRUM_ONE as u64, ARBITUM_ONE_ADDRESSES),
Expand Down
4 changes: 2 additions & 2 deletions src/entities/weth9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use alloc::string::ToString;
#[derive(Clone, PartialEq, Debug)]
pub struct WETH9 {
/// A mapping of chain IDs to corresponding WETH tokens.
tokens: FxHashMap<u64, Token>,
tokens: HashMap<u64, Token>,
}

/// Default implementation for [`WETH9`], creating an instance with predefined WETH tokens on
Expand All @@ -31,7 +31,7 @@ impl WETH9 {
#[inline]
#[must_use]
pub fn new() -> Self {
let tokens = FxHashMap::from_iter([
let tokens = HashMap::from_iter([
(1, Self::on_chain(1).unwrap()),
(3, Self::on_chain(3).unwrap()),
(4, Self::on_chain(4).unwrap()),
Expand Down
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
//! # uniswap-sdk-core
//!
//! The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap
//! decentralized exchange.
#![cfg_attr(not(any(feature = "std", test)), no_std)]
#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(
missing_copy_implementations,
missing_debug_implementations,
Expand Down Expand Up @@ -46,8 +42,11 @@ pub mod utils;
pub mod prelude {
pub use crate::{addresses::*, chains::*, constants::*, entities::*, error::Error, utils::*};

pub use alloc::{string::String, vec::Vec};
pub use alloy_primitives::{map::rustc_hash::FxHashMap, Address, Bytes, B256, U256};
pub use alloc::{
string::{String, ToString},
vec::Vec,
};
pub use alloy_primitives::{map::HashMap, Address, Bytes, B256, U256};

pub type BigInt = num_bigint::BigInt;
pub type BigUint = num_bigint::BigUint;
Expand All @@ -56,5 +55,5 @@ pub mod prelude {
}

/// Contains examples of how Uniswap sdk core can be used
#[cfg(test)]
#[cfg(all(feature = "std", test))]
mod examples;
1 change: 1 addition & 0 deletions src/utils/sorted_insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub fn sorted_insert<T: Clone>(
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;

fn cmp(a: &i32, b: &i32) -> Ordering {
a.cmp(b)
Expand Down

0 comments on commit 5e4f81b

Please sign in to comment.