-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccountOffersExercised.js
304 lines (253 loc) · 8.21 KB
/
accountOffersExercised.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
var winston = require('winston'),
moment = require('moment'),
_ = require('lodash'),
tools = require('../utils');
/*
* accountOffersExercised:
*
* list of offers excercised for a given account. providing a time increment or reduce option
* results in a count of transactions for the given interval or time period
*
* max returned results : 500
*
* request:
*
* {
* account : "r9aZAsv...." //required
* startTime : (any momentjs-readable date), // optional, defaults to 30 days before end time
* endTime : (any momentjs-readable date), // optional, defaults to now
* descending : true/false, // optional, defaults to true
* limit : limit the number of responses, ignored if time increment is set
* offset : offset by n transactions for pagination
* format : 'json', 'csv' // optional
* }
*
* response (default):
*
* [
* [
* "baseCurrency",
* "baseIssuer",
* "baseAmount",
* "counterCurrency",
* "counterIssuer",
* "counterAmount",
* "type",
* "rate",
* "counterparty",
* "time",
* "txHash",
* "ledgerIndex"
* ],
* [
* "BTC",
* "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
* 0.44114996160201914,
* "XDV",
* null,
* 19220.903827,
* "buy",
* 0.00002295157218269451,
* "rsYxmEL..........",
* "2014-03-18T22:15:40+00:00",
* "531025.....",
* 5592404
* ],
* ....
* ....
* ....
* ]
*
*
* response (json):
*
* {
* "account": "rZiaVx2.........",
* "timeRetrieved": "2014-04-04T19:13:45+00:00",
* "startTime": "2014-01-01T18:00:00+00:00",
* "endTime": "2015-01-10T18:00:00+00:00",
* "results": [
* {
* "base": {
* "currency": "BTC",
* "issuer": "rJHygWcTLVpSXkowott6kzgZU6viQSVYM1",
* "amount": 0.0008549999942999986
* },
* "counter": {
* "currency": "XDV",
* "issuer": null,
* "amount": 37.449374
* },
* "type": "buy",
* "rate": 0.00002283082206661181,
* "counterparty": "rPEZy......",
* "time": "2014-03-18T22:15:40+00:00",
* "txHash": "531025E..........",
* "ledgerIndex": 5592404
* },
* ...
* ...
* ...
* ]
* }
*
* response (reduce = true):
*
* [
* ["time", "count"],
* ["2014-03-18T22:00:00+00:00", 32],
* ["2014-04-03T16:00:00+00:00", 1],
* ["2014-04-03T18:00:00+00:00", 2],
* ...
* ...
* ...
* ]
*
*
*
curl -H "Content-Type: application/json" -X POST -d '{
"account" : "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
"format" : "json",
"timeIncrement" : "hour"
}' http://localhost:5993/api/accountOffersExercised
curl -H "Content-Type: application/json" -X POST -d '{
"account" : "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
"format" : "json",
"startTime" : "jan 1, 2014 10:00 am",
"endTime" : "jan 10, 2015 10:00 am"
}' http://localhost:5993/api/accountOffersExercised
curl -H "Content-Type: application/json" -X POST -d '{
"account" : "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
"timeIncrement" : "day"
}' http://localhost:5993/api/accountOffersExercised
curl -H "Content-Type: application/json" -X POST -d '{
"account" : "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV"
}' http://localhost:5993/api/accountOffersExercised
curl -H "Content-Type: application/json" -X POST -d '{
"account" : "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV",
"limit" : 5,
"offset" : 10,
"format" : "csv"
}' http://localhost:5993/api/accountOffersExercised
*
*
*/
function accountOffersExercised (params, callback, unlimit) {
var account = params.account,
limit = params.limit ? parseInt(params.limit, 10) : 0,
offset = params.offset ? parseInt(params.offset, 10) : 0,
maxLimit = unlimit ? Infinity : 500,
viewOpts = {},
intervalCount;
if (!account) return callback("Please specify an account");
if (!limit || limit>maxLimit) limit = maxLimit === Infinity ? null : maxLimit;
//Parse start and end times
var range = tools.parseTimeRange(params.startTime, params.endTime, params.descending);
if (range.error) return callback(range.error);
if (!range.end) range.end = moment.utc();
if (!range.start) range.start = moment.utc(range.end).subtract(30, "days");
// set startkey and endkey for couchdb query
viewOpts.startkey = [account].concat(range.start.toArray().slice(0,6));
viewOpts.endkey = [account].concat(range.end.toArray().slice(0,6));
if (params.descending) viewOpts.descending = true;
//parse time increment and time multiple
var results = tools.parseTimeIncrement(params.timeIncrement);
//set reduce option only if its false
if (results.group_level) viewOpts.group_level = results.group_level + 2;
else if (!params.reduce) viewOpts.reduce = false;
if (viewOpts.reduce===false) {
if (limit && !isNaN(limit)) viewOpts.limit = limit;
if (offset && !isNaN(offset)) viewOpts.skip = offset;
}
if (results.group !== false) {
intervalCount = tools.countIntervals(range.start, range.end, results.name);
if (intervalCount>maxLimit) {
return callback("Please specify a smaller time range or larger interval");
}
}
viewOpts.stale = "ok"; //dont wait for updates
db.view('accountOffersExercised', 'v1', viewOpts, function(error, couchRes) {
if (error) return callback ('CouchDB - ' + error);
handleResponse(couchRes.rows);
});
/*
* handleResponse - format the data according to the requirements
* of the request and return it to the caller.
*
*/
function handleResponse (rows) {
if (params.format === 'json') {
// send as an array of json objects
var apiRes = {};
apiRes.account = account;
apiRes.startTime = range.start.format();
apiRes.endTime = range.end.format();
apiRes.timeIncrement = params.timeIncrement;
apiRes.results = [];
if (viewOpts.reduce === false) {
rows.forEach(function(d){
apiRes.results.push({
base : {
currency : d.value[0],
issuer : d.value[1],
amount : d.value[2]
},
counter : {
currency : d.value[3],
issuer : d.value[4],
amount : d.value[5]
},
type : d.value[6],
rate : d.value[7],
counterparty : d.value[8],
time : moment.utc(d.value[9]).format(),
txHash : d.value[10],
ledgerIndex : parseInt(d.id, 10)
});
});
} else {
rows.forEach(function(d){
apiRes.results.push({
time : moment.utc(d.key.slice(1)).format(),
count : d.value
});
});
}
if (params.timeIncrement) apiRes.timeIncrement = params.timeIncrement;
return callback(null, apiRes);
} else {
var data = [], keys = {}, nKeys = 0;
if (viewOpts.reduce === false) {
for (var i=0; i<rows.length; i++) {
rows[i].value[9] = moment.utc(rows[i].value[9]).format(),
rows[i].value[11] = parseInt(rows[i].id, 10) //ledger_index
rows[i] = rows[i].value;
}
rows.unshift(["baseCurrency", "baseIssuer", "baseAmount",
"counterCurrency", "counterIssuer", "counterAmount",
"type", "rate", "counterparty",
"time", "txHash", "ledgerIndex"
]);
} else {
for (var j=0; j<rows.length; j++) {
rows[j] = [
moment.utc(rows[j].key.slice(1)).format(),
rows[j].value
]
}
rows.unshift(["time","count"]);
}
if (params.format === 'csv') {
var csvStr = _.map(rows, function(row){
return row.join(', ');
}).join('\n');
// provide output as CSV
return callback(null, csvStr);
} else {
//no format or incorrect format specified
return callback(null, rows);
}
}
}
}
module.exports = accountOffersExercised;