Skip to content

Commit

Permalink
Forward Order Maturity to Solvers (#763)
Browse files Browse the repository at this point in the history
* Forward Order Creation Time to Solvers

* fmt fix

* Set is_mature flag at the beginning of the auction

* Removed created_at field

* fix failing test

* add print for min_order_age argument
  • Loading branch information
sunce86 authored Nov 15, 2022
1 parent fd7231d commit 6209cba
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 5 deletions.
12 changes: 12 additions & 0 deletions crates/driver/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ pub struct Arguments {
#[clap(long, env, default_value = "1", value_parser = shared::arguments::parse_unbounded_factor)]
pub fee_objective_scaling_factor: f64,

/// A settlement must contain at least one order older than this duration in seconds for it
/// to be applied. Larger values delay individual settlements more but have a higher
/// coincidence of wants chance.
#[clap(
long,
env,
default_value = "30",
value_parser = shared::arguments::duration_from_seconds,
)]
pub min_order_age: Duration,

/// How to to submit settlement transactions.
/// Expected to contain either:
/// 1. One value equal to TransactionStrategyArg::DryRun or
Expand Down Expand Up @@ -297,6 +308,7 @@ impl std::fmt::Display for Arguments {
"fee_objective_scaling_factor: {}",
self.fee_objective_scaling_factor,
)?;
writeln!(f, "min_order_age: {:?}", self.min_order_age,)?;
writeln!(f, "transaction_strategy: {:?}", self.transaction_strategy)?;
writeln!(f, "eden_api_url: {}", self.eden_api_url)?;
writeln!(
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/auction_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ mod tests {
let order_converter = Arc::new(OrderConverter {
native_token: native_token.clone(),
fee_objective_scaling_factor: 2.,
min_order_age: Duration::from_secs(30),
});
let converter = AuctionConverter::new(
gas_estimator,
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ async fn init_common_components(args: &Arguments) -> CommonComponents {
let order_converter = Arc::new(OrderConverter {
native_token: native_token_contract.clone(),
fee_objective_scaling_factor: args.fee_objective_scaling_factor,
min_order_age: args.min_order_age,
});

CommonComponents {
Expand Down
1 change: 1 addition & 0 deletions crates/e2e/tests/e2e/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ pub fn create_order_converter(web3: &Web3, weth_address: H160) -> Arc<OrderConve
Arc::new(OrderConverter {
native_token: WETH9::at(web3, weth_address),
fee_objective_scaling_factor: 1.,
min_order_age: Duration::from_secs(0),
})
}

Expand Down
5 changes: 4 additions & 1 deletion crates/shared/src/http_solver/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct OrderModel {
pub fee: TokenAmount,
pub cost: TokenAmount,
pub is_liquidity_order: bool,
pub is_mature: bool,
#[serde(default)]
pub mandatory: bool,
/// Signals if the order will be executed as an atomic unit. In that case the order's
Expand Down Expand Up @@ -504,6 +505,7 @@ mod tests {
mandatory: false,
has_atomic_execution: false,
reward: 3.,
is_mature: false,
};
let constant_product_pool_model = AmmModel {
parameters: AmmParameters::ConstantProduct(ConstantProductPoolParameters {
Expand Down Expand Up @@ -659,7 +661,8 @@ mod tests {
},
"mandatory": false,
"has_atomic_execution": false,
"reward": 3.0
"reward": 3.0,
"is_mature": false,
},
},
"amms": {
Expand Down
1 change: 1 addition & 0 deletions crates/shared/src/price_estimation/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl HttpPriceEstimator {
has_atomic_execution: false,
// TODO: is it possible to set a more accurate reward?
reward: 35.,
is_mature: true, // irrelevant for price estimation
},
};

Expand Down
6 changes: 4 additions & 2 deletions crates/solver/src/auction_preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

use crate::liquidity::LimitOrder;

// vk: I would like to extend this to also check that the order has minimum age but for this we need
// access to the creation date which is a more involved change.
pub fn has_at_least_one_user_order(orders: &[LimitOrder]) -> bool {
orders.iter().any(|order| !order.is_liquidity_order)
}

pub fn has_at_least_one_mature_order(orders: &[LimitOrder]) -> bool {
orders.iter().any(|order| order.is_mature)
}
4 changes: 3 additions & 1 deletion crates/solver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ impl Driver {
self.metrics.orders_fetched(&orders);
self.metrics.liquidity_fetched(&liquidity);

if !auction_preprocessing::has_at_least_one_user_order(&orders) {
if !auction_preprocessing::has_at_least_one_user_order(&orders)
|| !auction_preprocessing::has_at_least_one_mature_order(&orders)
{
return Ok(());
}

Expand Down
3 changes: 3 additions & 0 deletions crates/solver/src/liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ pub struct LimitOrder {
/// perspective.
pub scaled_unsubsidized_fee: U256,
pub is_liquidity_order: bool,
/// Indicator if the order is mature at the creation of the Auction. Relevant to user orders.
pub is_mature: bool,
#[cfg_attr(test, derivative(PartialEq = "ignore"))]
pub settlement_handling: Arc<dyn SettlementHandling<Self>>,
pub exchange: Exchange,
Expand Down Expand Up @@ -203,6 +205,7 @@ impl Default for LimitOrder {
scaled_unsubsidized_fee: Default::default(),
settlement_handling: tests::CapturingSettlementHandler::arc(),
is_liquidity_order: false,
is_mature: false,
id: Default::default(),
exchange: Exchange::GnosisProtocol,
reward: Default::default(),
Expand Down
8 changes: 7 additions & 1 deletion crates/solver/src/liquidity/order_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use anyhow::{Context, Result};
use contracts::WETH9;
use ethcontract::U256;
use model::order::{Order, OrderClass, BUY_ETH_ADDRESS};
use std::sync::Arc;
use std::{sync::Arc, time::Duration};

pub struct OrderConverter {
pub native_token: WETH9,
pub fee_objective_scaling_factor: f64,
pub min_order_age: Duration,
}

impl OrderConverter {
Expand All @@ -19,6 +20,7 @@ impl OrderConverter {
Self {
native_token: shared::dummy_contract!(WETH9, native_token),
fee_objective_scaling_factor: 1.,
min_order_age: Duration::from_secs(30),
}
}

Expand All @@ -42,6 +44,9 @@ impl OrderConverter {
* self.fee_objective_scaling_factor,
);
let is_liquidity_order = order.metadata.class == OrderClass::Liquidity;
let is_mature = order.metadata.creation_date
+ chrono::Duration::from_std(self.min_order_age).unwrap()
<= chrono::offset::Utc::now();

let (sell_amount, fee_amount) = match order.metadata.class {
OrderClass::Limit => compute_synthetic_order_amounts_for_limit_order(&order)?,
Expand All @@ -67,6 +72,7 @@ impl OrderConverter {
exchange: Exchange::GnosisProtocol,
// TODO: It would be nicer to set this here too but we need #529 first.
reward: 0.,
is_mature,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/solver/src/liquidity/zeroex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl ZeroExLiquidity {
}),
exchange: Exchange::ZeroEx,
reward: 0.,
is_mature: false, // irrelevant for liquidity orders
};
Some(Liquidity::LimitOrder(limit_order))
}
Expand Down
1 change: 1 addition & 0 deletions crates/solver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ async fn main() {
let order_converter = Arc::new(OrderConverter {
native_token: native_token_contract.clone(),
fee_objective_scaling_factor: args.fee_objective_scaling_factor,
min_order_age: args.min_order_age,
});

let market_makable_token_list_configuration = TokenListConfiguration {
Expand Down
1 change: 1 addition & 0 deletions crates/solver/src/settlement_simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ mod tests {
let order_converter = OrderConverter {
native_token: native_token_contract.clone(),
fee_objective_scaling_factor: 0.91_f64,
min_order_age: std::time::Duration::from_secs(30),
};
let value = json!(
{
Expand Down
1 change: 1 addition & 0 deletions crates/solver/src/solver/http_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ fn order_models(
mandatory: false,
has_atomic_execution: !matches!(order.exchange, Exchange::GnosisProtocol),
reward: order.reward,
is_mature: order.is_mature,
},
))
})
Expand Down

0 comments on commit 6209cba

Please sign in to comment.