@@ -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