Skip to content

Instantly share code, notes, and snippets.

@gtklocker
Created September 21, 2018 20:41
Show Gist options
  • Save gtklocker/4b9323c9120d505dda7d97f31b5b81de to your computer and use it in GitHub Desktop.
Save gtklocker/4b9323c9120d505dda7d97f31b5b81de to your computer and use it in GitHub Desktop.
Watch for a Bitcoin Cash transaction (new or historical) using bcash
'use strict';
const bcash = require('bcash').set('testnet');
const Logger = require('blgr');
//const logger = new Logger({ level: 'info' });
// SPV chains only store the chain headers.
const chain = new bcash.Chain({
memory: false,
location: './bcash-bak/spvchain/',
spv: true,
//logger,
});
const pool = new bcash.Pool({
chain: chain,
spv: true,
//logger,
});
const WATCH_ADDRESS = bcash.Address.fromString('mo2TnQCHUheuCshwxrFFenvTPWjHyGGWHh');
const CHAIN_RESET_HEIGHT = 1258105;
(async () => {
//await logger.open();
await chain.open();
await pool.open();
console.log('watch address = %s', WATCH_ADDRESS);
pool.watchAddress(WATCH_ADDRESS);
console.log('watch address called');
await pool.connect();
pool.startSync();
pool.on('tx', (tx) => {
console.log('received tx = %s', tx);
});
chain.on('block', (blk, ch) => {
if (blk.txs.length > 0)
console.log('received block %d with txs = %O', ch.height, blk.txs);
});
if (chain.height > CHAIN_RESET_HEIGHT) {
console.log('resetting chain to %d', CHAIN_RESET_HEIGHT);
await chain.reset(CHAIN_RESET_HEIGHT);
}
setInterval(() => {
console.log('chain height = %d', chain.height);
}, 10000);
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment