rosacastillo
commited on
Commit
·
cf2f2ff
1
Parent(s):
3751c36
updating scripts to be compatible with a separate roi analysis
Browse files- scripts/get_mech_info.py +36 -0
- scripts/profitability.py +27 -35
- scripts/pull_data.py +37 -30
- scripts/roi_analysis.py +130 -0
- scripts/tools.py +5 -2
- scripts/update_tools_accuracy.py +2 -0
- scripts/utils.py +19 -1
scripts/get_mech_info.py
CHANGED
@@ -64,6 +64,42 @@ def fetch_block_number(timestamp_from: int, timestamp_to: int) -> dict:
|
|
64 |
return blocks[0]
|
65 |
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
def get_mech_info_last_60_days() -> dict[str, Any]:
|
68 |
"""Query the subgraph to get the last 60 days of information from mech."""
|
69 |
|
|
|
64 |
return blocks[0]
|
65 |
|
66 |
|
67 |
+
def get_mech_info_2024() -> dict[str, Any]:
|
68 |
+
"""Query the subgraph to get the 2024 information from mech."""
|
69 |
+
|
70 |
+
date = "2024-01-01"
|
71 |
+
datetime_jan_2024 = datetime.strptime(date, "%Y-%m-%d")
|
72 |
+
timestamp_jan_2024 = int(datetime_jan_2024.timestamp())
|
73 |
+
margin = timedelta(seconds=5)
|
74 |
+
timestamp_jan_2024_plus_margin = int((datetime_jan_2024 + margin).timestamp())
|
75 |
+
|
76 |
+
jan_block_number = fetch_block_number(
|
77 |
+
timestamp_jan_2024, timestamp_jan_2024_plus_margin
|
78 |
+
)
|
79 |
+
# expecting only one block
|
80 |
+
jan_block_number = jan_block_number.get("number", "")
|
81 |
+
if jan_block_number.isdigit():
|
82 |
+
jan_block_number = int(jan_block_number)
|
83 |
+
|
84 |
+
if jan_block_number == "":
|
85 |
+
raise ValueError(
|
86 |
+
"Could not find a valid block number for the first of January 2024"
|
87 |
+
)
|
88 |
+
MECH_TO_INFO = {
|
89 |
+
# this block number is when the creator had its first tx ever, and after this mech's creation
|
90 |
+
"0xff82123dfb52ab75c417195c5fdb87630145ae81": (
|
91 |
+
"old_mech_abi.json",
|
92 |
+
jan_block_number,
|
93 |
+
),
|
94 |
+
# this block number is when this mech was created
|
95 |
+
"0x77af31de935740567cf4ff1986d04b2c964a786a": (
|
96 |
+
"new_mech_abi.json",
|
97 |
+
jan_block_number,
|
98 |
+
),
|
99 |
+
}
|
100 |
+
return MECH_TO_INFO
|
101 |
+
|
102 |
+
|
103 |
def get_mech_info_last_60_days() -> dict[str, Any]:
|
104 |
"""Query the subgraph to get the last 60 days of information from mech."""
|
105 |
|
scripts/profitability.py
CHANGED
@@ -22,14 +22,14 @@ import requests
|
|
22 |
import datetime
|
23 |
import pandas as pd
|
24 |
from collections import defaultdict
|
25 |
-
from typing import Any
|
26 |
from string import Template
|
27 |
from enum import Enum
|
28 |
from tqdm import tqdm
|
29 |
import numpy as np
|
30 |
from pathlib import Path
|
31 |
from get_mech_info import DATETIME_60_DAYS_AGO
|
32 |
-
from utils import SUBGRAPH_API_KEY
|
33 |
|
34 |
IRRELEVANT_TOOLS = [
|
35 |
"openai-text-davinci-002",
|
@@ -45,14 +45,12 @@ IRRELEVANT_TOOLS = [
|
|
45 |
]
|
46 |
QUERY_BATCH_SIZE = 1000
|
47 |
DUST_THRESHOLD = 10000000000000
|
48 |
-
INVALID_ANSWER_HEX = (
|
49 |
-
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
50 |
-
)
|
51 |
INVALID_ANSWER = -1
|
52 |
FPMM_CREATOR = "0x89c5cc945dd550bcffb72fe42bff002429f46fec"
|
53 |
DEFAULT_FROM_DATE = "1970-01-01T00:00:00"
|
54 |
DEFAULT_TO_DATE = "2038-01-19T03:14:07"
|
55 |
DEFAULT_FROM_TIMESTAMP = 0
|
|
|
56 |
DEFAULT_TO_TIMESTAMP = 2147483647
|
57 |
WXDAI_CONTRACT_ADDRESS = "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d"
|
58 |
DEFAULT_MECH_FEE = 0.01
|
@@ -251,7 +249,6 @@ def _query_omen_xdai_subgraph(
|
|
251 |
fpmm_to_timestamp: float,
|
252 |
) -> dict[str, Any]:
|
253 |
"""Query the subgraph."""
|
254 |
-
# url = "https://api.thegraph.com/subgraphs/name/protofire/omen-xdai"
|
255 |
OMEN_SUBGRAPH_URL = Template(
|
256 |
"""https://gateway-arbitrum.network.thegraph.com/api/${subgraph_api_key}/subgraphs/id/9fUVQpFwzpdWS9bq5WkAnmKbNNcoBwatMR4yZq81pbbz"""
|
257 |
)
|
@@ -301,7 +298,6 @@ def _query_omen_xdai_subgraph(
|
|
301 |
|
302 |
def _query_conditional_tokens_gc_subgraph(creator: str) -> dict[str, Any]:
|
303 |
"""Query the subgraph."""
|
304 |
-
# url = "https://api.thegraph.com/subgraphs/name/gnosis/conditional-tokens-gc"
|
305 |
SUBGRAPH_URL = Template(
|
306 |
"""https://gateway-arbitrum.network.thegraph.com/api/${subgraph_api_key}/subgraphs/id/7s9rGBffUTL8kDZuxvvpuc46v44iuDarbrADBFw5uVp2"""
|
307 |
)
|
@@ -338,22 +334,6 @@ def _query_conditional_tokens_gc_subgraph(creator: str) -> dict[str, Any]:
|
|
338 |
return all_results
|
339 |
|
340 |
|
341 |
-
def convert_hex_to_int(x: Union[str, float]) -> Union[int, float]:
|
342 |
-
"""Convert hex to int"""
|
343 |
-
if isinstance(x, float):
|
344 |
-
return np.nan
|
345 |
-
elif isinstance(x, str):
|
346 |
-
if x == INVALID_ANSWER_HEX:
|
347 |
-
return -1
|
348 |
-
else:
|
349 |
-
return int(x, 16)
|
350 |
-
|
351 |
-
|
352 |
-
def wei_to_unit(wei: int) -> float:
|
353 |
-
"""Converts wei to currency unit."""
|
354 |
-
return wei / 10**18
|
355 |
-
|
356 |
-
|
357 |
def _is_redeemed(user_json: dict[str, Any], fpmmTrade: dict[str, Any]) -> bool:
|
358 |
"""Returns whether the user has redeemed the position."""
|
359 |
user_positions = user_json["data"]["user"]["userPositions"]
|
@@ -404,12 +384,17 @@ def create_fpmmTrades(rpc: str, from_timestamp: float = DEFAULT_FROM_TIMESTAMP):
|
|
404 |
return df
|
405 |
|
406 |
|
407 |
-
def prepare_profitalibity_data(
|
|
|
|
|
|
|
|
|
|
|
408 |
"""Prepare data for profitalibity analysis."""
|
409 |
|
410 |
# Check if tools.parquet is in the same directory
|
411 |
try:
|
412 |
-
tools = pd.read_parquet(DATA_DIR /
|
413 |
|
414 |
# make sure creator_address is in the columns
|
415 |
assert "trader_address" in tools.columns, "trader_address column not found"
|
@@ -420,20 +405,18 @@ def prepare_profitalibity_data(rpc: str):
|
|
420 |
# drop duplicates
|
421 |
tools.drop_duplicates(inplace=True)
|
422 |
|
423 |
-
print("
|
424 |
except FileNotFoundError:
|
425 |
print("tools.parquet not found. Please run tools.py first.")
|
426 |
return
|
427 |
|
428 |
# Check if fpmmTrades.parquet is in the same directory
|
429 |
try:
|
430 |
-
fpmmTrades = pd.read_parquet(DATA_DIR /
|
431 |
-
print("
|
432 |
except FileNotFoundError:
|
433 |
print("fpmmTrades.parquet not found. Creating fpmmTrades.parquet...")
|
434 |
-
|
435 |
-
timestamp_60_days_ago = (DATETIME_60_DAYS_AGO).timestamp()
|
436 |
-
fpmmTrades = create_fpmmTrades(rpc, from_timestamp=timestamp_60_days_ago)
|
437 |
fpmmTrades.to_parquet(DATA_DIR / "fpmmTrades.parquet", index=False)
|
438 |
|
439 |
# make sure trader_address is in the columns
|
@@ -621,19 +604,28 @@ def summary_analyse(df):
|
|
621 |
return summary_df
|
622 |
|
623 |
|
624 |
-
def run_profitability_analysis(
|
|
|
|
|
|
|
|
|
|
|
625 |
"""Create all trades analysis."""
|
626 |
|
627 |
# load dfs from data folder for analysis
|
628 |
-
print("Preparing data
|
629 |
-
fpmmTrades, tools = prepare_profitalibity_data(
|
|
|
|
|
630 |
tools["trader_address"] = tools["trader_address"].str.lower()
|
631 |
|
632 |
# all trades profitability df
|
633 |
print("Analysing trades...")
|
634 |
all_trades_df = analyse_all_traders(fpmmTrades, tools)
|
635 |
|
636 |
-
# filter invalid markets. Condition: "is_invalid"
|
|
|
|
|
637 |
invalid_market_mask = all_trades_df["is_invalid"]
|
638 |
all_trades_df = all_trades_df[~invalid_market_mask]
|
639 |
|
|
|
22 |
import datetime
|
23 |
import pandas as pd
|
24 |
from collections import defaultdict
|
25 |
+
from typing import Any
|
26 |
from string import Template
|
27 |
from enum import Enum
|
28 |
from tqdm import tqdm
|
29 |
import numpy as np
|
30 |
from pathlib import Path
|
31 |
from get_mech_info import DATETIME_60_DAYS_AGO
|
32 |
+
from utils import SUBGRAPH_API_KEY, wei_to_unit, convert_hex_to_int
|
33 |
|
34 |
IRRELEVANT_TOOLS = [
|
35 |
"openai-text-davinci-002",
|
|
|
45 |
]
|
46 |
QUERY_BATCH_SIZE = 1000
|
47 |
DUST_THRESHOLD = 10000000000000
|
|
|
|
|
|
|
48 |
INVALID_ANSWER = -1
|
49 |
FPMM_CREATOR = "0x89c5cc945dd550bcffb72fe42bff002429f46fec"
|
50 |
DEFAULT_FROM_DATE = "1970-01-01T00:00:00"
|
51 |
DEFAULT_TO_DATE = "2038-01-19T03:14:07"
|
52 |
DEFAULT_FROM_TIMESTAMP = 0
|
53 |
+
DEFAULT_60_DAYS_AGO_TIMESTAMP = (DATETIME_60_DAYS_AGO).timestamp()
|
54 |
DEFAULT_TO_TIMESTAMP = 2147483647
|
55 |
WXDAI_CONTRACT_ADDRESS = "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d"
|
56 |
DEFAULT_MECH_FEE = 0.01
|
|
|
249 |
fpmm_to_timestamp: float,
|
250 |
) -> dict[str, Any]:
|
251 |
"""Query the subgraph."""
|
|
|
252 |
OMEN_SUBGRAPH_URL = Template(
|
253 |
"""https://gateway-arbitrum.network.thegraph.com/api/${subgraph_api_key}/subgraphs/id/9fUVQpFwzpdWS9bq5WkAnmKbNNcoBwatMR4yZq81pbbz"""
|
254 |
)
|
|
|
298 |
|
299 |
def _query_conditional_tokens_gc_subgraph(creator: str) -> dict[str, Any]:
|
300 |
"""Query the subgraph."""
|
|
|
301 |
SUBGRAPH_URL = Template(
|
302 |
"""https://gateway-arbitrum.network.thegraph.com/api/${subgraph_api_key}/subgraphs/id/7s9rGBffUTL8kDZuxvvpuc46v44iuDarbrADBFw5uVp2"""
|
303 |
)
|
|
|
334 |
return all_results
|
335 |
|
336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
def _is_redeemed(user_json: dict[str, Any], fpmmTrade: dict[str, Any]) -> bool:
|
338 |
"""Returns whether the user has redeemed the position."""
|
339 |
user_positions = user_json["data"]["user"]["userPositions"]
|
|
|
384 |
return df
|
385 |
|
386 |
|
387 |
+
def prepare_profitalibity_data(
|
388 |
+
rpc: str,
|
389 |
+
tools_filename: str = "tools.parquet",
|
390 |
+
trades_filename: str = "fpmmTrades.parquet",
|
391 |
+
from_timestamp: float = DEFAULT_60_DAYS_AGO_TIMESTAMP,
|
392 |
+
):
|
393 |
"""Prepare data for profitalibity analysis."""
|
394 |
|
395 |
# Check if tools.parquet is in the same directory
|
396 |
try:
|
397 |
+
tools = pd.read_parquet(DATA_DIR / tools_filename)
|
398 |
|
399 |
# make sure creator_address is in the columns
|
400 |
assert "trader_address" in tools.columns, "trader_address column not found"
|
|
|
405 |
# drop duplicates
|
406 |
tools.drop_duplicates(inplace=True)
|
407 |
|
408 |
+
print(f"{tools_filename} loaded")
|
409 |
except FileNotFoundError:
|
410 |
print("tools.parquet not found. Please run tools.py first.")
|
411 |
return
|
412 |
|
413 |
# Check if fpmmTrades.parquet is in the same directory
|
414 |
try:
|
415 |
+
fpmmTrades = pd.read_parquet(DATA_DIR / trades_filename)
|
416 |
+
print(f"{trades_filename} loaded")
|
417 |
except FileNotFoundError:
|
418 |
print("fpmmTrades.parquet not found. Creating fpmmTrades.parquet...")
|
419 |
+
fpmmTrades = create_fpmmTrades(rpc, from_timestamp=from_timestamp)
|
|
|
|
|
420 |
fpmmTrades.to_parquet(DATA_DIR / "fpmmTrades.parquet", index=False)
|
421 |
|
422 |
# make sure trader_address is in the columns
|
|
|
604 |
return summary_df
|
605 |
|
606 |
|
607 |
+
def run_profitability_analysis(
|
608 |
+
rpc: str,
|
609 |
+
tools_filename: str = "tools.parquet",
|
610 |
+
trades_filename: str = "fpmmTrades.parquet",
|
611 |
+
from_timestamp: float = DEFAULT_60_DAYS_AGO_TIMESTAMP,
|
612 |
+
):
|
613 |
"""Create all trades analysis."""
|
614 |
|
615 |
# load dfs from data folder for analysis
|
616 |
+
print(f"Preparing data with {tools_filename} and {trades_filename}")
|
617 |
+
fpmmTrades, tools = prepare_profitalibity_data(
|
618 |
+
rpc, tools_filename, trades_filename, from_timestamp
|
619 |
+
)
|
620 |
tools["trader_address"] = tools["trader_address"].str.lower()
|
621 |
|
622 |
# all trades profitability df
|
623 |
print("Analysing trades...")
|
624 |
all_trades_df = analyse_all_traders(fpmmTrades, tools)
|
625 |
|
626 |
+
# filter invalid markets. Condition: "is_invalid" is True
|
627 |
+
# TODO fix this mask
|
628 |
+
print(all_trades_df.head())
|
629 |
invalid_market_mask = all_trades_df["is_invalid"]
|
630 |
all_trades_df = all_trades_df[~invalid_market_mask]
|
631 |
|
scripts/pull_data.py
CHANGED
@@ -19,7 +19,7 @@ from tools import (
|
|
19 |
)
|
20 |
from profitability import run_profitability_analysis
|
21 |
from utils import get_question, current_answer
|
22 |
-
|
23 |
import gc
|
24 |
|
25 |
logging.basicConfig(level=logging.INFO)
|
@@ -46,35 +46,8 @@ def parallelize_timestamp_conversion(df: pd.DataFrame, function: callable) -> li
|
|
46 |
return results
|
47 |
|
48 |
|
49 |
-
def
|
50 |
-
"""Run weekly analysis for the FPMMS project."""
|
51 |
-
rpc = "https://lb.nodies.app/v1/406d8dcc043f4cb3959ed7d6673d311a"
|
52 |
web3 = Web3(Web3.HTTPProvider(rpc))
|
53 |
-
|
54 |
-
# Run markets ETL
|
55 |
-
logging.info("Running markets ETL")
|
56 |
-
mkt_etl(MARKETS_FILENAME)
|
57 |
-
logging.info("Markets ETL completed")
|
58 |
-
|
59 |
-
# Run tools ETL
|
60 |
-
logging.info("Running tools ETL")
|
61 |
-
|
62 |
-
# This etl is saving already the tools parquet file
|
63 |
-
tools_etl(
|
64 |
-
rpcs=[rpc],
|
65 |
-
filename=TOOLS_FILENAME,
|
66 |
-
)
|
67 |
-
logging.info("Tools ETL completed")
|
68 |
-
|
69 |
-
# Run profitability analysis
|
70 |
-
logging.info("Running profitability analysis")
|
71 |
-
if os.path.exists(DATA_DIR / "fpmmTrades.parquet"):
|
72 |
-
os.remove(DATA_DIR / "fpmmTrades.parquet")
|
73 |
-
run_profitability_analysis(
|
74 |
-
rpc=rpc,
|
75 |
-
)
|
76 |
-
logging.info("Profitability analysis completed")
|
77 |
-
|
78 |
# Get currentAnswer from FPMMS
|
79 |
fpmms = pd.read_parquet(DATA_DIR / MARKETS_FILENAME)
|
80 |
tools = pd.read_parquet(DATA_DIR / TOOLS_FILENAME)
|
@@ -134,8 +107,42 @@ def weekly_analysis():
|
|
134 |
del t_map
|
135 |
gc.collect()
|
136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
logging.info("Weekly analysis files generated and saved")
|
138 |
|
139 |
|
140 |
if __name__ == "__main__":
|
141 |
-
weekly_analysis()
|
|
|
|
|
|
19 |
)
|
20 |
from profitability import run_profitability_analysis
|
21 |
from utils import get_question, current_answer
|
22 |
+
from get_mech_info import get_mech_info_last_60_days
|
23 |
import gc
|
24 |
|
25 |
logging.basicConfig(level=logging.INFO)
|
|
|
46 |
return results
|
47 |
|
48 |
|
49 |
+
def updating_timestamps(rpc: str):
|
|
|
|
|
50 |
web3 = Web3(Web3.HTTPProvider(rpc))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
# Get currentAnswer from FPMMS
|
52 |
fpmms = pd.read_parquet(DATA_DIR / MARKETS_FILENAME)
|
53 |
tools = pd.read_parquet(DATA_DIR / TOOLS_FILENAME)
|
|
|
107 |
del t_map
|
108 |
gc.collect()
|
109 |
|
110 |
+
|
111 |
+
def weekly_analysis():
|
112 |
+
"""Run weekly analysis for the FPMMS project."""
|
113 |
+
rpc = "https://lb.nodies.app/v1/406d8dcc043f4cb3959ed7d6673d311a"
|
114 |
+
|
115 |
+
# Run markets ETL
|
116 |
+
logging.info("Running markets ETL")
|
117 |
+
mkt_etl(MARKETS_FILENAME)
|
118 |
+
logging.info("Markets ETL completed")
|
119 |
+
|
120 |
+
# Run tools ETL
|
121 |
+
logging.info("Running tools ETL")
|
122 |
+
|
123 |
+
# This etl is saving already the tools parquet file
|
124 |
+
tools_etl(
|
125 |
+
rpcs=[rpc],
|
126 |
+
mech_info=get_mech_info_last_60_days(),
|
127 |
+
filename=TOOLS_FILENAME,
|
128 |
+
)
|
129 |
+
logging.info("Tools ETL completed")
|
130 |
+
|
131 |
+
# Run profitability analysis
|
132 |
+
logging.info("Running profitability analysis")
|
133 |
+
if os.path.exists(DATA_DIR / "fpmmTrades.parquet"):
|
134 |
+
os.remove(DATA_DIR / "fpmmTrades.parquet")
|
135 |
+
run_profitability_analysis(
|
136 |
+
rpc=rpc,
|
137 |
+
)
|
138 |
+
logging.info("Profitability analysis completed")
|
139 |
+
|
140 |
+
updating_timestamps(rpc)
|
141 |
+
|
142 |
logging.info("Weekly analysis files generated and saved")
|
143 |
|
144 |
|
145 |
if __name__ == "__main__":
|
146 |
+
# weekly_analysis()
|
147 |
+
rpc = "https://lb.nodies.app/v1/406d8dcc043f4cb3959ed7d6673d311a"
|
148 |
+
updating_timestamps(rpc)
|
scripts/roi_analysis.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import os
|
3 |
+
import pickle
|
4 |
+
from web3 import Web3
|
5 |
+
import pandas as pd
|
6 |
+
from functools import partial
|
7 |
+
from datetime import datetime
|
8 |
+
from markets import (
|
9 |
+
etl as mkt_etl,
|
10 |
+
DEFAULT_FILENAME as MARKETS_FILENAME,
|
11 |
+
)
|
12 |
+
|
13 |
+
TOOLS_FILENAME = "tools_2024.parquet"
|
14 |
+
from tools import (
|
15 |
+
etl as tools_etl,
|
16 |
+
update_tools_accuracy,
|
17 |
+
)
|
18 |
+
from pull_data import (
|
19 |
+
DATA_DIR,
|
20 |
+
parallelize_timestamp_conversion,
|
21 |
+
block_number_to_timestamp,
|
22 |
+
)
|
23 |
+
from profitability import run_profitability_analysis
|
24 |
+
from get_mech_info import get_mech_info_2024
|
25 |
+
from utils import get_question, current_answer
|
26 |
+
import gc
|
27 |
+
|
28 |
+
logging.basicConfig(level=logging.INFO)
|
29 |
+
|
30 |
+
|
31 |
+
def roi_analysis():
|
32 |
+
"""Run ROI analysis for the trades done in 2024."""
|
33 |
+
rpc = "https://lb.nodies.app/v1/406d8dcc043f4cb3959ed7d6673d311a"
|
34 |
+
web3 = Web3(Web3.HTTPProvider(rpc))
|
35 |
+
|
36 |
+
# Run markets ETL
|
37 |
+
logging.info("Running markets ETL")
|
38 |
+
mkt_etl(MARKETS_FILENAME)
|
39 |
+
logging.info("Markets ETL completed")
|
40 |
+
|
41 |
+
# Run tools ETL
|
42 |
+
logging.info("Running tools ETL")
|
43 |
+
|
44 |
+
# This etl is saving already the tools parquet file
|
45 |
+
tools_etl(
|
46 |
+
rpcs=[rpc],
|
47 |
+
mech_info=get_mech_info_2024(),
|
48 |
+
filename=TOOLS_FILENAME,
|
49 |
+
)
|
50 |
+
logging.info("Tools ETL completed")
|
51 |
+
|
52 |
+
# Run profitability analysis
|
53 |
+
if os.path.exists(DATA_DIR / "fpmmTrades.parquet"):
|
54 |
+
os.remove(DATA_DIR / "fpmmTrades.parquet")
|
55 |
+
logging.info("Running profitability analysis")
|
56 |
+
date = "2024-01-01"
|
57 |
+
datetime_jan_2024 = datetime.strptime(date, "%Y-%m-%d")
|
58 |
+
timestamp_jan_2024 = int(datetime_jan_2024.timestamp())
|
59 |
+
run_profitability_analysis(
|
60 |
+
rpc=rpc,
|
61 |
+
tools_filename=TOOLS_FILENAME,
|
62 |
+
trades_filename="fpmmTrades.parquet",
|
63 |
+
from_timestamp=timestamp_jan_2024,
|
64 |
+
)
|
65 |
+
logging.info("Profitability analysis completed")
|
66 |
+
|
67 |
+
# Get currentAnswer from FPMMS
|
68 |
+
fpmms = pd.read_parquet(DATA_DIR / MARKETS_FILENAME)
|
69 |
+
tools = pd.read_parquet(DATA_DIR / TOOLS_FILENAME)
|
70 |
+
|
71 |
+
# Get the question from the tools
|
72 |
+
logging.info("Getting the question and current answer for the tools")
|
73 |
+
tools["title"] = tools["prompt_request"].apply(lambda x: get_question(x))
|
74 |
+
tools["currentAnswer"] = tools["title"].apply(lambda x: current_answer(x, fpmms))
|
75 |
+
|
76 |
+
tools["currentAnswer"] = tools["currentAnswer"].str.replace("yes", "Yes")
|
77 |
+
tools["currentAnswer"] = tools["currentAnswer"].str.replace("no", "No")
|
78 |
+
|
79 |
+
# Convert block number to timestamp
|
80 |
+
logging.info("Converting block number to timestamp")
|
81 |
+
t_map = pickle.load(open(DATA_DIR / "t_map.pkl", "rb"))
|
82 |
+
tools["request_time"] = tools["request_block"].map(t_map)
|
83 |
+
|
84 |
+
# Identify tools with missing request_time and fill them
|
85 |
+
missing_time_indices = tools[tools["request_time"].isna()].index
|
86 |
+
if not missing_time_indices.empty:
|
87 |
+
partial_block_number_to_timestamp = partial(
|
88 |
+
block_number_to_timestamp, web3=web3
|
89 |
+
)
|
90 |
+
missing_timestamps = parallelize_timestamp_conversion(
|
91 |
+
tools.loc[missing_time_indices], partial_block_number_to_timestamp
|
92 |
+
)
|
93 |
+
|
94 |
+
# Update the original DataFrame with the missing timestamps
|
95 |
+
for i, timestamp in zip(missing_time_indices, missing_timestamps):
|
96 |
+
tools.at[i, "request_time"] = timestamp
|
97 |
+
|
98 |
+
tools["request_month_year"] = pd.to_datetime(tools["request_time"]).dt.strftime(
|
99 |
+
"%Y-%m"
|
100 |
+
)
|
101 |
+
tools["request_month_year_week"] = (
|
102 |
+
pd.to_datetime(tools["request_time"]).dt.to_period("W").astype(str)
|
103 |
+
)
|
104 |
+
|
105 |
+
# Save the tools data after the updates on the content
|
106 |
+
tools.to_parquet(DATA_DIR / TOOLS_FILENAME, index=False)
|
107 |
+
|
108 |
+
# Update t_map with new timestamps
|
109 |
+
new_timestamps = (
|
110 |
+
tools[["request_block", "request_time"]]
|
111 |
+
.dropna()
|
112 |
+
.set_index("request_block")
|
113 |
+
.to_dict()["request_time"]
|
114 |
+
)
|
115 |
+
t_map.update(new_timestamps)
|
116 |
+
|
117 |
+
with open(DATA_DIR / "t_map_2024.pkl", "wb") as f:
|
118 |
+
pickle.dump(t_map, f)
|
119 |
+
|
120 |
+
# clean and release all memory
|
121 |
+
del tools
|
122 |
+
del fpmms
|
123 |
+
del t_map
|
124 |
+
gc.collect()
|
125 |
+
|
126 |
+
logging.info("ROI analysis files generated and saved")
|
127 |
+
|
128 |
+
|
129 |
+
if __name__ == "__main__":
|
130 |
+
roi_analysis()
|
scripts/tools.py
CHANGED
@@ -26,6 +26,7 @@ from typing import (
|
|
26 |
List,
|
27 |
Dict,
|
28 |
Union,
|
|
|
29 |
)
|
30 |
import pandas as pd
|
31 |
import requests
|
@@ -46,7 +47,6 @@ from web3 import Web3, HTTPProvider
|
|
46 |
from web3.exceptions import MismatchedABI
|
47 |
from web3.types import BlockParams
|
48 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
49 |
-
from get_mech_info import get_mech_info_last_60_days
|
50 |
from utils import (
|
51 |
clean,
|
52 |
BLOCK_FIELD,
|
@@ -376,6 +376,7 @@ def store_progress(
|
|
376 |
|
377 |
def etl(
|
378 |
rpcs: List[str],
|
|
|
379 |
filename: Optional[str] = None,
|
380 |
) -> pd.DataFrame:
|
381 |
"""Fetch from on-chain events, process, store and return the tools' results on
|
@@ -392,7 +393,7 @@ def etl(
|
|
392 |
os.path.join(CONTRACTS_PATH, filename),
|
393 |
earliest_block,
|
394 |
)
|
395 |
-
for address, (filename, earliest_block) in
|
396 |
}
|
397 |
|
398 |
event_to_contents = {}
|
@@ -526,6 +527,8 @@ def update_tools_accuracy(
|
|
526 |
# update the old information
|
527 |
print("Updating accuracy information")
|
528 |
tools_to_update = list(acc_info["tool"].values)
|
|
|
|
|
529 |
existing_tools = list(tools_acc["tool"].values)
|
530 |
for tool in tools_to_update:
|
531 |
if tool in existing_tools:
|
|
|
26 |
List,
|
27 |
Dict,
|
28 |
Union,
|
29 |
+
Any,
|
30 |
)
|
31 |
import pandas as pd
|
32 |
import requests
|
|
|
47 |
from web3.exceptions import MismatchedABI
|
48 |
from web3.types import BlockParams
|
49 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
|
50 |
from utils import (
|
51 |
clean,
|
52 |
BLOCK_FIELD,
|
|
|
376 |
|
377 |
def etl(
|
378 |
rpcs: List[str],
|
379 |
+
mech_info: dict[str, Any],
|
380 |
filename: Optional[str] = None,
|
381 |
) -> pd.DataFrame:
|
382 |
"""Fetch from on-chain events, process, store and return the tools' results on
|
|
|
393 |
os.path.join(CONTRACTS_PATH, filename),
|
394 |
earliest_block,
|
395 |
)
|
396 |
+
for address, (filename, earliest_block) in mech_info.items()
|
397 |
}
|
398 |
|
399 |
event_to_contents = {}
|
|
|
527 |
# update the old information
|
528 |
print("Updating accuracy information")
|
529 |
tools_to_update = list(acc_info["tool"].values)
|
530 |
+
print("tools to update")
|
531 |
+
print(tools_to_update)
|
532 |
existing_tools = list(tools_acc["tool"].values)
|
533 |
for tool in tools_to_update:
|
534 |
if tool in existing_tools:
|
scripts/update_tools_accuracy.py
CHANGED
@@ -20,9 +20,11 @@ def compute_tools_accuracy():
|
|
20 |
if os.path.exists(DATA_DIR / ACCURACY_FILENAME):
|
21 |
acc_data = pd.read_csv(DATA_DIR / ACCURACY_FILENAME)
|
22 |
acc_data = update_tools_accuracy(acc_data, tools, INC_TOOLS)
|
|
|
23 |
# save acc_data into a CSV file
|
24 |
print("Saving into a csv file")
|
25 |
acc_data.to_csv(DATA_DIR / ACCURACY_FILENAME, index=False)
|
|
|
26 |
|
27 |
# save the data into IPFS
|
28 |
client = ipfshttpclient.connect(IPFS_SERVER)
|
|
|
20 |
if os.path.exists(DATA_DIR / ACCURACY_FILENAME):
|
21 |
acc_data = pd.read_csv(DATA_DIR / ACCURACY_FILENAME)
|
22 |
acc_data = update_tools_accuracy(acc_data, tools, INC_TOOLS)
|
23 |
+
|
24 |
# save acc_data into a CSV file
|
25 |
print("Saving into a csv file")
|
26 |
acc_data.to_csv(DATA_DIR / ACCURACY_FILENAME, index=False)
|
27 |
+
print(acc_data.head())
|
28 |
|
29 |
# save the data into IPFS
|
30 |
client = ipfshttpclient.connect(IPFS_SERVER)
|
scripts/utils.py
CHANGED
@@ -3,7 +3,7 @@ import json
|
|
3 |
import os
|
4 |
import time
|
5 |
from tqdm import tqdm
|
6 |
-
from typing import List, Any, Optional
|
7 |
import pandas as pd
|
8 |
import gc
|
9 |
import re
|
@@ -27,6 +27,9 @@ HTTP = "http://"
|
|
27 |
HTTPS = HTTP[:4] + "s" + HTTP[4:]
|
28 |
IPFS_ADDRESS = f"{HTTPS}gateway.autonolas.tech/ipfs/"
|
29 |
FORMAT_UPDATE_BLOCK_NUMBER = 30411638
|
|
|
|
|
|
|
30 |
|
31 |
INC_TOOLS = [
|
32 |
"prediction-online",
|
@@ -330,3 +333,18 @@ def current_answer(text: str, fpmms: pd.DataFrame) -> Optional[str]:
|
|
330 |
if row.shape[0] == 0:
|
331 |
return None
|
332 |
return row["currentAnswer"].values[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import os
|
4 |
import time
|
5 |
from tqdm import tqdm
|
6 |
+
from typing import List, Any, Optional, Union
|
7 |
import pandas as pd
|
8 |
import gc
|
9 |
import re
|
|
|
27 |
HTTPS = HTTP[:4] + "s" + HTTP[4:]
|
28 |
IPFS_ADDRESS = f"{HTTPS}gateway.autonolas.tech/ipfs/"
|
29 |
FORMAT_UPDATE_BLOCK_NUMBER = 30411638
|
30 |
+
INVALID_ANSWER_HEX = (
|
31 |
+
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
32 |
+
)
|
33 |
|
34 |
INC_TOOLS = [
|
35 |
"prediction-online",
|
|
|
333 |
if row.shape[0] == 0:
|
334 |
return None
|
335 |
return row["currentAnswer"].values[0]
|
336 |
+
|
337 |
+
|
338 |
+
def convert_hex_to_int(x: Union[str, float]) -> Union[int, float]:
|
339 |
+
"""Convert hex to int"""
|
340 |
+
if isinstance(x, float):
|
341 |
+
return np.nan
|
342 |
+
if isinstance(x, str):
|
343 |
+
if x == INVALID_ANSWER_HEX:
|
344 |
+
return -1
|
345 |
+
return int(x, 16)
|
346 |
+
|
347 |
+
|
348 |
+
def wei_to_unit(wei: int) -> float:
|
349 |
+
"""Converts wei to currency unit."""
|
350 |
+
return wei / 10**18
|