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

Clean-up score #10

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/example.balancer.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
node-url = "http://localhost:8545"
absolute-slippage = "40000000000000000" # Denominated in wei, optional
relative-slippage = "0.001" # Percentage in the [0, 1] range
risk-parameters = [0,0,0,0]

[dex]
endpoint = "https://balancer.sor.eth/api"
Expand Down
1 change: 0 additions & 1 deletion config/example.oneinch.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
node-url = "http://localhost:8545"
absolute-slippage = "40000000000000000" # Denominated in wei, optional
relative-slippage = "0.001" # Percentage in the [0, 1] range
risk-parameters = [0,0,0,0]

[dex]
chain-id = "1"
1 change: 0 additions & 1 deletion config/example.paraswap.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
node-url = "http://localhost:8545"
absolute-slippage = "40000000000000000" # Denominated in wei, optional
relative-slippage = "0.001" # Percentage in the [0, 1] range
risk-parameters = [0,0,0,0]

[dex]
# these 2 dexs should always be excluded because they are incompatible with `ignoreChecks`
Expand Down
1 change: 0 additions & 1 deletion config/example.zeroex.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
node-url = "http://localhost:8545"
absolute-slippage = "40000000000000000" # Denominated in wei, optional
relative-slippage = "0.001" # Percentage in the [0, 1] range
risk-parameters = [0,0,0,0]

[dex]
# See here how to get a free key: https://0x.org/docs/introduction/getting-started
Expand Down
28 changes: 0 additions & 28 deletions openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,6 @@ components:
- prices
- trades
- interactions
- score
properties:
id:
description: An opaque identifier for the solution.
Expand All @@ -781,30 +780,3 @@ components:
type: array
items:
$ref: "#/components/schemas/Interaction"
score:
description: |
Information required by the driver to score the solution.

Can either be a raw score or a success probability.
oneOf:
- type: object
properties:
kind:
type: string
enum: [solver]
score:
description: |
The raw score of the solution. This is the score that is
used by the driver to rank solutions.
allOf:
- $ref: "#/components/schemas/Decimal"
- type: object
properties:
kind:
type: string
enum: [riskAdjusted]
successProbability:
description: |
The revert probability of the solution. Used by the driver to compute a risk-adjusted score.
type: number
example: 0.9
20 changes: 0 additions & 20 deletions src/api/routes/solve/dto/solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,6 @@ impl Solutions {
}
})
.collect(),
score: match solution.score.clone() {
solution::Score::Solver(score) => Score::Solver { score },
solution::Score::RiskAdjusted(score) => Score::RiskAdjusted {
success_probability: score.0,
},
},
gas: solution.gas.map(|gas| gas.0.as_u64()),
})
.collect(),
Expand All @@ -150,7 +144,6 @@ struct Solution {
prices: HashMap<H160, U256>,
trades: Vec<Trade>,
interactions: Vec<Interaction>,
score: Score,
#[serde(skip_serializing_if = "Option::is_none")]
gas: Option<u64>,
}
Expand Down Expand Up @@ -311,16 +304,3 @@ enum SigningScheme {
PreSign,
Eip1271,
}

/// A score for a solution. The score is used to rank solutions.
#[serde_as]
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase", tag = "kind")]
pub enum Score {
Solver {
#[serde_as(as = "serialize::U256")]
score: U256,
},
#[serde(rename_all = "camelCase")]
RiskAdjusted { success_probability: f64 },
}
6 changes: 1 addition & 5 deletions src/domain/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ impl Swap {
order: order::Order,
gas_price: auction::GasPrice,
sell_token: Option<auction::Price>,
risk: &domain::Risk,
simulator: &infra::dex::Simulator,
) -> Option<solution::Solution> {
let gas = match simulator.gas(order.owner(), &self).await {
Expand All @@ -117,9 +116,6 @@ impl Swap {
return None;
}
};
let score = solution::Score::RiskAdjusted(solution::SuccessProbability(
risk.success_probability(gas, gas_price, 1),
));

let allowance = self.allowance();
let interactions = vec![solution::Interaction::Custom(solution::CustomInteraction {
Expand All @@ -139,7 +135,7 @@ impl Swap {
interactions,
gas,
}
.into_solution(gas_price, sell_token, score)
.into_solution(gas_price, sell_token)
}

pub fn satisfies(&self, order: &domain::order::Order) -> bool {
Expand Down
3 changes: 0 additions & 3 deletions src/domain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ pub mod eth;
pub mod liquidity;
pub mod notification;
pub mod order;
pub mod risk;
pub mod solution;
pub mod solver;

pub use risk::Risk;
20 changes: 1 addition & 19 deletions src/domain/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {
super::{
auction,
eth::{self, Ether, TokenAddress},
solution::{self, SuccessProbability},
solution,
},
std::collections::BTreeSet,
};
Expand Down Expand Up @@ -30,7 +30,6 @@ pub enum Kind {
EmptySolution,
DuplicatedSolutionId,
SimulationFailed(BlockNo, Transaction, SimulationSucceededAtLeastOnce),
ScoringFailed(ScoreKind),
NonBufferableTokensUsed(TokensUsed),
SolverAccountInsufficientBalance(RequiredEther),
Settled(Settlement),
Expand All @@ -46,23 +45,6 @@ pub enum Settlement {
Fail,
}

#[derive(Debug)]
pub enum ScoreKind {
ZeroScore,
ScoreHigherThanQuality(Score, Quality),
SuccessProbabilityOutOfRange(SuccessProbability),
ObjectiveValueNonPositive(Quality, GasCost),
}

#[derive(Debug, Copy, Clone)]
pub struct Score(pub eth::U256);

impl From<eth::U256> for Score {
fn from(value: eth::U256) -> Self {
Self(value)
}
}

#[derive(Debug, Copy, Clone)]
pub struct Quality(pub eth::U256);

Expand Down
26 changes: 0 additions & 26 deletions src/domain/risk.rs

This file was deleted.

50 changes: 1 addition & 49 deletions src/domain/solution.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
domain::{auction, eth, liquidity, order, solution, Risk},
domain::{auction, eth, liquidity, order},
util,
},
ethereum_types::{Address, U256},
Expand All @@ -23,7 +23,6 @@ pub struct Solution {
pub prices: ClearingPrices,
pub trades: Vec<Trade>,
pub interactions: Vec<Interaction>,
pub score: Score,
pub gas: Option<eth::Gas>,
}

Expand All @@ -33,23 +32,6 @@ impl Solution {
Self { id, ..self }
}

/// Returns `self` with a new score.
pub fn with_score(self, score: Score) -> Self {
Self { score, ..self }
}

pub fn with_risk_adjusted_score(
self,
risk: &Risk,
gas: eth::Gas,
gas_price: auction::GasPrice,
) -> Self {
let nmb_orders = self.trades.len();
self.with_score(Score::RiskAdjusted(SuccessProbability(
risk.success_probability(gas, gas_price, nmb_orders),
)))
}

/// Returns `self` with eligible interactions internalized using the
/// Settlement contract buffers.
///
Expand Down Expand Up @@ -142,7 +124,6 @@ impl Single {
self,
gas_price: auction::GasPrice,
sell_token: Option<auction::Price>,
score: solution::Score,
) -> Option<Solution> {
let Self {
order,
Expand Down Expand Up @@ -210,7 +191,6 @@ impl Single {
]),
trades: vec![Trade::Fulfillment(Fulfillment::new(order, executed, fee)?)],
interactions,
score,
gas: Some(self.gas),
})
}
Expand Down Expand Up @@ -387,34 +367,6 @@ pub struct Allowance {
pub asset: eth::Asset,
}

/// Represents the probability that a solution will be successfully settled.
#[derive(Debug, Copy, Clone)]
pub struct SuccessProbability(pub f64);

impl From<f64> for SuccessProbability {
fn from(value: f64) -> Self {
Self(value)
}
}

/// A score for a solution. The score is used to rank solutions.
#[derive(Debug, Clone)]
pub enum Score {
/// The score value is provided as is from solver.
/// Success probability is not incorporated into this value.
Solver(U256),
/// This option is used to indicate that the solver did not provide a score.
/// Instead, the score should be computed by the protocol given the success
/// probability.
RiskAdjusted(SuccessProbability),
}

impl Default for Score {
fn default() -> Self {
Self::RiskAdjusted(SuccessProbability(1.0))
}
}

// initial tx gas used to call the settle function from the settlement contract
pub const INITIALIZATION_COST: u64 = 32_000;
/// minimum gas every settlement takes (isSolver)
Expand Down
7 changes: 1 addition & 6 deletions src/domain/solver/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use {
crate::{
boundary::rate_limiter::{RateLimiter, RateLimiterError},
domain::{
self,
auction,
dex::{self, slippage},
order::{self, Order},
Expand Down Expand Up @@ -38,9 +37,6 @@ pub struct Dex {
/// fillable orders).
fills: Fills,

/// Parameters used to calculate the revert risk of a solution.
risk: domain::Risk,

/// Handles 429 Too Many Requests error with a retry mechanism
rate_limiter: RateLimiter,
}
Expand All @@ -63,7 +59,6 @@ impl Dex {
slippage: config.slippage,
concurrent_requests: config.concurrent_requests,
fills: Fills::new(config.smallest_partial_fill),
risk: config.risk,
rate_limiter,
}
}
Expand Down Expand Up @@ -181,7 +176,7 @@ 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.risk, &self.simulator)
.into_solution(order.clone(), gas_price, sell, &self.simulator)
.await
else {
tracing::debug!("no solution for swap");
Expand Down
12 changes: 1 addition & 11 deletions src/infra/config/dex/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use {
crate::{
boundary::rate_limiter::RateLimitingStrategy,
domain::{dex::slippage, eth, Risk},
domain::{dex::slippage, eth},
infra::{blockchain, config::unwrap_or_log, contracts},
util::serialize,
},
Expand Down Expand Up @@ -45,10 +45,6 @@ struct Config {
#[serde_as(as = "serialize::U256")]
smallest_partial_fill: eth::U256,

/// Parameters used to calculate the revert risk of a solution.
/// (gas_amount_factor, gas_price_factor, nmb_orders_factor, intercept)
risk_parameters: (f64, f64, f64, f64),

/// Back-off growth factor for rate limiting.
#[serde(default = "default_back_off_growth_factor")]
back_off_growth_factor: f64,
Expand Down Expand Up @@ -141,12 +137,6 @@ pub async fn load<T: DeserializeOwned>(path: &Path) -> (super::Config, T) {
.expect("invalid slippage limits"),
concurrent_requests: config.concurrent_requests,
smallest_partial_fill: eth::Ether(config.smallest_partial_fill),
risk: Risk {
gas_amount_factor: config.risk_parameters.0,
gas_price_factor: config.risk_parameters.1,
nmb_orders_factor: config.risk_parameters.2,
intercept: config.risk_parameters.3,
},
rate_limiting_strategy: RateLimitingStrategy::try_new(
config.back_off_growth_factor,
config.min_back_off,
Expand Down
3 changes: 1 addition & 2 deletions src/infra/config/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod zeroex;
use {
crate::{
boundary::rate_limiter::RateLimitingStrategy,
domain::{dex::slippage, eth, Risk},
domain::{dex::slippage, eth},
},
std::num::NonZeroUsize,
};
Expand All @@ -25,7 +25,6 @@ pub struct Config {
pub slippage: slippage::Limits,
pub concurrent_requests: NonZeroUsize,
pub smallest_partial_fill: eth::Ether,
pub risk: Risk,
pub rate_limiting_strategy: RateLimitingStrategy,
pub solution_gas_offset: eth::SignedGas,
}
Loading
Loading