-
Notifications
You must be signed in to change notification settings - Fork 4
/
chainradar.js
98 lines (86 loc) · 2.42 KB
/
chainradar.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import request from 'request';
import _ from 'underscore';
export class Api {
constructor(options={}) {
this.options = _.defaults(options, {
apiUrl: 'http://chainradar.com/api',
version: 'v1',
// Coins
// Each API request is relative to a coin, so there is a required parameter coin: string.
// aeon, bbr, bcn, btc, dsh, fcn, mcn, qcn, duck, mro, rd
coin: 'bcn'
});
}
// generate object key: value to URL /value/value/value/
objectToUrl(o) {
return _.map(o, (value, key) => value).join('/');
}
// Generate api endpoint URL
getUrl(endpoint) {
const { apiUrl, version, coin } = this.options
return `${apiUrl}/${version}/${coin}/${endpoint}`;
}
get(endpoint, callback) {
request.get(this.getUrl(endpoint), (err, res, body) => {
return callback(JSON.parse(body));
});
}
/* Status {coin}/status
* Get actual coin statistics data.
* coin: string Coin symbol alias
*/
getStatus(callback) {
return this.get('status', callback);
}
/*
* Get blocks
* Header data in height range.
* {coin}/blocks/range/{from}/{to}/header
* from: integer Blocks starting height.
* to: integer Blocks ending height.
*/
getBlocksHeader(o, callback) {
return this.get(`blocks/range/${this.objectToUrl(o)}/summary`, callback);
}
/*
* Block header
* Get block header data by height or hash.
* height|hash: string Block height or hash.
*/
getBlockHeader(heightOrHash, callback) {
return this.get(`blocks/${heightOrHash}/summary`, callback);
}
/*
* Blocks data
* Get blocks full data in height range.
* from: integer Blocks starting height.
* to: integer Blocks ending height.
*/
getBlocksData(o, callback) {
return this.get(`blocks/range/${this.objectToUrl(o)}/full`, callback);
}
/*
* Block data
* Get block data by height or hash.
* height|hash: string Block height or hash.
*/
getBlockData(heightOrHash, callback) {
return this.get(`blocks/${heightOrHash}/full`, callback);
}
/*
* Transaction header
* Get transaction header data.
* hash: string Transaction hash.
*/
getTransactionHeader(hash, callback) {
return this.get(`transactions/${hash}/summary`, callback);
}
/*
* Transaction data
* Get transaction full data.
* hash: string Transaction hash.
*/
getTransactionData(hash, callback) {
return this.get(`transactions/${hash}/full`, callback);
}
}