Skip to content

Instantly share code, notes, and snippets.

@amiller amiller/stakepoll.py
Last active Dec 1, 2019

Embed
What would you like to do?
Stake poll update code
import json, sys
import subprocess
import requests
import time
import csv
import re
balances = {} # map addr => balance
# Parse the vote string
regexp_search = ''.join('%d([YAN])' % (i+1) for i in range(13))
tally = [[0., 0., 0.] for _ in range(13)]
# Loop over all received transactions
with open('stakepoll.csv', 'w') as csvfile:
# Step 1: Accumulate rows of data
rows = []
for r in json.load(sys.stdin):
# No minimum to vote
if r['amount'] < 0.000:
continue
print
print r['txid']
memo = r['memo'].decode('hex').strip()
print memo
# Get the inputs via api
url = "https://api.zcha.in/v2/mainnet/transactions/%s" % (r['txid'],)
resp = requests.get(url)
time.sleep(0.2)
tx = json.loads(resp.content)
for txin in tx['vin']:
addr = txin['retrievedVout']['scriptPubKey']['addresses'][0]
# Access the current balance using zchain api
if not addr in balances:
url = "https://api.zcha.in/v2/mainnet/accounts/%s" % (addr,)
resp = requests.get(url)
time.sleep(0.2)
balance = json.loads(resp.content)['balance']
balances[addr] = balance
balance = balances[addr]
# Store the raw vote
print 'addr:', addr, 'balance:', balance
rows.append([addr, balance, memo, r['txid']])
# Try to parse
memo = memo.upper()
match = re.search(regexp_search, memo)
if match is None: break
g = match.groups()[:13]
for i in range(13):
tally[i][0] += balance if g[i]=='Y' else 0
tally[i][1] += balance if g[i]=='A' else 0
tally[i][2] += balance if g[i]=='N' else 0
# Only look at first vin of a transaction
break
# Step 2: Sort
rows = sorted(rows, key=lambda a: a[1], reverse=True)
# Step 3: Write to file
writer = csv.writer(csvfile)
writer.writerow(["t Addr", "Current Balance", "Memo", "Txid"])
for row in rows:
writer.writerow(row)
with open('stakepoll_tally.csv', 'w') as csvfile:
# Step 4: Write tally to file
writer = csv.writer(csvfile)
writer.writerow(["Proposal", "Yes", "Abstain", "No"])
for i in range(13):
writer.writerow([i+1, tally[i][0], tally[i][1], tally[i][2]])
set -x
while true
do
# zs1ud5cyusg... is the Stake Poll address, see https://forum.zcashcommunity.com/t/staked-poll-on-zcash-dev-fund-debate/34846/85
src/zcash-cli z_listreceivedbyaddress zs1ud5cyusgfsqgkfqlxery0ttyvuvj66wztlqgv8tc4qaukaeaejxj7u70j0qgcctjfvzuzadejxd | python stakepoll.py
sleep 300 # 5 minutes
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.