diff --git a/crates/database/src/order_execution.rs b/crates/database/src/order_execution.rs index fed8f6f7bd..c0c0dbe637 100644 --- a/crates/database/src/order_execution.rs +++ b/crates/database/src/order_execution.rs @@ -10,7 +10,7 @@ pub async fn save( order: &OrderUid, auction: AuctionId, surplus_fee: Option<&BigDecimal>, - solver_fee: Option<&BigDecimal>, + scoring_fee: Option<&BigDecimal>, ) -> Result<(), sqlx::Error> { const QUERY: &str = r#" INSERT INTO order_execution (order_uid, auction_id, reward, surplus_fee, solver_fee) @@ -21,7 +21,7 @@ VALUES ($1, $2, $3, $4, $5) .bind(auction) .bind(0.) // reward is deprecated but saved for historical analysis .bind(surplus_fee) - .bind(solver_fee) + .bind(scoring_fee) .execute(ex) .await?; Ok(()) diff --git a/crates/driver/src/boundary/settlement.rs b/crates/driver/src/boundary/settlement.rs index 4fb64e5956..29a89d9ab8 100644 --- a/crates/driver/src/boundary/settlement.rs +++ b/crates/driver/src/boundary/settlement.rs @@ -98,7 +98,7 @@ impl Settlement { to_boundary_order(trade.order()), LimitOrderExecution { filled: trade.executed().into(), - solver_fee: trade.scoring_fee().into(), + scoring_fee: trade.scoring_fee().into(), }, ) } @@ -106,7 +106,7 @@ impl Settlement { to_boundary_jit_order(&DomainSeparator(domain.0), trade.order()), LimitOrderExecution { filled: trade.executed().into(), - solver_fee: 0.into(), + scoring_fee: 0.into(), }, ), }; @@ -233,7 +233,7 @@ impl Settlement { )?; let surplus = self.inner.total_surplus(&prices); - let solver_fees = self.inner.total_solver_fees(&prices); + let solver_fees = self.inner.total_scoring_fees(&prices); let quality = surplus + solver_fees; Ok(eth::U256::from_big_rational(&quality)?.into()) diff --git a/crates/model/src/solver_competition.rs b/crates/model/src/solver_competition.rs index cc45a35a03..9b2d401db2 100644 --- a/crates/model/src/solver_competition.rs +++ b/crates/model/src/solver_competition.rs @@ -47,7 +47,7 @@ pub struct Execution { #[serde_as(as = "Option")] pub surplus_fee: Option, #[serde_as(as = "HexOrDecimalU256")] - pub solver_fee: U256, + pub scoring_fee: U256, } /// Stored directly in the database and turned into SolverCompetitionAPI for the diff --git a/crates/orderbook/src/database/solver_competition.rs b/crates/orderbook/src/database/solver_competition.rs index 1207d8f10d..96c62ad086 100644 --- a/crates/orderbook/src/database/solver_competition.rs +++ b/crates/orderbook/src/database/solver_competition.rs @@ -46,7 +46,7 @@ impl SolverCompetitionStoring for Postgres { &ByteArray(order.0), request.auction, surplus_fee.as_ref(), - Some(&u256_to_big_decimal(&execution.solver_fee)), + Some(&u256_to_big_decimal(&execution.scoring_fee)), ) .await .context("order_execution::save")?; diff --git a/crates/solver/src/driver.rs b/crates/solver/src/driver.rs index adb871020e..2b621047de 100644 --- a/crates/solver/src/driver.rs +++ b/crates/solver/src/driver.rs @@ -421,7 +421,7 @@ impl Driver { .map(|trade| { let execution = Execution { surplus_fee: trade.surplus_fee(), - solver_fee: trade.solver_fee, + scoring_fee: trade.scoring_fee, }; (trade.order.metadata.uid, execution) }) diff --git a/crates/solver/src/liquidity.rs b/crates/solver/src/liquidity.rs index b6a39743b1..817dd1ce5a 100644 --- a/crates/solver/src/liquidity.rs +++ b/crates/solver/src/liquidity.rs @@ -186,7 +186,7 @@ pub struct LimitOrder { pub partially_fillable: bool, /// The fee that should be used for objective value computations. /// Takes partiall fill into account. - pub solver_fee: U256, + pub scoring_fee: U256, #[cfg_attr(test, derivative(PartialEq = "ignore"))] pub settlement_handling: Arc>, pub exchange: Exchange, @@ -224,17 +224,19 @@ pub struct LimitOrderExecution { /// this trade. pub filled: U256, /// The fee (for the objective value) associated with this order. - /// For partially fillable limit orders this value gets computed by the + /// For limit orders this value gets computed by the /// solver already refers to the `filled` amount. In this case no - /// further scaling is necessary for partial fills. For all other orders - /// this is the `solver_fee` for the entire order and will get scaled - /// correctly by the [`SettlementEncoder`]. - pub solver_fee: U256, + /// further scaling is necessary for partial fills. For market orders + /// this is unsubsidized fee amount. + pub scoring_fee: U256, } impl LimitOrderExecution { - pub fn new(filled: U256, solver_fee: U256) -> Self { - Self { filled, solver_fee } + pub fn new(filled: U256, scoring_fee: U256) -> Self { + Self { + filled, + scoring_fee, + } } } @@ -265,7 +267,7 @@ impl Default for LimitOrder { buy_amount: Default::default(), kind: Default::default(), partially_fillable: Default::default(), - solver_fee: Default::default(), + scoring_fee: Default::default(), settlement_handling: tests::CapturingSettlementHandler::arc(), id: Default::default(), exchange: Exchange::GnosisProtocol, diff --git a/crates/solver/src/liquidity/order_converter.rs b/crates/solver/src/liquidity/order_converter.rs index 048ce4ef2a..c112f7ba1f 100644 --- a/crates/solver/src/liquidity/order_converter.rs +++ b/crates/solver/src/liquidity/order_converter.rs @@ -66,7 +66,7 @@ impl OrderConverter { // The reported fee amount that is used for objective computation is the // order's full full amount scaled by a constant factor. - let solver_fee = match order.solver_determines_fee() { + let scoring_fee = match order.solver_determines_fee() { true => 0.into(), false => remaining.remaining(order.metadata.solver_fee)?, }; @@ -85,7 +85,7 @@ impl OrderConverter { buy_amount, kind: order.data.kind, partially_fillable: order.data.partially_fillable, - solver_fee, + scoring_fee, settlement_handling: Arc::new(OrderSettlementHandler { order, native_token: self.native_token.clone(), @@ -116,7 +116,7 @@ impl SettlementHandling for OrderSettlementHandler { } let trade = - encoder.add_trade(self.order.clone(), execution.filled, execution.solver_fee)?; + encoder.add_trade(self.order.clone(), execution.filled, execution.scoring_fee)?; if is_native_token_buy_order { encoder.add_unwrap(UnwrapWethInteraction { @@ -195,7 +195,7 @@ pub mod tests { let execution = LimitOrderExecution::new(1337.into(), 0.into()); let executed_buy_amount = U256::from(2 * 1337); - let solver_fee = U256::from(1234); + let scoring_fee = U256::from(1234); let prices = hashmap! { native_token.address() => U256::from(100), @@ -231,7 +231,7 @@ pub mod tests { amount: executed_buy_amount, }); assert!(encoder - .add_trade(order, execution.filled, solver_fee) + .add_trade(order, execution.filled, scoring_fee) .is_ok()); }, ); @@ -359,7 +359,7 @@ pub mod tests { // Amounts are halved because the order is half executed. assert_eq!(order_.sell_amount, 10.into()); assert_eq!(order_.buy_amount, 20.into()); - assert_eq!(order_.solver_fee, 100.into()); + assert_eq!(order_.scoring_fee, 100.into()); let order_ = converter .normalize_limit_order(BalancedOrder { @@ -370,7 +370,7 @@ pub mod tests { // Amounts are quartered because of balance. assert_eq!(order_.sell_amount, 5.into()); assert_eq!(order_.buy_amount, 10.into()); - assert_eq!(order_.solver_fee, 50.into()); + assert_eq!(order_.scoring_fee, 50.into()); order.metadata.executed_sell_amount_before_fees = 0.into(); let order_ = converter @@ -382,7 +382,7 @@ pub mod tests { // Amounts are still quartered because of balance. assert_eq!(order_.sell_amount, 5.into()); assert_eq!(order_.buy_amount, 10.into()); - assert_eq!(order_.solver_fee, 50.into()); + assert_eq!(order_.scoring_fee, 50.into()); } #[test] diff --git a/crates/solver/src/liquidity/zeroex.rs b/crates/solver/src/liquidity/zeroex.rs index 923ee32309..233b03789d 100644 --- a/crates/solver/src/liquidity/zeroex.rs +++ b/crates/solver/src/liquidity/zeroex.rs @@ -67,7 +67,7 @@ impl ZeroExLiquidity { buy_amount: record.metadata.remaining_fillable_taker_amount.into(), kind: OrderKind::Buy, partially_fillable: true, - solver_fee: U256::zero(), + scoring_fee: U256::zero(), settlement_handling: Arc::new(OrderSettlementHandler { order: record.order, zeroex: self.zeroex.clone(), diff --git a/crates/solver/src/objective_value.rs b/crates/solver/src/objective_value.rs index 094c872f32..263490948e 100644 --- a/crates/solver/src/objective_value.rs +++ b/crates/solver/src/objective_value.rs @@ -25,7 +25,7 @@ impl Inputs { Self { surplus_given: settlement.total_surplus(prices), - solver_fees: settlement.total_solver_fees(prices), + solver_fees: settlement.total_scoring_fees(prices), gas_price, gas_amount, } diff --git a/crates/solver/src/settlement.rs b/crates/solver/src/settlement.rs index e9a5a67155..9964c0500d 100644 --- a/crates/solver/src/settlement.rs +++ b/crates/solver/src/settlement.rs @@ -24,14 +24,14 @@ pub struct Trade { pub order: Order, pub executed_amount: U256, /// The fee amount used for objective value computations. - pub solver_fee: U256, + pub scoring_fee: U256, } impl Trade { /// Returns the fee taken from the surplus. pub fn surplus_fee(&self) -> Option { match self.order.solver_determines_fee() { - true => Some(self.solver_fee), + true => Some(self.scoring_fee), false => None, } } @@ -146,22 +146,13 @@ impl Trade { self.scale_amount(self.order.data.fee_amount) } - /// Returns the solver fee used for computing the objective value adjusted - /// for partial fills. - pub fn executed_solver_fee(&self) -> Option { - match self.order.solver_determines_fee() { - true => Some(self.solver_fee), - false => self.scale_amount(self.solver_fee), - } - } - /// Returns the actual fees taken by the protocol. pub fn executed_earned_fee(&self) -> Option { let user_fee = self.order.data.fee_amount; match self.order.solver_determines_fee() { true => { - // Solvers already scale the `solver_fee` for these orders. - self.scale_amount(user_fee)?.checked_add(self.solver_fee) + // Solvers already scale the `scoring_fee` for these orders. + self.scale_amount(user_fee)?.checked_add(self.scoring_fee) } false => self.scale_amount(user_fee), } @@ -442,12 +433,12 @@ impl Settlement { /// Computes the total solver fees (in wei) used to compute the objective /// value. - pub fn total_solver_fees(&self, external_prices: &ExternalPrices) -> BigRational { + pub fn total_scoring_fees(&self, external_prices: &ExternalPrices) -> BigRational { self.user_trades() .filter_map(|trade| { external_prices.try_get_native_amount( trade.order.data.sell_token, - trade.executed_solver_fee()?.to_big_rational(), + trade.scoring_fee.to_big_rational(), ) }) .sum() @@ -1335,7 +1326,7 @@ pub mod tests { // Note that the scaled fee amount is different than the order's // signed fee amount. This happens for subsidized orders, and when // a fee objective scaling factor is configured. - solver_fee: 5.into(), + scoring_fee: 5.into(), }; let trade1 = Trade { order: Order { @@ -1349,7 +1340,7 @@ pub mod tests { ..Default::default() }, executed_amount: 10.into(), - solver_fee: 2.into(), + scoring_fee: 2.into(), }; let clearing_prices = hashmap! {token0 => 5.into(), token1 => 10.into()}; @@ -1361,14 +1352,14 @@ pub mod tests { // Fee in sell tokens assert_eq!(trade0.executed_fee().unwrap(), 1.into()); - assert_eq!(trade0.executed_solver_fee().unwrap(), 5.into()); + assert_eq!(trade0.scoring_fee, 5.into()); assert_eq!(trade1.executed_fee().unwrap(), 2.into()); - assert_eq!(trade1.executed_solver_fee().unwrap(), 2.into()); + assert_eq!(trade1.scoring_fee, 2.into()); // Fee in wei of ETH let settlement = test_settlement(clearing_prices, vec![trade0, trade1]); assert_eq!( - settlement.total_solver_fees(&external_prices), + settlement.total_scoring_fees(&external_prices), BigRational::from_integer(45.into()) ); } @@ -1395,7 +1386,7 @@ pub mod tests { }, executed_amount: 1.into(), // This is what matters for the objective value - solver_fee: 42.into(), + scoring_fee: 42.into(), }, Trade { order: Order { @@ -1415,13 +1406,13 @@ pub mod tests { }, executed_amount: 1.into(), // Doesn't count because it is a "liquidity order" - solver_fee: 1337.into(), + scoring_fee: 1337.into(), }, ], ); assert_eq!( - settlement.total_solver_fees(&externalprices! { native_token: token0 }), + settlement.total_scoring_fees(&externalprices! { native_token: token0 }), r(42), ); } @@ -1447,7 +1438,7 @@ pub mod tests { ..Default::default() }, executed_amount: 99760667014_u128.into(), - solver_fee: 239332986_u128.into(), + scoring_fee: 239332986_u128.into(), }], ); @@ -1472,7 +1463,7 @@ pub mod tests { ..Default::default() }, executed_amount: 99760667014_u128.into(), - solver_fee: 239332986_u128.into(), + scoring_fee: 239332986_u128.into(), }, Trade { order: Order { @@ -1492,7 +1483,7 @@ pub mod tests { ..Default::default() }, executed_amount: 99760667014_u128.into(), - solver_fee: 77577144_u128.into(), + scoring_fee: 77577144_u128.into(), }, ], ); @@ -1509,7 +1500,7 @@ pub mod tests { let gas_price = 105386573044; let objective_value = |settlement: &Settlement, gas: u128| { settlement.total_surplus(&external_prices) - + settlement.total_solver_fees(&external_prices) + + settlement.total_scoring_fees(&external_prices) - r(gas * gas_price) }; @@ -1555,7 +1546,7 @@ pub mod tests { ..Default::default() }, executed_amount: 99_000_u128.into(), - solver_fee: 1_000_u128.into(), + scoring_fee: 1_000_u128.into(), }], ); @@ -1590,7 +1581,7 @@ pub mod tests { OrderKind::Sell => 99_000_u128, } .into(), - solver_fee: 1_000_u128.into(), + scoring_fee: 1_000_u128.into(), }], ); @@ -1628,7 +1619,7 @@ pub mod tests { ..Default::default() }, executed_amount: 99_000_u128.into(), - solver_fee: 1_000_u128.into(), + scoring_fee: 1_000_u128.into(), }], ) .encode(InternalizationStrategy::SkipInternalizableInteraction); diff --git a/crates/solver/src/settlement/settlement_encoder.rs b/crates/solver/src/settlement/settlement_encoder.rs index 6cde960352..745d986352 100644 --- a/crates/solver/src/settlement/settlement_encoder.rs +++ b/crates/solver/src/settlement/settlement_encoder.rs @@ -130,7 +130,7 @@ impl SettlementEncoder { let mut result = Self::new(clearing_prices); for trade in trades { result - .add_trade(trade.order, trade.executed_amount, trade.solver_fee) + .add_trade(trade.order, trade.executed_amount, trade.scoring_fee) .unwrap(); } result @@ -207,7 +207,7 @@ impl SettlementEncoder { &mut self, order: Order, executed_amount: U256, - solver_fee: U256, + scoring_fee: U256, ) -> Result { verify_executed_amount(&order, executed_amount)?; let sell_price = self @@ -230,7 +230,7 @@ impl SettlementEncoder { data: Trade { order: order.clone(), executed_amount, - solver_fee, + scoring_fee, }, tokens: TokenReference::Indexed { sell_token_index, @@ -252,23 +252,23 @@ impl SettlementEncoder { &mut self, order: Order, executed_amount: U256, - solver_fee: U256, + scoring_fee: U256, ) -> Result { let interactions = order.interactions.clone(); let execution = match &order.metadata.class { - OrderClass::Market => self.add_market_trade(order, executed_amount, solver_fee)?, + OrderClass::Market => self.add_market_trade(order, executed_amount, scoring_fee)?, OrderClass::Liquidity => { let (sell_price, buy_price) = (order.data.buy_amount, order.data.sell_amount); self.add_custom_price_trade( order, executed_amount, - solver_fee, + scoring_fee, sell_price, buy_price, )? } OrderClass::Limit(_) => { - let surplus_fee = solver_fee; + let surplus_fee = scoring_fee; // Solvers calculate with slightly adjusted amounts compared to // the signed order, so adjust by the surplus fee (if needed) to @@ -286,7 +286,7 @@ impl SettlementEncoder { self.add_custom_price_trade( order, executed_amount, - solver_fee, + scoring_fee, sell_price, buy_price, )? @@ -380,7 +380,7 @@ impl SettlementEncoder { &mut self, order: Order, executed_amount: U256, - solver_fee: U256, + scoring_fee: U256, sell_price: U256, buy_price: U256, ) -> Result { @@ -389,7 +389,7 @@ impl SettlementEncoder { data: Trade { order, executed_amount, - solver_fee, + scoring_fee, }, tokens: TokenReference::CustomPrice { sell_token_price: sell_price, @@ -1092,7 +1092,7 @@ pub mod tests { data: Trade { order: order13, executed_amount: 11.into(), - solver_fee: 0.into() + scoring_fee: 0.into() }, tokens: TokenReference::Indexed { sell_token_index: 0, @@ -1103,7 +1103,7 @@ pub mod tests { data: Trade { order: order12, executed_amount: 11.into(), - solver_fee: 0.into() + scoring_fee: 0.into() }, tokens: TokenReference::CustomPrice { sell_token_price: 11.into(), @@ -1114,7 +1114,7 @@ pub mod tests { data: Trade { order: order24, executed_amount: 22.into(), - solver_fee: 0.into() + scoring_fee: 0.into() }, tokens: TokenReference::Indexed { sell_token_index: 1, @@ -1125,7 +1125,7 @@ pub mod tests { data: Trade { order: order23, executed_amount: 11.into(), - solver_fee: 0.into() + scoring_fee: 0.into() }, tokens: TokenReference::CustomPrice { sell_token_price: 11.into(), @@ -1498,7 +1498,7 @@ pub mod tests { data: Trade { order, executed_amount: 1_010_000_000_000_000_000u128.into(), // 1.01 WETH - solver_fee: U256::exp10(16) // 0.01 WETH (10 USDC) + scoring_fee: U256::exp10(16) // 0.01 WETH (10 USDC) }, tokens: TokenReference::CustomPrice { sell_token_price: U256::exp10(9), @@ -1554,7 +1554,7 @@ pub mod tests { data: Trade { order, executed_amount: U256::exp10(18), // 1 WETH - solver_fee: U256::exp10(7) // 10 USDC + scoring_fee: U256::exp10(7) // 10 USDC }, tokens: TokenReference::CustomPrice { sell_token_price: U256::exp10(18), diff --git a/crates/solver/src/solver/http_solver/instance_creation.rs b/crates/solver/src/solver/http_solver/instance_creation.rs index fac73d0665..215228f419 100644 --- a/crates/solver/src/solver/http_solver/instance_creation.rs +++ b/crates/solver/src/solver/http_solver/instance_creation.rs @@ -262,7 +262,7 @@ fn order_models( allow_partial_fill: order.partially_fillable, is_sell_order: matches!(order.kind, OrderKind::Sell), fee: TokenAmount { - amount: order.solver_fee, + amount: order.scoring_fee, token: order.sell_token, }, cost, diff --git a/crates/solver/src/solver/http_solver/settlement.rs b/crates/solver/src/solver/http_solver/settlement.rs index c4801ee15b..58bbd6ce9d 100644 --- a/crates/solver/src/solver/http_solver/settlement.rs +++ b/crates/solver/src/solver/http_solver/settlement.rs @@ -91,7 +91,7 @@ impl Execution { match self { LimitOrder(order) => { - let solver_fee = match order.order.solver_determines_fee() { + let scoring_fee = match order.order.solver_determines_fee() { true => { let fee = order.executed_fee_amount; match enforce_correct_fees { @@ -99,12 +99,12 @@ impl Execution { false => fee.unwrap_or_default(), } } - false => order.order.solver_fee, + false => order.order.scoring_fee, }; let execution = LimitOrderExecution { filled: order.executed_amount(), - solver_fee, + scoring_fee, }; settlement.with_liquidity(&order.order, execution) @@ -742,7 +742,7 @@ mod tests { ..Default::default() }, executed_amount: 101.into(), - solver_fee: 0.into(), + scoring_fee: 0.into(), }, sell_token_price: 102.into(), buy_token_price: 101.into(), diff --git a/crates/solver/src/solver/naive_solver/multi_order_solver.rs b/crates/solver/src/solver/naive_solver/multi_order_solver.rs index a748dd946f..7d820b3d61 100644 --- a/crates/solver/src/solver/naive_solver/multi_order_solver.rs +++ b/crates/solver/src/solver/naive_solver/multi_order_solver.rs @@ -117,7 +117,7 @@ fn solve_without_uniswap( for order in orders { let execution = LimitOrderExecution { filled: order.full_execution_amount(), - solver_fee: order.solver_fee, + scoring_fee: order.scoring_fee, }; settlement.with_liquidity(order, execution)?; } @@ -152,8 +152,8 @@ fn solve_with_uniswap( for order in orders { let execution = LimitOrderExecution { filled: order.full_execution_amount(), - // TODO: We still need to compute a `solver_fee` for partially fillable limit orders. - solver_fee: order.solver_fee, + // TODO: We still need to compute a `scoring_fee` for partially fillable limit orders. + scoring_fee: order.scoring_fee, }; settlement.with_liquidity(order, execution).ok()?; } @@ -829,7 +829,7 @@ mod tests { let limit_order = LimitOrder::from(order); let execution = LimitOrderExecution::new( limit_order.full_execution_amount(), - limit_order.solver_fee, + limit_order.scoring_fee, ); settlement.with_liquidity(&limit_order, execution).unwrap(); } diff --git a/crates/solver/src/solver/single_order_solver.rs b/crates/solver/src/solver/single_order_solver.rs index 49d8247a0f..8c60f6ee30 100644 --- a/crates/solver/src/solver/single_order_solver.rs +++ b/crates/solver/src/solver/single_order_solver.rs @@ -493,7 +493,7 @@ impl SingleOrderSettlement { // Compute the surplus fee that needs to be incorporated into the prices // and solver fee which will be used for scoring. - let (surplus_fee, solver_fee) = if order.solver_determines_fee() { + let (surplus_fee, scoring_fee) = if order.solver_determines_fee() { let fee = number::conversions::big_rational_to_u256( &prices .try_get_token_amount( @@ -506,7 +506,7 @@ impl SingleOrderSettlement { (fee, fee) } else { - (U256::zero(), order.solver_fee) + (U256::zero(), order.scoring_fee) }; // Compute the actual executed amounts accounting for surplus fees. @@ -569,7 +569,7 @@ impl SingleOrderSettlement { OrderKind::Buy => executed_buy_amount, OrderKind::Sell => executed_sell_amount - surplus_fee, }, - solver_fee, + scoring_fee, }; settlement.with_liquidity(order, execution)?; for interaction in &self.interactions { diff --git a/crates/solvers/src/boundary/naive.rs b/crates/solvers/src/boundary/naive.rs index 68b8acea4d..34d45fd5d9 100644 --- a/crates/solvers/src/boundary/naive.rs +++ b/crates/solvers/src/boundary/naive.rs @@ -70,7 +70,7 @@ pub fn solve( order::Side::Sell => OrderKind::Sell, }, partially_fillable: order.partially_fillable, - solver_fee: order.fee().amount, + scoring_fee: order.fee().amount, settlement_handling: Arc::new(OrderHandler { order: Order { metadata: OrderMetadata { @@ -213,7 +213,7 @@ impl SettlementHandling for OrderHandler { execution: LimitOrderExecution, encoder: &mut SettlementEncoder, ) -> anyhow::Result<()> { - encoder.add_trade(self.order.clone(), execution.filled, execution.solver_fee)?; + encoder.add_trade(self.order.clone(), execution.filled, execution.scoring_fee)?; Ok(()) } }