Skip to content

Instantly share code, notes, and snippets.

@Aareon
Last active August 17, 2017 04:17
Show Gist options
  • Select an option

  • Save Aareon/7f3f436fff04ad1555836d6a582bde54 to your computer and use it in GitHub Desktop.

Select an option

Save Aareon/7f3f436fff04ad1555836d6a582bde54 to your computer and use it in GitHub Desktop.
import sqlite3, keys
def get_debit_mempool():
"""Get the debit mempool from the mempool database and return the
result given that the result is not None. Otherwise, return 0"""
mempool = sqlite3.connect('mempool.db')
mempool.text_factory = str
mempool_cursor = mempool.cursor()
mempool_cursor.execute("SELECT sum(amount) FROM transactions WHERE address = ?;", (address,))
debit_mempool = m.fetchone()[0]
mempool.close()
debit_mempool = 0 if debit_mempool is None else debit_mempool
return debit_mempool
def get_from_ledger():
"""Using a dictionary to neatly organize what result goes to what,
recursively iterate over said dictionary, executing the query in the
value of a given key and store the result under the same key in an
alternate dictionary"""
conn = sqlite3.connect('static/ledger.db')
conn.text_factory = str
cursor = conn.cursor()
queries = {
"credit":"SELECT sum(amount) FROM transactions WHERE recipient = ?;",
"debit":"SELECT sum(amount) FROM transactions WHERE address = ?;",
"fees":"SELECT sum(fee) FROM transactions WHERE address = ?;",
"rewards":"SELECT sum(reward) FROM transactions WHERE address = ?;",
"bl_height":"SELECT MAX(block_height) FROM transactions;"}
results = {}
for key,query in queries.items():
cursor.execute(query, (address,))
result = cursor.fetchone()[0]
results[key] = 0 if result is None else result
conn.close()
return results
def get_balance(credit, debit, fees, rewards, debit_mempool):
"""Calculate balance from credit, debit,
fees, rewards, and debit_mempool"""
return credit - debit - fees + rewards - debit_mempool
if __name__ == "__main__":
ledger = get_from_ledger()
credit = ledger["credit"]
debit = ledger["debit"]
fees = ledger["fees"]
rewards = ledger["rewards"]
debit_mempool = get_debit_mempool()
balance = get_balance(credit, debit, fees, rewards, debit_mempool)
print("Public key address: {}".format(address))
print("Total fees paid: {}".format(fees))
print("Total rewards mined: {}".format(rewards))
print("Total tokens received: {}".format(credit))
print("Total tokens spent: {}".format(debit))
print("Transction address balance: {}".format(balance))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment