Skip to content

Instantly share code, notes, and snippets.

@janajri
Last active August 29, 2015 14:08
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 janajri/a6c1fe7f5f8853698e04 to your computer and use it in GitHub Desktop.
Save janajri/a6c1fe7f5f8853698e04 to your computer and use it in GitHub Desktop.
simple btc address monitor
var address = process.env.BTC_ADDR;
if(!address) {
throw err('Provide btc address');
}
var request = require('request');
var f; // set interval ref;
var BTC_URL = 'https://blockchain.info/address/' + address + '?format=json';
var balance;
function checkBalance(){
request(BTC_URL, function(err, res, body){
if(err){
throw err;
}
if(body && body.final_balance && balance) { // if successful and balance already set
var diff = Math.abs(balance - body.final_balance);
if(diff > 0) {
console.log('balance changed');
f = null;
doSomethingElse(); // trigger something else.
}
} else { // else set initial balance
balance = body.final_balance;
console.log('Setting initial balance');
}
});
}
f = setInterval(checkBalance, 5000); // run every 5 seconds.
//usage: BTC_ADDR=<btcaddr> node btcmonitor.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment