|
30 | 30 | from typing import Any, ClassVar, Dict |
31 | 31 |
|
32 | 32 | import requests |
| 33 | +from gql import Client, gql |
| 34 | +from gql.transport.requests import RequestsHTTPTransport |
33 | 35 | from tqdm import tqdm |
34 | 36 | from web3.datastructures import AttributeDict |
35 | 37 |
|
|
51 | 53 | "Content-Type": "application/json", |
52 | 54 | } |
53 | 55 | QUERY_BATCH_SIZE = 1000 |
54 | | -MECH_EVENTS_SUBGRAPH_QUERY = Template( |
| 56 | +MECH_EVENTS_SUBGRAPH_QUERY_TEMPLATE = Template( |
55 | 57 | """ |
56 | | - query { |
| 58 | + query mech_events_subgraph_query($sender: Bytes, $id_gt: Bytes, $first: Int) { |
57 | 59 | ${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 |
63 | 62 | orderBy: id |
64 | 63 | orderDirection: asc |
65 | 64 | ) { |
|
75 | 74 | """ |
76 | 75 | ) |
77 | 76 |
|
78 | | - |
79 | 77 | @dataclass |
80 | 78 | class MechBaseEvent: # pylint: disable=too-many-instance-attributes |
81 | 79 | """Base class for mech's on-chain event representation.""" |
@@ -197,24 +195,21 @@ def _query_mech_events_subgraph( |
197 | 195 | ) -> dict[str, Any]: |
198 | 196 | """Query the subgraph.""" |
199 | 197 |
|
| 198 | + transport = RequestsHTTPTransport(url=MECH_SUBGRAPH_URL) |
| 199 | + client = Client(transport=transport, fetch_schema_from_transport=True) |
| 200 | + |
200 | 201 | subgraph_event_set_name = f"{event_cls.subgraph_event_name}s" |
201 | 202 | 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) |
202 | 204 | id_gt = "" |
203 | 205 | 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, []) |
218 | 213 |
|
219 | 214 | if not events: |
220 | 215 | break |
@@ -279,7 +274,8 @@ def _update_mech_events_db( |
279 | 274 | "You may attempt to rerun this script to retry synchronizing the database." |
280 | 275 | ) |
281 | 276 | input("Press Enter to continue...") |
282 | | - except Exception: # pylint: disable=broad-except |
| 277 | + except Exception as e: # pylint: disable=broad-except |
| 278 | + print(e) |
283 | 279 | print( |
284 | 280 | "WARNING: An error occurred while updating the local Mech events database. " |
285 | 281 | "Therefore, the Mech calls and costs might not be reflected accurately. " |
|
0 commit comments