From 6ce5c828829f892af6cd4e35928885dc4b9e8e12 Mon Sep 17 00:00:00 2001 From: Malik672 Date: Thu, 4 Jan 2024 17:49:59 +0100 Subject: [PATCH 1/3] add compute_price_impcat and removed unnesscessary dependencies + version 0.7.0d --- Cargo.lock | 43 +-------------------- Cargo.toml | 3 -- src/entities/fractions/percent.rs | 2 +- src/utils/compute_price_impact.rs | 62 ++++++++++++++++++++++++++++++- 4 files changed, 63 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2761f5d..2ba3f3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -539,15 +539,6 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" -[[package]] -name = "keccak" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f6d5ed8676d904364de097082f4e7d240b571b67989ced0240f08b7f966f940" -dependencies = [ - "cpufeatures", -] - [[package]] name = "keccak-asm" version = "0.1.0" @@ -631,15 +622,6 @@ dependencies = [ "libm", ] -[[package]] -name = "ordered-float" -version = "2.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68f19d67e5a2795c94e73e0bb1cc1a7edeb2e28efd39e2e1c9b7a40c1108b11c" -dependencies = [ - "num-traits", -] - [[package]] name = "parity-scale-codec" version = "3.6.9" @@ -1049,16 +1031,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde-value" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c" -dependencies = [ - "ordered-float", - "serde", -] - [[package]] name = "serde_derive" version = "1.0.193" @@ -1070,16 +1042,6 @@ dependencies = [ "syn 2.0.44", ] -[[package]] -name = "sha3" -version = "0.10.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60" -dependencies = [ - "digest 0.10.7", - "keccak", -] - [[package]] name = "sha3-asm" version = "0.1.0" @@ -1231,7 +1193,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] -name = "uniswap-core" +name = "uniswap-sdk-core" version = "0.6.1" dependencies = [ "alloy-primitives", @@ -1243,9 +1205,6 @@ dependencies = [ "num-rational", "num-traits", "regex", - "rustc-hex", - "serde-value", - "sha3", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 50c7869..bcf20e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,3 @@ num-integer = "0.1.45" num-rational = "0.4.1" num-traits = "0.2.17" regex = "1.10.2" -rustc-hex = "2.1.0" -serde-value = "0.7.0" -sha3 = "0.10.8" diff --git a/src/entities/fractions/percent.rs b/src/entities/fractions/percent.rs index 003c722..3163a4b 100644 --- a/src/entities/fractions/percent.rs +++ b/src/entities/fractions/percent.rs @@ -7,7 +7,7 @@ lazy_static! { } /// Unit struct to distinguish between a fraction and a percent -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub struct IsPercent; // Type alias for a Percent, a Fraction with the IsPercent metadata diff --git a/src/utils/compute_price_impact.rs b/src/utils/compute_price_impact.rs index d3f5a12..d2d8076 100644 --- a/src/utils/compute_price_impact.rs +++ b/src/utils/compute_price_impact.rs @@ -1 +1,61 @@ - +use crate::prelude::*; + +pub fn compute_price_impact( + mid_price: Price, + input_amount: CurrencyAmount, + output_amount: CurrencyAmount, +) -> Percent { + let quoted_output_amount = mid_price.quote(input_amount); + // calculate price impact := (exactQuote - outputAmount) / exactQuote + let price_impact = quoted_output_amount + .subtract(&output_amount) + .divide("ed_output_amount); + Percent::new( + price_impact.numerator().clone(), + price_impact.denominator().clone(), + ) +} + +#[cfg(test)] + +mod tests { + use crate::token; + + use super::*; + + #[test] + fn test_compute_price_impact() { + let address_zero = "0x0000000000000000000000000000000000000000"; + let address_one = "0x0000000000000000000000000000000000000001"; + + let token = token!(4, address_zero, 25, "Test", "Te"); + let token_1 = token!(3, address_one, 25, "Test", "Te"); + + //is correct for zero + assert!( + compute_price_impact( + Price::new(Ether::on_chain(1), token.clone(), 10, 100), + CurrencyAmount::from_raw_amount(Ether::on_chain(1), 10), + CurrencyAmount::from_raw_amount(token.clone(), 100) + ) == Percent::new(0, 10000), + ); + + //is correct for half output + assert!( + compute_price_impact( + Price::new(token.clone(), token_1.clone(), 10, 100), + CurrencyAmount::from_raw_amount(token.clone(), 10), + CurrencyAmount::from_raw_amount(token_1.clone(), 50) + ) == Percent::new(5000, 10000), + ); + + //is negative for more output + assert!( + compute_price_impact( + Price::new(token.clone(), token_1.clone(), 10, 100), + CurrencyAmount::from_raw_amount(token.clone(), 10), + CurrencyAmount::from_raw_amount(token_1.clone(), 200) + ) == Percent::new(-10000, 10000) + ) + } +} From f22bb10ab96ffb9f7ae7ca5d487dfd39b7835513 Mon Sep 17 00:00:00 2001 From: Malik672 Date: Thu, 4 Jan 2024 17:53:01 +0100 Subject: [PATCH 2/3] add compute_price_impcat and removed unnesscessary dependencies + version 0.7.0d --- src/utils/compute_price_impact.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/utils/compute_price_impact.rs b/src/utils/compute_price_impact.rs index d2d8076..926f150 100644 --- a/src/utils/compute_price_impact.rs +++ b/src/utils/compute_price_impact.rs @@ -1,5 +1,15 @@ use crate::prelude::*; +/// Returns the percent difference between the mid price and the execution price, i.e. price impact. +/// +/// # Arguments +/// +/// * midPrice mid price before the trade +/// * inputAmount the input amount of the trade +/// * outputAmount the output amount of the trade +/// +/// returns: Percent +/// pub fn compute_price_impact( mid_price: Price, input_amount: CurrencyAmount, From 818afb630c9ef0bead4e2214f09cf0263254ca9b Mon Sep 17 00:00:00 2001 From: Malik672 Date: Thu, 4 Jan 2024 17:54:39 +0100 Subject: [PATCH 3/3] add compute_price_impcat and removed unnesscessary dependencies + version 0.7.0d --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2ba3f3b..6c1c462 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1194,7 +1194,7 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "uniswap-sdk-core" -version = "0.6.1" +version = "0.7.0" dependencies = [ "alloy-primitives", "bigdecimal", diff --git a/Cargo.toml b/Cargo.toml index bcf20e4..a9651c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uniswap-sdk-core" -version = "0.6.1" +version = "0.7.0" edition = "2021" authors = ["malik ", "Shuhui Luo "] description = "The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange"