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

Risk calculation in solvers crate #1919

Merged
merged 12 commits into from
Oct 12, 2023
20 changes: 17 additions & 3 deletions crates/solvers/src/boundary/naive.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use {
crate::{
boundary::liquidity::constant_product::to_boundary_pool,
domain::{eth, liquidity, order, solution},
domain::{self, auction, eth, liquidity, order, solution},
},
ethereum_types::H160,
itertools::Itertools,
model::order::{Order, OrderClass, OrderData, OrderKind, OrderMetadata, OrderUid},
num::{BigRational, One},
shared::external_prices::ExternalPrices,
shared::{
external_prices::ExternalPrices,
price_estimation::gas::{ERC20_TRANSFER, INITIALIZATION_COST, SETTLEMENT},
},
solver::{
liquidity::{
slippage::{SlippageCalculator, SlippageContext},
Expand All @@ -29,6 +32,8 @@ use {
pub fn solve(
orders: &[&order::Order],
liquidity: &liquidity::Liquidity,
risk: &domain::Risk,
gas_price: auction::GasPrice,
) -> Option<solution::Solution> {
let pool = match &liquidity.state {
liquidity::State::ConstantProduct(pool) => pool,
Expand Down Expand Up @@ -108,6 +113,15 @@ pub fn solve(
let boundary_solution =
multi_order_solver::solve(&slippage.context(), boundary_orders, &boundary_pool)?;

let nmb_orders = boundary_solution.trades().count();
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
let gas = INITIALIZATION_COST
+ SETTLEMENT
+ nmb_orders as u64 * (2 * ERC20_TRANSFER + liquidity.gas.0.as_u64()); // this is pessimistic in case the pool is not used
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
let score = solution::Score::RiskAdjusted(risk.success_probability(
eth::Gas(gas.into()),
gas_price,
nmb_orders,
));
let swap = pool_handler.swap.lock().unwrap().take();
Some(solution::Solution {
prices: solution::ClearingPrices::new(
Expand Down Expand Up @@ -143,7 +157,7 @@ pub fn solve(
})
})
.collect(),
score: Default::default(),
score,
})
}

Expand Down
2 changes: 1 addition & 1 deletion crates/solvers/src/domain/risk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{auction::GasPrice, eth::Gas};

/// Parameters that define the possibility of a revert when executing a
/// solution.
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct Risk {
pub gas_amount_factor: f64,
pub gas_price_factor: f64,
Expand Down
8 changes: 4 additions & 4 deletions crates/solvers/src/domain/solver/naive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ use {

pub struct Naive {
/// Parameters used to calculate the revert risk of a solution.
///
/// [ CURRENTLY NOT USED ]
/// TODO: Waiting for proper gas estimation to be implemented
risk: domain::Risk,
}

Expand All @@ -31,14 +28,17 @@ impl Naive {
/// Solves the specified auction, returning a vector of all possible
/// solutions.
pub async fn solve(&self, auction: auction::Auction) -> Vec<solution::Solution> {
let risk = self.risk.clone();
// Make sure to push the CPU-heavy code to a separate thread in order to
// not lock up the [`tokio`] runtime and cause it to slow down handling
// the real async things.
tokio::task::spawn_blocking(move || {
let groups = group_by_token_pair(&auction);
groups
.values()
.filter_map(|group| boundary::naive::solve(&group.orders, group.liquidity))
.filter_map(|group| {
boundary::naive::solve(&group.orders, group.liquidity, &risk, auction.gas_price)
})
.collect()
})
.await
Expand Down