Created
June 1, 2023 19:32
-
-
Save trevorbernard/928be21e8e1d9464c3a9b2453d9fd886 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cloned from https://gist.github.com/jrwashburn/a46f67dd49500842b13638af85c35a63 | |
# Modifed for MIP4 voting | |
from gql import gql, Client | |
from gql.transport.aiohttp import AIOHTTPTransport | |
# Select your transport with a defined url endpoint | |
transport = AIOHTTPTransport(url="https://graphql.minaexplorer.com") | |
# Create a GraphQL client using the defined transport | |
client = Client(transport=transport, fetch_schema_from_transport=True) | |
# Get all memos that fit the MIP criteria (it appears allowing for some variation e.g. MIP4 mip4 no MIP4 NO MIP4) | |
query = gql(""" | |
query MIP4 { | |
transactions(limit: 10000, query: {canonical: true, OR: [{memo: "E4YVPwLUR2LrP9tSSi3fjw1svcZys1gJHrGvRefwVTCMbP2NQRqdW"}, {memo: "E4YVe5wRALCJJ2dGEwRMyH7Z8y1QxzM76M8rXivFo5XbeBJdKryV6"}, {memo: "E4YbUmaZjNgLgezBD3JzyGKuCn4iugZ5EcXT1JuNTudm5tT4MHvKz"}, {memo: "E4YbUmaZZqAoUdTZYvZkSmLjHfccTMbb5RnTQHixwRWq2YqLdLZyE"}], dateTime_gte: "2023-05-20T06:00:00Z", dateTime_lte: "2023-05-28T06:00:00Z"}, sortBy: NONCE_DESC) { | |
memo | |
canonical | |
from | |
to | |
hash | |
blockHeight | |
dateTime | |
} | |
} | |
""") | |
result = client.execute(query) | |
votes = result["transactions"] | |
print("All transactions in voting proposal: " + str(len(votes))) | |
collection = [] | |
# Loop through all votes to check if they meet the criteria | |
for vote in votes: | |
# This must be a transaction to yourself | |
if vote["from"] == vote["to"]: | |
if vote["memo"] == "E4YVPwLUR2LrP9tSSi3fjw1svcZys1gJHrGvRefwVTCMbP2NQRqdW" or vote["memo"] == "E4YVe5wRALCJJ2dGEwRMyH7Z8y1QxzM76M8rXivFo5XbeBJdKryV6": | |
collection.append( | |
[vote["blockHeight"], vote["dateTime"], vote["from"], "MIP4"]) | |
elif vote["memo"] == "E4YbUmaZjNgLgezBD3JzyGKuCn4iugZ5EcXT1JuNTudm5tT4MHvKz" or vote["memo"] == "E4YbUmaZZqAoUdTZYvZkSmLjHfccTMbb5RnTQHixwRWq2YqLdLZyE": | |
collection.append([ | |
vote["blockHeight"], vote["dateTime"], vote["from"], "noMIP4" | |
]) | |
# else: | |
# print(f"Non valid vote: {vote}") | |
print("All valid transactions in voting proposal: " + str(len(collection))) | |
latest_collection = {} | |
for k in collection: | |
# Just take the first instance, as we are sorting by nonce we must get the latest valid vote | |
if k[2] not in latest_collection: | |
latest_collection[k[2]] = k | |
print("All transactions no duplicates: " + str(len(latest_collection))) | |
votes = {} | |
for l in latest_collection.values(): | |
# Get the stake of the account but only count if same as delegated account | |
query2 = gql(""" | |
query nextLedgerStakes {{ | |
nextstakes(query: {{public_key: "{publicKey}"}}) {{ | |
balance | |
delegate | |
public_key | |
nextDelegationTotals {{ | |
countDelegates | |
totalDelegated | |
}} | |
}} | |
}} | |
""".format(publicKey=l[2])) | |
result2 = client.execute(query2) | |
balances = result2["nextstakes"] | |
if not balances: | |
# Should not be empty but being defensive | |
continue | |
# initializing delegate and delegator if not in votes | |
if l[2] not in votes: | |
votes[l[2]] = [0, "NOVOTE", 0] | |
if balances[0]["delegate"] not in votes: | |
votes[balances[0]["delegate"]] = [0, "NOVOTE", 0] | |
votes[l[2]][0] += balances[0]["nextDelegationTotals"]["totalDelegated"] if balances[0]["nextDelegationTotals"]["totalDelegated"] is not None else 0 | |
votes[l[2]][1] = l[3] | |
votes[l[2]][2] = balances[0]["nextDelegationTotals"]["totalDelegated"] if balances[0]["nextDelegationTotals"]["totalDelegated"] is not None else 0 | |
if balances[0]["delegate"] != l[2]: | |
votes[l[2]][0] += balances[0]["balance"] | |
votes[balances[0]["delegate"]][0] -= balances[0]["balance"] | |
# Counters | |
mip4 = 0 | |
mip4_stake = 0 | |
nomip4 = 0 | |
nomip4_stake = 0 | |
for vote in list(votes.values()): | |
if vote[1] == "MIP4": | |
mip4 += 1 | |
mip4_stake += vote[0] | |
elif vote[1] == "noMIP4": | |
nomip4 += 1 | |
nomip4_stake += vote[0] | |
print(f"Total votes: {mip4 + nomip4}") | |
print(f"In favour: {mip4}") | |
print(f"Against: {nomip4}") | |
print(f"Total vote stake: {mip4_stake + nomip4_stake}") | |
print(f"In favour stake: {mip4_stake}") | |
print(f"Against stake: {nomip4_stake}") | |
# print(votes) | |
# import json | |
# json_object = json.dumps(votes, indent=2) | |
# with open("/Users/tbernard/mip4-results.json", "w") as outfile: | |
# outfile.write(json_object) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment