File size: 9,346 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#   -*- coding: utf-8 -*-
#   ------------------------------------------------------------------------------
#
#     Copyright 2024 Valory AG
#
#     Licensed under the Apache License, Version 2.0 (the "License");
#     you may not use this file except in compliance with the License.
#     You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.
#
#   ------------------------------------------------------------------------------

import functools
import warnings
from typing import Optional, Generator, Callable
import os
import pandas as pd
from datetime import datetime, timedelta, UTC
import requests
from tqdm import tqdm
from typing import List, Dict
from live_traders_data import add_trading_info
from utils import SUBGRAPH_API_KEY, measure_execution_time
from live_utils import OMEN_SUBGRAPH_URL, CREATOR, BATCH_SIZE, DATA_DIR
from queries import (
    FPMMS_WITH_TOKENS_QUERY,
    ID_FIELD,
    DATA_FIELD,
    ANSWER_FIELD,
    ANSWER_TIMESTAMP_FIELD,
    QUERY_FIELD,
    TITLE_FIELD,
    OUTCOMES_FIELD,
    OPENING_TIMESTAMP_FIELD,
    CREATION_TIMESTAMP_FIELD,
    LIQUIDITY_FIELD,
    LIQUIDIY_MEASURE_FIELD,
    TOKEN_AMOUNTS_FIELD,
    ERROR_FIELD,
    QUESTION_FIELD,
    FPMMS_FIELD,
)


ResponseItemType = List[Dict[str, str]]
SubgraphResponseType = Dict[str, ResponseItemType]


class RetriesExceeded(Exception):
    """Exception to raise when retries are exceeded during data-fetching."""

    def __init__(
        self, msg="Maximum retries were exceeded while trying to fetch the data!"
    ):
        super().__init__(msg)


def hacky_retry(func: Callable, n_retries: int = 3) -> Callable:
    """Create a hacky retry strategy.
        Unfortunately, we cannot use `requests.packages.urllib3.util.retry.Retry`,
        because the subgraph does not return the appropriate status codes in case of failure.
        Instead, it always returns code 200. Thus, we raise exceptions manually inside `make_request`,
        catch those exceptions in the hacky retry decorator and try again.
        Finally, if the allowed number of retries is exceeded, we raise a custom `RetriesExceeded` exception.

    :param func: the input request function.
    :param n_retries: the maximum allowed number of retries.
    :return: The request method with the hacky retry strategy applied.
    """

    @functools.wraps(func)
    def wrapper_hacky_retry(*args, **kwargs) -> SubgraphResponseType:
        """The wrapper for the hacky retry.

        :return: a response dictionary.
        """
        retried = 0

        while retried <= n_retries:
            try:
                if retried > 0:
                    warnings.warn(f"Retrying {retried}/{n_retries}...")

                return func(*args, **kwargs)
            except (ValueError, ConnectionError) as e:
                warnings.warn(e.args[0])
            finally:
                retried += 1

        raise RetriesExceeded()

    return wrapper_hacky_retry


@hacky_retry
def query_subgraph(url: str, query: str, key: str) -> SubgraphResponseType:
    """Query a subgraph.

    Args:
        url: the subgraph's URL.
        query: the query to be used.
        key: the key to use in order to access the required data.

    Returns:
        a response dictionary.
    """
    content = {QUERY_FIELD: query}
    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json",
    }
    res = requests.post(url, json=content, headers=headers)

    if res.status_code != 200:
        raise ConnectionError(
            "Something went wrong while trying to communicate with the subgraph "
            f"(Error: {res.status_code})!\n{res.text}"
        )

    body = res.json()
    if ERROR_FIELD in body.keys():
        raise ValueError(f"The given query is not correct: {body[ERROR_FIELD]}")

    data = body.get(DATA_FIELD, {}).get(key, None)
    if data is None:
        raise ValueError(f"Unknown error encountered!\nRaw response: \n{body}")

    return data


def fpmms_fetcher(current_timestamp: int) -> Generator[ResponseItemType, int, None]:
    """An indefinite fetcher for the FPMMs."""
    omen_subgraph = OMEN_SUBGRAPH_URL.substitute(subgraph_api_key=SUBGRAPH_API_KEY)
    print(f"omen_subgraph = {omen_subgraph}")
    while True:
        fpmm_id = yield
        fpmms_query = FPMMS_WITH_TOKENS_QUERY.substitute(
            creator=CREATOR,
            fpmm_id=fpmm_id,
            current_timestamp=current_timestamp,
            fpmms_field=FPMMS_FIELD,
            first=BATCH_SIZE,
            id_field=ID_FIELD,
            answer_timestamp_field=ANSWER_TIMESTAMP_FIELD,
            question_field=QUESTION_FIELD,
            outcomes_field=OUTCOMES_FIELD,
            title_field=TITLE_FIELD,
            opening_timestamp_field=OPENING_TIMESTAMP_FIELD,
            creation_timestamp_field=CREATION_TIMESTAMP_FIELD,
            liquidity_field=LIQUIDITY_FIELD,
            liquidity_measure_field=LIQUIDIY_MEASURE_FIELD,
            token_amounts_field=TOKEN_AMOUNTS_FIELD,
        )
        print(f"Executing query {fpmms_query}")
        yield query_subgraph(omen_subgraph, fpmms_query, FPMMS_FIELD)


def fetch_fpmms(current_timestamp: int) -> pd.DataFrame:
    """Fetch all the fpmms of the creator."""
    print("Fetching all markets")
    latest_id = ""
    fpmms = []
    fetcher = fpmms_fetcher(current_timestamp)
    for _ in tqdm(fetcher, unit="fpmms", unit_scale=BATCH_SIZE):
        batch = fetcher.send(latest_id)
        if len(batch) == 0:
            print("no data")
            break

        # TODO Add the incremental batching system from market creator
        # prev_fpmms is the previous local file with the markets
        # for fpmm in batch:
        #     if fpmm["id"] not in fpmms or "trades" not in prev_fpmms[fpmm["id"]]:
        #         prev_fpmms[fpmm["id"]] = fpmm
        print(f"length of the data received = {len(batch)}")
        latest_id = batch[-1].get(ID_FIELD, "")
        if latest_id == "":
            raise ValueError(f"Unexpected data format retrieved: {batch}")

        fpmms.extend(batch)

    print("Finished collecting data")
    return pd.DataFrame(fpmms)


def get_answer(fpmm: pd.Series) -> str:
    """Get an answer from its index, using Series of an FPMM."""
    return fpmm[QUESTION_FIELD][OUTCOMES_FIELD][fpmm[ANSWER_FIELD]]


def get_first_token_perc(row):
    if row["total_tokens"] == 0.0:
        return 0
    return round((row["token_first_amount"] / row["total_tokens"]) * 100, 2)


def get_second_token_perc(row):
    if row["total_tokens"] == 0.0:
        return 0
    return round((row["token_second_amount"] / row["total_tokens"]) * 100, 2)


def transform_fpmms(fpmms: pd.DataFrame, filename: str, current_timestamp: int) -> None:
    """Transform an FPMMS dataframe."""

    # prepare the new ones
    # Add current timestamp
    fpmms["tokens_timestamp"] = current_timestamp
    fpmms["open"] = True

    # computation of token distributions
    fpmms["token_first_amount"] = fpmms.outcomeTokenAmounts.apply(lambda x: int(x[0]))
    fpmms["token_second_amount"] = fpmms.outcomeTokenAmounts.apply(lambda x: int(x[1]))
    fpmms["total_tokens"] = fpmms.apply(
        lambda x: x.token_first_amount + x.token_second_amount, axis=1
    )
    fpmms["first_token_perc"] = fpmms.apply(lambda x: get_first_token_perc(x), axis=1)
    fpmms["second_token_perc"] = fpmms.apply(lambda x: get_second_token_perc(x), axis=1)
    fpmms.drop(
        columns=["token_first_amount", "token_second_amount", "total_tokens"],
        inplace=True,
    )
    # previous file to update?
    old_fpmms = None
    if os.path.exists(DATA_DIR / filename):
        old_fpmms = pd.read_parquet(DATA_DIR / filename)

    if old_fpmms is not None:
        # update which markets are not open anymore
        open_markets = list(fpmms.id.unique())
        print("Updating market status of old markets")
        open_mask = old_fpmms["id"].isin(open_markets)
        old_fpmms.loc[~open_mask, "status"] = False

        # now concatenate
        print("Appending new data to previous data")
        fpmms = pd.concat([old_fpmms, fpmms], ignore_index=True)
        # fpmms.drop_duplicates(inplace=True)

    return


@measure_execution_time
def compute_distributions(filename: Optional[str]) -> pd.DataFrame:
    """Fetch, process, store and return the markets as a Dataframe."""

    print("fetching new markets information")
    current_timestamp = int(datetime.now(UTC).timestamp())
    fpmms = fetch_fpmms(current_timestamp)
    print(fpmms.head())

    print("transforming and updating previous data")

    transform_fpmms(fpmms, filename, current_timestamp)
    print(fpmms.head())

    # WIP
    # print("Adding trading information")
    add_trading_info(fpmms)
    print("saving the data")
    print(fpmms.info())
    if filename:
        fpmms.to_parquet(DATA_DIR / filename, index=False)

    return fpmms


if __name__ == "__main__":
    compute_distributions("markets_live_data.parquet")