Skip to content

Commit ea0c5f7

Browse files
committed
Integer calculations
1 parent dfb50cb commit ea0c5f7

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

crates/orderbook/src/arguments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub struct FeeFactor(f64);
156156

157157
impl FeeFactor {
158158
/// Number of basis points that make up 100%.
159-
const MAX_BPS: u32 = 10_000;
159+
pub const MAX_BPS: u32 = 10_000;
160160

161161
/// Converts the fee factor to basis points (BPS).
162162
/// For example, 0.0002 -> 2 BPS

crates/orderbook/src/quoter.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,16 @@ fn get_adjusted_quote_data(
172172
// Calculate the volume (surplus token amount) to apply fee to
173173
// Following driver's logic in
174174
// crates/driver/src/domain/competition/solution/fee.rs:189-202:
175-
let factor_f64: f64 = factor.into();
176175
let (adjusted_sell_amount, adjusted_buy_amount) = match side {
177176
OrderQuoteSide::Sell { .. } => {
178177
// For SELL orders, fee is calculated on buy amount
179-
let fee_f64 = quote.buy_amount.to_f64_lossy() * factor_f64;
180-
let protocol_fee = U256::from_f64_lossy(fee_f64);
178+
let protocol_fee = quote
179+
.buy_amount
180+
.full_mul(U256::from(factor.to_bps()))
181+
.checked_div(U256::from(FeeFactor::MAX_BPS).into())
182+
.ok_or_else(|| anyhow::anyhow!("volume fee calculation division by zero"))?
183+
.try_into()
184+
.map_err(|_| anyhow::anyhow!("volume fee calculation overflow"))?;
181185

182186
// Reduce buy amount by protocol fee
183187
let adjusted_buy = quote.buy_amount.saturating_sub(protocol_fee);
@@ -188,8 +192,12 @@ fn get_adjusted_quote_data(
188192
// For BUY orders, fee is calculated on sell amount + network fee.
189193
// Network fee is already in sell token, so it is added to get the total volume.
190194
let total_sell_volume = quote.sell_amount.saturating_add(quote.fee_amount);
191-
let fee_f64 = total_sell_volume.to_f64_lossy() * factor_f64;
192-
let protocol_fee = U256::from_f64_lossy(fee_f64);
195+
let protocol_fee = total_sell_volume
196+
.full_mul(U256::from(factor.to_bps()))
197+
.checked_div(U256::from(FeeFactor::MAX_BPS).into())
198+
.ok_or_else(|| anyhow::anyhow!("volume fee calculation division by zero"))?
199+
.try_into()
200+
.map_err(|_| anyhow::anyhow!("volume fee calculation overflow"))?;
193201

194202
// Increase sell amount by protocol fee
195203
let adjusted_sell = quote.sell_amount.saturating_add(protocol_fee);

0 commit comments

Comments
 (0)