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

Ignore nonsensical native prices #3106

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Changes from 1 commit
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
24 changes: 22 additions & 2 deletions crates/shared/src/price_estimation/competition/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use {
native::{NativePriceEstimateResult, NativePriceEstimating},
PriceEstimationError,
},
anyhow::Context,
futures::{future::BoxFuture, FutureExt},
model::order::OrderKind,
primitive_types::H160,
Expand All @@ -14,18 +15,25 @@ impl NativePriceEstimating for CompetitionEstimator<Arc<dyn NativePriceEstimatin
fn estimate_native_price(&self, token: H160) -> BoxFuture<'_, NativePriceEstimateResult> {
async move {
let results = self
.produce_results(token, Result::is_ok, |e, q| e.estimate_native_price(q))
.produce_results(token, is_result_usable, |e, q| e.estimate_native_price(q))
.await;
let winner = results
.into_iter()
.filter(|(_index, result)| is_result_usable(result))
.max_by(|a, b| compare_native_result(&a.1, &b.1))
.expect("we get passed at least 1 result and did not filter out any of them");
.context("could not get any native price")?;
self.report_winner(&token, OrderKind::Buy, winner)
}
.boxed()
}
}

fn is_result_usable(result: &NativePriceEstimateResult) -> bool {
result
.as_ref()
.is_ok_and(|price| price.is_normal() && *price > 0.)
}

fn compare_native_result(
a: &Result<f64, PriceEstimationError>,
b: &Result<f64, PriceEstimationError>,
Expand Down Expand Up @@ -123,4 +131,16 @@ mod tests {
.await;
assert_eq!(best, native_price(1.));
}

/// Nonsensical prices like infinities, and non-positive values get ignored.
#[tokio::test]
async fn ignore_nonsensical_prices() {
let subnormal = f64::from_bits(1);
assert!(!subnormal.is_normal());

for price in [f64::NEG_INFINITY, -1., 0., f64::INFINITY, subnormal] {
let best = best_response(PriceRanking::MaxOutAmount, vec![native_price(price)]).await;
assert!(best.is_err());
}
}
}
Loading