Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update derive_more and fastnum #106

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-sdk-core"
version = "4.0.0-rc3"
version = "4.0.0-rc4"
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"
Expand All @@ -12,20 +12,18 @@ keywords = ["sdk-core", "ethereum", "sdk"]
exclude = [".github", ".gitignore", "rustfmt.toml"]

[dependencies]
alloy-primitives = { version = ">=0.8.5", features = ["map-fxhash"] }
alloy-primitives = { version = ">=0.8.5", default-features = false, features = ["map-fxhash"] }
bnum = "0.12.0"
derive_more = { version = "1.0.0", features = ["deref"] }
derive_more = { version = "2", default-features = false, features = ["deref", "from"] }
eth_checksum = { version = "0.1.2", optional = true }
fastnum = { version = "0.1.9", default-features = false, features = ["libm", "numtraits"] }
fastnum = { version = "0.1.11", default-features = false, features = ["numtraits"] }
lazy_static = "1.5"
num-integer = "0.1.46"
num-integer = { version = "0.1", default-features = false }
num-traits = { version = "0.2.19", default-features = false, features = ["libm"] }
regex = { version = "1.11", optional = true }
thiserror = { version = "2", default-features = false }

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

[lib]
doctest = true
71 changes: 34 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ exchange.**

Add this to your Cargo.toml

```
```toml
[dependencies]
uniswap-sdk-core = "4.0.0"
```

And this to your code:

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

Expand All @@ -29,48 +29,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 @@ -84,14 +79,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
18 changes: 9 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 All @@ -22,6 +18,7 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

extern crate alloc;
use num_traits as _;

/// Contains functionality related to All Contracts deployed and supported by the Uniswap SDK.
pub mod addresses;
Expand All @@ -46,8 +43,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 use bnum;
pub use fastnum;
pub use num_integer::Integer;
Expand All @@ -59,5 +59,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