Skip to content

Commit

Permalink
Merge pull request #16 from dOrgTech/execute-patch
Browse files Browse the repository at this point in the history
Remove dependency from Dipdup, Fix Execute Data State Error
  • Loading branch information
EightRice authored Oct 10, 2022
2 parents 762476c + 5b71023 commit 0fafa65
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
1 change: 0 additions & 1 deletion registrydao/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
DIPDUP_METADATA_API = 'https://metadata.dipdup.net/api/rest'
NETWORK_MAP = {
"mainnet": "mainnet",
"ghostnet": "ghostnet"
Expand Down
23 changes: 11 additions & 12 deletions registrydao/handlers/on_flush.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
21 changes: 11 additions & 10 deletions registrydao/handlers/on_origination.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand All @@ -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']:
Expand Down

0 comments on commit 0fafa65

Please sign in to comment.