forked from ccxt/ccxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtcbox.js
412 lines (390 loc) · 15.8 KB
/
btcbox.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
'use strict';
// ---------------------------------------------------------------------------
const Exchange = require ('./base/Exchange');
const { ExchangeError, InsufficientFunds, InvalidOrder, AuthenticationError, PermissionDenied, InvalidNonce, OrderNotFound, DDoSProtection } = require ('./base/errors');
const Precise = require ('./base/Precise');
// ---------------------------------------------------------------------------
module.exports = class btcbox extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'btcbox',
'name': 'BtcBox',
'countries': [ 'JP' ],
'rateLimit': 1000,
'version': 'v1',
'has': {
'cancelOrder': true,
'CORS': false,
'createOrder': true,
'fetchBalance': true,
'fetchOpenOrders': true,
'fetchOrder': true,
'fetchOrderBook': true,
'fetchOrders': true,
'fetchTicker': true,
'fetchTickers': false,
'fetchTrades': true,
},
'urls': {
'logo': 'https://user-images.githubusercontent.com/51840849/87327317-98c55400-c53c-11ea-9a11-81f7d951cc74.jpg',
'api': 'https://www.btcbox.co.jp/api',
'www': 'https://www.btcbox.co.jp/',
'doc': 'https://blog.btcbox.jp/en/archives/8762',
'fees': 'https://support.btcbox.co.jp/hc/en-us/articles/360001235694-Fees-introduction',
},
'api': {
'public': {
'get': [
'depth',
'orders',
'ticker',
],
},
'private': {
'post': [
'balance',
'trade_add',
'trade_cancel',
'trade_list',
'trade_view',
'wallet',
],
},
},
'markets': {
'BTC/JPY': { 'id': 'btc', 'symbol': 'BTC/JPY', 'base': 'BTC', 'quote': 'JPY', 'baseId': 'btc', 'quoteId': 'jpy', 'taker': 0.05 / 100, 'maker': 0.05 / 100 },
'ETH/JPY': { 'id': 'eth', 'symbol': 'ETH/JPY', 'base': 'ETH', 'quote': 'JPY', 'baseId': 'eth', 'quoteId': 'jpy', 'taker': 0.10 / 100, 'maker': 0.10 / 100 },
'LTC/JPY': { 'id': 'ltc', 'symbol': 'LTC/JPY', 'base': 'LTC', 'quote': 'JPY', 'baseId': 'ltc', 'quoteId': 'jpy', 'taker': 0.10 / 100, 'maker': 0.10 / 100 },
'BCH/JPY': { 'id': 'bch', 'symbol': 'BCH/JPY', 'base': 'BCH', 'quote': 'JPY', 'baseId': 'bch', 'quoteId': 'jpy', 'taker': 0.10 / 100, 'maker': 0.10 / 100 },
},
'exceptions': {
'104': AuthenticationError,
'105': PermissionDenied,
'106': InvalidNonce,
'107': InvalidOrder, // price should be an integer
'200': InsufficientFunds,
'201': InvalidOrder, // amount too small
'202': InvalidOrder, // price should be [0 : 1000000]
'203': OrderNotFound,
'401': OrderNotFound, // cancel canceled, closed or non-existent order
'402': DDoSProtection,
},
});
}
async fetchBalance (params = {}) {
await this.loadMarkets ();
const response = await this.privatePostBalance (params);
const result = { 'info': response };
const codes = Object.keys (this.currencies);
for (let i = 0; i < codes.length; i++) {
const code = codes[i];
const currency = this.currency (code);
const currencyId = currency['id'];
const free = currencyId + '_balance';
if (free in response) {
const account = this.account ();
const used = currencyId + '_lock';
account['free'] = this.safeString (response, free);
account['used'] = this.safeString (response, used);
result[code] = account;
}
}
return this.parseBalance (result, false);
}
async fetchOrderBook (symbol, limit = undefined, params = {}) {
await this.loadMarkets ();
const market = this.market (symbol);
const request = {};
const numSymbols = this.symbols.length;
if (numSymbols > 1) {
request['coin'] = market['baseId'];
}
const response = await this.publicGetDepth (this.extend (request, params));
return this.parseOrderBook (response, symbol);
}
parseTicker (ticker, market = undefined) {
const timestamp = this.milliseconds ();
let symbol = undefined;
if (market !== undefined) {
symbol = market['symbol'];
}
const last = this.safeNumber (ticker, 'last');
return {
'symbol': symbol,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'high': this.safeNumber (ticker, 'high'),
'low': this.safeNumber (ticker, 'low'),
'bid': this.safeNumber (ticker, 'buy'),
'bidVolume': undefined,
'ask': this.safeNumber (ticker, 'sell'),
'askVolume': undefined,
'vwap': undefined,
'open': undefined,
'close': last,
'last': last,
'previousClose': undefined,
'change': undefined,
'percentage': undefined,
'average': undefined,
'baseVolume': this.safeNumber (ticker, 'vol'),
'quoteVolume': this.safeNumber (ticker, 'volume'),
'info': ticker,
};
}
async fetchTicker (symbol, params = {}) {
await this.loadMarkets ();
const market = this.market (symbol);
const request = {};
const numSymbols = this.symbols.length;
if (numSymbols > 1) {
request['coin'] = market['baseId'];
}
const response = await this.publicGetTicker (this.extend (request, params));
return this.parseTicker (response, market);
}
parseTrade (trade, market = undefined) {
const timestamp = this.safeTimestamp (trade, 'date');
let symbol = undefined;
if (market !== undefined) {
symbol = market['symbol'];
}
const id = this.safeString (trade, 'tid');
const priceString = this.safeString (trade, 'price');
const amountString = this.safeString (trade, 'amount');
const price = this.parseNumber (priceString);
const amount = this.parseNumber (amountString);
const cost = this.parseNumber (Precise.stringMul (priceString, amountString));
const type = undefined;
const side = this.safeString (trade, 'type');
return {
'info': trade,
'id': id,
'order': undefined,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'symbol': symbol,
'type': type,
'side': side,
'takerOrMaker': undefined,
'price': price,
'amount': amount,
'cost': cost,
'fee': undefined,
};
}
async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) {
await this.loadMarkets ();
const market = this.market (symbol);
const request = {};
const numSymbols = this.symbols.length;
if (numSymbols > 1) {
request['coin'] = market['baseId'];
}
const response = await this.publicGetOrders (this.extend (request, params));
return this.parseTrades (response, market, since, limit);
}
async createOrder (symbol, type, side, amount, price = undefined, params = {}) {
await this.loadMarkets ();
const market = this.market (symbol);
const request = {
'amount': amount,
'price': price,
'type': side,
'coin': market['baseId'],
};
const response = await this.privatePostTradeAdd (this.extend (request, params));
//
// {
// "result":true,
// "id":"11"
// }
//
return this.parseOrder (response, market);
}
async cancelOrder (id, symbol = undefined, params = {}) {
await this.loadMarkets ();
// a special case for btcbox – default symbol is BTC/JPY
if (symbol === undefined) {
symbol = 'BTC/JPY';
}
const market = this.market (symbol);
const request = {
'id': id,
'coin': market['baseId'],
};
const response = await this.privatePostTradeCancel (this.extend (request, params));
//
// {"result":true, "id":"11"}
//
return this.parseOrder (response, market);
}
parseOrderStatus (status) {
const statuses = {
// TODO: complete list
'part': 'open', // partially or not at all executed
'all': 'closed', // fully executed
'cancelled': 'canceled',
'closed': 'closed', // never encountered, seems to be bug in the doc
'no': 'closed', // not clarified in the docs...
};
return this.safeString (statuses, status, status);
}
parseOrder (order, market = undefined) {
//
// {
// "id":11,
// "datetime":"2014-10-21 10:47:20",
// "type":"sell",
// "price":42000,
// "amount_original":1.2,
// "amount_outstanding":1.2,
// "status":"closed",
// "trades":[]
// }
//
const id = this.safeString (order, 'id');
const datetimeString = this.safeString (order, 'datetime');
let timestamp = undefined;
if (datetimeString !== undefined) {
timestamp = this.parse8601 (order['datetime'] + '+09:00'); // Tokyo time
}
const amount = this.safeNumber (order, 'amount_original');
const remaining = this.safeNumber (order, 'amount_outstanding');
const price = this.safeNumber (order, 'price');
// status is set by fetchOrder method only
let status = this.parseOrderStatus (this.safeString (order, 'status'));
// fetchOrders do not return status, use heuristic
if (status === undefined) {
if (remaining !== undefined && remaining === 0) {
status = 'closed';
}
}
const trades = undefined; // todo: this.parseTrades (order['trades']);
let symbol = undefined;
if (market !== undefined) {
symbol = market['symbol'];
}
const side = this.safeString (order, 'type');
return this.safeOrder ({
'id': id,
'clientOrderId': undefined,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'lastTradeTimestamp': undefined,
'amount': amount,
'remaining': remaining,
'filled': undefined,
'side': side,
'type': undefined,
'timeInForce': undefined,
'postOnly': undefined,
'status': status,
'symbol': symbol,
'price': price,
'stopPrice': undefined,
'cost': undefined,
'trades': trades,
'fee': undefined,
'info': order,
'average': undefined,
});
}
async fetchOrder (id, symbol = undefined, params = {}) {
await this.loadMarkets ();
// a special case for btcbox – default symbol is BTC/JPY
if (symbol === undefined) {
symbol = 'BTC/JPY';
}
const market = this.market (symbol);
const request = this.extend ({
'id': id,
'coin': market['baseId'],
}, params);
const response = await this.privatePostTradeView (this.extend (request, params));
return this.parseOrder (response, market);
}
async fetchOrdersByType (type, symbol = undefined, since = undefined, limit = undefined, params = {}) {
await this.loadMarkets ();
// a special case for btcbox – default symbol is BTC/JPY
if (symbol === undefined) {
symbol = 'BTC/JPY';
}
const market = this.market (symbol);
const request = {
'type': type, // 'open' or 'all'
'coin': market['baseId'],
};
const response = await this.privatePostTradeList (this.extend (request, params));
const orders = this.parseOrders (response, market, since, limit);
// status (open/closed/canceled) is undefined
// btcbox does not return status, but we know it's 'open' as we queried for open orders
if (type === 'open') {
for (let i = 0; i < orders.length; i++) {
orders[i]['status'] = 'open';
}
}
return orders;
}
async fetchOrders (symbol = undefined, since = undefined, limit = undefined, params = {}) {
return await this.fetchOrdersByType ('all', symbol, since, limit, params);
}
async fetchOpenOrders (symbol = undefined, since = undefined, limit = undefined, params = {}) {
return await this.fetchOrdersByType ('open', symbol, since, limit, params);
}
nonce () {
return this.milliseconds ();
}
sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let url = this.urls['api'] + '/' + this.version + '/' + path;
if (api === 'public') {
if (Object.keys (params).length) {
url += '?' + this.urlencode (params);
}
} else {
this.checkRequiredCredentials ();
const nonce = this.nonce ().toString ();
const query = this.extend ({
'key': this.apiKey,
'nonce': nonce,
}, params);
const request = this.urlencode (query);
const secret = this.hash (this.encode (this.secret));
query['signature'] = this.hmac (this.encode (request), this.encode (secret));
body = this.urlencode (query);
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
};
}
return { 'url': url, 'method': method, 'body': body, 'headers': headers };
}
handleErrors (httpCode, reason, url, method, headers, body, response, requestHeaders, requestBody) {
if (response === undefined) {
return; // resort to defaultErrorHandler
}
// typical error response: {"result":false,"code":"401"}
if (httpCode >= 400) {
return; // resort to defaultErrorHandler
}
const result = this.safeValue (response, 'result');
if (result === undefined || result === true) {
return; // either public API (no error codes expected) or success
}
const code = this.safeValue (response, 'code');
const feedback = this.id + ' ' + body;
this.throwExactlyMatchedException (this.exceptions, code, feedback);
throw new ExchangeError (feedback); // unknown message
}
async request (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let response = await this.fetch2 (path, api, method, params, headers, body);
if (typeof response === 'string') {
// sometimes the exchange returns whitespace prepended to json
response = this.strip (response);
if (!this.isJsonEncodedObject (response)) {
throw new ExchangeError (this.id + ' ' + response);
}
response = JSON.parse (response);
}
return response;
}
};