How I built a Bitcoin indexer using ZeroMQ

Indexers are node operators that fetch data from a node then transform it and store on a database to provide easy access to the blockchain data. You can use it to build a history of user’s transactions, or get a transaction/block to build a decentraliz…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Emanuel Ferreira

Indexers are node operators that fetch data from a node then transform it and store on a database to provide easy access to the blockchain data. You can use it to build a history of user's transactions, or get a transaction/block to build a decentralized application.

ZeroMQ is an open source messaging library that you can connect and use some socket patterns. We will use a pub-sub pattern to subscribe on the node and listen new blocks and transactions on real time to store on our database and have a fast access than accessing directly the blockchain, and have a easy way to scale it.

Bitcoind is a node interacting who will allow us to interact with the blockchain and get the desired data.

Subscribing to Bitcoind using ZeroMQ

Three blocks with arrow one for each other bitcoin, NodeJS, database

To Listen to new blocks and transactions you need to run a node locally on your own infrastructure, you can download bitcoind here

Using ZeroMQ you can set a address to publish your events (blocks and transactions) then on each new event your node emit and the application can deal with it.

To run a node with ZeroMQ using bitcoind:

bitcoind -signet -zmqpubrawblock=tcp://127.0.0.1:3000 -zmqpubrawtx=tcp://127.0.0.1:3000

After run the node, you can install zeromq package and listen rawblock and rawtx event, then insert on your database.

import zmq from "zeromq";

const SOCKET_ADDRESS = "tcp://127.0.0.1:3000"

const connectZeroMQ = () => {
    const socket = zmq.socket('sub');
    socket.connect(SOCKET_ADDRESS);
    socket.subscribe('');
    return socket;
}

const zmq = connectZeroMQ();

zmq.on('rawblock', (error, block) => {
    if(err !== null) return;
    // parse and store on your database
})

zmq.on('rawtxn', (error, txn) => {
    if(err !== null) return;
    // parse and store on your database
})

Conclusion

You can achieve this result on different ways, but ZeroMQ gives to you more security and reliability to receive the blocks and transactions on real-time, this code can be used in production, but I highly recommend that you create a script to check every block and transaction from time to time to make sure none are lost.

In my repository below you can find all the code above connected to the database, and getting the lost blocks, or old blocks, feel free to contribute =)
https://github.com/EmanuelCampos/bitcoin-indexer

If you have some suggestions/feedbacks you can call me on:
Twitter


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Emanuel Ferreira


Print Share Comment Cite Upload Translate Updates
APA

Emanuel Ferreira | Sciencx (2022-11-04T18:29:55+00:00) How I built a Bitcoin indexer using ZeroMQ. Retrieved from https://www.scien.cx/2022/11/04/how-i-built-a-bitcoin-indexer-using-zeromq/

MLA
" » How I built a Bitcoin indexer using ZeroMQ." Emanuel Ferreira | Sciencx - Friday November 4, 2022, https://www.scien.cx/2022/11/04/how-i-built-a-bitcoin-indexer-using-zeromq/
HARVARD
Emanuel Ferreira | Sciencx Friday November 4, 2022 » How I built a Bitcoin indexer using ZeroMQ., viewed ,<https://www.scien.cx/2022/11/04/how-i-built-a-bitcoin-indexer-using-zeromq/>
VANCOUVER
Emanuel Ferreira | Sciencx - » How I built a Bitcoin indexer using ZeroMQ. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/04/how-i-built-a-bitcoin-indexer-using-zeromq/
CHICAGO
" » How I built a Bitcoin indexer using ZeroMQ." Emanuel Ferreira | Sciencx - Accessed . https://www.scien.cx/2022/11/04/how-i-built-a-bitcoin-indexer-using-zeromq/
IEEE
" » How I built a Bitcoin indexer using ZeroMQ." Emanuel Ferreira | Sciencx [Online]. Available: https://www.scien.cx/2022/11/04/how-i-built-a-bitcoin-indexer-using-zeromq/. [Accessed: ]
rf:citation
» How I built a Bitcoin indexer using ZeroMQ | Emanuel Ferreira | Sciencx | https://www.scien.cx/2022/11/04/how-i-built-a-bitcoin-indexer-using-zeromq/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.