From 52e366273373cd30fd1b54f41a557c206e2e5fc2 Mon Sep 17 00:00:00 2001 From: jmoreira-valory Date: Wed, 27 Nov 2024 14:09:30 +0100 Subject: [PATCH 1/3] fix: use GQL for mech events --- scripts/mech_events.py | 47 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/scripts/mech_events.py b/scripts/mech_events.py index cbfac2d1..868b41ef 100644 --- a/scripts/mech_events.py +++ b/scripts/mech_events.py @@ -30,6 +30,8 @@ from typing import Any, ClassVar, Dict import requests +from gql import Client, gql +from gql.transport.requests import RequestsHTTPTransport from tqdm import tqdm from web3.datastructures import AttributeDict @@ -51,15 +53,12 @@ "Content-Type": "application/json", } QUERY_BATCH_SIZE = 1000 -MECH_EVENTS_SUBGRAPH_QUERY = Template( +MECH_EVENTS_SUBGRAPH_QUERY_TEMPLATE = Template( """ - query { + query mech_events_subgraph_query($sender: Bytes, $id_gt: Bytes, $first: Int) { ${subgraph_event_set_name}( - where: { - sender: "${sender}" - id_gt: "${id_gt}" - } - first: ${first} + where: {sender: $sender, id_gt: $id_gt} + first: $first orderBy: id orderDirection: asc ) { @@ -75,7 +74,6 @@ """ ) - @dataclass class MechBaseEvent: # pylint: disable=too-many-instance-attributes """Base class for mech's on-chain event representation.""" @@ -197,24 +195,22 @@ def _query_mech_events_subgraph( ) -> dict[str, Any]: """Query the subgraph.""" + transport = RequestsHTTPTransport(url=MECH_SUBGRAPH_URL) + client = Client(transport=transport, fetch_schema_from_transport=True) + subgraph_event_set_name = f"{event_cls.subgraph_event_name}s" all_results: dict[str, Any] = {"data": {subgraph_event_set_name: []}} + query = MECH_EVENTS_SUBGRAPH_QUERY_TEMPLATE.safe_substitute(subgraph_event_set_name=subgraph_event_set_name) id_gt = "" while True: - query = MECH_EVENTS_SUBGRAPH_QUERY.substitute( - subgraph_event_set_name=subgraph_event_set_name, - sender=sender, - id_gt=id_gt, - first=QUERY_BATCH_SIZE, - ) - response = requests.post( - MECH_SUBGRAPH_URL, - headers=SUBGRAPH_HEADERS, - json={"query": query}, - timeout=300, - ) - result_json = response.json() - events = result_json.get("data", {}).get(subgraph_event_set_name, []) + print("iteration") + variables = { + "sender": sender, + "id_gt": id_gt, + "first": QUERY_BATCH_SIZE, + } + response = client.execute(gql(query), variable_values=variables) + events = response.get(subgraph_event_set_name, []) if not events: break @@ -279,7 +275,12 @@ def _update_mech_events_db( "You may attempt to rerun this script to retry synchronizing the database." ) input("Press Enter to continue...") - except Exception: # pylint: disable=broad-except + except Exception as e: # pylint: disable=broad-except + print(e) + import traceback + + traceback.print_exc() + print( "WARNING: An error occurred while updating the local Mech events database. " "Therefore, the Mech calls and costs might not be reflected accurately. " From 4bf0cc825be2fa258b6cc1a32819751dca9e64c7 Mon Sep 17 00:00:00 2001 From: jmoreira-valory Date: Wed, 27 Nov 2024 14:11:37 +0100 Subject: [PATCH 2/3] fix: exception --- scripts/mech_events.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scripts/mech_events.py b/scripts/mech_events.py index 868b41ef..14a72e6b 100644 --- a/scripts/mech_events.py +++ b/scripts/mech_events.py @@ -277,10 +277,6 @@ def _update_mech_events_db( input("Press Enter to continue...") except Exception as e: # pylint: disable=broad-except print(e) - import traceback - - traceback.print_exc() - print( "WARNING: An error occurred while updating the local Mech events database. " "Therefore, the Mech calls and costs might not be reflected accurately. " From eca8d168c8825a8b5c85be2244e2e0cd93e8cb6f Mon Sep 17 00:00:00 2001 From: jmoreira-valory Date: Wed, 27 Nov 2024 15:54:23 +0100 Subject: [PATCH 3/3] chore: remove print --- scripts/mech_events.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/mech_events.py b/scripts/mech_events.py index 14a72e6b..76cd607b 100644 --- a/scripts/mech_events.py +++ b/scripts/mech_events.py @@ -203,7 +203,6 @@ def _query_mech_events_subgraph( query = MECH_EVENTS_SUBGRAPH_QUERY_TEMPLATE.safe_substitute(subgraph_event_set_name=subgraph_event_set_name) id_gt = "" while True: - print("iteration") variables = { "sender": sender, "id_gt": id_gt,