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

Commit c44f726

Browse files
Merge pull request #394 from valory-xyz/fix/use_gql_for_mech_events
Use GQL for mech events
2 parents e0dd807 + eca8d16 commit c44f726

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

scripts/mech_events.py

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

3232
import requests
33+
from gql import Client, gql
34+
from gql.transport.requests import RequestsHTTPTransport
3335
from tqdm import tqdm
3436
from web3.datastructures import AttributeDict
3537

@@ -51,15 +53,12 @@
5153
"Content-Type": "application/json",
5254
}
5355
QUERY_BATCH_SIZE = 1000
54-
MECH_EVENTS_SUBGRAPH_QUERY = Template(
56+
MECH_EVENTS_SUBGRAPH_QUERY_TEMPLATE = Template(
5557
"""
56-
query {
58+
query mech_events_subgraph_query($sender: Bytes, $id_gt: Bytes, $first: Int) {
5759
${subgraph_event_set_name}(
58-
where: {
59-
sender: "${sender}"
60-
id_gt: "${id_gt}"
61-
}
62-
first: ${first}
60+
where: {sender: $sender, id_gt: $id_gt}
61+
first: $first
6362
orderBy: id
6463
orderDirection: asc
6564
) {
@@ -75,7 +74,6 @@
7574
"""
7675
)
7776

78-
7977
@dataclass
8078
class MechBaseEvent: # pylint: disable=too-many-instance-attributes
8179
"""Base class for mech's on-chain event representation."""
@@ -197,24 +195,21 @@ def _query_mech_events_subgraph(
197195
) -> dict[str, Any]:
198196
"""Query the subgraph."""
199197

198+
transport = RequestsHTTPTransport(url=MECH_SUBGRAPH_URL)
199+
client = Client(transport=transport, fetch_schema_from_transport=True)
200+
200201
subgraph_event_set_name = f"{event_cls.subgraph_event_name}s"
201202
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)
202204
id_gt = ""
203205
while True:
204-
query = MECH_EVENTS_SUBGRAPH_QUERY.substitute(
205-
subgraph_event_set_name=subgraph_event_set_name,
206-
sender=sender,
207-
id_gt=id_gt,
208-
first=QUERY_BATCH_SIZE,
209-
)
210-
response = requests.post(
211-
MECH_SUBGRAPH_URL,
212-
headers=SUBGRAPH_HEADERS,
213-
json={"query": query},
214-
timeout=300,
215-
)
216-
result_json = response.json()
217-
events = result_json.get("data", {}).get(subgraph_event_set_name, [])
206+
variables = {
207+
"sender": sender,
208+
"id_gt": id_gt,
209+
"first": QUERY_BATCH_SIZE,
210+
}
211+
response = client.execute(gql(query), variable_values=variables)
212+
events = response.get(subgraph_event_set_name, [])
218213

219214
if not events:
220215
break
@@ -279,7 +274,8 @@ def _update_mech_events_db(
279274
"You may attempt to rerun this script to retry synchronizing the database."
280275
)
281276
input("Press Enter to continue...")
282-
except Exception: # pylint: disable=broad-except
277+
except Exception as e: # pylint: disable=broad-except
278+
print(e)
283279
print(
284280
"WARNING: An error occurred while updating the local Mech events database. "
285281
"Therefore, the Mech calls and costs might not be reflected accurately. "

0 commit comments

Comments
 (0)