Bitcoin Forum
December 17, 2017, 10:15:54 AM *
News: Latest stable version of Bitcoin Core: 0.15.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Electrum - Auto Forward  (Read 3317 times)
CryptCard
Full Member
***
Offline Offline

Activity: 124



View Profile
May 23, 2013, 11:58:39 AM
 #1

Hello,

With my Master Public Key I generate bitcoin addresses in my Webshop using 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!
1513505754
Hero Member
*
Offline Offline

Posts: 1513505754

View Profile Personal Message (Offline)

Ignore
1513505754
Reply with quote  #2

1513505754
Report to moderator
1513505754
Hero Member
*
Offline Offline

Posts: 1513505754

View Profile Personal Message (Offline)

Ignore
1513505754
Reply with quote  #2

1513505754
Report to moderator
Some PGP public keys you should import: theymos, BadBear, Sirius, Stefan, Wladimir, Gavin, Gregory, Jeff, Pieter
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1513505754
Hero Member
*
Offline Offline

Posts: 1513505754

View Profile Personal Message (Offline)

Ignore
1513505754
Reply with quote  #2

1513505754
Report to moderator
1513505754
Hero Member
*
Offline Offline

Posts: 1513505754

View Profile Personal Message (Offline)

Ignore
1513505754
Reply with quote  #2

1513505754
Report to moderator
CryptCard
Full Member
***
Offline Offline

Activity: 124



View Profile
May 24, 2013, 12:43:23 AM
 #2

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.

Code:
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)
ThomasV
Moderator
Legendary
*
Offline Offline

Activity: 1892



View Profile WWW
May 24, 2013, 08:48:33 AM
 #3

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.

Code:
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)

Electrum: the convenience of a web wallet, without the risks
harningt
Member
**
Offline Offline

Activity: 92



View Profile
July 02, 2013, 11:37:45 PM
 #4

I'll give this a try Smiley
CryptCard
Full Member
***
Offline Offline

Activity: 124



View Profile
July 03, 2013, 03:22:28 PM
 #5

I'll give this a try Smiley

You are awesome! The script works fine. (0.15BTC sent!)
harningt
Member
**
Offline Offline

Activity: 92



View Profile
July 05, 2013, 02:53:03 PM
 #6

I'll give this a try Smiley

You are awesome! The script works fine. (0.15BTC sent!)
Thanks Smiley Fun hacking Electrum src code.
Confirming receipt for benefit of others.
CryptCard
Full Member
***
Offline Offline

Activity: 124



View Profile
October 18, 2013, 12:38:20 PM
 #7

Start
Code:
sudo python  /root/.electrum/forwarder.py --config /root/.electrum/forwarder.conf

./forwarder.conf
Code:
[electrum]
wallet_path = /root/.electrum/electrum.dat
password =
address = 1DCKtUCFZZCr1wCxwsYkzemQvxrUq8byps
minimum_transaction = 0.11
fee = 0.001

./forwarder.py
Code:
forwarder.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 harningt

PS: 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.
CryptCard
Full Member
***
Offline Offline

Activity: 124



View Profile
October 22, 2013, 10:09:52 PM
 #8

Would be awesome if someone could fix this Smiley (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?
sueche
Member
**
Offline Offline

Activity: 75


View Profile
March 12, 2015, 04:47:47 PM
 #9

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.
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!