|
import requests |
|
import os |
|
from datetime import datetime, timedelta, UTC |
|
import pandas as pd |
|
from collections import defaultdict |
|
from typing import Any, Optional |
|
from tqdm import tqdm |
|
from live_utils import OMEN_SUBGRAPH_URL, CREATOR, BATCH_SIZE, DATA_DIR |
|
from utils import SUBGRAPH_API_KEY, _to_content |
|
from queries import omen_trader_votes_query |
|
|
|
|
|
headers = { |
|
"Accept": "application/json, multipart/mixed", |
|
"Content-Type": "application/json", |
|
} |
|
|
|
|
|
def _query_omen_xdai_subgraph( |
|
fpmm_id: str, |
|
) -> dict[str, Any]: |
|
"""Query the subgraph.""" |
|
omen_subgraph = OMEN_SUBGRAPH_URL.substitute(subgraph_api_key=SUBGRAPH_API_KEY) |
|
print(f"omen_subgraph = {omen_subgraph}") |
|
grouped_results = defaultdict(list) |
|
id_gt = "" |
|
|
|
while True: |
|
query = omen_trader_votes_query.substitute( |
|
fpmm_creator=CREATOR.lower(), |
|
first=BATCH_SIZE, |
|
id_gt=id_gt, |
|
fpmm_id=fpmm_id, |
|
) |
|
print(f"query for the omen to collect trades {query}") |
|
content_json = _to_content(query) |
|
|
|
res = requests.post(omen_subgraph, headers=headers, json=content_json) |
|
result_json = res.json() |
|
|
|
user_trades = result_json.get("data", {}).get("fpmmTrades", []) |
|
|
|
if not user_trades: |
|
break |
|
|
|
for trade in user_trades: |
|
fpmm_id = trade.get("fpmm", {}).get("id") |
|
grouped_results[fpmm_id].append(trade) |
|
|
|
id_gt = user_trades[len(user_trades) - 1]["id"] |
|
|
|
all_results = { |
|
"data": { |
|
"fpmmTrades": [ |
|
trade |
|
for trades_list in grouped_results.values() |
|
for trade in trades_list |
|
] |
|
} |
|
} |
|
|
|
return all_results |
|
|
|
|
|
def transform_trades(trades_json: dict) -> pd.DataFrame: |
|
|
|
print("transforming trades") |
|
df = pd.DataFrame(trades_json["data"]["fpmmTrades"]) |
|
if len(df) == 0: |
|
print("No trades for this market") |
|
return df |
|
|
|
|
|
|
|
|
|
df["trade_creator"] = df["creator"].apply(lambda x: x["id"]) |
|
|
|
|
|
fpmm = pd.json_normalize(df["fpmm"]) |
|
fpmm.columns = [f"fpmm.{col}" for col in fpmm.columns] |
|
df = pd.concat([df, fpmm], axis=1) |
|
|
|
|
|
df.drop(["fpmm"], axis=1, inplace=True) |
|
|
|
|
|
df.outcomeIndex = pd.to_numeric(df.outcomeIndex, errors="coerce") |
|
return df |
|
|
|
|
|
def compute_from_timestamp_value( |
|
fpmm_id: str, opening_timestamp: int, fpmms: pd.DataFrame |
|
) -> Optional[int]: |
|
"""Function to find the latest timestamp registered for a specific market""" |
|
try: |
|
market_data = fpmms.loc[fpmms["id"] == fpmm_id] |
|
|
|
if len(market_data) == 1: |
|
|
|
return opening_timestamp |
|
timestamps = (market_data.tokens_timestamp.values).sort() |
|
|
|
return timestamps[-2] |
|
except Exception as e: |
|
print( |
|
f"Error when trying to get the from timestamp value of the market id {fpmm_id}" |
|
) |
|
return None |
|
|
|
|
|
def compute_votes_distribution(market_trades: pd.DataFrame): |
|
"""Function to compute the distribution of votes for the trades of a market""" |
|
total_trades = len(market_trades) |
|
print(f"The total number of trades is {total_trades}") |
|
|
|
sum_outcome_index_1 = sum(market_trades.outcomeIndex) |
|
print(f"The total number of votes for index 1 is {sum_outcome_index_1}") |
|
percentage_index_1 = round((sum_outcome_index_1 / total_trades) * 100, 2) |
|
return (100 - percentage_index_1), percentage_index_1 |
|
|
|
|
|
def add_trading_info(fpmms: pd.DataFrame) -> None: |
|
|
|
print("Adding votes distribution per market") |
|
fpmms["votes_first_outcome_perc"] = 0.0 |
|
fpmms["votes_second_outcome_perc"] = 0.0 |
|
for i, fpmm in tqdm(fpmms.iterrows(), total=len(fpmms), desc="Analysing trades"): |
|
|
|
market_id = fpmm["id"] |
|
print(f"Adding information for the market {market_id}") |
|
market_trades_json = _query_omen_xdai_subgraph( |
|
fpmm_id=market_id, |
|
) |
|
market_trades = transform_trades(market_trades_json) |
|
if len(market_trades) == 0: |
|
continue |
|
|
|
print("Computing the votes distribution") |
|
first_outcome, second_outcome = compute_votes_distribution(market_trades) |
|
print( |
|
f"first outcome votes ={first_outcome}, second outcome votes = {second_outcome}" |
|
) |
|
fpmms.loc[fpmms["id"] == market_id, "votes_first_outcome_perc"] = first_outcome |
|
fpmms.loc[fpmms["id"] == market_id, "votes_second_outcome_perc"] = ( |
|
second_outcome |
|
) |
|
print("Dataset after adding trading info") |
|
print(fpmms.head()) |
|
return |
|
|
|
|
|
if __name__ == "__main__": |
|
print("collecting votes distribution") |
|
|