Skip to content

Commit 46875c0

Browse files
committed
Incorporate network fee
1 parent b0107a4 commit 46875c0

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

crates/orderbook/src/quoter.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ fn get_adjusted_quote_data(
189189
(protocol_fee, quote.sell_amount, adjusted_buy)
190190
}
191191
OrderQuoteSide::Buy { .. } => {
192-
// For BUY orders, fee is calculated on sell amount
193-
let fee_f64 = quote.sell_amount.to_f64_lossy() * factor_f64;
192+
// For BUY orders, fee is calculated on sell amount + network fee.
193+
// Network fee is already in sell token, so it is added to get the total volume.
194+
let total_sell_volume = quote.sell_amount.saturating_add(quote.fee_amount);
195+
let fee_f64 = total_sell_volume.to_f64_lossy() * factor_f64;
194196
let protocol_fee = U256::from_f64_lossy(fee_f64);
195197

196198
// Increase sell amount by protocol fee
@@ -333,22 +335,22 @@ mod tests {
333335
fn test_volume_fee_buy_order() {
334336
let volume_fee = FeeFactor::try_from(0.0002).unwrap(); // 0.02% = 2 bps
335337

336-
// Buying 100 tokens, expecting to sell 100 tokens
338+
// Buying 100 tokens, expecting to sell 100 tokens, with no network fee
337339
let quote = create_test_quote(to_wei(100), to_wei(100));
338340
let side = OrderQuoteSide::Buy {
339341
buy_amount_after_fee: number::nonzero::U256::try_from(to_wei(100)).unwrap(),
340342
};
341343

342344
let result = get_adjusted_quote_data(&quote, Some(volume_fee), &side).unwrap();
343345

344-
// For BUY orders:
346+
// For BUY orders with no network fee:
345347
// - buy_amount stays the same
346348
// - sell_amount is increased by 0.02% of original sell_amount
347349
// - protocol_fee_bps = "2"
348350
assert_eq!(result.buy_amount, to_wei(100));
349351
assert_eq!(result.protocol_fee_bps, Some("2".to_string()));
350352

351-
// sell_amount should be increased by 0.02%
353+
// sell_amount should be increased by 0.02% of sell_amount (no network fee)
352354
// Expected: 100 + (100 * 0.0002) = 100 + 0.02 = 100.02
353355
let expected_sell = to_wei(100) + (to_wei(100) / U256::from(5000)); // 0.02% = 1/5000
354356
assert_eq!(result.sell_amount, expected_sell);
@@ -358,6 +360,39 @@ mod tests {
358360
assert_eq!(result.protocol_fee_sell_amount, Some(expected_fee));
359361
}
360362

363+
#[test]
364+
fn test_volume_fee_buy_order_with_network_fee() {
365+
let volume_fee = FeeFactor::try_from(0.0002).unwrap(); // 0.02% = 2 bps
366+
367+
// Buying 100 tokens, expecting to sell 100 tokens, with 5 token network fee
368+
let mut quote = create_test_quote(to_wei(100), to_wei(100));
369+
quote.fee_amount = to_wei(5); // Network fee in sell token
370+
let side = OrderQuoteSide::Buy {
371+
buy_amount_after_fee: number::nonzero::U256::try_from(to_wei(100)).unwrap(),
372+
};
373+
374+
let result = get_adjusted_quote_data(&quote, Some(volume_fee), &side).unwrap();
375+
376+
// For BUY orders with network fee:
377+
// - buy_amount stays the same
378+
// - protocol fee is calculated on (sell_amount + network_fee)
379+
// - sell_amount is increased by protocol fee
380+
assert_eq!(result.buy_amount, to_wei(100));
381+
assert_eq!(result.protocol_fee_bps, Some("2".to_string()));
382+
383+
// Total volume = sell_amount + network_fee = 100 + 5 = 105
384+
// Protocol fee = 105 * 0.0002 = 0.021
385+
// sell_amount should be increased by protocol fee
386+
// Expected: 100 + 0.021 = 100.021
387+
let total_volume = to_wei(100) + to_wei(5); // 105
388+
let expected_protocol_fee = total_volume / U256::from(5000); // 0.021
389+
let expected_sell = to_wei(100) + expected_protocol_fee; // 100.021
390+
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));
394+
}
395+
361396
#[test]
362397
fn test_volume_fee_different_prices() {
363398
let volume_fee = FeeFactor::try_from(0.001).unwrap(); // 0.1% = 10 bps

0 commit comments

Comments
 (0)