Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mlopezpollux opened this issue May 2, 2024 · 5 comments
Closed

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

mlopezpollux opened this issue May 2, 2024 · 5 comments

Comments

@mlopezpollux
Copy link

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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

@Futuresso

This comment was marked as spam.

@junderw
Copy link
Member

junderw commented Jul 22, 2024

@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 junderw closed this as completed Jul 22, 2024
@bitcoinjs bitcoinjs deleted a comment from Futuresso Jul 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants