Skip to content

Commit

Permalink
Merge branch 'main' into update_solver_engine_api
Browse files Browse the repository at this point in the history
  • Loading branch information
fleupold authored Dec 21, 2023
2 parents 035c557 + c390494 commit 6dbe351
Show file tree
Hide file tree
Showing 43 changed files with 456 additions and 343 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ futures = "0.3"
gas-estimation = { git = "https://github.com/cowprotocol/gas-estimation", tag = "v0.7.3", features = ["web3_", "tokio_"] }
hex = { version = "0.4", default-features = false }
hex-literal = "0.4"
humantime = "2.1.0"
humantime-serde = "1.1.1"
itertools = "0.11"
lazy_static = "1"
maplit = "1"
Expand Down
1 change: 1 addition & 0 deletions crates/alerter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "MIT OR Apache-2.0"
anyhow = { workspace = true }
chrono = { workspace = true }
clap = { workspace = true }
humantime = { workspace = true }
observe = { path = "../observe" }
model = { path = "../model" }
number = { path = "../number" }
Expand Down
18 changes: 9 additions & 9 deletions crates/alerter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,35 +329,35 @@ struct Arguments {
#[clap(
long,
env,
default_value = "30",
value_parser = shared::arguments::duration_from_seconds,
default_value = "30s",
value_parser = humantime::parse_duration,
)]
update_interval: Duration,

/// Minimum time without a trade before alerting.
#[clap(
long,
env,
default_value = "600",
value_parser = shared::arguments::duration_from_seconds,
default_value = "10m",
value_parser = humantime::parse_duration,
)]
time_without_trade: Duration,

/// Minimum time an order must have been matchable for before alerting.
#[clap(
long,
env,
default_value = "180",
value_parser = shared::arguments::duration_from_seconds,
default_value = "3m",
value_parser = humantime::parse_duration,
)]
min_order_age: Duration,

/// Do not repeat the alert more often than this.
#[clap(
long,
env,
default_value = "1800",
value_parser = shared::arguments::duration_from_seconds,
default_value = "30m",
value_parser = humantime::parse_duration,
)]
min_alert_interval: Duration,

Expand All @@ -374,7 +374,7 @@ struct Arguments {

/// Minimum time between get order requests to the api. Without this the api
/// can rate limit us.
#[clap(long, env, default_value = "0.2", value_parser = shared::arguments::duration_from_seconds)]
#[clap(long, env, default_value = "200ms", value_parser = humantime::parse_duration)]
api_get_order_min_interval: Duration,

#[clap(long, env)]
Expand Down
1 change: 1 addition & 0 deletions crates/autopilot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ gas-estimation = { workspace = true }
observe = { path = "../observe" }
hex = { workspace = true }
hex-literal = { workspace = true }
humantime = { workspace = true }
itertools = { workspace = true }
maplit = { workspace = true }
model = { path = "../model" }
Expand Down
48 changes: 19 additions & 29 deletions crates/autopilot/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ use {
http_client,
price_estimation::{self, NativePriceEstimators},
},
std::{
net::SocketAddr,
num::{NonZeroUsize, ParseFloatError},
str::FromStr,
time::Duration,
},
std::{net::SocketAddr, num::NonZeroUsize, str::FromStr, time::Duration},
url::Url,
};

Expand Down Expand Up @@ -74,13 +69,13 @@ pub struct Arguments {
#[clap(long, env, use_value_delimiter = true)]
pub unsupported_tokens: Vec<H160>,

/// The amount of time in seconds a classification of a token into good or
/// The amount of time a classification of a token into good or
/// bad is valid for.
#[clap(
long,
env,
default_value = "600",
value_parser = shared::arguments::duration_from_seconds,
default_value = "10m",
value_parser = humantime::parse_duration,
)]
pub token_quality_cache_expiry: Duration,

Expand All @@ -102,12 +97,12 @@ pub struct Arguments {
#[clap(long, env, default_value = "2")]
pub native_price_estimation_results_required: NonZeroUsize,

/// The minimum amount of time in seconds an order has to be valid for.
/// The minimum amount of time an order has to be valid for.
#[clap(
long,
env,
default_value = "60",
value_parser = shared::arguments::duration_from_seconds,
default_value = "1m",
value_parser = humantime::parse_duration,
)]
pub min_order_validity_period: Duration,

Expand All @@ -120,16 +115,16 @@ pub struct Arguments {
#[clap(
long,
env,
default_value = "300",
value_parser = shared::arguments::duration_from_seconds,
default_value = "5m",
value_parser = humantime::parse_duration,
)]
pub max_auction_age: Duration,

#[clap(long, env, default_value = "0")]
pub limit_order_price_factor: f64,

/// The time between auction updates.
#[clap(long, env, default_value = "10", value_parser = shared::arguments::duration_from_seconds)]
#[clap(long, env, default_value = "10s", value_parser = humantime::parse_duration)]
pub auction_update_interval: Duration,

/// Fee scaling factor for objective value. This controls the constant
Expand All @@ -153,8 +148,8 @@ pub struct Arguments {
#[clap(
long,
env,
default_value = "3600",
value_parser = shared::arguments::duration_from_seconds,
default_value = "1h",
value_parser = humantime::parse_duration,
)]
pub trusted_tokens_update_interval: Duration,

Expand Down Expand Up @@ -188,8 +183,8 @@ pub struct Arguments {
#[clap(
long,
env,
default_value = "60",
value_parser = shared::arguments::duration_from_seconds,
default_value = "1m",
value_parser = humantime::parse_duration,
)]
pub max_settlement_transaction_wait: Duration,

Expand All @@ -204,12 +199,12 @@ pub struct Arguments {
#[clap(long, env)]
pub shadow: Option<Url>,

/// Time in seconds solvers have to compute a score per auction.
/// Time solvers have to compute a score per auction.
#[clap(
long,
env,
default_value = "15",
value_parser = shared::arguments::duration_from_seconds,
default_value = "15s",
value_parser = humantime::parse_duration,
)]
pub solve_deadline: Duration,

Expand All @@ -219,12 +214,12 @@ pub struct Arguments {

/// Time interval in days between each cleanup operation of the
/// `order_events` database table.
#[clap(long, env, default_value = "1", value_parser = duration_from_days)]
#[clap(long, env, default_value = "1d", value_parser = humantime::parse_duration)]
pub order_events_cleanup_interval: Duration,

/// Age threshold in days for order events to be eligible for cleanup in the
/// `order_events` database table.
#[clap(long, env, default_value = "30", value_parser = duration_from_days)]
#[clap(long, env, default_value = "30d", value_parser = humantime::parse_duration)]
pub order_events_cleanup_threshold: Duration,
}

Expand Down Expand Up @@ -374,8 +369,3 @@ impl FromStr for FeePolicyKind {
}
}
}

fn duration_from_days(s: &str) -> Result<Duration, ParseFloatError> {
let days = s.parse::<f64>()?;
Ok(Duration::from_secs_f64(days * 86_400.0))
}
2 changes: 1 addition & 1 deletion crates/autopilot/src/driver_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub mod solve {
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(rename_all = "lowercase", tag = "kind")]
pub enum FeePolicy {
/// If the order receives more than expected (positive deviation from
/// quoted amounts) pay the protocol a factor of the achieved
Expand Down
50 changes: 50 additions & 0 deletions crates/autopilot/src/protocol/fee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! Protocol fee implementation.
//!
//! The protocol fee is a fee that is defined by the protocol and for each order
//! in the auction we define the way to calculate the protocol fee based on the
//! configuration parameters.
use {
crate::{
arguments,
driver_model::solve::{fee_policy_to_dto, FeePolicy},
},
model::{
auction::Auction,
order::{OrderClass, OrderUid},
},
std::collections::HashMap,
};

pub struct Policies {
policies: HashMap<OrderUid, Vec<FeePolicy>>,
}

impl Policies {
pub fn new(auction: &Auction, config: arguments::FeePolicy) -> Self {
Self {
policies: auction
.orders
.iter()
.filter_map(|order| {
match order.metadata.class {
OrderClass::Market => None,
OrderClass::Liquidity => None,
// TODO: https://github.com/cowprotocol/services/issues/2092
// skip protocol fee for limit orders with in-market price

// TODO: https://github.com/cowprotocol/services/issues/2115
// skip protocol fee for TWAP limit orders
OrderClass::Limit(_) => {
Some((order.metadata.uid, vec![fee_policy_to_dto(&config)]))
}
}
})
.collect(),
}
}

pub fn get(&self, order: &OrderUid) -> Option<Vec<FeePolicy>> {
self.policies.get(order).cloned()
}
}
1 change: 1 addition & 0 deletions crates/autopilot/src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A top-level containing implementations of clients to other CoW Protocol
//! components.
pub mod fee;
pub mod orderbook;

pub use self::orderbook::Orderbook;
8 changes: 4 additions & 4 deletions crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub async fn run(args: Arguments) {
number_of_entries_to_auto_update: args.pool_cache_lru_size,
maximum_recent_block_age: args.shared.pool_cache_maximum_recent_block_age,
max_retries: args.shared.pool_cache_maximum_retries,
delay_between_retries: args.shared.pool_cache_delay_between_retries_seconds,
delay_between_retries: args.shared.pool_cache_delay_between_retries,
};
let pool_fetcher = Arc::new(
PoolCache::new(
Expand Down Expand Up @@ -481,15 +481,15 @@ pub async fn run(args: Arguments) {
Arc::new(db.clone()),
order_quoting::Validity {
eip1271_onchain_quote: chrono::Duration::from_std(
args.order_quoting.eip1271_onchain_quote_validity_seconds,
args.order_quoting.eip1271_onchain_quote_validity,
)
.unwrap(),
presign_onchain_quote: chrono::Duration::from_std(
args.order_quoting.presign_onchain_quote_validity_seconds,
args.order_quoting.presign_onchain_quote_validity,
)
.unwrap(),
standard_quote: chrono::Duration::from_std(
args.order_quoting.standard_offchain_quote_validity_seconds,
args.order_quoting.standard_offchain_quote_validity,
)
.unwrap(),
},
Expand Down
Loading

0 comments on commit 6dbe351

Please sign in to comment.