Skip to content

Commit 544ee88

Browse files
authored
Merge pull request #10 from cowprotocol/fix_missing_price_bug
Handle missing native prices for jit orders
2 parents abec4f9 + 95f6407 commit 544ee88

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

circuit_breaker_validator/scores.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
"""Functionality to compute scores"""
22

3+
# pylint: disable=logging-fstring-interpolation
4+
35
from fractions import Fraction
46

7+
from circuit_breaker_validator.logger import logger
58
from circuit_breaker_validator.models import (
69
OffchainSettlementData,
710
OnchainSettlementData,
@@ -33,6 +36,15 @@ def compute_score(
3336
"""
3437
score = 0
3538
for trade in onchain_data.trades:
39+
# missing native price counts as zero score
40+
if trade.buy_token not in offchain_data.native_prices:
41+
logger.info(
42+
f"Auction {onchain_data.auction_id}, settlement {onchain_data.tx_hash!r}: "
43+
f"Missing native price for trade {trade.order_uid!r} and buy token "
44+
f"{trade.buy_token!r}. Skipping score computation for this order."
45+
)
46+
continue
47+
3648
raw_surplus = trade.raw_surplus(
3749
offchain_data.trade_fee_policies.get(trade.order_uid, [])
3850
)

tests/test_scores.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,49 @@ def test_compute_score(order_type, surplus_token, expected_score):
6666
offchain_data.native_prices = {buy_token: price}
6767

6868
assert compute_score(onchain_data, offchain_data) == expected_score
69+
70+
71+
@pytest.mark.parametrize(
72+
"auction_id,tx_hash,solver,order_uid,sell_token,buy_token,raw_surplus,expected_score",
73+
[
74+
(
75+
1,
76+
HexBytes("0x00"),
77+
HexBytes("0x01"),
78+
HexBytes("0x02"),
79+
HexBytes("0x03"),
80+
HexBytes("0x04"),
81+
0,
82+
0,
83+
),
84+
],
85+
)
86+
def test_compute_score_missing_native_price(
87+
auction_id,
88+
tx_hash,
89+
solver,
90+
order_uid,
91+
sell_token,
92+
buy_token,
93+
raw_surplus,
94+
expected_score,
95+
):
96+
"Test for scores being zero for trades with missing native price"
97+
offchain_trade = Mock(spec=OffchainTrade)
98+
offchain_trade.order_uid = order_uid
99+
100+
trade = Mock(spec=OnchainTrade)
101+
trade.order_uid = order_uid
102+
trade.raw_surplus = Mock(return_value=raw_surplus)
103+
trade.sell_token = sell_token
104+
trade.buy_token = buy_token
105+
trade.surplus_token = Mock(return_value=trade.buy_token)
106+
107+
onchain_data = OnchainSettlementData(auction_id, tx_hash, solver, [trade])
108+
109+
offchain_data = Mock(spec=OffchainSettlementData)
110+
offchain_data.trades = [offchain_trade]
111+
offchain_data.trade_fee_policies = {}
112+
offchain_data.native_prices = {}
113+
114+
assert compute_score(onchain_data, offchain_data) == expected_score

0 commit comments

Comments
 (0)