Skip to content

Commit

Permalink
[OrderbookDB] Different Reward Tables per Environment (#140)
Browse files Browse the repository at this point in the history
* use different tables for each environment

* handle both possiblities
  • Loading branch information
bh2smith authored Nov 22, 2022
1 parent 243efc5 commit 203da97
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
6 changes: 3 additions & 3 deletions queries/orderbook/order_rewards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ select concat('0x', encode(trade_hashes.order_uid, 'hex')) as order_uid,
when reward is null and fee_amount = 0 then False
end as safe_liquidity
from trade_hashes
left outer join order_rewards
on trade_hashes.order_uid = order_rewards.order_uid
and trade_hashes.auction_id = order_rewards.auction_id;
left outer join {{reward_table}} o
on trade_hashes.order_uid = o.order_uid
and trade_hashes.auction_id = o.auction_id;
22 changes: 20 additions & 2 deletions src/pg_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pandas as pd
from dotenv import load_dotenv
from pandas import DataFrame
from sqlalchemy.exc import ProgrammingError
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine

Expand Down Expand Up @@ -48,12 +49,29 @@ def _pg_engine(db_env: OrderbookEnv) -> Engine:
db_string = f"postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}"
return create_engine(db_string)

@classmethod
def _exec_query(cls, query: str, engine: Engine) -> DataFrame:
# TODO - once both environments have been migrated, this will no longer be necessary.
try:
# New Query
return pd.read_sql(
sql=query.replace("{{reward_table}}", "order_executions"), con=engine
)
except ProgrammingError:
# Use Old Query
# Unfortunately it appears impossible to capture the Base Error:
# psycopg2.errors.UndefinedTable
# But we know what it is.
return pd.read_sql(
sql=query.replace("{{reward_table}}", "order_rewards"), con=engine
)

@classmethod
def from_query(cls, query: str) -> DualEnvDataframe:
"""Fetch results of DB query on both prod and barn and returns the results as a pair"""
return cls(
barn=pd.read_sql(sql=query, con=cls._pg_engine(OrderbookEnv.PROD)),
prod=pd.read_sql(sql=query, con=cls._pg_engine(OrderbookEnv.BARN)),
barn=cls._exec_query(query, cls._pg_engine(OrderbookEnv.BARN)),
prod=cls._exec_query(query, cls._pg_engine(OrderbookEnv.PROD)),
)

def merge(self) -> DataFrame:
Expand Down

0 comments on commit 203da97

Please sign in to comment.