From 5b7102369b5fa50fde859cf47952e25439683a7f Mon Sep 17 00:00:00 2001 From: Manank Patni Date: Thu, 6 Oct 2022 07:39:27 +0530 Subject: [PATCH] Remove dependency from Dipdup, Fix Execute Data State Error Fixes #342 Signed-off-by: Manank Patni --- registrydao/constants.py | 1 - registrydao/handlers/on_flush.py | 23 +++++++++++------------ registrydao/handlers/on_origination.py | 21 +++++++++++---------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/registrydao/constants.py b/registrydao/constants.py index afabc5f..1d6e5c3 100644 --- a/registrydao/constants.py +++ b/registrydao/constants.py @@ -1,4 +1,3 @@ -DIPDUP_METADATA_API = 'https://metadata.dipdup.net/api/rest' NETWORK_MAP = { "mainnet": "mainnet", "ghostnet": "ghostnet" diff --git a/registrydao/handlers/on_flush.py b/registrydao/handlers/on_flush.py index e763e14..f65730d 100644 --- a/registrydao/handlers/on_flush.py +++ b/registrydao/handlers/on_flush.py @@ -27,25 +27,24 @@ async def on_flush( dao.guardian = flush.data.storage["guardian"] await dao.save() - created_status = await models.ProposalStatus.get(description='created') executed_status = await models.ProposalStatus.get(description='executed') rejected_and_flushed_status = await models.ProposalStatus.get(description='rejected_and_flushed') dropped_status = await models.ProposalStatus.get(description='dropped') - created_proposals = await models.Proposal.filter(dao=dao, status_updates__status=created_status) - - for i in range(len(created_proposals)): - if created_proposals[i].key not in non_flushed_or_executed_keys: - is_rejected = int(created_proposals[i].downvotes) >= int(created_proposals[i].quorum_threshold) - is_passed = (int(created_proposals[i].upvotes) >= int(created_proposals[i].quorum_threshold)) and not is_rejected - is_dropped = await models.ProposalStatusUpdates.exists(proposal=created_proposals[i], status=dropped_status) - is_executed = await models.ProposalStatusUpdates.exists(proposal=created_proposals[i], status=executed_status) + all_proposals = await models.Proposal.filter(dao=dao) + + for i in range(len(all_proposals)): + if all_proposals[i].key not in non_flushed_or_executed_keys: + is_rejected = int(all_proposals[i].downvotes) >= int(all_proposals[i].quorum_threshold) + is_passed = (int(all_proposals[i].upvotes) >= int(all_proposals[i].quorum_threshold)) and not is_rejected + is_dropped = await models.ProposalStatusUpdates.exists(proposal=all_proposals[i], status=dropped_status) + is_executed = await models.ProposalStatusUpdates.exists(proposal=all_proposals[i], status=executed_status) if is_passed and not is_dropped and not is_executed: - await models.ProposalStatusUpdates.get_or_create(status=executed_status, proposal=created_proposals[i], timestamp=flush.data.timestamp, level=flush.data.level) + await models.ProposalStatusUpdates.get_or_create(status=executed_status, proposal=all_proposals[i], timestamp=flush.data.timestamp, level=flush.data.level) elif is_rejected and not is_dropped and not is_executed: - await models.ProposalStatusUpdates.get_or_create(status=rejected_and_flushed_status, proposal=created_proposals[i], timestamp=flush.data.timestamp, level=flush.data.level) + await models.ProposalStatusUpdates.get_or_create(status=rejected_and_flushed_status, proposal=all_proposals[i], timestamp=flush.data.timestamp, level=flush.data.level) elif not is_dropped and not is_executed: - await models.ProposalStatusUpdates.get_or_create(status=dropped_status, proposal=created_proposals[i], timestamp=flush.data.timestamp, level=flush.data.level) + await models.ProposalStatusUpdates.get_or_create(status=dropped_status, proposal=all_proposals[i], timestamp=flush.data.timestamp, level=flush.data.level) except Exception as e: print("Error in on_flush: " + flush.data.target_address) print(e) \ No newline at end of file diff --git a/registrydao/handlers/on_origination.py b/registrydao/handlers/on_origination.py index 5bbdcce..3ec162a 100644 --- a/registrydao/handlers/on_origination.py +++ b/registrydao/handlers/on_origination.py @@ -1,7 +1,9 @@ +import json + from asyncio import sleep from datetime import datetime from registrydao.utils.ctx import extract_network_from_ctx -from registrydao.constants import DIPDUP_METADATA_API, NETWORK_MAP +from registrydao.constants import NETWORK_MAP from registrydao.utils.http import fetch from dipdup.context import HandlerContext @@ -16,14 +18,14 @@ def find_in_json(key_to_compare: str, key_name: str, data): return i async def wait_and_fetch_metadata(network: str, dao_address: str): - fetched_metadata = await fetch(f'{DIPDUP_METADATA_API}/contract_metadata?contract={dao_address}&network={NETWORK_MAP[network]}') + fetched_metadata_location = await fetch(f'https://api.{NETWORK_MAP[network]}.tzkt.io/v1/contracts/{dao_address}/bigmaps/metadata/keys') + metadata_location_hex = fetched_metadata_location[0]["value"] + metadata_uri = bytes.fromhex(metadata_location_hex).decode('utf-8') + metadata_contract = metadata_uri.split('/')[2] + fetched_metadata = await fetch(f'https://api.{NETWORK_MAP[network]}.tzkt.io/v1/contracts/{metadata_contract}/bigmaps/metadata/keys/metadataKey') + metadata = bytes.fromhex(fetched_metadata["value"]).decode('utf-8') - while fetched_metadata == None: - print(f'Metadata not yet indexed for DAO {dao_address}') - await sleep(5) - fetched_metadata = await fetch(f'{DIPDUP_METADATA_API}/contract_metadata?contract={dao_address}&network={NETWORK_MAP[network]}') - - return fetched_metadata + return json.loads(metadata) async def on_origination( @@ -48,8 +50,7 @@ async def on_origination( if('symbol' not in fetched_token_metadata): return - fetched_metadata_arr = await wait_and_fetch_metadata(network, dao_address) - fetched_metadata = fetched_metadata_arr["contract_metadata"][0]["metadata"] + fetched_metadata = await wait_and_fetch_metadata(network, dao_address) dao_type = fetched_metadata['template'] if 'discourse' in fetched_metadata and fetched_metadata['discourse']: