-
Notifications
You must be signed in to change notification settings - Fork 68
/
accountOrders.js
172 lines (145 loc) · 3.74 KB
/
accountOrders.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
'use strict';
var config = require('../../config')
var Logger = require('../../lib/logger');
var log = new Logger({scope : 'account orders'});
var request = require('request');
var smoment = require('../../lib/smoment');
var rippled = require('../../lib/rippled')
var hbase = require('../../lib/hbase')
function accountOrders(req, res) {
var options = {
ledger_index: req.query.ledger_index || req.query.ledger,
ledger_hash: req.query.ledger_hash,
closeTime: req.query.close_time || req.query.date,
account: req.params.address,
format: (req.query.format || 'json').toLowerCase(),
limit: req.query.limit || 200
};
if (!options.account) {
errorResponse({
error: 'account is required.',
code: 400
});
return;
}
// validate and fomat close time
if (options.closeTime) {
options.closeTime = smoment(options.closeTime);
if (options.closeTime) {
options.closeTime = options.closeTime.format();
} else {
errorResponse({
error: 'invalid date format',
code: 400
});
return;
}
}
// validate and format limit
if (options.limit && options.limit === 'all') {
options.limit = undefined;
} else {
options.limit = Number(options.limit);
if (isNaN(options.limit)) {
errorResponse({
error: 'invalid limit',
code: 400
});
return;
// max limit of 400
} else if (options.limit > 400) {
options.limit = 400
}
}
// if requesting latest ledger,
// add leeway to rippled request
// since it may not be perfectly
// in sync
if (!options.ledger_index &&
!options.ledger_hash &&
!options.closeTime) {
options.pad = 5;
}
log.info(options.account);
options.currency = req.query.currency;
options.limit = options.limit;
if (options.closeTime) {
hbase.getLedger(options, function(err, ledger) {
if (err) {
errorResponse(err);
return;
} else if (ledger) {
options.ledger_index = ledger.ledger_index;
options.closeTime = smoment(ledger.close_time).format()
getOrders(options)
} else {
errorResponse('ledger not found');
}
})
} else {
getOrders(options);
}
/**
* getOrders
* use ledger_index from getLedger api call
* to get orders using rippleAPI
*/
function getOrders(opts) {
rippled.getOrders({
account: opts.account,
ledger: opts.ledger_index,
limit: opts.limit
})
.then(function(resp) {
var results = {
result: 'success'
};
results.ledger_index = resp.ledger_index;
results.close_time = opts.closeTime;
results.limit = opts.limit;
results.orders = resp.orders;
successResponse(results, opts);
}).catch(function(e) {
if (e.message === 'Account not found.') {
errorResponse({
code: 404,
error: 'account not found'
});
} else if (e.message === 'ledgerNotFound') {
errorResponse({
code: 400,
error: 'the date provided is too old'
});
} else {
errorResponse(e.toString());
}
});
}
/**
* errorResponse
* return an error response
* @param {Object} err
*/
function errorResponse(err) {
var code = err.code || 500;
var message = err.error || 'unable to retrieve orders';
log.error(err.error || err);
res.status(code).json({
result: 'error',
message: message
});
}
/**
* successResponse
* return a successful response
* @param {Object} balances
*/
function successResponse(results, opts) {
if (opts.format === 'csv') {
res.csv(results.orders, opts.account + ' - orders.csv');
} else {
res.json(results);
}
}
}
module.exports = accountOrders;