-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitstamp.js
370 lines (351 loc) · 13.6 KB
/
bitstamp.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"use strict";
// ---------------------------------------------------------------------------
const Exchange = require ('./base/Exchange')
const { ExchangeError, AuthenticationError } = require ('./base/errors')
// ---------------------------------------------------------------------------
module.exports = class bitstamp extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'bitstamp',
'name': 'Bitstamp',
'countries': 'GB',
'rateLimit': 1000,
'version': 'v2',
'hasCORS': false,
'hasFetchOrder': true,
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/27786377-8c8ab57e-5fe9-11e7-8ea4-2b05b6bcceec.jpg',
'api': 'https://www.bitstamp.net/api',
'www': 'https://www.bitstamp.net',
'doc': 'https://www.bitstamp.net/api',
},
'requiredCredentials': {
'apiKey': true,
'secret': true,
'uid': true,
},
'api': {
'public': {
'get': [
'order_book/{pair}/',
'ticker_hour/{pair}/',
'ticker/{pair}/',
'transactions/{pair}/',
'trading-pairs-info/',
],
},
'private': {
'post': [
'balance/',
'balance/{pair}/',
'user_transactions/',
'user_transactions/{pair}/',
'open_orders/all/',
'open_orders/{pair}',
'order_status/',
'cancel_order/',
'buy/{pair}/',
'buy/market/{pair}/',
'sell/{pair}/',
'sell/market/{pair}/',
'ltc_withdrawal/',
'ltc_address/',
'eth_withdrawal/',
'eth_address/',
'transfer-to-main/',
'transfer-from-main/',
'xrp_withdrawal/',
'xrp_address/',
'withdrawal/open/',
'withdrawal/status/',
'withdrawal/cancel/',
'liquidation_address/new/',
'liquidation_address/info/',
],
},
'v1': {
'post': [
'bitcoin_deposit_address/',
'unconfirmed_btc/',
'bitcoin_withdrawal/',
],
},
},
'fees': {
'trading': {
'tierBased': true,
'percentage': true,
'taker': 0.25 / 100,
'maker': 0.25 / 100,
'tiers': {
'taker': [
[0, 0.25 / 100],
[20000, 0.24 / 100],
[100000, 0.22 / 100],
[400000, 0.20 / 100],
[600000, 0.15 / 100],
[1000000, 0.14 / 100],
[2000000, 0.13 / 100],
[4000000, 0.12 / 100],
[20000000, 0.11 / 100],
[20000001, 0.10 / 100],
],
'maker': [
[0, 0.25 / 100],
[20000, 0.24 / 100],
[100000, 0.22 / 100],
[400000, 0.20 / 100],
[600000, 0.15 / 100],
[1000000, 0.14 / 100],
[2000000, 0.13 / 100],
[4000000, 0.12 / 100],
[20000000, 0.11 / 100],
[20000001, 0.10 / 100],
],
},
},
'funding': {
'tierBased': false,
'percentage': false,
'withdraw': {
'BTC': 0,
'LTC': 0,
'ETH': 0,
'XRP': 0,
'USD': 25,
'EUR': 0.90,
},
'deposit': {
'BTC': 0,
'LTC': 0,
'ETH': 0,
'XRP': 0,
'USD': 25,
'EUR': 0,
},
},
},
});
}
async fetchMarkets () {
let markets = await this.publicGetTradingPairsInfo ();
let result = [];
for (let i = 0; i < markets.length; i++) {
let market = markets[i];
let symbol = market['name'];
let [ base, quote ] = symbol.split ('/');
let id = market['url_symbol'];
let precision = {
'amount': market['base_decimals'],
'price': market['counter_decimals'],
};
let [ cost, currency ] = market['minimum_order'].split (' ');
let active = (market['trading'] == 'Enabled');
let lot = Math.pow (10, -precision['amount']);
result.push ({
'id': id,
'symbol': symbol,
'base': base,
'quote': quote,
'info': market,
'lot': lot,
'active': active,
'precision': precision,
'limits': {
'amount': {
'min': lot,
'max': undefined,
},
'price': {
'min': Math.pow (10, -precision['price']),
'max': undefined,
},
'cost': {
'min': parseFloat (cost),
'max': undefined,
},
},
});
}
return result;
}
async fetchOrderBook (symbol, params = {}) {
await this.loadMarkets ();
let orderbook = await this.publicGetOrderBookPair (this.extend ({
'pair': this.marketId (symbol),
}, params));
let timestamp = parseInt (orderbook['timestamp']) * 1000;
return this.parseOrderBook (orderbook, timestamp);
}
async fetchTicker (symbol, params = {}) {
await this.loadMarkets ();
let ticker = await this.publicGetTickerPair (this.extend ({
'pair': this.marketId (symbol),
}, params));
let timestamp = parseInt (ticker['timestamp']) * 1000;
let vwap = parseFloat (ticker['vwap']);
let baseVolume = parseFloat (ticker['volume']);
let quoteVolume = baseVolume * vwap;
return {
'symbol': symbol,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'high': parseFloat (ticker['high']),
'low': parseFloat (ticker['low']),
'bid': parseFloat (ticker['bid']),
'ask': parseFloat (ticker['ask']),
'vwap': vwap,
'open': parseFloat (ticker['open']),
'close': undefined,
'first': undefined,
'last': parseFloat (ticker['last']),
'change': undefined,
'percentage': undefined,
'average': undefined,
'baseVolume': baseVolume,
'quoteVolume': quoteVolume,
'info': ticker,
};
}
parseTrade (trade, market = undefined) {
let timestamp = undefined;
if ('date' in trade) {
timestamp = parseInt (trade['date']) * 1000;
} else if ('datetime' in trade) {
// timestamp = this.parse8601 (trade['datetime']);
timestamp = parseInt (trade['datetime']) * 1000;
}
let side = (trade['type'] == 0) ? 'buy' : 'sell';
let order = undefined;
if ('order_id' in trade)
order = trade['order_id'].toString ();
if ('currency_pair' in trade) {
if (trade['currency_pair'] in this.markets_by_id)
market = this.markets_by_id[trade['currency_pair']];
}
return {
'id': trade['tid'].toString (),
'info': trade,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'symbol': market['symbol'],
'order': order,
'type': undefined,
'side': side,
'price': parseFloat (trade['price']),
'amount': parseFloat (trade['amount']),
};
}
async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) {
await this.loadMarkets ();
let market = this.market (symbol);
let response = await this.publicGetTransactionsPair (this.extend ({
'pair': market['id'],
'time': 'minute',
}, params));
return this.parseTrades (response, market, since, limit);
}
async fetchBalance (params = {}) {
await this.loadMarkets ();
let balance = await this.privatePostBalance ();
let result = { 'info': balance };
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let lowercase = currency.toLowerCase ();
let total = lowercase + '_balance';
let free = lowercase + '_available';
let used = lowercase + '_reserved';
let account = this.account ();
if (free in balance)
account['free'] = parseFloat (balance[free]);
if (used in balance)
account['used'] = parseFloat (balance[used]);
if (total in balance)
account['total'] = parseFloat (balance[total]);
result[currency] = account;
}
return this.parseBalance (result);
}
async createOrder (symbol, type, side, amount, price = undefined, params = {}) {
await this.loadMarkets ();
let method = 'privatePost' + this.capitalize (side);
let order = {
'pair': this.marketId (symbol),
'amount': amount,
};
if (type == 'market')
method += 'Market';
else
order['price'] = price;
method += 'Pair';
let response = await this[method] (this.extend (order, params));
return {
'info': response,
'id': response['id'],
};
}
async cancelOrder (id, symbol = undefined, params = {}) {
await this.loadMarkets ();
return await this.privatePostCancelOrder ({ 'id': id });
}
parseOrderStatus (order) {
if ((order['status'] == 'Queue') || (order['status'] == 'Open'))
return 'open';
if (order['status'] == 'Finished')
return 'closed';
return order['status'];
}
async fetchOrderStatus (id, symbol = undefined) {
await this.loadMarkets ();
let response = await this.privatePostOrderStatus ({ 'id': id });
return this.parseOrderStatus (response);
}
async fetchMyTrades (symbol = undefined, since = undefined, limit = undefined, params = {}) {
await this.loadMarkets ();
let market = undefined;
if (symbol)
market = this.market (symbol);
let pair = market ? market['id'] : 'all';
let request = this.extend ({ 'pair': pair }, params);
let response = await this.privatePostOpenOrdersPair (request);
return this.parseTrades (response, market, since, limit);
}
async fetchOrder (id, symbol = undefined, params = {}) {
await this.loadMarkets ();
return await this.privatePostOrderStatus ({ 'id': id });
}
sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let url = this.urls['api'] + '/';
if (api != 'v1')
url += this.version + '/';
url += this.implodeParams (path, params);
let query = this.omit (params, this.extractParams (path));
if (api == 'public') {
if (Object.keys (query).length)
url += '?' + this.urlencode (query);
} else {
this.checkRequiredCredentials ();
let nonce = this.nonce ().toString ();
let auth = nonce + this.uid + this.apiKey;
let signature = this.encode (this.hmac (this.encode (auth), this.encode (this.secret)));
query = this.extend ({
'key': this.apiKey,
'signature': signature.toUpperCase (),
'nonce': nonce,
}, query);
body = this.urlencode (query);
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
};
}
return { 'url': url, 'method': method, 'body': body, 'headers': headers };
}
async request (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let response = await this.fetch2 (path, api, method, params, headers, body);
if ('status' in response)
if (response['status'] == 'error')
throw new ExchangeError (this.id + ' ' + this.json (response));
return response;
}
}