Browse Source

pep8

master
Maxime Biais 13 years ago
parent
commit
6075f8ece5
  1. 35
      scripts/get_balance

35
scripts/get_balance

@ -4,51 +4,60 @@ import sys
from electrum import Interface from electrum import Interface
from electrum import bitcoin, Transaction from electrum import bitcoin, Transaction
def get_transaction(interface, tx_hash, tx_height): def get_transaction(interface, tx_hash, tx_height):
raw_tx = interface.synchronous_get([ ('blockchain.transaction.get',[tx_hash, tx_height]) ])[0] raw_tx = interface.synchronous_get([(
'blockchain.transaction.get', [tx_hash, tx_height])])[0]
tx = Transaction(raw_tx) tx = Transaction(raw_tx)
return tx return tx
def get_history(interface, addr): def get_history(interface, addr):
transactions = interface.synchronous_get([ ('blockchain.address.get_history',[addr]) ])[0] transactions = interface.synchronous_get([(
transactions.sort(key=lambda x:x["height"]) 'blockchain.address.get_history', [addr])])[0]
return [(i["tx_hash"],i["height"]) for i in transactions] transactions.sort(key=lambda x: x["height"])
return [(i["tx_hash"], i["height"]) for i in transactions]
def get_addr_balance(interface, address): def get_addr_balance(interface, address):
prevout_values = {} prevout_values = {}
h = get_history(interface, address) h = get_history(interface, address)
if h == ['*']: return 0, 0 if h == ['*']:
return 0, 0
c = u = 0 c = u = 0
received_coins = [] # list of coins received at address received_coins = [] # list of coins received at address
transactions = {} transactions = {}
# fetch transactions # fetch transactions
for tx_hash, tx_height in h: for tx_hash, tx_height in h:
transactions[(tx_hash, tx_height)] = get_transaction(interface, tx_hash, tx_height) transactions[(tx_hash, tx_height)] = get_transaction(
interface, tx_hash, tx_height)
for tx_hash, tx_height in h: for tx_hash, tx_height in h:
tx = transactions[(tx_hash, tx_height)] tx = transactions[(tx_hash, tx_height)]
if not tx: continue if not tx:
continue
update_tx_outputs(tx, prevout_values) update_tx_outputs(tx, prevout_values)
for i, (addr, value) in enumerate(tx.outputs): for i, (addr, value) in enumerate(tx.outputs):
if addr == address: if addr == address:
key = tx_hash + ':%d'%i key = tx_hash + ':%d' % i
received_coins.append(key) received_coins.append(key)
for tx_hash, tx_height in h: for tx_hash, tx_height in h:
tx = transactions[(tx_hash, tx_height)] tx = transactions[(tx_hash, tx_height)]
if not tx: continue if not tx:
continue
v = 0 v = 0
for item in tx.inputs: for item in tx.inputs:
addr = item.get('address') addr = item.get('address')
if addr == address: if addr == address:
key = item['prevout_hash'] + ':%d'%item['prevout_n'] key = item['prevout_hash'] + ':%d' % item['prevout_n']
value = prevout_values.get(key) value = prevout_values.get(key)
if key in received_coins: if key in received_coins:
v -= value v -= value
for i, (addr, value) in enumerate(tx.outputs): for i, (addr, value) in enumerate(tx.outputs):
key = tx_hash + ':%d'%i key = tx_hash + ':%d' % i
if addr == address: if addr == address:
v += value v += value
if tx_height: if tx_height:
@ -57,17 +66,19 @@ def get_addr_balance(interface, address):
u += v u += v
return c, u return c, u
def update_tx_outputs(tx, prevout_values): def update_tx_outputs(tx, prevout_values):
for i, (addr, value) in enumerate(tx.outputs): for i, (addr, value) in enumerate(tx.outputs):
key = tx.hash() + ':%d' % i key = tx.hash() + ':%d' % i
prevout_values[key] = value prevout_values[key] = value
def main(address): def main(address):
interface = Interface() interface = Interface()
interface.start() interface.start()
c, u = get_addr_balance(interface, address) c, u = get_addr_balance(interface, address)
print("Final balance: confirmed: %d (%.8f BTC), unconfirmed: %d (%.8f BTC)" % print("Balance - confirmed: %d (%.8f BTC), unconfirmed: %d (%.8f BTC)" %
(c, c / 100000000., u, u / 100000000.)) (c, c / 100000000., u, u / 100000000.))
if __name__ == "__main__": if __name__ == "__main__":

Loading…
Cancel
Save