Skip to content

Commit 8045a91

Browse files
committed
Fixed bug in score computation with price improvement fees
Solves #6
1 parent ce6daa7 commit 8045a91

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

circuit_breaker_validator/models.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,13 @@ def reverse_protocol_fee(self, trade: OnchainTrade) -> OnchainTrade:
311311
"""Reverse the price improvement-based protocol fee to get the pre-fee trade.
312312
313313
This method calculates what the trade would have been before improvement fee was applied:
314-
1. Calculate the price improvement compared to the reference quote
315-
2. Calculate the price improvement fee based on price_improvement_factor
316-
3. Calculate a maximum volume-based fee using price_improvement_max_volume_factor
317-
4. Take the minimum of these two fees to determine the actual fee
318-
5. For sell orders: Increase buy_amount by adding back the fee
319-
6. For buy orders: Decrease sell_amount by removing the fee
314+
1. Calculate the price improvement compared to the reference quote and limit price
315+
2. Use the better of quote or limit price for fee calculation
316+
3. Calculate the price improvement fee based on price_improvement_factor
317+
4. Calculate a maximum volume-based fee using price_improvement_max_volume_factor
318+
5. Take the minimum of these two fees to determine the actual fee
319+
6. For sell orders: Increase buy_amount by adding back the fee
320+
7. For buy orders: Decrease sell_amount by removing the fee
320321
321322
The fee calculations use:
322323
- price_improvement_fee = price_improvement * price_improvement_factor /
@@ -328,7 +329,16 @@ def reverse_protocol_fee(self, trade: OnchainTrade) -> OnchainTrade:
328329
Returns a new trade object with the fee reversed.
329330
"""
330331
new_trade = deepcopy(trade)
331-
price_improvement = trade.price_improvement(self.quote)
332+
333+
# Calculate price improvement compared to quote
334+
quote_price_improvement = trade.price_improvement(self.quote)
335+
336+
# Calculate price improvement compared to limit price (surplus)
337+
limit_price_improvement = trade.surplus()
338+
339+
# Use the better of quote or limit price for fee calculation
340+
# "Better" means smaller improvement, as that results in a smaller fee
341+
price_improvement = min(quote_price_improvement, limit_price_improvement)
332342
volume = trade.volume()
333343
price_improvement_fee = max(
334344
0,

0 commit comments

Comments
 (0)