File size: 5,145 Bytes
2cf93ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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()
        # print(f"result = {result_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:
    # convert to dataframe
    print("transforming trades")
    df = pd.DataFrame(trades_json["data"]["fpmmTrades"])
    if len(df) == 0:
        print("No trades for this market")
        return df

    # print(df.info())

    # convert creator to address
    df["trade_creator"] = df["creator"].apply(lambda x: x["id"])

    # normalize fpmm column
    fpmm = pd.json_normalize(df["fpmm"])
    fpmm.columns = [f"fpmm.{col}" for col in fpmm.columns]
    df = pd.concat([df, fpmm], axis=1)

    # drop fpmm column
    df.drop(["fpmm"], axis=1, inplace=True)

    # convert into int
    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]
        # how many previous samples do we have?
        if len(market_data) == 1:
            # take the opening Timestamp of the Market
            return opening_timestamp
        timestamps = (market_data.tokens_timestamp.values).sort()
        # the last value is the current timestamp so we need to take the previous one
        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}")
    # outcomeIndex is always 1 or 0?
    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:
    # Iterate over the markets
    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"):
        # read trades from latest read timestamp
        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
        # to compute the votes distribution
        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")