|
30 | 30 | from typing import Any, ClassVar, Dict
|
31 | 31 |
|
32 | 32 | import requests
|
| 33 | +from dotenv import dotenv_values |
33 | 34 | from gql import Client, gql
|
34 | 35 | from gql.transport.requests import RequestsHTTPTransport
|
35 | 36 | from tqdm import tqdm
|
|
38 | 39 |
|
39 | 40 | SCRIPT_PATH = Path(__file__).resolve().parent
|
40 | 41 | STORE_PATH = Path(SCRIPT_PATH, "..", ".trader_runner")
|
41 |
| -MECH_EVENTS_JSON_PATH = Path(STORE_PATH, "mech_events.json") |
| 42 | +ENV_FILENAME = ".env" |
| 43 | +DOTENV_PATH = STORE_PATH / ENV_FILENAME |
| 44 | +MECH_EVENTS_JSON_PATH = STORE_PATH / "mech_events.json" |
42 | 45 | HTTP = "http://"
|
43 | 46 | HTTPS = HTTP[:4] + "s" + HTTP[4:]
|
44 | 47 | CID_PREFIX = "f01701220"
|
|
47 | 50 | DEFAULT_MECH_FEE = 10000000000000000
|
48 | 51 | DEFAULT_FROM_TIMESTAMP = 0
|
49 | 52 | DEFAULT_TO_TIMESTAMP = 2147483647
|
50 |
| -MECH_SUBGRAPH_URL = "https://api.studio.thegraph.com/query/57238/mech/0.0.2" |
| 53 | +MECH_SUBGRAPH_URL_TEMPLATE = Template( |
| 54 | + "https://gateway.thegraph.com/api/${SUBGRAPH_API_KEY}/subgraphs/id/4YGoX3iXUni1NBhWJS5xyKcntrAzssfytJK7PQxxQk5g" |
| 55 | +) |
51 | 56 | SUBGRAPH_HEADERS = {
|
52 | 57 | "Accept": "application/json, multipart/mixed",
|
53 | 58 | "Content-Type": "application/json",
|
|
74 | 79 | """
|
75 | 80 | )
|
76 | 81 |
|
| 82 | + |
77 | 83 | @dataclass
|
78 | 84 | class MechBaseEvent: # pylint: disable=too-many-instance-attributes
|
79 | 85 | """Base class for mech's on-chain event representation."""
|
@@ -159,7 +165,7 @@ def _read_mech_events_data_from_file() -> Dict[str, Any]:
|
159 | 165 | if mech_events_data.get("db_version", 0) < MECH_EVENTS_DB_VERSION:
|
160 | 166 | current_time = time.strftime("%Y-%m-%d_%H-%M-%S")
|
161 | 167 | old_db_filename = f"mech_events.{current_time}.old.json"
|
162 |
| - os.rename(MECH_EVENTS_JSON_PATH, Path(STORE_PATH, old_db_filename)) |
| 168 | + os.rename(MECH_EVENTS_JSON_PATH, STORE_PATH / old_db_filename) |
163 | 169 | mech_events_data = {}
|
164 | 170 | mech_events_data["db_version"] = MECH_EVENTS_DB_VERSION
|
165 | 171 | except FileNotFoundError:
|
@@ -190,17 +196,26 @@ def _write_mech_events_data_to_file(
|
190 | 196 | last_write_time = now
|
191 | 197 |
|
192 | 198 |
|
| 199 | +def get_mech_subgraph_url() -> str: |
| 200 | + """Get the mech subgraph's URL.""" |
| 201 | + env_file_vars = dotenv_values(DOTENV_PATH) |
| 202 | + return MECH_SUBGRAPH_URL_TEMPLATE.substitute(env_file_vars) |
| 203 | + |
| 204 | + |
193 | 205 | def _query_mech_events_subgraph(
|
194 | 206 | sender: str, event_cls: type[MechBaseEvent]
|
195 | 207 | ) -> dict[str, Any]:
|
196 | 208 | """Query the subgraph."""
|
197 | 209 |
|
198 |
| - transport = RequestsHTTPTransport(url=MECH_SUBGRAPH_URL) |
| 210 | + mech_subgraph_url = get_mech_subgraph_url() |
| 211 | + transport = RequestsHTTPTransport(mech_subgraph_url) |
199 | 212 | client = Client(transport=transport, fetch_schema_from_transport=True)
|
200 | 213 |
|
201 | 214 | subgraph_event_set_name = f"{event_cls.subgraph_event_name}s"
|
202 | 215 | all_results: dict[str, Any] = {"data": {subgraph_event_set_name: []}}
|
203 |
| - query = MECH_EVENTS_SUBGRAPH_QUERY_TEMPLATE.safe_substitute(subgraph_event_set_name=subgraph_event_set_name) |
| 216 | + query = MECH_EVENTS_SUBGRAPH_QUERY_TEMPLATE.safe_substitute( |
| 217 | + subgraph_event_set_name=subgraph_event_set_name |
| 218 | + ) |
204 | 219 | id_gt = ""
|
205 | 220 | while True:
|
206 | 221 | variables = {
|
|
0 commit comments