feat: add retries for jsonrpc requests (#40)

* feat: add jsonrpc retries for logs extraction

Signed-off-by: Luca Georges Francois <luca@quartz.technology>

* feat: add jsonrpc retries for analysis

Signed-off-by: Luca Georges Francois <luca@quartz.technology>

Signed-off-by: Luca Georges Francois <luca@quartz.technology>
This commit is contained in:
Luca G.F 2023-01-24 17:48:59 -08:00 committed by GitHub
parent 3a58c82327
commit 24ea73a0b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 22 deletions

View File

@ -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(

View File

@ -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)