Startsudo python /root/.electrum/forwarder.py --config /root/.electrum/forwarder.conf
./forwarder.conf[electrum]
wallet_path = /root/.electrum/electrum.dat
password =
address = 1DCKtUCFZZCr1wCxwsYkzemQvxrUq8byps
minimum_transaction = 0.11
fee = 0.001
./forwarder.pyforwarder.py#!/usr/bin/env python
import time, thread, sys, os
import getopt
import Queue
from electrum import Wallet, Interface, WalletVerifier, SimpleConfig, WalletSynchronizer, Commands
try:
opts, args = getopt.getopt(sys.argv[1:], 'fc:', ["foreground", "config="])
except getopt.GetoptError:
print 'forwarder.py [-f|--foreground] [-c filename|--config filename]'
sys.exit(2)
foreground_scanning = False
config_file_path = "forwarder.conf"
for opt, arg in opts:
if opt in ('-f', '--foreground'):
foreground_scanning = True
elif opt in ('-c', '--config'):
config_file_path = arg
# Load the configuration file with details
import ConfigParser
forwarder_config = ConfigParser.ConfigParser()
forwarder_config.read(config_file_path)
# Load the wallet from the configured location
config_data = {'wallet_path': forwarder_config.get('electrum', 'wallet_path')}
config = SimpleConfig(config_data)
wallet = Wallet(config)
# Retrieve details about transaction sending
target_address = forwarder_config.get('electrum', 'address')
minimum_transaction = float(forwarder_config.get('electrum', 'minimum_transaction'))
fee = float(forwarder_config.get('electrum', 'fee'))
# Construct the remote Electrum interface
interface = Interface(config)
interface.start(wait = True)
interface.send([('server.peers.subscribe', [])])
# Prepare the 'commands' utility object to make operating easy
commands = Commands(wallet, interface)
# Load in the password used to decrypt the signing keys
if forwarder_config.get('electrum', 'password'):
commands.password = forwarder_config.get('electrum', 'password')
# Prepare a queue as a simple synchronization primitive to decouple update from send
queue = Queue.Queue()
# Global boolean to prevent recursive update/send events and too-early handling
global allow_put
allow_put = False
# Global number indicating the last height transactions were sent from
global last_send_height
last_send_height = 0
def on_wallet_update():
global allow_put
global last_send_height
if not allow_put:
return
if verifier.local_height == last_send_height:
#print "Last send height == local height: " + str(last_send_height)
return
queue.put(True)
# Setup the wallet's interface and hookup the wallet update callback
wallet.interface = interface
interface.register_callback('updated', on_wallet_update)
# Load in the transaction / tree verifier
verifier = WalletVerifier(interface, config)
wallet.set_verifier(verifier)
verifier.start()
# Load in the wallet synchronizer to load in new data
synchronizer = WalletSynchronizer(wallet, config)
synchronizer.start()
def try_send():
global allow_put
global last_send_height
# Collect balance information to determine if we should continue
balance = commands.getbalance()
print "Collected updated balance: " + str(balance)
confirmed = balance.get('confirmed')
if not confirmed:
confirmed = 0
else:
confirmed = float(confirmed)
unconfirmed = balance.get('unconfirmed')
if not unconfirmed:
unconfirmed = 0
else:
unconfirmed = float(unconfirmed)
print "Total Balance: " + str(confirmed + unconfirmed)
# Make sure that we have enough to cover the fee.
# Also make sure that prior unconfirmed items are enough to cover the fee
# to avoid pending withdrawals being re-spent and causing unnecessary server errors.
if (confirmed + unconfirmed) >= (minimum_transaction + fee):
# Store the last_send_height to avoid re-sending while we are waiting for it to get in the queue
print "Sending %s to %s w/ fee %s" % (confirmed + unconfirmed - fee, target_address, fee)
last_send_height = verifier.local_height
#print "New height " + str(last_send_height)
try:
print target_address
print confirmed + unconfirmed - fee
print fee
h = commands.payto(target_address, confirmed + unconfirmed - fee - 0.001, fee)
print "Transaction sent - " + str(h)
except BaseException as e:
print "Failed to send transaction: " + str(e)
except:
print "Unknown exception occurred"
# Force a full wallet update up front - then enable wallet updates
wallet.update()
allow_put = True
# Perform an initial send try to bootstrap the process
try_send()
# If foreground scanning - use loop w/ Ctrl+C break method
if foreground_scanning:
try:
while True:
ready = queue.get(True, 1000000)
if not ready:
continue
allow_put = False
try_send()
allow_put = True
except KeyboardInterrupt as e:
print "Ending server"
# Shutdown server objects
verifier.stop()
synchronizer.stop()
interface.stop()
# Due to daemon threads - follow electrum GUI rules for waiting
time.sleep(0.1)
print "terminated"
The script shows sometimes some errors but it does what it should.
All the incoming payments will be forwarded to the address provided in the forwarder.conf.
-> BIG THANKS to
harningtPS: It doesn't work with the latest development version (git 1.9 electrum) as their are some errors with the WalletVerifier function.
Perhaps someone can fix this.