diff --git a/Cargo.toml b/Cargo.toml index a79f38b..763d76f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,25 +1,23 @@ [package] name = "uniswap-sdk-core" -version = "3.4.0" +version = "3.5.0" edition = "2021" authors = ["malik ", "Shuhui Luo "] 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 diff --git a/README.md b/README.md index ce76b93..b2f8714 100644 --- a/README.md +++ b/README.md @@ -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::*; ``` @@ -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. +
- 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. + Click to expand ```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), } ``` -
- 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). @@ -83,6 +78,8 @@ 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). + + ## Contribution Contributions are welcome! If you find a bug or have suggestions for improvements, feel free to open an issue or submit @@ -90,7 +87,7 @@ a pull request on the [GitHub repository](https://github.com/malik672/uniswap-sd ## 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 diff --git a/src/addresses.rs b/src/addresses.rs index 6f60c6f..0d78b5a 100644 --- a/src/addresses.rs +++ b/src/addresses.rs @@ -2,7 +2,7 @@ use crate::prelude::*; use alloy_primitives::address; use lazy_static::lazy_static; -pub type AddressMap = FxHashMap; +pub type AddressMap = HashMap; #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] pub struct ChainAddresses { @@ -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 = { - FxHashMap::from_iter([ + pub static ref CHAIN_TO_ADDRESSES_MAP: HashMap = { + HashMap::from_iter([ (ChainId::MAINNET as u64, MAINNET_ADDRESSES), (ChainId::OPTIMISM as u64, OPTIMISM_ADDRESSES), (ChainId::ARBITRUM_ONE as u64, ARBITUM_ONE_ADDRESSES), diff --git a/src/entities/weth9.rs b/src/entities/weth9.rs index 46b4c15..66a90aa 100644 --- a/src/entities/weth9.rs +++ b/src/entities/weth9.rs @@ -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, + tokens: HashMap, } /// Default implementation for [`WETH9`], creating an instance with predefined WETH tokens on @@ -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()), diff --git a/src/lib.rs b/src/lib.rs index a0e7245..be2190a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, @@ -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; @@ -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; diff --git a/src/utils/sorted_insert.rs b/src/utils/sorted_insert.rs index be165cc..816b44b 100644 --- a/src/utils/sorted_insert.rs +++ b/src/utils/sorted_insert.rs @@ -37,6 +37,7 @@ pub fn sorted_insert( #[cfg(test)] mod tests { use super::*; + use alloc::vec; fn cmp(a: &i32, b: &i32) -> Ordering { a.cmp(b)