diff --git a/mev_inspect/block.py b/mev_inspect/block.py index 1b82a1b..fd2c51d 100644 --- a/mev_inspect/block.py +++ b/mev_inspect/block.py @@ -1,5 +1,6 @@ import asyncio import logging +from time import sleep from typing import Dict, List, Optional, Tuple from sqlalchemy import orm @@ -22,17 +23,23 @@ UNI_TOKEN_1 = "0xd21220a7" async def _get_logs_for_topics(base_provider, after_block, before_block, topics): - logs = await base_provider.make_request( - "eth_getLogs", - [ - { - "fromBlock": hex(after_block), - "toBlock": hex(before_block), - "topics": topics, - } - ], - ) - return logs["result"] + while True: + try: + logs = await base_provider.make_request( + "eth_getLogs", + [ + { + "fromBlock": hex(after_block), + "toBlock": hex(before_block), + "topics": topics, + } + ], + ) + + return logs["result"] + except Exception as e: + print(f"Error, retrying {e}") + sleep(0.05) def _logs_by_tx(logs): @@ -83,13 +90,19 @@ async def classify_logs(logs, pool_reserves, w3): token0, token1 = pool_reserves[pool_address] else: addr = Web3.toChecksumAddress(pool_address) - token0, token1 = await asyncio.gather( - w3.eth.call({"to": addr, "data": UNI_TOKEN_0}), - w3.eth.call({"to": addr, "data": UNI_TOKEN_1}), - ) - token0 = w3.toHex(token0) - token1 = w3.toHex(token1) - pool_reserves[pool_address] = (token0, token1) + while True: + try: + token0, token1 = await asyncio.gather( + w3.eth.call({"to": addr, "data": UNI_TOKEN_0}), + w3.eth.call({"to": addr, "data": UNI_TOKEN_1}), + ) + token0 = w3.toHex(token0) + token1 = w3.toHex(token1) + pool_reserves[pool_address] = (token0, token1) + break + except Exception as e: + print(f"Error, retrying {e}") + sleep(0.05) am0in, am1in, am0out, am1out = get_swap(log["data"]) swap = Swap( diff --git a/profit_analysis/block_utils.py b/profit_analysis/block_utils.py index 0d3378d..21243a5 100644 --- a/profit_analysis/block_utils.py +++ b/profit_analysis/block_utils.py @@ -1,4 +1,5 @@ import datetime +from time import sleep import pandas as pd from profit_analysis.column_names import BLOCK_KEY, TIMESTAMP_KEY @@ -15,7 +16,13 @@ def add_block_timestamp(w3, profit_by_block): def get_block_timestamp(w3, block): - block_info = w3.eth.get_block(int(block)) - ts = block_info[TIMESTAMP_KEY] - dt = datetime.datetime.fromtimestamp(ts) - return dt + while True: + try: + block_info = w3.eth.get_block(int(block)) + ts = block_info[TIMESTAMP_KEY] + dt = datetime.datetime.fromtimestamp(ts) + + return dt + except Exception as e: + print(f"Error, retrying {e}") + sleep(0.05)