-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from cowprotocol/add-logging
logging and writing to db
- Loading branch information
Showing
10 changed files
with
418 additions
and
120 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,12 +1,19 @@ | ||
# .env.sample | ||
|
||
# URLs for DB connection | ||
ETHEREUM_DB_URL= | ||
GNOSIS_DB_URL= | ||
# DB connection | ||
DB_URL= | ||
|
||
# URLs for Node provider connection | ||
ETHEREUM_NODE_URL= | ||
GNOSIS_NODE_URL= | ||
# Node provider connection | ||
NODE_URL= | ||
|
||
# connecting to Solver Slippage DB | ||
SOLVER_SLIPPAGE_DB_URL= | ||
|
||
# configure chain sleep time, e.g. CHAIN_SLEEP_TIME=60 | ||
CHAIN_SLEEP_TIME= | ||
|
||
# add chain name, e.g. CHAIN_NAME=Ethereum | ||
CHAIN_NAME= | ||
|
||
# optional | ||
INFURA_KEY=infura_key_here | ||
INFURA_KEY= |
This file contains 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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
""" | ||
ERC20 ABI contract | ||
""" | ||
erc20_abi = [ | ||
{ | ||
"constant": True, | ||
|
This file contains 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 |
---|---|---|
|
@@ -8,4 +8,6 @@ black==23.3.0 | |
mypy==1.4.1 | ||
pylint==3.2.5 | ||
pytest==7.4.0 | ||
setuptools | ||
setuptools | ||
pandas-stubs | ||
types-psycopg2 |
This file contains 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 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 |
---|---|---|
@@ -1,10 +1,75 @@ | ||
import os | ||
from typing import Optional | ||
from sqlalchemy import text | ||
from sqlalchemy.exc import OperationalError | ||
from sqlalchemy import create_engine, Engine | ||
from dotenv import load_dotenv | ||
from src.helper_functions import get_logger | ||
|
||
|
||
load_dotenv() | ||
ETHEREUM_NODE_URL = os.getenv("ETHEREUM_NODE_URL") | ||
GNOSIS_NODE_URL = os.getenv("GNOSIS_NODE_URL") | ||
NODE_URL = os.getenv("NODE_URL") | ||
|
||
logger = get_logger("raw_token_imbalances") | ||
|
||
# Utilized by imbalances_script for computing for single tx hash | ||
CHAIN_RPC_ENDPOINTS = { | ||
"Ethereum": os.getenv("ETHEREUM_NODE_URL"), | ||
"Gnosis": os.getenv("GNOSIS_NODE_URL"), | ||
} | ||
|
||
|
||
def get_env_int(var_name: str) -> int: | ||
""" | ||
Function for safe conversion to int (prevents None -> int conversion issues raised by mypy) | ||
Retrieve environment variable and convert to int. Raise an error if not set. | ||
""" | ||
value = os.getenv(var_name) | ||
if value is None: | ||
raise ValueError(f"Environment variable {var_name} is not set.") | ||
try: | ||
return int(value) | ||
except ValueError: | ||
raise ValueError(f"Environment variable {var_name} must be a int.") | ||
|
||
|
||
CHAIN_SLEEP_TIME = get_env_int("CHAIN_SLEEP_TIME") | ||
|
||
|
||
def create_backend_db_connection(chain_name: str) -> Engine: | ||
"""function that creates a connection to the CoW db.""" | ||
read_db_url = os.getenv("DB_URL") | ||
|
||
if not read_db_url: | ||
raise ValueError(f"No database URL found for chain: {chain_name}") | ||
|
||
return create_engine(f"postgresql+psycopg2://{read_db_url}") | ||
|
||
|
||
def create_solver_slippage_db_connection() -> Engine: | ||
"""function that creates a connection to the CoW db.""" | ||
solver_db_url = os.getenv("SOLVER_SLIPPAGE_DB_URL") | ||
if not solver_db_url: | ||
raise ValueError( | ||
"Solver slippage database URL not found in environment variables." | ||
) | ||
|
||
return create_engine(f"postgresql+psycopg2://{solver_db_url}") | ||
|
||
CHAIN_RPC_ENDPOINTS = {"Ethereum": ETHEREUM_NODE_URL, "Gnosis": GNOSIS_NODE_URL} | ||
|
||
CHAIN_SLEEP_TIMES = {"Ethereum": 60, "Gnosis": 120} | ||
def check_db_connection(connection: Engine, chain_name: Optional[str] = None) -> Engine: | ||
""" | ||
Check if the database connection is still active. If not, create a new one. | ||
""" | ||
try: | ||
if connection: | ||
with connection.connect() as conn: # Use connection.connect() to get a Connection object | ||
conn.execute(text("SELECT 1")) | ||
except OperationalError: | ||
# if connection is closed, create new one | ||
connection = ( | ||
create_backend_db_connection(chain_name) | ||
if chain_name | ||
else create_solver_slippage_db_connection() | ||
) | ||
return connection |
This file contains 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
Oops, something went wrong.