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

Fix div by 0 in slippage computation #2024

Merged
merged 3 commits into from
Oct 28, 2023
Merged
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
13 changes: 9 additions & 4 deletions crates/solver/src/liquidity/slippage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {
clap::{Parser, ValueEnum as _},
ethcontract::{H160, U256},
model::order::OrderKind,
num::{BigInt, BigRational, Integer as _, ToPrimitive as _},
num::{BigInt, BigRational, CheckedDiv, Integer as _, ToPrimitive as _},
once_cell::sync::OnceCell,
shared::{external_prices::ExternalPrices, http_solver::model::TokenAmount},
std::{
Expand Down Expand Up @@ -327,10 +327,15 @@ impl SlippageCalculator {
) -> Result<(Cow<BigRational>, BigInt)> {
let relative = if let Some(max_absolute_native_token) = self.absolute.clone() {
let price = price.context("missing token price")?;
let max_absolute_slippage =
BigRational::new(max_absolute_native_token, 1.into()) / price;
let max_absolute_slippage = BigRational::new(max_absolute_native_token, 1.into())
.checked_div(price)
.context("price is zero")?;

let max_relative_slippage_respecting_absolute_limit = max_absolute_slippage / &amount;
let amount = BigRational::new(amount.clone(), 1.into());

let max_relative_slippage_respecting_absolute_limit = max_absolute_slippage
.checked_div(&amount)
.context("amount is zero")?;

cmp::min(
Cow::Owned(max_relative_slippage_respecting_absolute_limit),
Expand Down
Loading