-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissuerCapitalization.js
150 lines (118 loc) · 3.88 KB
/
issuerCapitalization.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var winston = require('winston'),
moment = require('moment'),
_ = require('lodash'),
async = require('async'),
util = require('util'),
tools = require('../utils');
/**
* issuerCapitalization returns the total capitalization (outstanding balance)
* of a specified issuer & specified currency pair, over the given time range.
*
* Available options are:
* {
* currencies: [
* {
* issuer: ('Bitstamp' or 'rvY...'),
* currency: ('USD', 'BTC', etc)
* },{
* issuer: ('Bitstamp' or 'rvY...'),
* currency: ('USD', 'BTC', etc)
* },
* .
* .
* .
*
*
* // the following options are optional
* // by default it will return the gateway's current balance
*
* startTime : (any momentjs-readable date),
* endTime : (any momentjs-readable date),
* timeIncrement : (any of the following: "all", "year", "month", "day", "hour", "minute", "second") // defaults to 'all'
* }
*
*
curl -H "Content-Type: application/json" -X POST -d '{
"startTime" : "June 26, 2014 10:00 am",
"endTime" : "June 30, 2014 10:47 am",
"currencies" : [{"currency" : "BTC", "issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q"}],
"timeIncrement" : "hour"
}' http://localhost:5993/api/issuer_capitalization
curl -H "Content-Type: application/json" -X POST -d '{
"startTime" : "Mar 5, 2011 10:00 am",
"endTime" : "Mar 6, 2015 10:47 am",
"currencies" : [{"currency" : "USD", "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"}],
"timeIncrement" : "month"
}' http://localhost:5993/api/issuer_capitalization
curl -H "Content-Type: application/json" -X POST -d '{
"currencies" : [{"currency" : "USD", "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"}]
}' http://localhost:5993/api/issuer_capitalization
*/
function issuerCapitalization(params, callback) {
var error,
maxLimit = 500,
currencies = [];
//validate incoming currencies
if (Array.isArray(params.currencies)) {
params.currencies.forEach(function(c){
if (c.issuer) {
c.name = tools.getGatewayName(c.issuer);
c.hotwallets = tools.getHotWalletsForGateway(c.name);
currencies.push(c);
} else {
error = 'issuer is required: '+JSON.stringify(c);
return;
}
});
if (error) return callback(error);
} else return callback('please specify at least one issuer-currency pair');
if (currencies.length>25) return callback("Cannot retrieve more than 25 currencies");
if (!params.startTime) params.startTime = "Jan 1 2013 12:00+0:00";
//Parse start and end times
var time = tools.parseTimeRange(params.startTime, params.endTime, params.descending);
if (time.error) {
callback(time.error);
return;
}
var startTime = time.start;
var endTime = time.end;
// get capitalization data for each currency
async.map(currencies, function(c, asyncCallbackPair) {
var options = {
currency: c.currency,
issuer: c.issuer,
start: startTime,
end: endTime,
descending: false,
adjusted: true
};
if (!params.timeIncrement) {
options.limit = 1;
options.descending = true;
} else {
options.interval = params.timeIncrement;
options.descending = false;
}
hbase.getCapitalization(options, function(err, resp) {
if (err) {
asyncCallbackPair(err);
return;
}
resp.rows.forEach(function(row, i) {
resp.rows[i] = [
row.date,
row.amount
];
});
asyncCallbackPair(null, {
currency: c.currency,
issuer: c.issuer,
results: resp.rows
});
});
}, function(error, results){
if (error) return callback(error);
return callback(null, results); //final results from API call
});
}
module.exports = issuerCapitalization;