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

Compute price impact #24

Merged
merged 3 commits into from
Jan 5, 2024
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
45 changes: 2 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-sdk-core"
version = "0.6.1"
version = "0.7.0"
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 @@ -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"
2 changes: 1 addition & 1 deletion src/entities/fractions/percent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 71 additions & 1 deletion src/utils/compute_price_impact.rs
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@

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<TBase: CurrencyTrait, TQuote: CurrencyTrait>(
mid_price: Price<TBase, TQuote>,
input_amount: CurrencyAmount<TBase>,
output_amount: CurrencyAmount<TQuote>,
) -> 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(&quoted_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)
)
}
}
Loading