Forked from cryptocracy/bitswitch_intel_edison_main.js
Last active
August 29, 2015 14:20
-
-
Save makevoid/635a64d24b37404567b3 to your computer and use it in GitHub Desktop.
BitSwitch Countertparty version
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
/*jslint node:true, vars:true, bitwise:true, unparam:true */ | |
/*jshint unused:false */ | |
/*global */ | |
var mraa = require('mraa'); //require mraa | |
var request = require('request'); | |
var _ = require('underscore') | |
var addr2watch = '1LJ4rddYGzT8GqwQioMNw3cYQJdwjM3Drs' // bitcoin address to watch | |
var assetName = 'PROG' | |
//var loop_time = 10000 // ms (checks every 10 sec) | |
var loop_time = 3000 // ms (checks every 3 sec) | |
var balance = null | |
var relayPin = new mraa.Gpio(13) | |
var check_balance = function(){ | |
// this version uses xcp.blockscan.com API | |
var url = 'http://xcp.blockscan.com/api2?module=address&action=balance&btc_address='+addr2watch+'&asset='+assetName | |
request(url, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var json = JSON.parse(body) | |
if (json.status != "error") { | |
// var unconfirmed = Math.abs(json.data[0].unconfirmed_balance) | |
// var value = parseInt(unconfirmed) | |
// | |
// you can use var unconfirmed and value above to test, otherwise you have to wait for btc blockchain confirmation time | |
var bal = json.data[0].balance | |
var value = parseInt(bal) | |
// this is the simplest approach, just check if the balance if different | |
// you could check if the price is increased by X amount or better if you got a transaction exactly of x btc | |
// console.log("balance:", balance, value) // uncomment this to see the balance at every tick (check) | |
if (balance && balance != value) | |
activate() | |
balance = value | |
} else { | |
console.log("error:", json.message) | |
} | |
} | |
_.delay(check_balance, loop_time) | |
}) | |
} | |
var activate = function() { | |
console.log("Address balance changed, new one is:", balance, assetName) | |
console.log("Let there be light!") | |
relayPin.write(0); | |
} | |
var main = function() { | |
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console | |
relayPin.dir(mraa.DIR_OUT); | |
relayPin.write(1); | |
console.log("check_balance() initialized") | |
check_balance() | |
} | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment