Bitcoin Forum
May 14, 2024, 07:37:38 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How to listen to a Bitcoin transaction?  (Read 154 times)
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
February 21, 2021, 09:32:54 AM
Last edit: February 21, 2021, 10:09:33 AM by Sanka555
 #1

My blochaincom  wallet has a set of my addresses. I have the keys to them, and everything I need.
A payment is received at one of these addresses.
I want to make an automatic reaction to this fact. So that this payment, immediately after receipt to any of these addresses, is sent to one other address (as quickly as possible, directly with the nearest block).
how do smart people do it?
Thank you in advance.
OmegaStarScream
Staff
Legendary
*
Offline Offline

Activity: 3472
Merit: 6135



View Profile
February 21, 2021, 10:34:34 AM
Merited by vapourminer (1), Welsh (1)
 #2

There are two ways you can use to detect payments (I'd recommend the second one but it depends on what you're trying to do):

- By subscribing to the addresses using the WebSocket API: https://www.blockchain.com/api/api_websocket
- Using the xPub from your wallet with the "Receive payments" API: https://www.blockchain.com/api/api_receive

And then for the payment forwarding part, you can use the wallet API: https://www.blockchain.com/api/blockchain_wallet_api

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
February 21, 2021, 11:59:52 AM
 #3

many thanks.
 I will try this method.
But I would like to have some ready-made software solutions for this or existing online services.
OmegaStarScream
Staff
Legendary
*
Offline Offline

Activity: 3472
Merit: 6135



View Profile
February 21, 2021, 12:10:57 PM
 #4

many thanks.
 I will try this method.
But I would like to have some ready-made software solutions for this or existing online services.

You can use Blocknomics payment forwarder for that[1]. You've also got the necessary plugins, in case you're planning to set up an e-commerce site[2].

[1] https://www.blockonomics.co/views/payment_forwarding.html#
[2] https://www.blockonomics.co/views/integrations.html

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
February 21, 2021, 05:20:46 PM
 #5

Thank you so much. this is not what I wanted. This service only monitors addresses and reports changes to the mail.
I need the service to automatically transfer it to another address as soon as the payment arrives at one of the specified addresses
without me.  automatically transfer
Pmalek
Legendary
*
Offline Offline

Activity: 2758
Merit: 7140



View Profile
February 21, 2021, 05:44:04 PM
 #6

Does it have to be blockchain.com or can you switch to another wallet like Electrum? I had no success finding anything for the blockchain wallet, but I came across an old thread where someone created an Electrum forwarded script that worked back in 2013. I don't have the skills to go over the code, so I suggest that someone who knows what he is doing checks if this is safe to use and still works:

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.


.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
DannyHamilton
Legendary
*
Offline Offline

Activity: 3388
Merit: 4653



View Profile
February 21, 2021, 07:54:21 PM
Merited by OgNasty (1)
 #7

I want to make an automatic reaction to this fact. So that this payment, immediately after receipt to any of these addresses, is sent to one other address (as quickly as possible, directly with the nearest block).
how do smart people do it?

They don't.  There is VERY RARELY any good reason to do this.  You are increasing your transaction costs, and you generally aren't gaining anything that couldn't have been accomplished through a better design and proper use of hierarchical deterministic keys.
ABCbits
Legendary
*
Offline Offline

Activity: 2870
Merit: 7497


Crypto Swap Exchange


View Profile
February 22, 2021, 11:19:38 AM
Merited by Pmalek (1)
 #8

Does it have to be blockchain.com or can you switch to another wallet like Electrum? I had no success finding anything for the blockchain wallet, but I came across an old thread where someone created an Electrum forwarded script that worked back in 2013. I don't have the skills to go over the code, so I suggest that someone who knows what he is doing checks if this is safe to use and still works:

--snip--

I don't know if the code is safe (as in no malicious code), but i know won't work because the code is for Python 2 and Electrum 1.X.
For the security aspect, it store password of the wallet on plain text, which isn't best practice. At very least you need to make sure only root only can read/modify the file.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!