-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsight.js
67 lines (55 loc) · 1.9 KB
/
insight.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Interface to access the BitPay insight server over socket.io and REST.
var colors = require('colors/safe')
var request = require('request-promise')
var logger = require('winston')
var Insight = exports.Insight = function (options) {
options = options || {}
this._api = options.api || 'https://insight.bitpay.com/'
}
Insight.prototype.listen = function () {
var socket = require('socket.io-client')(this._api)
this._socket = socket
socket.on('connect', function () {
socket.emit('subscribe', 'inv')
logger.info(colors.green('Socket connected'))
})
socket.on('disconnect', function () {
logger.error(colors.red('Socket disconnected'))
})
}
Insight.prototype.onTransaction = function (cb) {
this._socket.on('tx', function (data) {
cb(data)
})
}
Insight.prototype.onNewBlockHash = function (cb) {
return this._socket.on('block', cb)
}
Insight.prototype._status = function () {
return request(this._api + 'api/status').then(JSON.parse)
}
Insight.prototype.getBlockHeight = function () {
return this._status().then(function (data) {
return data.info.blocks
})
}
Insight.prototype.getBlockIndex = function (height) {
return request(this._api + 'api/block-index/' + height).then(JSON.parse).then(function (data) {
return data.blockHash
})
}
Insight.prototype.getBlock = function (hash) {
logger.info('getBlock: api/block/' + hash)
return request(this._api + 'api/block/' + hash).then(JSON.parse)
}
Insight.prototype.getAddress = function (address) {
return request(this._api + 'api/addr/' + address).then(JSON.parse)
}
Insight.prototype.getTransactions = function (address) {
const url = this._api + 'api/txs/?address=' + address
return request(url).then(JSON.parse)
}
Insight.prototype.getTransactionsFromTo = function (address, from, to) {
const url = this._api + 'api/addrs/' + address + '/txs?from=' + from + '&to=' + to
return request(url).then(JSON.parse)
}