Blockdaemon bitcoin dedicated nodes have ZMQ enabled. ZMQ is a message queue that allows programs to subscribe to events. You can use this to be notified when new transactions or new blocks occur on your Bitcoin connection.
This tutorial shows a simple Python application that prints each new transaction as well as the total value of all the transactions outputs.
The following text assumes a basic working knowledge of Python and pip.
Install Python libraries
Install the Python library ZMQ. We will use this library to listen to the bitcoin events:
input type="text" value="pip3 install zmq" readonly="true"
Install the Python library bitcoin. We will use this library to convert the raw bitcoin transactions into a simpler format:
input type="text" value="pip3 install bitcoin" readonly="true"
Copy script
Copy the following Python program into a file called bitcoin_zmq.py
import binascii
import sys
import zmq
import bitcoin
import pprint
context = zmq.Context()
socket = context.socket(zmq.SUB)
# Connect to ZMQ
socket.connect("tcp://[your node's URL from the Connect page]:28332")
# Subscribe to all new raw transactions
socket.setsockopt_string(zmq.SUBSCRIBE, u"rawtx")
# Loop until the user presses CTRL+c
while True:
# Wait for the next raw transaction
msg = socket.recv_multipart()
topic = msg[0]
body = msg[1]
# Convert the binary transaction into a simple dict
tx = bitcoin.deserialize(body)
# Calculate the total value of all outputs
total_value = 0
for out in tx['outs']:
total_value += out['value']
# Print the result
if total_value / 100000000.0 > 10:
print("")
print("New transaction: {}".format(bitcoin.txhash(body)))
print("Total value of all outputs: {} BTC".format(total_value / 100000000.0))
Further Reading
Bitcoin ZMQ Protocol Documentation
ZMQ Documentation
Comments
Please sign in to leave a comment.