Skip to content

Commit

Permalink
Add objective Flag And use SuplusFeesCosts for Price Estimates (#229
Browse files Browse the repository at this point in the history
)

This PR adds a new field to `SolverConfig` to allow configuring Quasimodo for different environments. Specifically, requests to Quasimodo from the solver should use a different objective then when requesting price estimates (when solving, Quasimodo defaults to optimizing with capped surplus, which we don't want for price estimates).

### Test Plan

Run the orderbook locally and see the new parameter:
```
% cargo run -p orderbook -- --price-estimators Quasimodo --log-filter shared::http_solver=trace
...
2022-05-25T13:26:42.718Z TRACE shared::http_solver: request url=https://.../solve?instance_name=2022-05-25_13%3A26%3A42.718259_UTC_Ethereum___Mainnet_1_0&time_limit=2&max_nr_exec_orders=100&use_internal_buffers=false&objective=surplusfeescosts body=...
...
```
  • Loading branch information
Nicholas Rodrigues Lordello authored May 27, 2022
1 parent a3b8f48 commit 5e2f927
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 37 deletions.
9 changes: 4 additions & 5 deletions crates/orderbook/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use shared::{
},
baseline_solver::BaseTokens,
current_block::current_block_stream,
http_solver::{DefaultHttpSolverApi, SolverConfig},
http_solver::{DefaultHttpSolverApi, Objective, SolverConfig},
maintenance::ServiceMaintenance,
metrics::{serve_metrics, setup_metrics_registry, DEFAULT_METRICS_PORT},
network::network_name,
Expand Down Expand Up @@ -556,10 +556,9 @@ async fn main() {
),
client: client.clone(),
config: SolverConfig {
api_key: None,
max_nr_exec_orders: 100,
has_ucp_policy_parameter: false,
use_internal_buffers: args.shared.quasimodo_uses_internal_buffers.into(),
use_internal_buffers: Some(args.shared.quasimodo_uses_internal_buffers),
objective: Some(Objective::SurplusFeesCosts),
..Default::default()
},
}),
pool_fetcher.clone(),
Expand Down
40 changes: 36 additions & 4 deletions crates/shared/src/http_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct DefaultHttpSolverApi {
}

/// Configuration for solver requests.
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct SolverConfig {
/// Optional value for the `X-API-KEY` header.
pub api_key: Option<String>,
Expand All @@ -62,6 +62,27 @@ pub struct SolverConfig {

/// Controls if/how to set `use_internal_buffers`.
pub use_internal_buffers: Option<bool>,

/// Controls the objective function to optimize for.
pub objective: Option<Objective>,
}

impl Default for SolverConfig {
fn default() -> Self {
Self {
api_key: None,
max_nr_exec_orders: 100,
has_ucp_policy_parameter: false,
use_internal_buffers: None,
objective: None,
}
}
}

#[derive(Debug)]
pub enum Objective {
CappedSurplusFeesCosts,
SurplusFeesCosts,
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -105,11 +126,24 @@ impl HttpSolverApi for DefaultHttpSolverApi {
use_internal_buffers.to_string().as_str(),
);
}
match self.config.objective {
Some(Objective::CappedSurplusFeesCosts) => {
url.query_pairs_mut()
.append_pair("objective", "cappedsurplusfeescosts");
}
Some(Objective::SurplusFeesCosts) => {
url.query_pairs_mut()
.append_pair("objective", "surplusfeescosts");
}
_ => {}
}
if let Some(auction_id) = maybe_auction_id {
url.query_pairs_mut()
.append_pair("auction_id", auction_id.to_string().as_str());
}
let query = url.query().map(ToString::to_string).unwrap_or_default();
let body = serde_json::to_string(&model).context("failed to encode body")?;
tracing::trace!(%url, %body, "request");
let mut request = self
.client
.post(url)
Expand All @@ -121,8 +155,6 @@ impl HttpSolverApi for DefaultHttpSolverApi {
header.set_sensitive(true);
request = request.header("X-API-KEY", header);
}
let body = serde_json::to_string(&model).context("failed to encode body")?;
tracing::trace!("request {}", body);
let request = request.body(body.clone());
let mut response = request.send().await.context("failed to send request")?;
let status = response.status();
Expand All @@ -131,7 +163,7 @@ impl HttpSolverApi for DefaultHttpSolverApi {
.await
.context("response body")?;
let text = std::str::from_utf8(&response_body).context("failed to decode response body")?;
tracing::trace!("response {}", text);
tracing::trace!(body = %text, "response");
let context = || {
format!(
"request query {}, request body {}, response body {}",
Expand Down
6 changes: 2 additions & 4 deletions crates/shared/src/price_estimation/quasimodo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,8 @@ mod tests {
base: Url::parse(&quasimodo_url).expect("failed to parse quasimodo url"),
client,
config: SolverConfig {
api_key: None,
max_nr_exec_orders: 100,
has_ucp_policy_parameter: false,
use_internal_buffers: true.into(),
use_internal_buffers: Some(true),
..Default::default()
},
}),
sharing: Default::default(),
Expand Down
25 changes: 7 additions & 18 deletions crates/solver/src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,32 +282,23 @@ pub fn create(
mip_solver_url.clone(),
"Mip".to_string(),
SolverConfig {
api_key: None,
max_nr_exec_orders: 100,
has_ucp_policy_parameter: false,
use_internal_buffers: mip_uses_internal_buffers.into(),
use_internal_buffers: Some(mip_uses_internal_buffers),
..Default::default()
},
))),
SolverType::CowDexAg => Ok(shared(create_http_solver(
account,
cow_dex_ag_solver_url.clone(),
"CowDexAg".to_string(),
SolverConfig {
api_key: None,
max_nr_exec_orders: 100,
has_ucp_policy_parameter: false,
use_internal_buffers: None,
},
SolverConfig::default(),
))),
SolverType::Quasimodo => Ok(shared(create_http_solver(
account,
quasimodo_solver_url.clone(),
"Quasimodo".to_string(),
SolverConfig {
api_key: None,
max_nr_exec_orders: 100,
has_ucp_policy_parameter: true,
use_internal_buffers: quasimodo_uses_internal_buffers.into(),
use_internal_buffers: Some(quasimodo_uses_internal_buffers),
..Default::default()
},
))),
SolverType::OneInch => Ok(shared(SingleOrderSolver::new(
Expand Down Expand Up @@ -389,10 +380,8 @@ pub fn create(
solver.url,
solver.name,
SolverConfig {
api_key: None,
max_nr_exec_orders: 100,
has_ucp_policy_parameter: false,
use_internal_buffers: mip_uses_internal_buffers.into(),
use_internal_buffers: Some(mip_uses_internal_buffers),
..Default::default()
},
))
});
Expand Down
7 changes: 1 addition & 6 deletions crates/solver/src/solver/http_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,12 +508,7 @@ mod tests {
chain_id: 0,
base: url.parse().unwrap(),
client: Client::new(),
config: SolverConfig {
api_key: None,
max_nr_exec_orders: 0,
has_ucp_policy_parameter: false,
use_internal_buffers: None,
},
config: SolverConfig::default(),
},
Account::Local(Address::default(), None),
H160::zero(),
Expand Down

0 comments on commit 5e2f927

Please sign in to comment.