Skip to content

Instantly share code, notes, and snippets.

@maxme
Created May 22, 2013 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxme/5628167 to your computer and use it in GitHub Desktop.
Save maxme/5628167 to your computer and use it in GitHub Desktop.
get_balance electrum script
#!/usr/bin/env python
import sys
from electrum import Interface
from electrum import bitcoin, Transaction
def get_transactions(addr):
interface = Interface({'server':'electrum.be:50001:t'})
interface.start()
transactions = interface.synchronous_get([ ('blockchain.address.get_history',[addr]) ])[0]
transactions.sort(key=lambda x:x["height"])
res = []
for item in transactions:
raw_tx = interface.synchronous_get([ ('blockchain.transaction.get',[item['tx_hash'], item['height']]) ])[0]
tx = Transaction(raw_tx)
res.append(tx)
return res
def get_balance(addr):
transactions = get_transactions(addr)
_in = 0
_out = 0
balance = 0
for tx in transactions:
is_relevant, is_send, value, fee = tx.get_value([addr], {"addr": 0})
if not is_relevant:
continue
if value:
balance += value
if is_send:
_out += value
else:
_in += value
print is_relevant, is_send, value, fee, balance / 100000000.
return balance, _in, _out
if __name__ == "__main__":
try:
addr = sys.argv[1]
except:
print "usage: get_history <bitcoin_address>"
sys.exit(1)
print get_balance(addr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment