forked from slickage/baron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insightapi.js
48 lines (45 loc) · 1.22 KB
/
insightapi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var request = require('request');
var config = require(__dirname + '/config');
var insightUrl = config.insight.protocol + '://' + config.insight.host + ':' + config.insight.port;
var getLastBlockHash = function(cb) {
var requestUrl = insightUrl + '/api/status?q=getLastBlockHash';
request(requestUrl, function(error, response, body) {
if (error) {
return cb(error, null);
}
body = JSON.parse(body);
if (!body.lastblockhash) {
var err = new Error('Error retrieving last block from insight.');
return cb(err, null);
}
var lastBlockHash = {
hash: body.lastblockhash,
type: 'blockhash'
};
return cb(null, lastBlockHash);
});
};
var getBlock = function(blockHash, cb){
if (!blockHash) {
var err = new Error('No blockhash provided.');
return cb(err, null);
}
var requestUrl = insightUrl + '/api/block/' + blockHash;
request(requestUrl, function(error, response, body) {
if (error) {
return cb(error, null);
}
try {
var block = JSON.parse(body);
return cb(null, block);
}
catch (err) {
console.log(err);
return cb(err, null);
}
});
};
module.exports = {
getLastBlockHash: getLastBlockHash,
getBlock: getBlock
};