|
| 1 | +""" |
| 2 | +Checks whether native prices are far from UCP for a trade |
| 3 | +""" |
| 4 | + |
| 5 | +# pylint: disable=duplicate-code |
| 6 | +# pylint: disable=too-many-locals |
| 7 | +from typing import Any |
| 8 | +from fractions import Fraction |
| 9 | +from src.monitoring_tests.base_test import BaseTest |
| 10 | +from src.apis.orderbookapi import OrderbookAPI |
| 11 | +from src.constants import ( |
| 12 | + UCP_VS_NATIVE_SENSITIVITY_THRESHOLD, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class PriceSensitivityTest(BaseTest): |
| 17 | + """ |
| 18 | + This test checks whether the exchange rate implied by native prices |
| 19 | + is far from exchange rate implied by UCP |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, orderbook_api: OrderbookAPI) -> None: |
| 23 | + super().__init__() |
| 24 | + self.orderbook_api = orderbook_api |
| 25 | + |
| 26 | + def check_prices(self, competition_data: dict[str, Any]) -> bool: |
| 27 | + """ |
| 28 | + This function checks whether native prices are far from ucp |
| 29 | + """ |
| 30 | + winning_solution = competition_data["solutions"][-1] |
| 31 | + trades_dict = self.orderbook_api.get_uid_trades(winning_solution) |
| 32 | + if trades_dict is None: |
| 33 | + return False |
| 34 | + |
| 35 | + ucp = { |
| 36 | + token.lower(): int(price) |
| 37 | + for token, price in winning_solution["clearingPrices"].items() |
| 38 | + } |
| 39 | + |
| 40 | + native_prices = { |
| 41 | + token.lower(): int(price) |
| 42 | + for token, price in competition_data["auction"]["prices"].items() |
| 43 | + } |
| 44 | + |
| 45 | + for uid, trade in trades_dict.items(): |
| 46 | + sell_token = trade.data.sell_token.lower() |
| 47 | + buy_token = trade.data.buy_token.lower() |
| 48 | + ucp_rate = Fraction(ucp[sell_token], ucp[buy_token]) |
| 49 | + native_price_rate = Fraction( |
| 50 | + native_prices[sell_token], native_prices[buy_token] |
| 51 | + ) |
| 52 | + |
| 53 | + max_rate = max(ucp_rate, native_price_rate) |
| 54 | + min_rate = min(ucp_rate, native_price_rate) |
| 55 | + |
| 56 | + if max_rate > (1 + UCP_VS_NATIVE_SENSITIVITY_THRESHOLD) * min_rate: |
| 57 | + log_output = "\t".join( |
| 58 | + [ |
| 59 | + "Price sensitivity test:", |
| 60 | + f"Tx Hash: {competition_data['transactionHashes'][0]}", |
| 61 | + f"Winning Solver: {winning_solution['solver']}", |
| 62 | + f"Trade: {uid}", |
| 63 | + f"Gap: {float(max_rate / min_rate)}", |
| 64 | + ] |
| 65 | + ) |
| 66 | + self.alert(log_output) |
| 67 | + |
| 68 | + return True |
| 69 | + |
| 70 | + def run(self, tx_hash: str) -> bool: |
| 71 | + """ |
| 72 | + Wrapper function for the whole test. Checks if violation is more than |
| 73 | + UCP_VS_NATIVE_SENSITIVITY_THRESHOLD, in which case it generates an alert. |
| 74 | + """ |
| 75 | + solver_competition_data = self.orderbook_api.get_solver_competition_data( |
| 76 | + tx_hash |
| 77 | + ) |
| 78 | + if solver_competition_data is None: |
| 79 | + return False |
| 80 | + |
| 81 | + success = self.check_prices(solver_competition_data) |
| 82 | + |
| 83 | + return success |
0 commit comments