-
Notifications
You must be signed in to change notification settings - Fork 4
/
cryptowatch.js
224 lines (185 loc) · 4.3 KB
/
cryptowatch.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
const http = require("superagent");
const CW_STARTING_ALLOWANCE = 8000000000;
/**
* @class CryptoWatch
* Wrapper for cryptowat.ch api
*/
class CryptoWatch {
/**
* @constructor
*/
constructor(url = "https://api.cryptowat.ch") {
this.url = url;
this._allowance = CW_STARTING_ALLOWANCE;
}
/**
* Request helper function
* @private
*/
request(endpoint, params) {
return new Promise((resolve, reject) => {
const url = `${this.url}/${endpoint}`;
http
.get(url)
.query(params)
.end((err, res) => {
if (err) {
reject({
message: "Request failed",
error: String(err),
url
});
} else {
this._allowance = res.body.allowance.remaining;
resolve(res.body.result);
}
});
});
}
/**
* Gets remaining api allowance.
*/
allowance() {
return this._allowance;
}
/**
* Gets all assets (crypto or fiat currency).
*/
assets() {
return this.request("assets");
}
/**
* Gets an asset's information.
*
* @param {String} name
*/
asset(name) {
return this.request(`assets/${name}`);
}
/**
* Gets currency pairs info.
*/
pairs() {
return this.request("pairs");
}
/**
* Gets info for a currency pair.
*
* @param {String} name
*/
pair(name) {
return this.request(`pairs/${name}`);
}
/**
* Gets info for all the exchanges.
*
* @param {Boolean} [active] If true, only return active exchanges
*/
exchanges(active = true) {
return new Promise((resolve, reject) => {
this.request("exchanges")
.then(exchanges => {
if (active) {
exchanges = exchanges.filter(e => e.active);
}
resolve(exchanges);
})
.catch(reject);
});
}
/**
* Gets info for an exchange.
*
* @param {String} name
*/
exchange(name) {
return this.request(`exchanges/${name}`);
}
/**
* Gets info for markets. Optionally specify a single exchange.
*
* @param {String} [exchange] Optional. Supply a specific exchange to get
* markets for
*/
markets(exchange = "") {
let endpoint = "markets";
if (exchange.length > 0) {
endpoint = `${endpoint}/${exchange}`;
}
return this.request(endpoint);
}
/**
* Gets info for a market given an exchange and pair.
*
* @param {String} exchange
* @param {String} pair
*/
market(exchange, pair) {
return this.request(`markets/${exchange}/${pair}`);
}
/**
* Returns price for a given market and currency pair.
*
* @param {String} market
* @param {String} pair
*/
price(market, pair) {
return this.request(`markets/${market}/${pair}/price`);
}
/**
* Aggregate endpoint that returns the current price for all supported
* markets. Somes values may be out of date by a few seconds as results
* are cached.
*/
prices() {
return this.request("markets/prices");
}
/**
* Returns summary for a given market and currency pair.
*
* @param {String} market
* @param {String} pair
*/
summary(market, pair) {
return this.request(`markets/${market}/${pair}/summary`);
}
/**
* Aggregate endpoint that returns the market summary for all supported
* markets. Some values may be out of date by a few seconds as results are
* cached.
*/
summaries() {
return this.request("markets/summaries");
}
/**
* Returns trades for a given market and currency pair.
*
* @param {String} market
* @param {String} pair
* @param {Object} [params]
*/
trades(market, pair, params) {
return this.request(`markets/${market}/${pair}/trades`, params);
}
/**
* Returns the orderbook for a given market and currency pair.
*
* @param {String} market
* @param {String} pair
*/
orderbook(market, pair) {
return this.request(`markets/${market}/${pair}/orderbook`);
}
/**
* Returns a market’s OHLC candlestick data. Returns data as lists of lists
* of numbers for each time period integer.
*
* @param {String} market
* @param {String} pair
* @param {Object} [params]
*/
OHLC(market, pair, params) {
return this.request(`markets/${market}/${pair}/ohlc`, params);
}
}
module.exports = CryptoWatch;