Skip to content

Instantly share code, notes, and snippets.

@amiller amiller/
Last active Dec 1, 2019

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:
print r['txid']
memo = r['memo'].decode('hex').strip()
print memo
# Get the inputs via api
url = "" % (r['txid'],)
resp = requests.get(url)
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 = "" % (addr,)
resp = requests.get(url)
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 =, 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
# 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:
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
# zs1ud5cyusg... is the Stake Poll address, see
src/zcash-cli z_listreceivedbyaddress zs1ud5cyusgfsqgkfqlxery0ttyvuvj66wztlqgv8tc4qaukaeaejxj7u70j0qgcctjfvzuzadejxd | python
sleep 300 # 5 minutes
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.