11use {
2- crate :: { app_data, arguments:: VolumeFeeConfig } ,
2+ crate :: {
3+ app_data,
4+ arguments:: { FeeFactor , VolumeFeeConfig } ,
5+ } ,
36 chrono:: { TimeZone , Utc } ,
47 model:: {
58 order:: OrderCreationAppData ,
@@ -177,12 +180,16 @@ fn get_adjusted_quote_data(
177180 // Calculate the volume (surplus token amount) to apply fee to
178181 // Following driver's logic in
179182 // crates/driver/src/domain/competition/solution/fee.rs:189-202:
180- let factor_f64: f64 = factor. into ( ) ;
181183 let ( adjusted_sell_amount, adjusted_buy_amount) = match side {
182184 OrderQuoteSide :: Sell { .. } => {
183185 // For SELL orders, fee is calculated on buy amount
184- let fee_f64 = quote. buy_amount . to_f64_lossy ( ) * factor_f64;
185- let protocol_fee = U256 :: from_f64_lossy ( fee_f64) ;
186+ let protocol_fee = quote
187+ . buy_amount
188+ . full_mul ( U256 :: from ( factor. to_bps ( ) ) )
189+ . checked_div ( U256 :: from ( FeeFactor :: MAX_BPS ) . into ( ) )
190+ . ok_or_else ( || anyhow:: anyhow!( "volume fee calculation division by zero" ) ) ?
191+ . try_into ( )
192+ . map_err ( |_| anyhow:: anyhow!( "volume fee calculation overflow" ) ) ?;
186193
187194 // Reduce buy amount by protocol fee
188195 let adjusted_buy = quote. buy_amount . saturating_sub ( protocol_fee) ;
@@ -193,8 +200,12 @@ fn get_adjusted_quote_data(
193200 // For BUY orders, fee is calculated on sell amount + network fee.
194201 // Network fee is already in sell token, so it is added to get the total volume.
195202 let total_sell_volume = quote. sell_amount . saturating_add ( quote. fee_amount ) ;
196- let fee_f64 = total_sell_volume. to_f64_lossy ( ) * factor_f64;
197- let protocol_fee = U256 :: from_f64_lossy ( fee_f64) ;
203+ let protocol_fee = total_sell_volume
204+ . full_mul ( U256 :: from ( factor. to_bps ( ) ) )
205+ . checked_div ( U256 :: from ( FeeFactor :: MAX_BPS ) . into ( ) )
206+ . ok_or_else ( || anyhow:: anyhow!( "volume fee calculation division by zero" ) ) ?
207+ . try_into ( )
208+ . map_err ( |_| anyhow:: anyhow!( "volume fee calculation overflow" ) ) ?;
198209
199210 // Increase sell amount by protocol fee
200211 let adjusted_sell = quote. sell_amount . saturating_add ( protocol_fee) ;
0 commit comments