Skip to content

Instantly share code, notes, and snippets.

@jhodges10
Last active September 2, 2018 20:03
Show Gist options
  • Save jhodges10/a4d729080ecb4ccd58e4e5ae37a6efec to your computer and use it in GitHub Desktop.
Save jhodges10/a4d729080ecb4ccd58e4e5ae37a6efec to your computer and use it in GitHub Desktop.
Generates the final vote tally at the time voting closed
import requests
import pandas as pd
import json
import datetime
import time
import timeit
from bin.DashInfo import DataPreparation
# For calculating vote snapshots
def get_trigger():
trigger_url = "https://insight.dashevo.org/insight-api-dash/gobject/list/trigger"
response = requests.get(trigger_url)
triggers_sorted = {}
triggers = response.json()
# Find highest ranked trigger
if type(triggers) == list and len(triggers) > 1:
df = pd.DataFrame.from_records(triggers)
df.sort_values(by='AbsoluteYesCount', ascending=False, inplace=True)
trigger = df.to_dict(orient="records")[0]
else:
trigger = triggers
# Get trigger object
trigger_gobj_base_url = "https://insight.dashevo.org/insight-api-dash/gobject/get/"
trigger_data_url = trigger_gobj_base_url + trigger['Hash']
response = requests.get(trigger_data_url)
trigger_data = response.json()[0]
trigger_timestamp = trigger_data['CreationTime']
# Deserialize trigger object
deserialize_url = "https://insight.dashevo.org/insight-api-dash/gobject/deserialize/"
serialized_data = trigger_data['DataHex']
url = deserialize_url + serialized_data
response = requests.get(url).json()['result']
# Parse data for hash list
trim_1 = response.split("proposal_hashes")[1]
trim_2 = trim_1.split(",")[0]
trim_3 = trim_2.replace('"', '')
trim_4 = trim_3[1:].replace("|", ',')
hash_list = trim_4.split(',')
return {"creation_time": trigger_timestamp, "funded_gobjects": hash_list}
def get_mn_data():
insight_url = "https://insight.dashevo.org/insight-api-dash/masternodes/list"
response = requests.get(insight_url)
data = response.json()
return data
def get_vote_data(hash_list, sb_timestamp):
votes_url_base = "https://api.dashintel.org/dash_ninja_votes"
master_votes = []
if len(hash_list) > 0:
for count, p_hash in enumerate(hash_list):
count += 1
print("Getting votes on proposal: {}/{}".format(count, len(hash_list)))
votes_url = votes_url_base + "?select=proposal_hash,masternode_vin,vote_value,vote_time&vote_time=lt.{}&proposal_hash=eq.{}".format(sb_timestamp, p_hash)
# print(votes_url)
votes = requests.get(votes_url).json()
for vote in votes:
master_votes.append(vote)
else:
return []
return master_votes
def check_validity(votes, sb_timedelta, masternodes):
# Check that votes were cast by a node that was actually active at that time
for node in masternodes:
if node['activeseconds'] <= sb_timedelta:
del masternodes[node]
else:
continue
# Build new list of nodes that were online
online_at_voting_close_nodes = []
for node in masternodes:
online_at_voting_close_nodes.append(node['vin'])
for vote in votes:
if vote['masternode_vin'] in online_at_voting_close_nodes:
continue
else:
del votes[vote]
return votes
def gen_final_tally(master_votes):
df = pd.DataFrame.from_dict(master_votes, orient='records')
print(df.head())
return df
if __name__ == "__main__":
trigger_data = get_trigger()
print("Got Trigger")
masternode_data = get_mn_data()
print("Got Masternode data")
vote_data = get_vote_data(trigger_data['funded_gobjects'], trigger_data['creation_time'])
print("Got Vote data")
sb_timedelta = int(time.time() - trigger_data['creation_time'])
print("Calculated timedelta")
final_votes = check_validity(vote_data, sb_timedelta, masternode_data)
# Write data just in case something breaks and we have to run it again
DataPreparation.write_json(final_votes, 'final_votes')
if not final_votes:
final_votes = DataPreparation.read_json('final_votes')
final_tally = gen_final_tally(final_votes)
# print(vote_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment