-
Notifications
You must be signed in to change notification settings - Fork 1
Test for detecting discrepancies between native prices and ucp #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
harisang
merged 8 commits into
main
from
test_for_detecting_discrepancies_between_native_prices_and_ucp
Apr 24, 2025
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e891d56
add price sensitivity test
harisang e96d5bd
add e2e test
harisang cfb88ce
ignore pylint error
harisang d49c999
ignore pylint error again
harisang b42c69a
fix type
harisang 80f56d8
Update src/monitoring_tests/price_sensitivity_test.py
harisang 4d0968f
Update src/monitoring_tests/price_sensitivity_test.py
harisang 414e2e3
apply suggestion
harisang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| """ | ||
| Checks whether native prices are far from UCP for a trade | ||
| """ | ||
|
|
||
| # pylint: disable=duplicate-code | ||
| # pylint: disable=too-many-locals | ||
| from typing import Any | ||
| from fractions import Fraction | ||
| from src.monitoring_tests.base_test import BaseTest | ||
| from src.apis.orderbookapi import OrderbookAPI | ||
| from src.constants import ( | ||
| UCP_VS_NATIVE_SENSITIVITY_THRESHOLD, | ||
| ) | ||
|
|
||
|
|
||
| class PriceSensitivityTest(BaseTest): | ||
| """ | ||
| This test checks whether the exchange rate implied by native prices | ||
| is far from exchange rate implied by UCP | ||
| """ | ||
|
|
||
| def __init__(self, orderbook_api: OrderbookAPI) -> None: | ||
| super().__init__() | ||
| self.orderbook_api = orderbook_api | ||
|
|
||
| def check_prices(self, competition_data: dict[str, Any]) -> bool: | ||
| """ | ||
| This function checks whether native prices are far from ucp | ||
| """ | ||
| winning_solution = competition_data["solutions"][-1] | ||
| trades_dict = self.orderbook_api.get_uid_trades(winning_solution) | ||
| if trades_dict is None: | ||
| return False | ||
|
|
||
| ucp: dict[str, int] = {} | ||
| for token, price in winning_solution["clearingPrices"].items(): | ||
| ucp[token.lower()] = int(price) | ||
|
|
||
| native_prices: dict[str, int] = {} | ||
| for token, price in competition_data["auction"]["prices"].items(): | ||
| native_prices[token.lower()] = int(price) | ||
harisang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| for uid, trade in trades_dict.items(): | ||
| sell_token = trade.data.sell_token.lower() | ||
| buy_token = trade.data.buy_token.lower() | ||
| ucp_rate = Fraction(ucp[sell_token], ucp[buy_token]) | ||
| native_price_rate = Fraction( | ||
| native_prices[sell_token], native_prices[buy_token] | ||
| ) | ||
|
|
||
| if ucp_rate > native_price_rate: | ||
| max_rate = ucp_rate | ||
| min_rate = native_price_rate | ||
| else: | ||
| max_rate = native_price_rate | ||
| min_rate = ucp_rate | ||
harisang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if max_rate > (1 + UCP_VS_NATIVE_SENSITIVITY_THRESHOLD) * min_rate: | ||
| log_output = "\t".join( | ||
| [ | ||
| "Price sensitivity test:", | ||
| f"Tx Hash: {competition_data['transactionHashes'][0]}", | ||
| f"Winning Solver: {winning_solution['solver']}", | ||
| f"Trade: {uid}", | ||
| f"Gap: {float(max_rate / min_rate)}", | ||
| ] | ||
| ) | ||
| self.alert(log_output) | ||
|
|
||
| return True | ||
|
|
||
| def run(self, tx_hash: str) -> bool: | ||
| """ | ||
| Wrapper function for the whole test. Checks if violation is more than | ||
| UCP_VS_NATIVE_SENSITIVITY_THRESHOLD, in which case it generates an alert. | ||
| """ | ||
| solver_competition_data = self.orderbook_api.get_solver_competition_data( | ||
| tx_hash | ||
| ) | ||
| if solver_competition_data is None: | ||
| return False | ||
|
|
||
| success = self.check_prices(solver_competition_data) | ||
|
|
||
| return success | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| """ | ||
| Tests for surplus test. | ||
| """ | ||
|
|
||
| import unittest | ||
| from src.apis.orderbookapi import OrderbookAPI | ||
| from src.monitoring_tests.price_sensitivity_test import ( | ||
| PriceSensitivityTest, | ||
| ) | ||
|
|
||
|
|
||
| class TestPrices(unittest.TestCase): | ||
| def test_prices(self) -> None: | ||
| orderbook_api = OrderbookAPI("mainnet") | ||
| price_sensitivity_test = PriceSensitivityTest(orderbook_api) | ||
| # new competition format: no alert or info | ||
| tx_hash = "0x524b92cfc81e9b590a701bf157ca1b0f21b05d7c37ebb39f332cd215c8db1046" | ||
| self.assertTrue(price_sensitivity_test.run(tx_hash)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.