Skip to content

Instantly share code, notes, and snippets.

@chill117
Last active August 29, 2015 14:04
Show Gist options
  • Save chill117/41ebae82754f4337f75d to your computer and use it in GitHub Desktop.
Save chill117/41ebae82754f4337f75d to your computer and use it in GitHub Desktop.
Watches for new transactions in the default wallet of Electrum. When a new transaction is received at an address, the full history of that address is printed to the console.
#!/usr/bin/env python
# Place this file in the Electrum scripts directory. Then run from command line, like this: `~/.electrum/scripts/watch.py`.
import sys, time
from electrum import Network, print_json, SimpleConfig, Wallet, WalletStorage
def initialize():
config = SimpleConfig()
network = Network(config)
network.start(True)
if not network.is_connected:
print 'Not connected'
sys.exit(1)
storage = WalletStorage(config)
wallet = Wallet(storage)
wallet.start_threads(network)
return wallet, network
def get_address_history(addr):
h = wallet.history.get(addr, [])
history = []
for i, dummy in enumerate(h):
tx_hash = h[i][0]
tx = wallet.transactions.get(tx_hash)
if not tx: continue
value = 0
for o in tx.outputs:
o_address, o_value = o
if o_address == addr:
value += o_value
confirmations = wallet.verifier.get_confirmations(tx_hash)[0]
history.append({
'confirmations': confirmations,
'tx_hash': tx_hash,
'value': value / 1.e8
})
return history
def print_address_history(addr):
history = get_address_history(addr)
print_json({'address': addr, 'history': history})
def get_receiving_addresses():
return wallet.addresses(False)
def watch_receiving_addresses():
receiving_addresses = get_receiving_addresses()
for addr in receiving_addresses:
callback = (lambda _addr: lambda self,result: print_address_history(_addr))(addr)
network.interface.send([('blockchain.address.subscribe', [addr])], callback)
wallet, network = initialize()
watch_receiving_addresses()
# And now we wait..
while True:
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment