From 39e4582b179fb31b94bdf3fb1735b257aad22390 Mon Sep 17 00:00:00 2001 From: Shuhui Luo Date: Mon, 25 Dec 2023 03:52:58 -0800 Subject: [PATCH 1/2] restrict `decimals` to `u8` and add token tests --- src/entities/base_currency.rs | 2 +- src/entities/currency.rs | 2 +- src/entities/ether.rs | 7 +- src/entities/fractions/currency_amount.rs | 6 +- src/entities/fractions/fraction.rs | 8 +- src/entities/fractions/percent.rs | 4 +- src/entities/fractions/price.rs | 8 +- src/entities/token.rs | 119 ++++++++++++++++------ 8 files changed, 105 insertions(+), 51 deletions(-) diff --git a/src/entities/base_currency.rs b/src/entities/base_currency.rs index 20d17e9..98a833f 100644 --- a/src/entities/base_currency.rs +++ b/src/entities/base_currency.rs @@ -6,7 +6,7 @@ pub trait BaseCurrency: Clone { fn chain_id(&self) -> u32; /// The decimals used in representing currency amounts - fn decimals(&self) -> u32; + fn decimals(&self) -> u8; /// The symbol of the currency, i.e. a short textual non-unique identifier fn symbol(&self) -> Option; diff --git a/src/entities/currency.rs b/src/entities/currency.rs index c2c0919..7be4707 100644 --- a/src/entities/currency.rs +++ b/src/entities/currency.rs @@ -37,7 +37,7 @@ impl BaseCurrency for Currency { } } - fn decimals(&self) -> u32 { + fn decimals(&self) -> u8 { match self { Currency::NativeCurrency(native_currency) => native_currency.decimals(), Currency::Token(token) => token.decimals(), diff --git a/src/entities/ether.rs b/src/entities/ether.rs index 5f8fa57..7fba12c 100644 --- a/src/entities/ether.rs +++ b/src/entities/ether.rs @@ -1,5 +1,4 @@ -use super::{base_currency::BaseCurrency, currency::CurrencyTrait, token::Token}; -use crate::entities::weth9::WETH9; +use super::{base_currency::BaseCurrency, currency::CurrencyTrait, token::Token, weth9::WETH9}; use lazy_static::lazy_static; use std::{collections::HashMap, sync::Mutex}; @@ -11,7 +10,7 @@ lazy_static! { #[derive(Clone, PartialEq)] pub struct Ether { pub chain_id: u32, - pub decimals: u32, + pub decimals: u8, pub symbol: Option, pub name: Option, } @@ -54,7 +53,7 @@ impl BaseCurrency for Ether { self.chain_id } - fn decimals(&self) -> u32 { + fn decimals(&self) -> u8 { self.decimals } diff --git a/src/entities/fractions/currency_amount.rs b/src/entities/fractions/currency_amount.rs index 43e44c3..ac5fcd4 100644 --- a/src/entities/fractions/currency_amount.rs +++ b/src/entities/fractions/currency_amount.rs @@ -36,7 +36,7 @@ impl CurrencyAmount { denominator, meta: CurrencyMeta { currency, - decimal_scale: BigUint::from(10u64).pow(exponent), + decimal_scale: BigUint::from(10u64).pow(exponent as u32), }, } } @@ -156,7 +156,7 @@ impl FractionTrait> for CurrencyAmount { ) } - fn to_significant(&self, significant_digits: u32, rounding: Rounding) -> String { + fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> String { self.as_fraction() .divide(&Fraction::new( self.meta.decimal_scale.to_bigint().unwrap(), @@ -166,7 +166,7 @@ impl FractionTrait> for CurrencyAmount { .to_significant(significant_digits, rounding) } - fn to_fixed(&self, decimal_places: u32, rounding: Rounding) -> String { + fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String { assert!(decimal_places <= self.meta.currency.decimals(), "DECIMALS"); self.as_fraction() .divide(&Fraction::new( diff --git a/src/entities/fractions/fraction.rs b/src/entities/fractions/fraction.rs index c84525d..eb65ef0 100644 --- a/src/entities/fractions/fraction.rs +++ b/src/entities/fractions/fraction.rs @@ -116,7 +116,7 @@ pub trait FractionTrait: Sized { .div(Decimal::from_str(&self.denominator().to_str_radix(10)).unwrap()) } - fn to_significant(&self, significant_digits: u32, rounding: Rounding) -> String { + fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> String { assert!( significant_digits > 0, "Significant digits must be positive." @@ -125,16 +125,16 @@ pub trait FractionTrait: Sized { let rounding_strategy = to_rounding_strategy(rounding); let quotient = self .to_decimal() - .round_sf_with_strategy(significant_digits, rounding_strategy); + .round_sf_with_strategy(significant_digits as u32, rounding_strategy); quotient.unwrap().normalize().to_string() } - fn to_fixed(&self, decimal_places: u32, rounding: Rounding) -> String { + fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String { let rounding_strategy = to_rounding_strategy(rounding); let quotient = self .to_decimal() - .round_dp_with_strategy(decimal_places, rounding_strategy); + .round_dp_with_strategy(decimal_places as u32, rounding_strategy); format!("{:.1$}", quotient, decimal_places as usize) } diff --git a/src/entities/fractions/percent.rs b/src/entities/fractions/percent.rs index 2d08104..71f1208 100644 --- a/src/entities/fractions/percent.rs +++ b/src/entities/fractions/percent.rs @@ -38,13 +38,13 @@ impl FractionTrait<()> for Percent { &self.denominator } - fn to_significant(&self, significant_digits: u32, rounding: Rounding) -> String { + fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> String { self.as_fraction() .multiply(&ONE_HUNDRED) .to_significant(significant_digits, rounding) } - fn to_fixed(&self, decimal_places: u32, rounding: Rounding) -> String { + fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String { self.as_fraction() .multiply(&ONE_HUNDRED) .to_fixed(decimal_places, rounding) diff --git a/src/entities/fractions/price.rs b/src/entities/fractions/price.rs index 3abde75..b0c2b58 100644 --- a/src/entities/fractions/price.rs +++ b/src/entities/fractions/price.rs @@ -61,12 +61,12 @@ where &self.denominator } - fn to_significant(&self, significant_digits: u32, rounding: Rounding) -> String { + fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> String { self.adjusted_for_decimals() .to_significant(significant_digits, rounding) } - fn to_fixed(&self, decimal_places: u32, rounding: Rounding) -> String { + fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String { self.adjusted_for_decimals() .to_fixed(decimal_places, rounding) } @@ -84,8 +84,8 @@ where numerator: impl Into, ) -> Self { let scalar = Fraction::new( - BigInt::from(10).pow(base_currency.decimals()), - BigInt::from(10).pow(quote_currency.decimals()), + BigInt::from(10).pow(base_currency.decimals() as u32), + BigInt::from(10).pow(quote_currency.decimals() as u32), (), ); Self { diff --git a/src/entities/token.rs b/src/entities/token.rs index 4580023..752e644 100644 --- a/src/entities/token.rs +++ b/src/entities/token.rs @@ -6,7 +6,7 @@ use num_bigint::BigUint; pub struct Token { pub chain_id: u32, pub address: String, - pub decimals: u32, + pub decimals: u8, pub symbol: Option, pub name: Option, pub buy_fee_bps: Option, @@ -28,7 +28,7 @@ impl BaseCurrency for Token { self.chain_id } - fn decimals(&self) -> u32 { + fn decimals(&self) -> u8 { self.decimals } @@ -68,7 +68,7 @@ impl Token { pub fn new( chain_id: u32, address: String, - decimals: u32, + decimals: u8, symbol: Option, name: Option, buy_fee_bps: Option, @@ -107,7 +107,6 @@ impl Token { #[cfg(test)] mod tests { - use crate::entities::currency::Currency; //should test for neg chain_id or neg decimals or neg buy_fee or neg sell_fee, but the compiler will panic by itself, so no need use super::*; @@ -141,12 +140,12 @@ mod tests { } #[test] - #[should_panic] + #[should_panic(expected = "DECIMALS")] fn test_expect_revert_overflow_dec() { let _token = Token::new( 4, ADDRESS_ONE.to_string(), - 256, + 255, Some("Test".to_string()), Some("Te".to_string()), None, @@ -155,8 +154,7 @@ mod tests { } #[test] - #[should_panic] - fn test_expect_revert_diff_chain_id() { + fn test_false_if_diff_chain_id() { let token = Token::new( 4, ADDRESS_ONE.to_string(), @@ -166,7 +164,6 @@ mod tests { None, None, ); - let token_1 = Token::new( 3, ADDRESS_ONE.to_string(), @@ -177,10 +174,7 @@ mod tests { None, ); - assert!( - token.equals(&Currency::Token(token_1)), - "SHOULD_FAILS_EVEN_THOUGH_CHAIN_ID_IS_DIFFERENT" - ); + assert!(!token.equals(&token_1)); } #[test] @@ -194,7 +188,6 @@ mod tests { None, None, ); - let token_1 = Token::new( 4, ADDRESS_ONE.to_string(), @@ -205,10 +198,7 @@ mod tests { None, ); - assert!( - token.equals(&Currency::Token(token_1)), - "true even if names differ" - ); + assert!(token.equals(&token_1), "true even if names differ"); } #[test] @@ -222,7 +212,6 @@ mod tests { None, None, ); - let token_1 = Token::new( 4, ADDRESS_ONE.to_string(), @@ -233,15 +222,11 @@ mod tests { None, ); - assert!( - token.equals(&Currency::Token(token_1)), - "true even if symbols differ" - ); + assert!(token.equals(&token_1), "true even if symbols differ"); } #[test] - #[should_panic] - fn test_expect_revert_diff_address() { + fn test_false_if_diff_address() { let token = Token::new( 4, ADDRESS_ONE.to_string(), @@ -251,7 +236,6 @@ mod tests { None, None, ); - let token_1 = Token::new( 4, DAI_MAINNET.to_string(), @@ -262,9 +246,21 @@ mod tests { None, ); + assert!(!token.equals(&token_1)); + } + + #[test] + fn test_true_if_diff_decimals() { assert!( - token.equals(&Currency::Token(token_1)), - "SHOULD_FAILS_EVEN_THOUGH_ADDRESS_IS_DIFFERENT" + Token::new(1, ADDRESS_ONE.to_string(), 9, None, None, None, None,).equals(&Token::new( + 1, + ADDRESS_ONE.to_string(), + 18, + None, + None, + None, + None, + )) ); } @@ -290,10 +286,69 @@ mod tests { None, ); - assert_eq!( - token.equals(&Currency::Token(token_1.clone())), - token_1.equals(&Currency::Token(token)), - "SHOULD_FAILS_EVEN_THOUGH_ADDRESS_IS_DIFFERENT, SHOULD ONLY REVERT FOR DIFFERENT CHAIN_ID" + assert_eq!(token.equals(&token_1), token_1.equals(&token)); + } + + #[test] + fn test_true_on_reference_equality() { + let token = Token::new( + 1, + ADDRESS_ONE.to_string(), + 18, + Some("Test".to_string()), + Some("Te".to_string()), + None, + None, + ); + + assert!(token.equals(&token)); + } + + #[test] + fn test_true_if_same_address() { + let token = Token::new( + 1, + ADDRESS_ONE.to_string(), + 9, + Some("abc".to_string()), + Some("def".to_string()), + None, + None, + ); + let token_1 = Token::new( + 1, + ADDRESS_ONE.to_string(), + 18, + Some("ghi".to_string()), + Some("jkl".to_string()), + None, + None, + ); + + assert!(token.equals(&token_1)); + } + + #[test] + fn test_true_if_one_token_is_checksummed_and_the_other_is_not() { + let token_a = Token::new( + 1, + DAI_MAINNET.to_string(), + 18, + Some("DAI".to_string()), + None, + None, + None, ); + let token_b = Token::new( + 1, + DAI_MAINNET.to_string().to_lowercase(), + 18, + Some("DAI".to_string()), + None, + None, + None, + ); + + assert!(token_a.equals(&token_b)); } } From d065544ba7a346c750f3baa63928990b1c4eea8f Mon Sep 17 00:00:00 2001 From: Shuhui Luo Date: Mon, 25 Dec 2023 04:17:49 -0800 Subject: [PATCH 2/2] use `Address` from `alloy-rs` --- Cargo.lock | 818 ++++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 +- src/entities/currency.rs | 5 +- src/entities/ether.rs | 3 +- src/entities/token.rs | 26 +- 5 files changed, 833 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d19542f..8295459 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,18 +22,211 @@ dependencies = [ "memchr", ] +[[package]] +name = "alloy-primitives" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c0e5e60ff0e0c34c553822dabcfe0f5fece5a8c52f08a915be8c737de4b03fa" +dependencies = [ + "alloy-rlp", + "bytes", + "cfg-if", + "const-hex", + "derive_more", + "hex-literal", + "itoa", + "proptest", + "rand 0.8.5", + "ruint", + "serde", + "tiny-keccak", +] + +[[package]] +name = "alloy-rlp" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d58d9f5da7b40e9bfff0b7e7816700be4019db97d4b6359fe7f94a9e22e42ac" +dependencies = [ + "arrayvec", + "bytes", +] + +[[package]] +name = "ark-ff" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b3235cc41ee7a12aaaf2c575a2ad7b46713a8a50bda2fc3b003a04845c05dd6" +dependencies = [ + "ark-ff-asm 0.3.0", + "ark-ff-macros 0.3.0", + "ark-serialize 0.3.0", + "ark-std 0.3.0", + "derivative", + "num-bigint", + "num-traits", + "paste", + "rustc_version 0.3.3", + "zeroize", +] + +[[package]] +name = "ark-ff" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec847af850f44ad29048935519032c33da8aa03340876d351dfab5660d2966ba" +dependencies = [ + "ark-ff-asm 0.4.2", + "ark-ff-macros 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", + "derivative", + "digest 0.10.7", + "itertools", + "num-bigint", + "num-traits", + "paste", + "rustc_version 0.4.0", + "zeroize", +] + +[[package]] +name = "ark-ff-asm" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db02d390bf6643fb404d3d22d31aee1c4bc4459600aef9113833d17e786c6e44" +dependencies = [ + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-ff-asm" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed4aa4fe255d0bc6d79373f7e31d2ea147bcf486cba1be5ba7ea85abdb92348" +dependencies = [ + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-ff-macros" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db2fd794a08ccb318058009eefdf15bcaaaaf6f8161eb3345f907222bac38b20" +dependencies = [ + "num-bigint", + "num-traits", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-ff-macros" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abe79b0e4288889c4574159ab790824d0033b9fdcb2a112a3182fac2e514565" +dependencies = [ + "num-bigint", + "num-traits", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-serialize" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6c2b318ee6e10f8c2853e73a83adc0ccb88995aa978d8a3408d492ab2ee671" +dependencies = [ + "ark-std 0.3.0", + "digest 0.9.0", +] + +[[package]] +name = "ark-serialize" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb7b85a02b83d2f22f89bd5cac66c9c89474240cb6207cb1efc16d098e822a5" +dependencies = [ + "ark-std 0.4.0", + "digest 0.10.7", + "num-bigint", +] + +[[package]] +name = "ark-std" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df2c09229cbc5a028b1d70e00fdb2acee28b1055dfb5ca73eea49c5a25c4e7c" +dependencies = [ + "num-traits", + "rand 0.8.5", +] + +[[package]] +name = "ark-std" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94893f1e0c6eeab764ade8dc4c0db24caf4fe7cbbaafc0eba0a9030f447b5185" +dependencies = [ + "num-traits", + "rand 0.8.5", +] + [[package]] name = "arrayvec" version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" +[[package]] +name = "auto_impl" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fee3da8ef1276b0bee5dd1c7258010d8fffd31801447323115a25560e1327b89" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "bit-set" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + [[package]] name = "bitvec" version = "1.0.1" @@ -79,6 +272,12 @@ dependencies = [ "syn_derive", ] +[[package]] +name = "byte-slice-cast" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3ac9f8b63eca6fd385229b3675f6cc0dc5c8a5c8a54a59d4f52ffd670d87b0c" + [[package]] name = "bytecheck" version = "0.6.11" @@ -101,6 +300,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.5.0" @@ -119,6 +324,25 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" +[[package]] +name = "const-hex" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5104de16b218eddf8e34ffe2f86f74bfa4e61e95a1b89732fccf6325efd0557" +dependencies = [ + "cfg-if", + "cpufeatures", + "hex", + "proptest", + "serde", +] + +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "cpufeatures" version = "0.2.11" @@ -128,6 +352,12 @@ dependencies = [ "libc", ] +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "crypto-common" version = "0.1.6" @@ -138,6 +368,39 @@ dependencies = [ "typenum", ] +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version 0.4.0", + "syn 1.0.109", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + [[package]] name = "digest" version = "0.10.7" @@ -148,12 +411,28 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + [[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "errno" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "eth_checksum" version = "0.1.2" @@ -163,6 +442,41 @@ dependencies = [ "rust-crypto", ] +[[package]] +name = "fastrand" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" + +[[package]] +name = "fastrlp" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "139834ddba373bbdd213dffe02c8d110508dcf1726c2be27e8d1f7d7e1856418" +dependencies = [ + "arrayvec", + "auto_impl", + "bytes", +] + +[[package]] +name = "fixed-hash" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534" +dependencies = [ + "byteorder", + "rand 0.8.5", + "rustc-hex", + "static_assertions", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "fuchsia-cprng" version = "0.1.1" @@ -217,6 +531,38 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + +[[package]] +name = "impl-codec" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba6a270039626615617f3f36d15fc827041df3b78c439da2cadfa47455a77f2f" +dependencies = [ + "parity-scale-codec", +] + +[[package]] +name = "impl-trait-for-tuples" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11d7a9f6330b71fea57921c9b61c47ee6e84f72d394754eff6163ae67e7395eb" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "indexmap" version = "2.1.0" @@ -227,6 +573,15 @@ dependencies = [ "hashbrown 0.14.3", ] +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.10" @@ -254,6 +609,18 @@ version = "0.2.151" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" +[[package]] +name = "libm" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" + +[[package]] +name = "linux-raw-sys" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" + [[package]] name = "memchr" version = "2.6.4" @@ -300,6 +667,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" dependencies = [ "autocfg", + "libm", ] [[package]] @@ -317,12 +685,66 @@ dependencies = [ "num-traits", ] +[[package]] +name = "parity-scale-codec" +version = "3.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "881331e34fa842a2fb61cc2db9643a8fedc615e47cfcc52597d1af0db9a7e8fe" +dependencies = [ + "arrayvec", + "bitvec", + "byte-slice-cast", + "impl-trait-for-tuples", + "parity-scale-codec-derive", + "serde", +] + +[[package]] +name = "parity-scale-codec-derive" +version = "3.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be30eaf4b0a9fba5336683b38de57bb86d179a35862ba6bfcf57625d006bde5b" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "paste" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" + +[[package]] +name = "pest" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae9cee2a55a544be8b89dc6848072af97a20f2422603c10865be2a42b580fff5" +dependencies = [ + "memchr", + "thiserror", + "ucd-trie", +] + [[package]] name = "ppv-lite86" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "primitive-types" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b34d9fd68ae0b74a41b21c03c2f62847aa0ffea044eee893b4c140b37e244e2" +dependencies = [ + "fixed-hash", + "impl-codec", + "uint", +] + [[package]] name = "proc-macro-crate" version = "2.0.1" @@ -342,6 +764,7 @@ dependencies = [ "proc-macro-error-attr", "proc-macro2", "quote", + "syn 1.0.109", "version_check", ] @@ -365,6 +788,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "proptest" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31b476131c3c86cb68032fdc5cb6d5a1045e3e42d96b69fa599fd77701e1f5bf" +dependencies = [ + "bit-set", + "bit-vec", + "bitflags 2.4.1", + "lazy_static", + "num-traits", + "rand 0.8.5", + "rand_chacha", + "rand_xorshift", + "regex-syntax", + "rusty-fork", + "tempfile", + "unarray", +] + [[package]] name = "ptr_meta" version = "0.1.4" @@ -385,6 +828,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quote" version = "1.0.33" @@ -468,6 +917,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rand_xorshift" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f" +dependencies = [ + "rand_core 0.6.4", +] + [[package]] name = "rdrand" version = "0.4.0" @@ -477,6 +935,15 @@ dependencies = [ "rand_core 0.3.1", ] +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags 1.3.2", +] + [[package]] name = "regex" version = "1.10.2" @@ -544,6 +1011,46 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "rlp" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb919243f34364b6bd2fc10ef797edbfa75f33c252e7998527479c6d6b47e1ec" +dependencies = [ + "bytes", + "rustc-hex", +] + +[[package]] +name = "ruint" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608a5726529f2f0ef81b8fde9873c4bb829d6b5b5ca6be4d97345ddf0749c825" +dependencies = [ + "alloy-rlp", + "ark-ff 0.3.0", + "ark-ff 0.4.2", + "bytes", + "fastrlp", + "num-bigint", + "num-traits", + "parity-scale-codec", + "primitive-types", + "proptest", + "rand 0.8.5", + "rlp", + "ruint-macro", + "serde", + "valuable", + "zeroize", +] + +[[package]] +name = "ruint-macro" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e666a5496a0b2186dbcd0ff6106e29e093c15591bde62c20d3842007c6978a09" + [[package]] name = "rust-crypto" version = "0.2.36" @@ -585,6 +1092,49 @@ version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe834bc780604f4674073badbad26d7219cadfb4a2275802db12cbae17498401" +[[package]] +name = "rustc_version" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee" +dependencies = [ + "semver 0.11.0", +] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver 1.0.20", +] + +[[package]] +name = "rustix" +version = "0.38.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316" +dependencies = [ + "bitflags 2.4.1", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "rusty-fork" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb3dcc6e454c328bb824492db107ab7c0ae8fcffe4ad210136ef014458c1bc4f" +dependencies = [ + "fnv", + "quick-error", + "tempfile", + "wait-timeout", +] + [[package]] name = "ryu" version = "1.0.16" @@ -597,6 +1147,30 @@ version = "4.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" +[[package]] +name = "semver" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" + +[[package]] +name = "semver-parser" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" +dependencies = [ + "pest", +] + [[package]] name = "serde" version = "1.0.193" @@ -644,7 +1218,7 @@ version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60" dependencies = [ - "digest", + "digest 0.10.7", "keccak", ] @@ -654,6 +1228,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "syn" version = "1.0.109" @@ -694,6 +1274,39 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "tempfile" +version = "3.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef1adac450ad7f4b3c28589471ade84f25f731a7a0fe30d71dfa9f60fd808e5" +dependencies = [ + "cfg-if", + "fastrand", + "redox_syscall", + "rustix", + "windows-sys 0.48.0", +] + +[[package]] +name = "thiserror" +version = "1.0.51" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f11c217e1416d6f036b870f14e0413d480dbf28edbee1f877abaf0206af43bb7" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.51" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01742297787513b79cf8e29d1056ede1313e2420b7b3b15d0a768b4921f549df" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.42", +] + [[package]] name = "time" version = "0.1.45" @@ -705,6 +1318,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "tiny-keccak" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" +dependencies = [ + "crunchy", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -743,6 +1365,30 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "ucd-trie" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9" + +[[package]] +name = "uint" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76f64bba2c53b04fcab63c01a7d7427eadc821e3bc48c34dc9ba29c501164b52" +dependencies = [ + "byteorder", + "crunchy", + "hex", + "static_assertions", +] + +[[package]] +name = "unarray" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" + [[package]] name = "unicode-ident" version = "1.0.12" @@ -751,8 +1397,9 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "uniswap-sdk-core-rust" -version = "0.2.0" +version = "0.3.0" dependencies = [ + "alloy-primitives", "eth_checksum", "lazy_static", "num-bigint", @@ -772,12 +1419,27 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" @@ -812,6 +1474,138 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.0", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +dependencies = [ + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" + [[package]] name = "winnow" version = "0.5.30" @@ -829,3 +1623,23 @@ checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" dependencies = [ "tap", ] + +[[package]] +name = "zeroize" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.42", +] diff --git a/Cargo.toml b/Cargo.toml index a5f229a..900f128 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,12 @@ [package] name = "uniswap-sdk-core-rust" -version = "0.2.0" +version = "0.3.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +alloy-primitives = "0.5.3" eth_checksum = "0.1.2" lazy_static = "1.4.0" num-bigint = "0.4.4" diff --git a/src/entities/currency.rs b/src/entities/currency.rs index 7be4707..c574de5 100644 --- a/src/entities/currency.rs +++ b/src/entities/currency.rs @@ -1,4 +1,5 @@ use super::{base_currency::BaseCurrency, ether::Ether, token::Token}; +use alloy_primitives::Address; #[derive(Clone, PartialEq)] pub enum Currency { @@ -10,7 +11,7 @@ pub trait CurrencyTrait: BaseCurrency { /// Returns whether the currency is native to the chain and must be wrapped (e.g. Ether) fn is_native(&self) -> bool; - fn address(&self) -> String; + fn address(&self) -> Address; } impl CurrencyTrait for Currency { @@ -21,7 +22,7 @@ impl CurrencyTrait for Currency { } } - fn address(&self) -> String { + fn address(&self) -> Address { match self { Currency::NativeCurrency(native_currency) => native_currency.address(), Currency::Token(token) => token.address(), diff --git a/src/entities/ether.rs b/src/entities/ether.rs index 7fba12c..7be5211 100644 --- a/src/entities/ether.rs +++ b/src/entities/ether.rs @@ -1,4 +1,5 @@ use super::{base_currency::BaseCurrency, currency::CurrencyTrait, token::Token, weth9::WETH9}; +use alloy_primitives::Address; use lazy_static::lazy_static; use std::{collections::HashMap, sync::Mutex}; @@ -20,7 +21,7 @@ impl CurrencyTrait for Ether { true } - fn address(&self) -> String { + fn address(&self) -> Address { self.wrapped().address() } } diff --git a/src/entities/token.rs b/src/entities/token.rs index 752e644..bb7a150 100644 --- a/src/entities/token.rs +++ b/src/entities/token.rs @@ -1,11 +1,12 @@ use super::{base_currency::BaseCurrency, currency::CurrencyTrait}; +use alloy_primitives::Address; use num_bigint::BigUint; /// Represents an ERC20 token with a unique address and some metadata. #[derive(Clone, PartialEq)] pub struct Token { pub chain_id: u32, - pub address: String, + pub address: Address, pub decimals: u8, pub symbol: Option, pub name: Option, @@ -18,8 +19,8 @@ impl CurrencyTrait for Token { false } - fn address(&self) -> String { - self.address.to_string() + fn address(&self) -> Address { + self.address } } @@ -50,10 +51,7 @@ impl BaseCurrency for Token { /// fn equals(&self, other: &impl CurrencyTrait) -> bool { match other.is_native() { - false => { - self.chain_id == other.chain_id() - && self.address.to_lowercase() == other.address().to_lowercase() - } + false => self.chain_id == other.chain_id() && self.address == other.address(), _ => false, } } @@ -78,7 +76,7 @@ impl Token { assert!(decimals < 255, "DECIMALS"); Self { chain_id, - address, + address: address.parse().unwrap(), decimals, symbol, name, @@ -96,12 +94,8 @@ impl Token { /// pub fn sorts_before(&self, other: &Token) -> bool { assert_eq!(self.chain_id, other.chain_id, "CHAIN_IDS"); - assert_ne!( - self.address.to_lowercase(), - other.address.to_lowercase(), - "ADDRESSES" - ); - self.address.to_lowercase() < other.address.to_lowercase() + assert_ne!(self.address, other.address, "ADDRESSES"); + self.address.lt(&other.address) } } @@ -135,8 +129,8 @@ mod tests { None, ); - assert_eq!(token.address, *ADDRESS_ONE); - assert_eq!(token_1.address, *ADDRESS_TWO); + assert!(token.address.eq(&ADDRESS_ONE.parse::
().unwrap())); + assert!(token_1.address.eq(&ADDRESS_TWO.parse::
().unwrap())); } #[test]