bitcoinjs-lib icon indicating copy to clipboard operation
bitcoinjs-lib copied to clipboard

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

Open mlopezpollux opened this issue 1 year ago • 3 comments
trafficstars

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!

mlopezpollux avatar May 02 '24 18:05 mlopezpollux

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

jasonandjay avatar May 03 '24 02:05 jasonandjay

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 :/

mlopezpollux avatar May 03 '24 20:05 mlopezpollux

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

jasonandjay avatar May 05 '24 12:05 jasonandjay

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

Futuresso avatar Jul 21 '24 17:07 Futuresso

@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

junderw avatar Jul 22 '24 06:07 junderw