Skip to content

Commit 260b1b8

Browse files
committed
Get rid of protocol_fee_sell_amount
1 parent 46875c0 commit 260b1b8

File tree

4 files changed

+3
-87
lines changed

4 files changed

+3
-87
lines changed

crates/e2e/tests/e2e/quoting.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -465,19 +465,6 @@ async fn volume_fee(web3: Web3) {
465465
// Verify protocol fee fields are present
466466
assert!(sell_quote.protocol_fee_bps.is_some());
467467
assert_eq!(sell_quote.protocol_fee_bps.as_ref().unwrap(), "2");
468-
assert!(sell_quote.protocol_fee_sell_amount.is_some());
469-
470-
// For SELL orders: buy_amount should be reduced by the protocol fee
471-
let protocol_fee_sell_amount = sell_quote
472-
.protocol_fee_sell_amount
473-
.unwrap()
474-
.to_string()
475-
.parse::<u128>()
476-
.unwrap();
477-
assert!(
478-
protocol_fee_sell_amount > 0,
479-
"Protocol fee should be non-zero"
480-
);
481468

482469
tracing::info!("Testing BUY quote with volume fee");
483470
let buy_request = OrderQuoteRequest {
@@ -495,17 +482,4 @@ async fn volume_fee(web3: Web3) {
495482
// Verify protocol fee fields are present
496483
assert!(buy_quote.protocol_fee_bps.is_some());
497484
assert_eq!(buy_quote.protocol_fee_bps.as_ref().unwrap(), "2");
498-
assert!(buy_quote.protocol_fee_sell_amount.is_some());
499-
500-
// For BUY orders: sell_amount should be increased by the protocol fee
501-
let protocol_fee_sell_amount_buy = buy_quote
502-
.protocol_fee_sell_amount
503-
.unwrap()
504-
.to_string()
505-
.parse::<u128>()
506-
.unwrap();
507-
assert!(
508-
protocol_fee_sell_amount_buy > 0,
509-
"Protocol fee should be non-zero for buy orders"
510-
);
511485
}

crates/model/src/quote.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,6 @@ pub struct OrderQuoteResponse {
336336
/// Protocol fee in basis points (e.g., "2" for 0.02%)
337337
#[serde(skip_serializing_if = "Option::is_none")]
338338
pub protocol_fee_bps: Option<String>,
339-
/// Protocol fee amount in sell token, already included in sellAmount for
340-
/// SELL orders, or applied before network fees for BUY orders
341-
#[serde(skip_serializing_if = "Option::is_none")]
342-
#[serde_as(as = "Option<HexOrDecimalU256>")]
343-
pub protocol_fee_sell_amount: Option<U256>,
344339
}
345340

346341
#[derive(Debug, Serialize, Deserialize)]

crates/orderbook/src/api/post_quote.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ mod tests {
105105
},
106106
},
107107
number::nonzero::U256 as NonZeroU256,
108-
primitive_types::U256,
109108
reqwest::StatusCode,
110109
serde_json::json,
111110
shared::order_quoting::CalculateQuoteError,
@@ -320,7 +319,6 @@ mod tests {
320319
id: Some(0),
321320
verified: false,
322321
protocol_fee_bps: Some("2".to_string()),
323-
protocol_fee_sell_amount: Some(U256::from(20)),
324322
};
325323
let response = convert_json_response::<OrderQuoteResponse, OrderQuoteErrorWrapper>(Ok(
326324
order_quote_response.clone(),

crates/orderbook/src/quoter.rs

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ struct AdjustedQuoteData {
3131
buy_amount: U256,
3232
/// Protocol fee in basis points (e.g., "2" for 0.02%)
3333
protocol_fee_bps: Option<String>,
34-
/// Protocol fee amount in sell token
35-
protocol_fee_sell_amount: Option<U256>,
3634
}
3735

3836
/// A high-level interface for handling API quote requests.
@@ -151,7 +149,6 @@ impl QuoteHandler {
151149
id: quote.id,
152150
verified: quote.data.verified,
153151
protocol_fee_bps: adjusted_quote.protocol_fee_bps,
154-
protocol_fee_sell_amount: adjusted_quote.protocol_fee_sell_amount,
155152
};
156153

157154
tracing::debug!(?response, "finished computing quote");
@@ -170,14 +167,13 @@ fn get_adjusted_quote_data(
170167
sell_amount: quote.sell_amount,
171168
buy_amount: quote.buy_amount,
172169
protocol_fee_bps: None,
173-
protocol_fee_sell_amount: None,
174170
});
175171
};
176172
// Calculate the volume (surplus token amount) to apply fee to
177173
// Following driver's logic in
178174
// crates/driver/src/domain/competition/solution/fee.rs:189-202:
179175
let factor_f64: f64 = factor.into();
180-
let (protocol_fee_in_surplus_token, adjusted_sell_amount, adjusted_buy_amount) = match side {
176+
let (adjusted_sell_amount, adjusted_buy_amount) = match side {
181177
OrderQuoteSide::Sell { .. } => {
182178
// For SELL orders, fee is calculated on buy amount
183179
let fee_f64 = quote.buy_amount.to_f64_lossy() * factor_f64;
@@ -186,7 +182,7 @@ fn get_adjusted_quote_data(
186182
// Reduce buy amount by protocol fee
187183
let adjusted_buy = quote.buy_amount.saturating_sub(protocol_fee);
188184

189-
(protocol_fee, quote.sell_amount, adjusted_buy)
185+
(quote.sell_amount, adjusted_buy)
190186
}
191187
OrderQuoteSide::Buy { .. } => {
192188
// For BUY orders, fee is calculated on sell amount + network fee.
@@ -198,42 +194,14 @@ fn get_adjusted_quote_data(
198194
// Increase sell amount by protocol fee
199195
let adjusted_sell = quote.sell_amount.saturating_add(protocol_fee);
200196

201-
(protocol_fee, adjusted_sell, quote.buy_amount)
202-
}
203-
};
204-
205-
// Convert protocol fee to sell token for the response
206-
let protocol_fee_sell_amount = match side {
207-
OrderQuoteSide::Sell { .. } => {
208-
// Fee is in buy token, convert to sell token using price ratio
209-
// price = buy_amount / sell_amount
210-
// fee_in_sell = fee_in_buy * sell_amount / buy_amount
211-
protocol_fee_in_surplus_token
212-
.full_mul(quote.sell_amount)
213-
.checked_div(quote.buy_amount.into())
214-
.ok_or_else(|| {
215-
anyhow::anyhow!(
216-
"error converting protocol fee from buy to sell token: division by zero"
217-
)
218-
})?
219-
.try_into()
220-
.map_err(|_| {
221-
anyhow::anyhow!(
222-
"error converting protocol fee from buy to sell token: U256 overflow"
223-
)
224-
})?
225-
}
226-
OrderQuoteSide::Buy { .. } => {
227-
// Fee is already in sell token
228-
protocol_fee_in_surplus_token
197+
(adjusted_sell, quote.buy_amount)
229198
}
230199
};
231200

232201
Ok(AdjustedQuoteData {
233202
sell_amount: adjusted_sell_amount,
234203
buy_amount: adjusted_buy_amount,
235204
protocol_fee_bps: Some(factor.to_bps().to_string()),
236-
protocol_fee_sell_amount: Some(protocol_fee_sell_amount),
237205
})
238206
}
239207

@@ -323,12 +291,6 @@ mod tests {
323291
// Expected: 100 - (100 * 0.0002) = 100 - 0.02 = 99.98
324292
let expected_buy = to_wei(100) - (to_wei(100) / U256::from(5000)); // 0.02% = 1/5000
325293
assert_eq!(result.buy_amount, expected_buy);
326-
327-
// Protocol fee in sell token should be the fee converted from buy token
328-
// fee_in_buy = 100 * 0.0002 = 0.02
329-
// fee_in_sell = 0.02 * (sell_amount / buy_amount) = 0.02 * (100/100) = 0.02
330-
let expected_fee = to_wei(100) / U256::from(5000);
331-
assert_eq!(result.protocol_fee_sell_amount, Some(expected_fee));
332294
}
333295

334296
#[test]
@@ -354,10 +316,6 @@ mod tests {
354316
// Expected: 100 + (100 * 0.0002) = 100 + 0.02 = 100.02
355317
let expected_sell = to_wei(100) + (to_wei(100) / U256::from(5000)); // 0.02% = 1/5000
356318
assert_eq!(result.sell_amount, expected_sell);
357-
358-
// Protocol fee in sell token is just the fee amount
359-
let expected_fee = to_wei(100) / U256::from(5000);
360-
assert_eq!(result.protocol_fee_sell_amount, Some(expected_fee));
361319
}
362320

363321
#[test]
@@ -388,9 +346,6 @@ mod tests {
388346
let expected_protocol_fee = total_volume / U256::from(5000); // 0.021
389347
let expected_sell = to_wei(100) + expected_protocol_fee; // 100.021
390348
assert_eq!(result.sell_amount, expected_sell);
391-
392-
// Protocol fee in sell token
393-
assert_eq!(result.protocol_fee_sell_amount, Some(expected_protocol_fee));
394349
}
395350

396351
#[test]
@@ -413,12 +368,6 @@ mod tests {
413368
// buy_amount reduced by 0.1% of 200 = 0.2 tokens
414369
let expected_buy = to_wei(200) - (to_wei(200) / U256::from(1000));
415370
assert_eq!(result.buy_amount, expected_buy);
416-
417-
// fee_in_buy = 200 * 0.001 = 0.2
418-
// fee_in_sell = 0.2 * (100 / 200) = 0.1
419-
let fee_in_buy = to_wei(200) / U256::from(1000);
420-
let expected_fee = fee_in_buy * to_wei(100) / to_wei(200);
421-
assert_eq!(result.protocol_fee_sell_amount, Some(expected_fee));
422371
}
423372

424373
#[test]

0 commit comments

Comments
 (0)