bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.67k stars 2.1k forks source link

[Question] How to watch blocks like viem.watchBlocks? #2095

Closed mlopezpollux closed 2 months ago

mlopezpollux commented 5 months ago

Hey, guys! How is it going? Hope you are doing well :)

I'm making a monitoring server for deposits in different chains and I encountered that in EVM chains is really easy with viem's watchBlocks method but I'm not really sure what is the parallel in the bitcoin network.

Please and thank you so much!

jasonandjay commented 5 months ago

Hey, guys! How is it going? Hope you are doing well :)

I'm making a monitoring server for deposits in different chains and I encountered that in EVM chains is really easy with viem's watchBlocks method but I'm not really sure what is the parallel in the bitcoin network.

Please and thank you so much!

bitcoinjs don`t support watchBlocks

You can use service from mempool:https://mempool.space/signet/docs/api/websocket

mlopezpollux commented 5 months ago

Hey, guys! How is it going? Hope you are doing well :) I'm making a monitoring server for deposits in different chains and I encountered that in EVM chains is really easy with viem's watchBlocks method but I'm not really sure what is the parallel in the bitcoin network. Please and thank you so much!

bitcoinjs don`t support watchBlocks

You can use service from mempool:https://mempool.space/signet/docs/api/websocket

Thank you so much for your response, @jasonandjay. I was looking more for a connection with an own bitcoin node for cheaper costs because I'm bootstrapping the project right now. I'm running a Chainstack Bitcoin node but can't connect with the bitcoin-core library, always returns timeout :/

jasonandjay commented 5 months ago

I'm sorry that I can't help you. Ask help to @junderw He is experienced and may be able to help you.

Futuresso commented 2 months ago

Hey, guys! How is it going? Hope you are doing well :)

I'm making a monitoring server for deposits in different chains and I encountered that in EVM chains is really easy with viem's watchBlocks method but I'm not really sure what is the parallel in the bitcoin network.

Please and thank you so much!

Wow good . mail me

junderw commented 2 months ago

@mlopezpollux Sorry I dropped this.

You can connect to your Bitcoin Core node via ZeroMQ and receive notices for new blocks.

However, these messages are only the blockhash, so you will need to respond to those messages by querying the JSON-RPC getblock endpoint to get the full block details. getblock has a verbose option that will give you the full block as a JSON or I think you can also just get the full block and parse it yourself. (BitcoinJS-lib would be a good way to parse the raw block into Transactions.

(Edit: ZeroMQ has a rawblock topic that will return the full block binary, but will require parsing with something like bitcoinjs-lib... subbing to hashblock (remember to reverse the blockhash bytes before querying the RPC) and querying the JSON-RPC is better if you don't want to deal with parsing and you just want stats about the block (verbosity = 1) or stats about each tx (verbosity = 2))

ZeroMQ info: https://github.com/bitcoin/bitcoin/blob/v27.1/doc/zmq.md#usage

JSON-RPC info: https://developer.bitcoin.org/reference/rpc/getblock.html