Last active
August 29, 2015 14:08
-
-
Save janajri/a6c1fe7f5f8853698e04 to your computer and use it in GitHub Desktop.
simple btc address monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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