diff --git a/backend/moonstream/actions.py b/backend/moonstream/actions.py index 91b90ad7..9b8afeb3 100644 --- a/backend/moonstream/actions.py +++ b/backend/moonstream/actions.py @@ -379,15 +379,13 @@ def dashboards_abi_validation( return True -def validate_abi_string(abi: str) -> None: +def validate_abi_string(abi: Any) -> None: """ Transform string to json and run validation """ try: - validate_abi(json.loads(abi)) - except json.JSONDecodeError: - raise MoonstreamHTTPException(status_code=400, detail="Malformed abi body.") + validate_abi(abi) except ValueError as e: raise MoonstreamHTTPException(status_code=400, detail=e) except: diff --git a/backend/moonstream/routes/dashboards.py b/backend/moonstream/routes/dashboards.py index a60d0592..f2c40b21 100644 --- a/backend/moonstream/routes/dashboards.py +++ b/backend/moonstream/routes/dashboards.py @@ -406,11 +406,12 @@ async def get_dashboard_data_links_handler( for subscription in dashboard_subscriptions: + hash = subscription.resource_data["abi_hash"] available_timescales = [timescale.value for timescale in data.TimeScale] stats[subscription.id] = {} for timescale in available_timescales: try: - result_key = f'contracts_data/{subscription.resource_data["address"]}/v1/{timescale}.json' + result_key = f'contracts_data/{subscription.resource_data["address"]}/{hash}/v1/{timescale}.json' stats_presigned_url = s3_client.generate_presigned_url( "get_object", Params={"Bucket": SMARTCONTRACTS_ABI_BUCKET, "Key": result_key}, diff --git a/backend/moonstream/routes/subscriptions.py b/backend/moonstream/routes/subscriptions.py index 5acff8fb..2b878af9 100644 --- a/backend/moonstream/routes/subscriptions.py +++ b/backend/moonstream/routes/subscriptions.py @@ -1,6 +1,7 @@ """ The Moonstream subscriptions HTTP API """ +import hashlib import logging import json from typing import List, Optional, Dict, Any @@ -13,7 +14,7 @@ from fastapi import APIRouter, Depends, Request, Form from web3 import Web3 from ..actions import ( - validate_abi_string, + validate_abi_json, upload_abi_to_s3, ) from ..admin import subscription_types @@ -112,10 +113,21 @@ async def add_subscription_handler( if abi: - validate_abi_string(abi=abi) + try: + json_abi = json.loads(abi) + except json.JSONDecodeError: + raise MoonstreamHTTPException(status_code=400, detail="Malformed abi body.") + + validate_abi_json(json_abi) update_resource = upload_abi_to_s3(resource=resource, abi=abi, update={}) + abi_string = json.dumps(json_abi, sort_keys=True, indent=2) + + hash = hashlib.md5(abi_string.encode("utf-8")).hexdigest() + + update_resource["abi_hash"] = hash + try: updated_resource: BugoutResource = bc.update_resource( token=token, @@ -241,7 +253,16 @@ async def update_subscriptions_handler( if abi: - validate_abi_string(abi=abi) + try: + json_abi = json.loads(abi) + except json.JSONDecodeError: + raise MoonstreamHTTPException(status_code=400, detail="Malformed abi body.") + + validate_abi_json(json_abi) + + abi_string = json.dumps(json_abi, sort_keys=True, indent=2) + + hash = hashlib.md5(abi_string.encode("utf-8")).hexdigest() try: subscription_resource: BugoutResource = bc.get_resource( @@ -264,6 +285,8 @@ async def update_subscriptions_handler( resource=subscription_resource, abi=abi, update=update ) + update["abi_hash"] = hash + try: resource: BugoutResource = bc.update_resource( token=token, diff --git a/crawlers/mooncrawl/mooncrawl/stats_worker/dashboards_stats_worker.py b/crawlers/mooncrawl/mooncrawl/stats_worker/dashboards_stats_worker.py index d5e45eb9..6f64160b 100644 --- a/crawlers/mooncrawl/mooncrawl/stats_worker/dashboards_stats_worker.py +++ b/crawlers/mooncrawl/mooncrawl/stats_worker/dashboards_stats_worker.py @@ -304,7 +304,6 @@ def days_stats(type, abi): def crawlers_start(db_session): - token = os.getenv("MOONSTREAM_ADMIN_ACCESS_TOKEN") # read all subscriptions required_subscriptions: BugoutResources = bc.list_resources(