From 5dfcfd634ffbde8e93ed4616afee8a522d73b734 Mon Sep 17 00:00:00 2001 From: kompotkot Date: Mon, 31 Jul 2023 14:19:22 +0000 Subject: [PATCH] Fixes to work with spire entity --- moonstreamapi/moonstreamapi/actions.py | 24 ++++++------ .../generate_entity_subscriptions.py | 5 ++- moonstreamapi/moonstreamapi/data.py | 7 ---- .../moonstreamapi/providers/bugout.py | 12 +++--- .../moonstreamapi/routes/dashboards.py | 21 +++++----- .../moonstreamapi/routes/subscriptions.py | 38 +++++++++++++------ .../moonstreamapi/selectors_storage.py | 4 +- moonstreamapi/mypy.ini | 6 +++ 8 files changed, 66 insertions(+), 51 deletions(-) diff --git a/moonstreamapi/moonstreamapi/actions.py b/moonstreamapi/moonstreamapi/actions.py index 7c908126..16b6279c 100644 --- a/moonstreamapi/moonstreamapi/actions.py +++ b/moonstreamapi/moonstreamapi/actions.py @@ -477,7 +477,7 @@ def get_all_entries_from_search( results: List[BugoutSearchResult] = [] - existing_metods = bc.search( + existing_methods = bc.search( token=token, journal_id=journal_id, query=search_query, @@ -486,11 +486,11 @@ def get_all_entries_from_search( limit=limit, offset=offset, ) - results.extend(existing_metods.results) + results.extend(existing_methods.results) # type: ignore - if len(results) != existing_metods.total_results: - for offset in range(limit, existing_metods.total_results, limit): - existing_metods = bc.search( + if len(results) != existing_methods.total_results: + for offset in range(limit, existing_methods.total_results, limit): + existing_methods = bc.search( token=token, journal_id=journal_id, query=search_query, @@ -499,7 +499,7 @@ def get_all_entries_from_search( limit=limit, offset=offset, ) - results.extend(existing_metods.results) + results.extend(existing_methods.results) # type: ignore return results @@ -641,7 +641,7 @@ def get_entity_subscription_journal_id( token: Union[uuid.UUID, str], user_id: uuid.UUID, create_if_not_exist: bool = False, -) -> Optional[str]: +) -> str: """ Get collection_id (journal_id) from brood resources. If journal not exist and create_if_not_exist is True """ @@ -684,7 +684,7 @@ def generate_journal_for_user( journals: BugoutJournals = bc.list_journals(token=token) available_journals: Dict[str, str] = { - journal.name: journal.id for journal in journals.journals + journal.name: str(journal.id) for journal in journals.journals } subscription_journal_name = f"subscriptions_{user_id}" @@ -693,7 +693,7 @@ def generate_journal_for_user( journal: BugoutJournal = bc.create_journal( token=token, name=subscription_journal_name ) - journal_id = journal.id + journal_id = str(journal.id) else: journal_id = available_journals[subscription_journal_name] except Exception as e: @@ -705,7 +705,7 @@ def generate_journal_for_user( resource_data = { "type": resource_type, "user_id": str(user_id), - "collection_id": str(journal_id), + "collection_id": journal_id, } try: @@ -832,14 +832,14 @@ def get_list_of_support_interfaces( list_of_interfaces.sort() - for interaface in list_of_interfaces: + for interface in list_of_interfaces: calls.append( ( contract.address, FunctionSignature( contract.get_function_by_name("supportsInterface") ) - .encode_data([bytes.fromhex(interaface)]) + .encode_data([bytes.fromhex(interface)]) .hex(), ) ) diff --git a/moonstreamapi/moonstreamapi/admin/migrations/generate_entity_subscriptions.py b/moonstreamapi/moonstreamapi/admin/migrations/generate_entity_subscriptions.py index 11ab5961..a192197e 100644 --- a/moonstreamapi/moonstreamapi/admin/migrations/generate_entity_subscriptions.py +++ b/moonstreamapi/moonstreamapi/admin/migrations/generate_entity_subscriptions.py @@ -68,7 +68,8 @@ def add_entity_subscription( f"Unknown subscription type ID: {subscription_type_id}. " f"Known subscription type IDs: {CANONICAL_SUBSCRIPTION_TYPES.keys()}" ) - elif CANONICAL_SUBSCRIPTION_TYPES[subscription_type_id].blockchain is None: + blockchain = CANONICAL_SUBSCRIPTION_TYPES[subscription_type_id].blockchain + if blockchain is None: raise ValueError( f"Subscription type ID {subscription_type_id} is not a blockchain subscription type." ) @@ -77,7 +78,7 @@ def add_entity_subscription( token=MOONSTREAM_ADMIN_ACCESS_TOKEN, journal_id=journal_id, address=address, - blockchain=CANONICAL_SUBSCRIPTION_TYPES[subscription_type_id].blockchain, + blockchain=blockchain, title=label, required_fields=[ {"type": "subscription"}, diff --git a/moonstreamapi/moonstreamapi/data.py b/moonstreamapi/moonstreamapi/data.py index f58a4cc1..b092e151 100644 --- a/moonstreamapi/moonstreamapi/data.py +++ b/moonstreamapi/moonstreamapi/data.py @@ -57,13 +57,6 @@ class SubscriptionResourceData(BaseModel): updated_at: Optional[datetime] -class CreateSubscriptionRequest(BaseModel): - address: str - color: str - label: str - subscription_type_id: str - - class PingResponse(BaseModel): """ Schema for ping response diff --git a/moonstreamapi/moonstreamapi/providers/bugout.py b/moonstreamapi/moonstreamapi/providers/bugout.py index 18d5c205..f07cb284 100644 --- a/moonstreamapi/moonstreamapi/providers/bugout.py +++ b/moonstreamapi/moonstreamapi/providers/bugout.py @@ -4,10 +4,10 @@ Event providers powered by Bugout journals. import json import logging from datetime import datetime -from typing import Dict, List, Optional, Tuple +from typing import Dict, List, Optional, Tuple, Union from bugout.app import Bugout -from bugout.data import BugoutResource, BugoutSearchResult +from bugout.data import BugoutResource, BugoutSearchResult, BugoutSearchResultAsEntity from bugout.journal import SearchOrder from dateutil.parser import isoparse from dateutil.tz import UTC @@ -155,7 +155,7 @@ class BugoutEventProvider: timeout=self.timeout, order=SearchOrder.DESCENDING, ) - events.extend([self.entry_event(entry) for entry in search_results.results]) + events.extend([self.entry_event(entry) for entry in search_results.results]) # type: ignore offset = search_results.next_offset return stream_boundary, events @@ -192,7 +192,7 @@ class BugoutEventProvider: timeout=self.timeout, order=SearchOrder.DESCENDING, ) - return [self.entry_event(entry) for entry in search_results.results] + return [self.entry_event(entry) for entry in search_results.results] # type: ignore def next_event( self, @@ -233,7 +233,7 @@ class BugoutEventProvider: ) if not search_results.results: return None - return self.entry_event(search_results.results[0]) + return self.entry_event(search_results.results[0]) # type: ignore def previous_event( self, @@ -274,7 +274,7 @@ class BugoutEventProvider: ) if not search_results.results: return None - return self.entry_event(search_results.results[0]) + return self.entry_event(search_results.results[0]) # type: ignore class EthereumTXPoolProvider(BugoutEventProvider): diff --git a/moonstreamapi/moonstreamapi/routes/dashboards.py b/moonstreamapi/moonstreamapi/routes/dashboards.py index 2bb13278..ff66d339 100644 --- a/moonstreamapi/moonstreamapi/routes/dashboards.py +++ b/moonstreamapi/moonstreamapi/routes/dashboards.py @@ -5,9 +5,8 @@ from uuid import UUID import boto3 # type: ignore import requests # type: ignore -from bugout.data import BugoutResource, BugoutResources +from bugout.data import BugoutResource, BugoutResources, BugoutSearchResultAsEntity from bugout.exceptions import BugoutResponseException -from entity.data import EntitiesResponse, EntityResponse # type: ignore from fastapi import APIRouter, Body, Path, Query, Request from .. import actions, data @@ -59,7 +58,7 @@ async def add_dashboard_handler( token=MOONSTREAM_ADMIN_ACCESS_TOKEN, ) - subscriprions_list = bc.search( + subscriptions_list = bc.search( token=token, journal_id=journal_id, required_field=[f"type:subscription"], @@ -69,9 +68,9 @@ async def add_dashboard_handler( # process existing subscriptions with supplied ids - available_subscriptions_ids: Dict[Union[UUID, str], EntityResponse] = { + available_subscriptions_ids: Dict[Union[UUID, str], BugoutSearchResultAsEntity] = { subscription.entity_id: subscription - for subscription in subscriprions_list.entities + for subscription in subscriptions_list.entities } for dashboard_subscription in subscription_settings: @@ -232,7 +231,7 @@ async def update_dashboard_handler( token=MOONSTREAM_ADMIN_ACCESS_TOKEN, ) - subscriprions_list = bc.search( + subscriptions_list = bc.search( token=token, journal_id=journal_id, required_field=[f"type:subscription"], @@ -240,9 +239,9 @@ async def update_dashboard_handler( representation="entity", ) - available_subscriptions_ids: Dict[Union[UUID, str], EntityResponse] = { + available_subscriptions_ids: Dict[Union[UUID, str], BugoutSearchResultAsEntity] = { subscription.entity_id: subscription - for subscription in subscriprions_list.entities + for subscription in subscriptions_list.entities } for dashboard_subscription in subscription_settings: @@ -335,7 +334,7 @@ async def get_dashboard_data_links_handler( token=MOONSTREAM_ADMIN_ACCESS_TOKEN, ) - subscriprions_list = bc.search( + subscriptions_list = bc.search( token=token, journal_id=journal_id, required_field=[f"type:subscription"], @@ -352,9 +351,9 @@ async def get_dashboard_data_links_handler( ] ] - dashboard_subscriptions: Dict[Union[UUID, str], EntitiesResponse] = { + dashboard_subscriptions: Dict[Union[UUID, str], BugoutSearchResultAsEntity] = { subscription.entity_id: subscription - for subscription in subscriprions_list.entities + for subscription in subscriptions_list.entities if str(subscription.entity_id) in subscriptions_ids } diff --git a/moonstreamapi/moonstreamapi/routes/subscriptions.py b/moonstreamapi/moonstreamapi/routes/subscriptions.py index a15ddd2e..734c10c3 100644 --- a/moonstreamapi/moonstreamapi/routes/subscriptions.py +++ b/moonstreamapi/moonstreamapi/routes/subscriptions.py @@ -5,7 +5,7 @@ import hashlib import json import logging from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed -from typing import Any, Dict, List, Optional +from typing import Any, Dict, List, Optional, Union from bugout.data import BugoutSearchResult from bugout.exceptions import BugoutResponseException @@ -136,7 +136,7 @@ async def add_subscription_handler( if description: content["description"] = description - allowed_required_fields = [] + allowed_required_fields: List[Any] = [] if tags: allowed_required_fields = [ item @@ -144,7 +144,7 @@ async def add_subscription_handler( if not any(key in item for key in MOONSTREAM_ENTITIES_RESERVED_TAGS) ] - required_fields = [ + required_fields: List[Dict[str, Union[str, bool, int, List[Any]]]] = [ {"type": "subscription"}, {"subscription_type_id": f"{subscription_type_id}"}, {"color": f"{color}"}, @@ -162,13 +162,14 @@ async def add_subscription_handler( user_id=user.id, create_if_not_exist=True, ) + blockchain = subscription_types.CANONICAL_SUBSCRIPTION_TYPES[ + subscription_type_id + ].blockchain entity = bc.create_entity( token=token, journal_id=journal_id, address=address, - blockchain=subscription_types.CANONICAL_SUBSCRIPTION_TYPES[ - subscription_type_id - ].blockchain, + blockchain=blockchain if blockchain is not None else "", title=label, required_fields=required_fields, secondary_fields=content, @@ -186,10 +187,15 @@ async def add_subscription_handler( internal_error=e, detail="Currently unable to get journal id", ) - + entity_required_fields = ( + entity.required_fields if entity.required_fields is not None else [] + ) + entity_secondary_fields = ( + entity.secondary_fields if entity.secondary_fields is not None else {} + ) normalized_entity_tags = [ f"{key}:{value}" - for tag in entity.required_fields + for tag in entity_required_fields for key, value in tag.items() if key not in MOONSTREAM_ENTITIES_RESERVED_TAGS ] @@ -200,8 +206,8 @@ async def add_subscription_handler( address=address, color=color, label=label, - abi=entity.secondary_fields.get("abi"), - description=entity.secondary_fields.get("description"), + abi=entity_secondary_fields.get("abi"), + description=entity_secondary_fields.get("description"), tags=normalized_entity_tags, subscription_type_id=subscription_type_id, updated_at=entity.updated_at, @@ -252,6 +258,7 @@ async def delete_subscription_handler( color = None label = None abi = None + description = None if tags is not None: for tag in tags: @@ -266,6 +273,13 @@ async def delete_subscription_handler( if deleted_entity.secondary_fields is not None: abi = deleted_entity.secondary_fields.get("abi") + description = deleted_entity.secondary_fields.get("description") + + deleted_entity_required_fields = ( + deleted_entity.required_fields + if deleted_entity.required_fields is not None + else [] + ) return data.SubscriptionResourceData( id=str(deleted_entity.id), @@ -274,8 +288,8 @@ async def delete_subscription_handler( color=color, label=label, abi=abi, - description=deleted_entity.secondary_fields.get("description"), - tags=deleted_entity.required_fields, + description=description, + tags=deleted_entity_required_fields, subscription_type_id=subscription_type_id, updated_at=deleted_entity.updated_at, created_at=deleted_entity.created_at, diff --git a/moonstreamapi/moonstreamapi/selectors_storage.py b/moonstreamapi/moonstreamapi/selectors_storage.py index a4da64e3..9c5a0c82 100644 --- a/moonstreamapi/moonstreamapi/selectors_storage.py +++ b/moonstreamapi/moonstreamapi/selectors_storage.py @@ -1,4 +1,6 @@ -selectors = { +from typing import Any, Dict + +selectors: Dict[str, Any] = { "274c7b3c": { "name": "ERC20PresetMinterPauser", "selector": "274c7b3c", diff --git a/moonstreamapi/mypy.ini b/moonstreamapi/mypy.ini index 47838c47..7e4bc635 100644 --- a/moonstreamapi/mypy.ini +++ b/moonstreamapi/mypy.ini @@ -8,3 +8,9 @@ ignore_missing_imports = True [mypy-pyevmasm.*] ignore_missing_imports = True + +[mypy-requests.*] +ignore_missing_imports = True + +[mypy-dateutil.*] +ignore_missing_imports = True