Skip to content

Commit

Permalink
[EASY] Make gas offset configurable (#15)
Browse files Browse the repository at this point in the history
Same as #7 but without the changes
to the simulation logic (which lead to problems).
This PR basically now just allows to configure the gas that should be
added to any swap proposed by dex solvers to have an easy knob to tweak
to adjust the cost coverage of the dex solvers.
  • Loading branch information
MartinquaXD authored Apr 10, 2024
1 parent c963b3a commit 26a2c6b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/domain/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl Swap {
gas_price: auction::GasPrice,
sell_token: Option<auction::Price>,
simulator: &infra::dex::Simulator,
gas_offset: eth::Gas,
) -> Option<solution::Solution> {
let gas = if order.class == order::Class::Limit {
match simulator.gas(order.owner(), &self).await {
Expand Down Expand Up @@ -141,7 +142,7 @@ impl Swap {
interactions,
gas,
}
.into_solution(gas_price, sell_token)
.into_solution(gas_price, sell_token, gas_offset)
}

pub fn satisfies(&self, order: &domain::order::Order) -> bool {
Expand Down
8 changes: 3 additions & 5 deletions src/domain/solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,13 @@ pub struct Single {
}

impl Single {
/// An approximation for the overhead of executing a trade in a settlement.
const SETTLEMENT_OVERHEAD: u64 = 106_391;

/// Creates a full solution for a single order solution given gas and sell
/// token prices.
pub fn into_solution(
self,
gas_price: auction::GasPrice,
sell_token: Option<auction::Price>,
gas_offset: eth::Gas,
) -> Option<Solution> {
let Self {
order,
Expand All @@ -147,7 +145,7 @@ impl Single {
Fee::Surplus(
sell_token?.ether_value(eth::Ether(
swap.0
.checked_add(Self::SETTLEMENT_OVERHEAD.into())?
.checked_add(gas_offset.0)?
.checked_mul(gas_price.0 .0)?,
))?,
)
Expand Down Expand Up @@ -198,7 +196,7 @@ impl Single {
]),
trades: vec![Trade::Fulfillment(Fulfillment::new(order, executed, fee)?)],
interactions,
gas: Some(eth::Gas(Self::SETTLEMENT_OVERHEAD.into()) + self.gas),
gas: Some(gas_offset + self.gas),
})
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/domain/solver/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use {
domain::{
auction,
dex::{self, slippage},
eth,
order::{self, Order},
solution,
solver::dex::fills::Fills,
Expand Down Expand Up @@ -38,6 +39,10 @@ pub struct Dex {

/// Handles 429 Too Many Requests error with a retry mechanism
rate_limiter: rate_limit::RateLimiter,

/// Amount of gas that gets added to each swap to tweak the cost coverage of
/// the solver.
gas_offset: eth::Gas,
}

/// The amount of time we aim the solver to finish before the final deadline is
Expand All @@ -61,6 +66,7 @@ impl Dex {
concurrent_requests: config.concurrent_requests,
fills: Fills::new(config.smallest_partial_fill),
rate_limiter,
gas_offset: config.gas_offset,
}
}

Expand Down Expand Up @@ -177,7 +183,13 @@ impl Dex {
let swap = self.try_solve(order, &dex_order, tokens, gas_price).await?;
let sell = tokens.reference_price(&order.sell.token);
let Some(solution) = swap
.into_solution(order.clone(), gas_price, sell, &self.simulator)
.into_solution(
order.clone(),
gas_price,
sell,
&self.simulator,
self.gas_offset,
)
.await
else {
tracing::debug!("no solution for swap");
Expand Down
13 changes: 13 additions & 0 deletions src/infra/config/dex/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ struct Config {

/// Settings specific to the wrapped dex API.
dex: toml::Value,

/// Amount of gas that gets added to each swap to adjust the cost coverage
/// of the solver.
#[serde(default = "default_gas_offset")]
#[serde_as(as = "serialize::U256")]
gas_offset: eth::U256,
}

fn default_relative_slippage() -> BigDecimal {
Expand All @@ -84,6 +90,12 @@ fn default_max_back_off() -> Duration {
Duration::from_secs(8)
}

fn default_gas_offset() -> eth::U256 {
// Rough estimation of the gas overhead of settling a single
// trade via the settlement contract.
106_391.into()
}

/// Loads the base solver configuration from a TOML file.
///
/// # Panics
Expand Down Expand Up @@ -137,6 +149,7 @@ pub async fn load<T: DeserializeOwned>(path: &Path) -> (super::Config, T) {
config.max_back_off,
)
.unwrap(),
gas_offset: eth::Gas(config.gas_offset),
};
(config, dex)
}
1 change: 1 addition & 0 deletions src/infra/config/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ pub struct Config {
pub concurrent_requests: NonZeroUsize,
pub smallest_partial_fill: eth::Ether,
pub rate_limiting_strategy: rate_limit::Strategy,
pub gas_offset: eth::Gas,
}

0 comments on commit 26a2c6b

Please sign in to comment.