This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
totalNetworkValue.js
173 lines (143 loc) · 4.63 KB
/
totalNetworkValue.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
var winston = require('winston');
var moment = require('moment');
var ripple = require('ripple-lib');
var async = require('async');
var networkValue = require('../library/metrics/networkValue');
var utils = require('../library/utils');
/**
* totalNetworkValue:
*
* total value of currencies for the top gateways on the ripple network,
* normalized to a specific currrency.
*
* request :
*
* {
* time : "2014-03-13T20:39:26+00:00" //time of desired snapshot
* exchange : { // optional, defaults to XRP
* currency : (XRP, USD, BTC, etc.),
* issuer : "rAusZ...." // optional, required if currency != XRP
* }
* }
*
* response :
*
* {
* time : "2014-03-13T20:39:26+00:00", //time of desired snapshot
* exchange : {
* currency : "USD", //exchange currency
* issuer : "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B" //exchange issuer
* },
* exchangeRate : 0.014301217579817786, //exchange rate
* total : 726824.6504823748, //total network value in exchange currency
* components : [ //list of component currencies
* {
* currency : "USD",
* issuer : "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
* amount : 27606.296227064257,
* rate : 1,
* convertedAmount : 27606.296227064257
* },
* .
* .
* .
* .
* ]
* }
*
curl -H "Content-Type: application/json" -X POST -d '{
}' http://localhost:5993/api/totalnetworkvalue
curl -H "Content-Type: application/json" -X POST -d '{
"exchange" : {"currency": "USD", "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"}
}' http://localhost:5993/api/totalnetworkvalue
curl -H "Content-Type: application/json" -X POST -d '{
"exchange" : {"currency": "USD", "issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"},
"time" : "2014-03-13T20:39:26+00:00"
}' http://localhost:5993/api/totalnetworkvalue
*
*/
function totalNetworkValue(params, callback) {
var viewOpts = {};
var ex = params.exchange || {currency:'XRP'};
var time = moment.utc(params.time);
var cacheKey;
if (!time.isValid()) return callback('invalid time: ' + params.time);
if (typeof ex != 'object') {
return callback('invalid exchange currency');
} else if (!ex.currency) {
return callback('exchange currency is required');
} else if (typeof ex.currency != 'string') {
return callback('invalid exchange currency');
} else if (ex.currency.toUpperCase() != "XRP" && !ex.issuer) {
return callback('exchange issuer is required');
} else if (ex.currency == "XRP" && ex.issuer) {
return callback('XRP cannot have an issuer');
}
rowkey = 'network_value';
if (!params.time) {
rowkey += '|live';
} else {
time.startOf('day');
rowkey += '|' + utils.formatTime(time);
}
hbase.getRow({
table: 'agg_metrics',
rowkey: rowkey
}, function(err, row) {
var options = {
end : moment.utc(time),
ex : ex
};
if (row) {
row.components = JSON.parse(row.components);
row.exchange = JSON.parse(row.exchange);
row.total = parseFloat(row.total);
row.exchangeRate = parseFloat(row.exchangeRate);
handleResponse (options, row, callback);
} else {
callback(err, row);
}
});
function handleResponse (options, row, callback) {
var params;
if (options.ex.currency === 'XRP') {
callback(null, row);
return;
}
row.exchange = options.ex;
/*
//check for final rate within row first
for(var i=0; i<row.components.length; i++) {
if (row.components[i].currency === options.ex.currency &&
row.components[i].issuer === options.ex.issuer) {
//normalize and return
normalize(row.components[i].rate, row, callback);
return;
}
}
*/
utils.getConversion({
currency : options.ex.currency,
issuer : options.ex.issuer,
start : moment.utc(options.end).subtract(1, 'day'),
end : options.end,
interval : '1day'
}, function (err, rate) {
if (err) {
return callback(err);
}
normalize(rate, row, callback);
});
function normalize (rate, row, callback) {
row.total = 0;
row.components.forEach(function(c, index) {
c.convertedAmount *= rate;
c.rate = c.rate ? rate / c.rate : 0;
row.total += c.convertedAmount;
});
row.exchangeRate = rate;
callback(null, row);
}
}
}
module.exports = totalNetworkValue;