diff --git a/Cargo.lock b/Cargo.lock index 4ef2948..2e48289 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,17 +38,6 @@ dependencies = [ "cpufeatures", ] -[[package]] -name = "ahash" -version = "0.7.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a824f2aa7e75a0c98c5a504fceb80649e9c35265d44525b5f94de4771a395cd" -dependencies = [ - "getrandom", - "once_cell", - "version_check", -] - [[package]] name = "aho-corasick" version = "1.1.2" @@ -390,6 +379,19 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" +[[package]] +name = "bigdecimal" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06619be423ea5bb86c95f087d5707942791a08a85530df0db2209a3ecfb8bc9" +dependencies = [ + "autocfg", + "libm", + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "bit-set" version = "0.5.3" @@ -438,30 +440,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "borsh" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26d4d6dafc1a3bb54687538972158f07b2c948bc57d5890df22c0739098b3028" -dependencies = [ - "borsh-derive", - "cfg_aliases", -] - -[[package]] -name = "borsh-derive" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf4918709cc4dd777ad2b6303ed03cb37f3ca0ccede8c1b0d28ac6db8f4710e0" -dependencies = [ - "once_cell", - "proc-macro-crate 2.0.1", - "proc-macro2", - "quote", - "syn 2.0.43", - "syn_derive", -] - [[package]] name = "bs58" version = "0.5.0" @@ -484,28 +462,6 @@ version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3ac9f8b63eca6fd385229b3675f6cc0dc5c8a5c8a54a59d4f52ffd670d87b0c" -[[package]] -name = "bytecheck" -version = "0.6.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b6372023ac861f6e6dc89c8344a8f398fb42aaba2b5dbc649ca0c0e9dbcb627" -dependencies = [ - "bytecheck_derive", - "ptr_meta", - "simdutf8", -] - -[[package]] -name = "bytecheck_derive" -version = "0.6.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7ec4c6f261935ad534c0c22dbef2201b45918860eb1c574b972bd213a76af61" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "byteorder" version = "1.5.0" @@ -574,12 +530,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cfg_aliases" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" - [[package]] name = "chrono" version = "0.4.31" @@ -1028,7 +978,7 @@ dependencies = [ "sha2", "sha3", "thiserror", - "uuid 0.8.2", + "uuid", ] [[package]] @@ -1558,15 +1508,6 @@ version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" -[[package]] -name = "hashbrown" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -dependencies = [ - "ahash", -] - [[package]] name = "hashbrown" version = "0.14.3" @@ -1737,7 +1678,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" dependencies = [ "equivalent", - "hashbrown 0.14.3", + "hashbrown", ] [[package]] @@ -2293,26 +2234,6 @@ dependencies = [ "unarray", ] -[[package]] -name = "ptr_meta" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" -dependencies = [ - "ptr_meta_derive", -] - -[[package]] -name = "ptr_meta_derive" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "quick-error" version = "1.2.3" @@ -2478,15 +2399,6 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" -[[package]] -name = "rend" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2571463863a6bd50c32f94402933f03457a3fbaf697a707c5be741e459f08fd" -dependencies = [ - "bytecheck", -] - [[package]] name = "reqwest" version = "0.11.23" @@ -2556,35 +2468,6 @@ dependencies = [ "digest 0.10.7", ] -[[package]] -name = "rkyv" -version = "0.7.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "527a97cdfef66f65998b5f3b637c26f5a5ec09cc52a3f9932313ac645f4190f5" -dependencies = [ - "bitvec", - "bytecheck", - "bytes", - "hashbrown 0.12.3", - "ptr_meta", - "rend", - "rkyv_derive", - "seahash", - "tinyvec", - "uuid 1.6.1", -] - -[[package]] -name = "rkyv_derive" -version = "0.7.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5c462a1328c8e67e4d6dbad1eb0355dd43e8ab432c6e227a43657f16ade5033" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "rlp" version = "0.5.2" @@ -2650,22 +2533,6 @@ dependencies = [ "time 0.1.45", ] -[[package]] -name = "rust_decimal" -version = "1.33.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06676aec5ccb8fc1da723cc8c0f9a46549f21ebb8753d3915c6c41db1e7f1dc4" -dependencies = [ - "arrayvec", - "borsh", - "bytes", - "num-traits", - "rand 0.8.5", - "rkyv", - "serde", - "serde_json", -] - [[package]] name = "rustc-demangle" version = "0.1.23" @@ -2793,12 +2660,6 @@ dependencies = [ "sha2", ] -[[package]] -name = "seahash" -version = "4.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" - [[package]] name = "sec1" version = "0.7.3" @@ -2955,12 +2816,6 @@ dependencies = [ "rand_core 0.6.4", ] -[[package]] -name = "simdutf8" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" - [[package]] name = "simple_asn1" version = "0.6.2" @@ -3076,18 +2931,6 @@ dependencies = [ "syn 2.0.43", ] -[[package]] -name = "syn_derive" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" -dependencies = [ - "proc-macro-error", - "proc-macro2", - "quote", - "syn 2.0.43", -] - [[package]] name = "system-configuration" version = "0.5.1" @@ -3408,11 +3251,11 @@ checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" [[package]] name = "uniswap-sdk-core-rust" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2aeb9e2f0422d94b6faa743b0aa73b8fa2d3862be5d86f20be6cf538f8990870" +version = "0.5.0" +source = "git+https://github.com/malik672/uniswap-sdk-core-rust?branch=master#f52d1f7615119ce8ffd1d306ddcad1fb7e113814" dependencies = [ "alloy-primitives", + "bigdecimal", "eth_checksum", "lazy_static", "num-bigint", @@ -3420,7 +3263,6 @@ dependencies = [ "num-rational", "num-traits", "regex", - "rust_decimal", "rustc-hex", "serde-value", "sha3", @@ -3428,7 +3270,7 @@ dependencies = [ [[package]] name = "uniswap-v3-sdk-rs" -version = "0.1.0" +version = "0.2.0" dependencies = [ "alloy-dyn-abi", "alloy-json-abi", @@ -3483,12 +3325,6 @@ dependencies = [ "serde", ] -[[package]] -name = "uuid" -version = "1.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560" - [[package]] name = "valuable" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 7fa5d53..b382025 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uniswap-v3-sdk-rs" -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -15,7 +15,7 @@ num-bigint = "0.4.4" num-traits = "0.2.17" once_cell = "1.19.0" ruint = "1.11.1" -uniswap-sdk-core-rust = "0.4.0" +uniswap-sdk-core-rust = { git = "https://github.com/malik672/uniswap-sdk-core-rust", branch = "master" } uniswap_v3_math = "0.4.1" [dev-dependencies] diff --git a/src/utils/price_tick_conversions.rs b/src/utils/price_tick_conversions.rs index 00157e2..4bb7617 100644 --- a/src/utils/price_tick_conversions.rs +++ b/src/utils/price_tick_conversions.rs @@ -23,9 +23,9 @@ pub fn tick_to_price( tick: i32, ) -> Result> { let sqrt_ratio_x96 = get_sqrt_ratio_at_tick(tick)?; - let ratio_x192 = sqrt_ratio_x96 * sqrt_ratio_x96; - let q192 = BigUint::from_radix_be(&Q192.to_be_bytes::<32>(), 16).unwrap(); - let ratio_x192 = BigUint::from_radix_be(&ratio_x192.to_be_bytes::<32>(), 16).unwrap(); + let sqrt_ratio_x96 = BigUint::from_bytes_be(&sqrt_ratio_x96.to_be_bytes::<32>()); + let ratio_x192 = &sqrt_ratio_x96 * &sqrt_ratio_x96; + let q192 = BigUint::from_bytes_be(&Q192.to_be_bytes::<32>()); Ok(if base_token.sorts_before("e_token) { Price::new(base_token, quote_token, q192, ratio_x192) } else { @@ -68,3 +68,247 @@ pub fn price_to_closest_tick(price: Price) -> Result { tick }) } + +#[cfg(test)] +mod tests { + use super::*; + use num_bigint::BigInt; + use once_cell::sync::Lazy; + use uniswap_sdk_core_rust::constants::Rounding; + + static TOKEN0: Lazy = Lazy::new(|| { + Token::new( + 1, + "0x0000000000000000000000000000000000000000".to_string(), + 18, + Some("T0".to_string()), + Some("token0".to_string()), + None, + None, + ) + }); + static TOKEN1: Lazy = Lazy::new(|| { + Token::new( + 1, + "0x1111111111111111111111111111111111111111".to_string(), + 18, + Some("T1".to_string()), + Some("token1".to_string()), + None, + None, + ) + }); + static TOKEN2_6DECIMALS: Lazy = Lazy::new(|| { + Token::new( + 1, + "0x2222222222222222222222222222222222222222".to_string(), + 6, + Some("T2".to_string()), + Some("token2".to_string()), + None, + None, + ) + }); + + #[test] + fn tick_to_price_test_1() { + assert_eq!( + tick_to_price(TOKEN1.clone(), TOKEN0.clone(), -74959) + .unwrap() + .to_significant(5, Rounding::RoundHalfUp), + "1800" + ); + } + + #[test] + fn tick_to_price_test_2() { + assert_eq!( + tick_to_price(TOKEN0.clone(), TOKEN1.clone(), -74959) + .unwrap() + .to_significant(5, Rounding::RoundHalfUp), + "0.00055556" + ); + } + + #[test] + fn tick_to_price_test_3() { + assert_eq!( + tick_to_price(TOKEN0.clone(), TOKEN1.clone(), 74959) + .unwrap() + .to_significant(5, Rounding::RoundHalfUp), + "1800" + ); + } + + #[test] + fn tick_to_price_test_4() { + assert_eq!( + tick_to_price(TOKEN1.clone(), TOKEN0.clone(), 74959) + .unwrap() + .to_significant(5, Rounding::RoundHalfUp), + "0.00055556" + ); + } + + #[test] + fn tick_to_price_test_5() { + assert_eq!( + tick_to_price(TOKEN0.clone(), TOKEN2_6DECIMALS.clone(), -276225) + .unwrap() + .to_significant(5, Rounding::RoundHalfUp), + "1.01" + ); + } + + #[test] + fn tick_to_price_test_6() { + assert_eq!( + tick_to_price(TOKEN2_6DECIMALS.clone(), TOKEN0.clone(), -276225) + .unwrap() + .to_significant(5, Rounding::RoundHalfUp), + "0.99015" + ); + } + + #[test] + fn tick_to_price_test_7() { + assert_eq!( + tick_to_price(TOKEN0.clone(), TOKEN2_6DECIMALS.clone(), -276423) + .unwrap() + .to_significant(5, Rounding::RoundHalfUp), + "0.99015" + ); + } + + #[test] + fn tick_to_price_test_8() { + assert_eq!( + tick_to_price(TOKEN2_6DECIMALS.clone(), TOKEN0.clone(), -276423) + .unwrap() + .to_significant(5, Rounding::RoundHalfUp), + "1.0099" + ); + } + + #[test] + fn tick_to_price_test_9() { + assert_eq!( + tick_to_price(TOKEN0.clone(), TOKEN2_6DECIMALS.clone(), -276225) + .unwrap() + .to_significant(5, Rounding::RoundHalfUp), + "1.01" + ); + } + + #[test] + fn tick_to_price_test_10() { + assert_eq!( + tick_to_price(TOKEN2_6DECIMALS.clone(), TOKEN0.clone(), -276225) + .unwrap() + .to_significant(5, Rounding::RoundHalfUp), + "0.99015" + ); + } + + #[test] + fn price_to_closest_tick_test_1() { + assert_eq!( + price_to_closest_tick(Price::new(TOKEN1.clone(), TOKEN0.clone(), 1, 1800)).unwrap(), + -74960 + ); + } + + #[test] + fn price_to_closest_tick_test_2() { + assert_eq!( + price_to_closest_tick(Price::new(TOKEN0.clone(), TOKEN1.clone(), 1800, 1)).unwrap(), + -74960 + ); + } + + #[test] + fn price_to_closest_tick_test_3() { + assert_eq!( + price_to_closest_tick(Price::new( + TOKEN0.clone(), + TOKEN2_6DECIMALS.clone(), + BigInt::from(100) * BigInt::from(10).pow(18), + BigInt::from(101) * BigInt::from(10).pow(6), + )) + .unwrap(), + -276225 + ); + } + + #[test] + fn price_to_closest_tick_test_4() { + assert_eq!( + price_to_closest_tick(Price::new( + TOKEN2_6DECIMALS.clone(), + TOKEN0.clone(), + BigInt::from(101) * BigInt::from(10).pow(6), + BigInt::from(100) * BigInt::from(10).pow(18), + )) + .unwrap(), + -276225 + ); + } + + #[test] + fn price_to_closest_tick_test_5() { + assert_eq!( + price_to_closest_tick(tick_to_price(TOKEN1.clone(), TOKEN0.clone(), -74960).unwrap()) + .unwrap(), + -74960 + ); + } + + #[test] + fn price_to_closest_tick_test_6() { + assert_eq!( + price_to_closest_tick(tick_to_price(TOKEN1.clone(), TOKEN0.clone(), 74960).unwrap()) + .unwrap(), + 74960 + ); + } + + #[test] + fn price_to_closest_tick_test_7() { + assert_eq!( + price_to_closest_tick(tick_to_price(TOKEN0.clone(), TOKEN1.clone(), -74960).unwrap()) + .unwrap(), + -74960 + ); + } + + #[test] + fn price_to_closest_tick_test_8() { + assert_eq!( + price_to_closest_tick(tick_to_price(TOKEN0.clone(), TOKEN1.clone(), 74960).unwrap()) + .unwrap(), + 74960 + ); + } + + #[test] + fn price_to_closest_tick_test_9() { + assert_eq!( + price_to_closest_tick( + tick_to_price(TOKEN0.clone(), TOKEN2_6DECIMALS.clone(), -276225).unwrap(), + ) + .unwrap(), + -276225 + ); + } + + #[test] + fn price_to_closest_tick_test_10() { + assert_eq!( + price_to_closest_tick( + tick_to_price(TOKEN2_6DECIMALS.clone(), TOKEN0.clone(), -276225).unwrap(), + ) + .unwrap(), + -276225 + ); + } +}