Title: Electrum - Auto Forward
Post by: CryptCard on May 23, 2013, 11:58:39 AM
Hello,
With my Master Public Key I generate bitcoin addresses in my Webshop using Bitcoin Payments for WooCommerce (http://wordpress.org/plugins/bitcoin-payments-for-woocommerce/). As soon as I receive a payment it should be forwarded to my bitcoin address.
How can I automate this process? (forwarding the incoming payments to another bitcoin address using electrum)
Language: Python
Thanks!
Title: Re: Electrum - Auto Forward
Post by: CryptCard on May 24, 2013, 12:43:23 AM
Electrum is installed on a Ubuntu 12.04 VPS (Server edition - No GUI) I tried to write a python script that interacts with electrum (electrum getbalance, electrum payto) The main problem is that it does not only reply with json. root:~# electrum getbalance Connected to electrum.bitcoins.sk:50002 14qwj8tUBVVJ9sNqYnHSrp17z9PDywYUKN { "confirmed": "0.0190837" } What's the best way to get the actual balance of the wallet (confirmed + unconfirmed) in a python variable? (-> electrum payto *mybtcaddress* $BTCamout)
Title: Re: Electrum - Auto Forward
Post by: ThomasV on May 24, 2013, 08:48:33 AM
Electrum is installed on a Ubuntu 12.04 VPS (Server edition - No GUI) I tried to write a python script that interacts with electrum (electrum getbalance, electrum payto) The main problem is that it does not only reply with json. root:~# electrum getbalance Connected to electrum.bitcoins.sk:50002 14qwj8tUBVVJ9sNqYnHSrp17z9PDywYUKN { "confirmed": "0.0190837" } What's the best way to get the actual balance of the wallet (confirmed + unconfirmed) in a python variable? (-> electrum payto *mybtcaddress* $BTCamout) what you want is a json interface; this does not exist yet. you could try to do it entirely entirely in a python script (see examples in the 'scripts' directory), or with a simple script that you execute in the python console ( you just do run('filename') in the console)
Title: Re: [Bounty] Electrum - Auto Forward
Post by: harningt on July 02, 2013, 11:37:45 PM
I'll give this a try :)
Title: Re: [Bounty] Electrum - Auto Forward
Post by: CryptCard on July 03, 2013, 03:22:28 PM
I'll give this a try :)
You are awesome! The script works fine. ( 0.15BTC sent!)
Title: Re: [Bounty] Electrum - Auto Forward
Post by: harningt on July 05, 2013, 02:53:03 PM
I'll give this a try :)
You are awesome! The script works fine. ( 0.15BTC sent!) Thanks :) Fun hacking Electrum src code. Confirming receipt for benefit of others.
Title: Re: [Bounty] Electrum - Auto Forward
Post by: CryptCard on October 18, 2013, 12:38:20 PM
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.
Title: Re: Electrum - Auto Forward
Post by: CryptCard on October 22, 2013, 10:09:52 PM
Would be awesome if someone could fix this :) (In connection with the latest electrum version - which is still under development on git - 1.9)
Does a json interface exist already in the latest version?
Title: Re: Electrum - Auto Forward
Post by: sueche on March 12, 2015, 04:47:47 PM
Hey,
I am looking for somethething similar - automatically send % from every incomming payment to a particular BTC address with Electrum. Does that code work with the latest version 2.0.4 ? I tried to find any documentation about the Python console scripts but it was very poor.
|