Skip to content

Instantly share code, notes, and snippets.

@makevoid
Created September 14, 2018 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makevoid/70221d129f44f0a7fb2c5950bf076d5e to your computer and use it in GitHub Desktop.
Save makevoid/70221d129f44f0a7fb2c5950bf076d5e to your computer and use it in GitHub Desktop.
Try Bitcoin (Cash) ZeroMQ Api to retrieve and parse raw transactions (JS)
const zmq = require('zeromq')
const BitcoinCashZMQDecoder = require('bitcoincash-zmq-decoder')
const decoder = new BitcoinCashZMQDecoder("mainnet")
const subscriber = zmq.socket("sub")
const host = 'tcp://34.245.188.100:28332'
subscriber.subscribe("") // subscribe to all
const parseTransaction = (data) => {
const tx = decoder.decodeTransaction(data)
console.log( tx )
}
subscriber.on("message", function (topic, data) {
topic = topic.toString()
data = data.toString('hex')
console.log(topic, data)
if (topic == "rawtx") {
parseTransaction(data)
}
if (topic == "rawblock") {
console.log("got a new block!")
}
})
subscriber.connect(host)
// package.json
//
// {
// "dependencies": {
// "bitcoin-core": "^2.0.0",
// "bitcoincash-zmq-decoder": "^0.1.5",
// "bitcoincashjs": "^0.1.10",
// "insight-client": "^0.1.0",
// "zeromq": "^4.6.0"
// },
// "scripts": {
// "start": "node try-btczmq"
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment