Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit f3a0872

Browse files
committed
fix: update the mech subgraph's url
1 parent c59648f commit f3a0872

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

scripts/mech_events.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from typing import Any, ClassVar, Dict
3131

3232
import requests
33+
from dotenv import dotenv_values
3334
from gql import Client, gql
3435
from gql.transport.requests import RequestsHTTPTransport
3536
from tqdm import tqdm
@@ -38,7 +39,9 @@
3839

3940
SCRIPT_PATH = Path(__file__).resolve().parent
4041
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"
4245
HTTP = "http://"
4346
HTTPS = HTTP[:4] + "s" + HTTP[4:]
4447
CID_PREFIX = "f01701220"
@@ -47,7 +50,9 @@
4750
DEFAULT_MECH_FEE = 10000000000000000
4851
DEFAULT_FROM_TIMESTAMP = 0
4952
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+
)
5156
SUBGRAPH_HEADERS = {
5257
"Accept": "application/json, multipart/mixed",
5358
"Content-Type": "application/json",
@@ -74,6 +79,7 @@
7479
"""
7580
)
7681

82+
7783
@dataclass
7884
class MechBaseEvent: # pylint: disable=too-many-instance-attributes
7985
"""Base class for mech's on-chain event representation."""
@@ -159,7 +165,7 @@ def _read_mech_events_data_from_file() -> Dict[str, Any]:
159165
if mech_events_data.get("db_version", 0) < MECH_EVENTS_DB_VERSION:
160166
current_time = time.strftime("%Y-%m-%d_%H-%M-%S")
161167
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)
163169
mech_events_data = {}
164170
mech_events_data["db_version"] = MECH_EVENTS_DB_VERSION
165171
except FileNotFoundError:
@@ -190,17 +196,26 @@ def _write_mech_events_data_to_file(
190196
last_write_time = now
191197

192198

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+
193205
def _query_mech_events_subgraph(
194206
sender: str, event_cls: type[MechBaseEvent]
195207
) -> dict[str, Any]:
196208
"""Query the subgraph."""
197209

198-
transport = RequestsHTTPTransport(url=MECH_SUBGRAPH_URL)
210+
mech_subgraph_url = get_mech_subgraph_url()
211+
transport = RequestsHTTPTransport(mech_subgraph_url)
199212
client = Client(transport=transport, fetch_schema_from_transport=True)
200213

201214
subgraph_event_set_name = f"{event_cls.subgraph_event_name}s"
202215
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+
)
204219
id_gt = ""
205220
while True:
206221
variables = {

0 commit comments

Comments
 (0)