Last active
December 1, 2015 17:26
-
-
Save jointhepartypooper/5fa889e9e6e7fb6b6d5b to your computer and use it in GitHub Desktop.
hookGetFindstakeData
This file contains hidden or 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 http = require('http'); | |
function checkTxPresent(txId, txIndex, cb) { | |
var keytx = 'to' + txId + '_' + txIndex; | |
var str = ''; | |
var fileurl = "http://peercoinfindstakedata.divshot.io/json/" + keytx + '.json'; | |
var handleRes = function (res) { | |
var isOK = (res.statusCode == 200); | |
res.on('data', function (chunk) { | |
str += chunk; | |
}); | |
res.on('end', function () { | |
cb((isOK ? keytx : '')); | |
}); | |
}; | |
http.get(fileurl, handleRes).on('error', function () { | |
cb(''); | |
}); | |
} | |
function getStats(cb) { | |
cb('7.0'); | |
} | |
function getStatsorg(cb) { | |
var str = ''; | |
var fileurl = "http://peerchain.net/api/network/last"; | |
http.get(fileurl, function (res) { | |
res.on('data', function (chunk) { | |
str += chunk; | |
}); | |
res.on('end', function () { | |
var parsed = JSON.parse(str); | |
/*{ | |
"destroyed_fees": "5271.019824", | |
"last_block": 161221, | |
"mined_coins": "21887791.080000", | |
"minted_coins": "233084.450000", | |
"money_supply": "22115604.510176", | |
"pos_blocks": 123693, | |
"pos_difficulty": "14.68699991", | |
"pow_block_reward": "82.700000", | |
"pow_blocks": 37529, | |
"pow_difficulty": 213748685.664, | |
"time": "2015-02-28 16:18:16+0000", | |
"transactions": 425569 | |
} | |
*/ | |
if (parsed) { | |
cb((parsed.pos_difficulty)); | |
} else | |
cb('15.0'); | |
}); | |
}).on('error', function () { | |
console.log('err'); | |
cb('15.0'); | |
}); | |
} | |
function endHook(hook, diff, resarr) { | |
var jsonstr = JSON.stringify({ | |
difficulty : diff, | |
txo : resarr | |
}); | |
hook.res.end(hook.params.PeercoinAddress + '(' + jsonstr + ');'); | |
} | |
function getFindstakeData(adr, callback) { | |
var resarr = [], | |
str = '', | |
fileurl = 'http://ppc.blockr.io/api/v1/address/unspent/' + adr;//hook.params.PeercoinAddress; | |
var onChunk = function (chunk) { | |
str += chunk; | |
}; | |
var onReqEnd = function () { | |
var parsed = JSON.parse(str); | |
if (parsed && parsed.status && parsed.status == "success" && | |
parsed.data && parsed.data.unspent && parsed.data.unspent.length > 0) { | |
var ctotal = parsed.data.unspent.length, | |
count = 0; | |
parsed.data.unspent.forEach(function (obj) { | |
checkTxPresent(obj.tx, obj.n, function (s) { | |
count++; | |
if (s && s.length > 0) | |
resarr.push(s); | |
if (count == ctotal) { | |
callback(resarr); | |
} | |
}); | |
}); | |
} else { | |
callback(resarr); | |
} | |
}; | |
var onReqErr = function (e) { | |
callback(resarr); | |
}; | |
http.get(fileurl, function (res) { | |
res.on('data', onChunk); | |
res.on('end', onReqEnd); | |
}).on('error', onReqErr); | |
}; | |
module['exports'] = function hookGetFindstakeData(hook) { | |
if (hook.params.PeercoinAddress) { | |
getStats(function (difficulty) { | |
getFindstakeData(hook.params.PeercoinAddress, function (resarr) { | |
endHook(hook, difficulty, resarr); | |
}); | |
}); | |
} else | |
endHook(hook, "15.0", []); | |
}; | |
module['exports'].schema = { | |
"PeercoinAddress" : { | |
"type" : "string", | |
"default" : "" | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment