-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIonomy.js
293 lines (242 loc) · 8.18 KB
/
Ionomy.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
/* eslint-disable class-methods-use-this */
const axios = require('axios').default;
const crypto = require('crypto');
const https = require('https');
class Ionomy {
/**
* @constructor
* @param {Object} options - API configs
* @param {String} options.api - Ionomy API base URL
* @param {String} options.apiKey - Ionomy API key
* @param {String} options.apiSecret - Ionomy API secret
* @param {Boolean} options.keepAlive
*/
constructor({
api = null, apiKey = null, apiSecret = null, keepAlive = true,
} = {}) {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.client = axios.create({
baseURL: (api) || 'https://ionomy.com/api/v1/',
httpsAgent: new https.Agent({ keepAlive }),
});
}
requestSignature(path, params, timestamp) {
const query = new URLSearchParams(params).toString();
const url = `${this.client.defaults.baseURL}${path}${(query) ? `?${query}` : ''}`;
const hmac = crypto.createHmac('sha512', this.apiSecret);
return hmac.update(url + timestamp).digest('hex');
}
sanitizeParams(params = {}) {
const obj = {};
Object.keys(params).forEach((key) => {
if (params[key] !== undefined) obj[key] = params[key];
});
return obj;
}
async request(endpoint, params = {}) {
let headers = {};
// eslint-disable-next-line no-param-reassign
params = this.sanitizeParams(params);
if (this.apiKey && this.apiSecret) {
const timestamp = Math.floor(new Date() / 1000);
const hmac = this.requestSignature(endpoint, params, timestamp);
headers = {
'api-auth-time': timestamp,
'api-auth-key': this.apiKey,
'api-auth-token': hmac,
};
}
const { data } = await this.client.get(`${endpoint}`, { params, headers });
if (!data.success) {
throw new Error(data.message);
}
return data.data;
}
// PUBLIC
/**
* Returns all available markets
* @return {Promise}
*/
markets() {
return this.request('public/markets');
}
/**
* Returns available currencies
*/
currencies() {
return this.request('public/currencies');
}
/**
* Returns order book
* @param {Object} options - Order book options
* @param {String} options.market - Market name
* @param {String} options.type - Order type. Can be one of `ask`, `bid`, `both`
*/
orderBook({ market, type = 'both' }) {
if (!market) throw new Error('market is required');
if (!['ask', 'bid', 'both'].includes(type)) throw new Error('type must be one of: asks, bids, both');
return this.request('public/orderbook', { market, type });
}
/**
* Returns market summaries
*/
marketSummaries() {
return this.request('public/markets-summaries');
}
/**
* Returns market summary of the provided market
* @param {String} market - Market name
*/
marketSummary(market) {
if (!market) throw new Error('market is required');
return this.request('public/market-summary', { market });
}
/**
* Returns market history of the provided market
* @param {String} market - Market name
*/
marketHistory(market) {
if (!market) throw new Error('market is required');
return this.request('public/market-history', { market });
}
// MARKET
/**
* Places a limit buy order
* @param {Object} options - Buy order options
* @param {String} options.market - An unique identifier of the market. Example: `btc-hive`
* @param {Number|String} options.amount - Amount to buy. Example: `1.00`
* @param {Number|String} options.price - Price. Example: `1.00`
* @return {Promise<JSON>} orderId
*/
limitBuy({ market, amount, price }) {
if (!market) throw new Error('market is required');
if (!amount) throw new Error('amount is required');
if (!price) throw new Error('price is required');
const params = {
market,
amount: parseFloat(amount).toFixed(8),
price: parseFloat(price).toFixed(8),
};
return this.request('market/buy-limit', params);
}
/**
* Places a limit sell order
* @param {Object} options - Sell order options
* @param {String} options.market - An unique identifier of the market. Example: `btc-hive`
* @param {Number|String} options.amount - Amount to sell. Example: `1.00`
* @param {Number|String} options.price - Price. Example: `1.00`
* @return {Promise<JSON>} orderId
*/
limitSell({ market, amount, price }) {
if (!market) throw new Error('market is required');
if (!amount) throw new Error('amount is required');
if (!price) throw new Error('price is required');
const params = {
market,
amount: parseFloat(amount).toFixed(8),
price: parseFloat(price).toFixed(8),
};
return this.request('market/sell-limit', params);
}
/**
* Cancels an order
* @param {String} orderId - An unique order ID. Example: `5b8e8c980e454f2b807863ee`
* @return {Promise}
*/
cancelOrder(orderId) {
if (!orderId) throw new Error('orderId is required');
return this.request('market/cancel-order', { orderId });
}
/**
* Fetches open orders for a market
* @param {String} market - An unique identifier of the market. Example: `btc-hive`
* @return {Promise<JSON>}
*/
openOrders(market) {
if (!market) throw new Error('market is required');
return this.request('market/open-orders', { market });
}
// ACCOUNT
/**
* Fetches all balances for the account
* @return {Promise<JSON>}
*/
balances() {
return this.request('account/balances');
}
/**
* Fetches balance for the specified currency
* @param {String} currency - An unique identifier of the currency. Example - `hive`
* @return {Promise<JSON>}
*/
balance(currency) {
if (!currency) throw new Error('currency is required');
return this.request('account/balance', { currency });
}
/**
* Fetches deposit address for the specified currency
* @param {String} currency - An unique identifier of the currency. Example - `hive`
* @return {Promise<JSON>}
*/
depositAddress(currency) {
if (!currency) throw new Error('currency is required');
return this.request('account/deposit-address', { currency });
}
/**
* Fetches deposit history for the specified currency
* @param {String} currency - An unique identifier of the currency. Example - `hive`
* @return {Promise<JSON>}
*/
depositHistory(currency) {
if (!currency) throw new Error('currency is required');
return this.request('account/deposit-history', { currency });
}
/**
* Places a withdrawal request
* @param {Object} options - Withdrawal options
* @param {String} options.currency - An unique identifier of the currency. Example - `hive`
* @param {Number|String} options.amount - Amount to withdraw. Example: `1.00`
* @param {String} options.address - Wallet address. Example: `7ea4b0cb402320effd4309683290fdc5`
* @return {Promise<JSON>}
*/
withdraw({ currency, amount, address }) {
if (!currency) throw new Error('currency is required');
if (!amount) throw new Error('amount is required');
if (!address) throw new Error('address is required');
const params = {
currency,
amount: parseFloat(amount).toFixed(8),
address,
};
return this.request('account/withdraw', params);
}
/**
* Fetches withdrawal history for the specified currency
* @param {String} currency - An unique identifier of the currency. Example - `hive`
* @return {Promise<JSON>}
*/
withdrawalHistory(currency) {
if (!currency) throw new Error('currency is required');
return this.request('account/withdrawal-history', { currency });
}
/**
* Fetches order status
* @param {String} orderId - An unique order ID. Example: `5b8e8c980e454f2b807863ee`
* @return {Promise<JSON>}
*/
order(orderId) {
if (!orderId) throw new Error('orderId is required');
return this.request('account/order', { orderId });
}
/**
* Fetches order history for the specified currency
* @param {String} market - An unique identifier of the market. Example: `btc-hive`
* @return {Promise<JSON>}
*/
orderHistory(market) {
if (!market) throw new Error('market is required');
return this.request('account/order-history', { market });
}
}
module.exports = Ionomy;