Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn and merge if solvers appear in prod and barn #411

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/fetch/payouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,13 @@ def construct_payouts(
batch_rewards_df = batch_rewards_df.drop(
["partner_list", "partner_fee_eth"], axis=1
)

assert batch_rewards_df["solver"].is_unique, "solver not unique in batch rewards"
harisang marked this conversation as resolved.
Show resolved Hide resolved
assert quote_rewards_df["solver"].is_unique, "solver not unique in quote rewards"
merged_df = pandas.merge(
quote_rewards_df, batch_rewards_df, on="solver", how="outer"
).fillna(0)

service_fee_df = pandas.DataFrame(dune.get_service_fee_status())
service_fee_df["service_fee"] = [
datetime.strptime(time_string, "%Y-%m-%d %H:%M:%S.%f %Z") <= dune.period.start
Expand Down
51 changes: 48 additions & 3 deletions src/pg_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@


import pandas as pd
from pandas import DataFrame
from pandas import DataFrame, Series
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine

from src.logger import set_log
from src.utils.query_file import open_query

log = set_log(__name__)


class MultiInstanceDBFetcher:
"""
Expand Down Expand Up @@ -57,7 +60,38 @@ def get_solver_rewards(self, start_block: str, end_block: str) -> DataFrame:
self.exec_query(query=batch_reward_query_barn, engine=engine)
)

return pd.concat(results)
results_df = pd.concat(results)

# warn and merge in case of solvers in both environments
if not results_df["solver"].is_unique:
duplicated_entries = results_df[results_df["solver"].duplicated(keep=False)]
log.warning(
f"Solvers found in both environments:\n {duplicated_entries}.\n Merging results."
)

def merge_lists(series: Series) -> list | None:
merged = []
for lst in series:
if lst is not None:
merged.extend(lst)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do a check here to see if the merge adds duplicates? It might make debugging easier to figure out where the duplicates are introduced.

Copy link
Collaborator Author

@fhenneke fhenneke Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds useful, but I tried to come up with a clean solution and failed. So if you have a good idea, feel free to propose it.

In principle, we could check the final dataframe after merging. That is probably easiest.

If we want to check if while merging, we probably only want it for the addresses and not for the values. If we already have two very similar but different functions, we can probably just modify the two columns in function. Then we might as well just remove duplicates right away. But the code looks significantly more indirect then, with aggregating multiple columns into multiple values.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added more printing but for now there is no additional warning in this function.

return merged if merged else None

results_df = (
results_df.groupby("solver")
.agg(
{
"primary_reward_eth": "sum",
"protocol_fee_eth": "sum",
"network_fee_eth": "sum",
# there can be duplicate entries in partner_list now
"partner_list": merge_lists,
"partner_fee_eth": merge_lists,
}
)
.reset_index()
)

return results_df

def get_quote_rewards(self, start_block: str, end_block: str) -> DataFrame:
"""Returns aggregated solver quote rewards for block range"""
Expand All @@ -70,8 +104,19 @@ def get_quote_rewards(self, start_block: str, end_block: str) -> DataFrame:
self.exec_query(query=quote_reward_query, engine=engine)
for engine in self.connections
]
results_df = pd.concat(results)

# warn and merge in case of solvers in both environments
if not results_df["solver"].is_unique:
duplicated_entries = results_df[results_df["solver"].duplicated(keep=False)]
log.warning(
f"Solvers found in both environments:\n {duplicated_entries}.\n Merging results."
)
results_df = (
results_df.groupby("solver").agg({"num_quotes": "sum"}).reset_index()
)

return pd.concat(results)
return results_df


def pg_hex2bytea(hex_address: str) -> str:
Expand Down
Loading