From 667bca866dc03c2d9cd6df5f8a29424e3340933c Mon Sep 17 00:00:00 2001 From: harisang Date: Sat, 7 Sep 2024 19:14:02 +0300 Subject: [PATCH 1/4] experiment with making api calls more robust --- src/fees/compute_fees.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/fees/compute_fees.py b/src/fees/compute_fees.py index c1b7391..7f01872 100644 --- a/src/fees/compute_fees.py +++ b/src/fees/compute_fees.py @@ -13,7 +13,7 @@ from hexbytes import HexBytes from web3 import Web3 -from src.constants import REQUEST_TIMEOUT, NULL_ADDRESS +from src.constants import REQUEST_TIMEOUT, NULL_ADDRESS, API_NUM_OF_RETRIES # types for trades @@ -405,21 +405,23 @@ def get_all_data(self, tx_hash: HexBytes) -> SettlementData: return settlement_data def get_auction_data(self, tx_hash: HexBytes): - for environment, url in self.orderbook_urls.items(): - try: - response = requests.get( - url + f"solver_competition/by_tx_hash/{tx_hash.to_0x_hex()}", - timeout=REQUEST_TIMEOUT, - ) - response.raise_for_status() - auction_data = response.json() - sleep(0.5) # introducing some delays so that we don't overload the api - return auction_data, environment - except requests.exceptions.HTTPError as err: - if err.response.status_code == 404: - pass + for i in range(API_NUM_OF_RETRIES): + sleep(0.5) + for environment, url in self.orderbook_urls.items(): + try: + response = requests.get( + url + f"solver_competition/by_tx_hash/{tx_hash.to_0x_hex()}", + timeout=REQUEST_TIMEOUT, + ) + response.raise_for_status() + auction_data = response.json() + sleep(0.5) # introducing some delays so that we don't overload the api + return auction_data, environment + except requests.exceptions.HTTPError as err: + if err.response.status_code == 404: + pass raise ConnectionError( - f"Error fetching off-chain data for tx {tx_hash.to_0x_hex()}" + f"Error fetching api auction data for tx {tx_hash.to_0x_hex()}" ) def get_order_data(self, uid: HexBytes, environment: str): From 1e93fc2986b01e4caf2cbe129140a8a02e9961a0 Mon Sep 17 00:00:00 2001 From: harisang Date: Sat, 7 Sep 2024 19:16:23 +0300 Subject: [PATCH 2/4] black fix --- src/fees/compute_fees.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/fees/compute_fees.py b/src/fees/compute_fees.py index 7f01872..1f1c4ce 100644 --- a/src/fees/compute_fees.py +++ b/src/fees/compute_fees.py @@ -415,7 +415,9 @@ def get_auction_data(self, tx_hash: HexBytes): ) response.raise_for_status() auction_data = response.json() - sleep(0.5) # introducing some delays so that we don't overload the api + sleep( + 0.5 + ) # introducing some delays so that we don't overload the api return auction_data, environment except requests.exceptions.HTTPError as err: if err.response.status_code == 404: From 98e94dc03bd3287833ad62f2c7d366dfce671768 Mon Sep 17 00:00:00 2001 From: harisang Date: Sat, 7 Sep 2024 19:17:17 +0300 Subject: [PATCH 3/4] ensure some small delay is introduced after each api call --- src/fees/compute_fees.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/fees/compute_fees.py b/src/fees/compute_fees.py index 1f1c4ce..e69455e 100644 --- a/src/fees/compute_fees.py +++ b/src/fees/compute_fees.py @@ -406,18 +406,17 @@ def get_all_data(self, tx_hash: HexBytes) -> SettlementData: def get_auction_data(self, tx_hash: HexBytes): for i in range(API_NUM_OF_RETRIES): - sleep(0.5) for environment, url in self.orderbook_urls.items(): try: response = requests.get( url + f"solver_competition/by_tx_hash/{tx_hash.to_0x_hex()}", timeout=REQUEST_TIMEOUT, ) - response.raise_for_status() - auction_data = response.json() sleep( 0.5 ) # introducing some delays so that we don't overload the api + response.raise_for_status() + auction_data = response.json() return auction_data, environment except requests.exceptions.HTTPError as err: if err.response.status_code == 404: From 38bf8ac238905342cf6c4af343e214642f8b52a8 Mon Sep 17 00:00:00 2001 From: harisang Date: Sat, 7 Sep 2024 19:19:29 +0300 Subject: [PATCH 4/4] Add constant --- src/constants.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/constants.py b/src/constants.py index 215f8ca..4a8276a 100644 --- a/src/constants.py +++ b/src/constants.py @@ -22,6 +22,7 @@ NULL_ADDRESS_STRING = "0x0000000000000000000000000000000000000000" REQUEST_TIMEOUT = 5 +API_NUM_OF_RETRIES = 5 # Time limit, currently set to 1 full day, after which Coingecko Token List is re-fetched (in seconds) COINGECKO_TOKEN_LIST_RELOAD_TIME = 86400