-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitflyer.js
383 lines (363 loc) · 13.9 KB
/
bitflyer.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
'use strict';
// ---------------------------------------------------------------------------
const Exchange = require ('./base/Exchange');
const { ExchangeError, OrderNotFound } = require ('./base/errors');
// ---------------------------------------------------------------------------
module.exports = class bitflyer extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'bitflyer',
'name': 'bitFlyer',
'countries': 'JP',
'version': 'v1',
'rateLimit': 1000, // their nonce-timestamp is in seconds...
'has': {
'CORS': false,
'withdraw': true,
'fetchOrders': true,
'fetchOrder': true,
'fetchOpenOrders': 'emulated',
'fetchClosedOrders': 'emulated',
},
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/28051642-56154182-660e-11e7-9b0d-6042d1e6edd8.jpg',
'api': 'https://api.bitflyer.jp',
'www': 'https://bitflyer.jp',
'doc': 'https://bitflyer.jp/API',
},
'api': {
'public': {
'get': [
'getmarkets/usa', // new (wip)
'getmarkets/eu', // new (wip)
'getmarkets', // or 'markets'
'getboard', // ...
'getticker',
'getexecutions',
'gethealth',
'getchats',
],
},
'private': {
'get': [
'getpermissions',
'getbalance',
'getcollateral',
'getcollateralaccounts',
'getaddresses',
'getcoinins',
'getcoinouts',
'getbankaccounts',
'getdeposits',
'getwithdrawals',
'getchildorders',
'getparentorders',
'getparentorder',
'getexecutions',
'getpositions',
'gettradingcommission',
],
'post': [
'sendcoin',
'withdraw',
'sendchildorder',
'cancelchildorder',
'sendparentorder',
'cancelparentorder',
'cancelallchildorders',
],
},
},
'fees': {
'trading': {
'maker': 0.25 / 100,
'taker': 0.25 / 100,
},
},
});
}
async fetchMarkets () {
let jp_markets = await this.publicGetGetmarkets ();
let us_markets = await this.publicGetGetmarketsUsa ();
let eu_markets = await this.publicGetGetmarketsEu ();
let markets = this.arrayConcat (jp_markets, us_markets);
markets = this.arrayConcat (markets, eu_markets);
let result = [];
for (let p = 0; p < markets.length; p++) {
let market = markets[p];
let id = market['product_code'];
let currencies = id.split ('_');
let base = undefined;
let quote = undefined;
let symbol = id;
let numCurrencies = currencies.length;
if (numCurrencies === 1) {
base = symbol.slice (0, 3);
quote = symbol.slice (3, 6);
} else if (numCurrencies === 2) {
base = currencies[0];
quote = currencies[1];
symbol = base + '/' + quote;
} else {
base = currencies[1];
quote = currencies[2];
}
result.push ({
'id': id,
'symbol': symbol,
'base': base,
'quote': quote,
'info': market,
});
}
return result;
}
async fetchBalance (params = {}) {
await this.loadMarkets ();
let response = await this.privateGetGetbalance ();
let balances = {};
for (let b = 0; b < response.length; b++) {
let account = response[b];
let currency = account['currency_code'];
balances[currency] = account;
}
let result = { 'info': response };
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let account = this.account ();
if (currency in balances) {
account['total'] = balances[currency]['amount'];
account['free'] = balances[currency]['available'];
account['used'] = account['total'] - account['free'];
}
result[currency] = account;
}
return this.parseBalance (result);
}
async fetchOrderBook (symbol, limit = undefined, params = {}) {
await this.loadMarkets ();
let orderbook = await this.publicGetGetboard (this.extend ({
'product_code': this.marketId (symbol),
}, params));
return this.parseOrderBook (orderbook, undefined, 'bids', 'asks', 'price', 'size');
}
async fetchTicker (symbol, params = {}) {
await this.loadMarkets ();
let ticker = await this.publicGetGetticker (this.extend ({
'product_code': this.marketId (symbol),
}, params));
let timestamp = this.parse8601 (ticker['timestamp']);
let last = parseFloat (ticker['ltp']);
return {
'symbol': symbol,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'high': undefined,
'low': undefined,
'bid': parseFloat (ticker['best_bid']),
'bidVolume': undefined,
'ask': parseFloat (ticker['best_ask']),
'askVolume': undefined,
'vwap': undefined,
'open': undefined,
'close': last,
'last': last,
'previousClose': undefined,
'change': undefined,
'percentage': undefined,
'average': undefined,
'baseVolume': parseFloat (ticker['volume_by_product']),
'quoteVolume': undefined,
'info': ticker,
};
}
parseTrade (trade, market = undefined) {
let side = undefined;
let order = undefined;
if ('side' in trade)
if (trade['side']) {
side = trade['side'].toLowerCase ();
let id = side + '_child_order_acceptance_id';
if (id in trade)
order = trade[id];
}
let timestamp = this.parse8601 (trade['exec_date']);
return {
'id': trade['id'].toString (),
'info': trade,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'symbol': market['symbol'],
'order': order,
'type': undefined,
'side': side,
'price': trade['price'],
'amount': trade['size'],
};
}
async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) {
await this.loadMarkets ();
let market = this.market (symbol);
let response = await this.publicGetGetexecutions (this.extend ({
'product_code': market['id'],
}, params));
return this.parseTrades (response, market, since, limit);
}
async createOrder (symbol, type, side, amount, price = undefined, params = {}) {
await this.loadMarkets ();
let order = {
'product_code': this.marketId (symbol),
'child_order_type': type.toUpperCase (),
'side': side.toUpperCase (),
'price': price,
'size': amount,
};
let result = await this.privatePostSendchildorder (this.extend (order, params));
return {
'info': result,
'id': result['child_order_acceptance_id'],
};
}
async cancelOrder (id, symbol = undefined, params = {}) {
if (typeof symbol === 'undefined')
throw new ExchangeError (this.id + ' cancelOrder() requires a symbol argument');
await this.loadMarkets ();
return await this.privatePostCancelchildorder (this.extend ({
'product_code': this.marketId (symbol),
'child_order_acceptance_id': id,
}, params));
}
parseOrderStatus (status) {
let statuses = {
'ACTIVE': 'open',
'COMPLETED': 'closed',
'CANCELED': 'canceled',
'EXPIRED': 'canceled',
'REJECTED': 'canceled',
};
if (status in statuses)
return statuses[status];
return status.toLowerCase ();
}
parseOrder (order, market = undefined) {
let timestamp = this.parse8601 (order['child_order_date']);
let amount = this.safeFloat (order, 'size');
let remaining = this.safeFloat (order, 'outstanding_size');
let filled = this.safeFloat (order, 'executed_size');
let price = this.safeFloat (order, 'price');
let cost = price * filled;
let status = this.parseOrderStatus (order['child_order_state']);
let type = order['child_order_type'].toLowerCase ();
let side = order['side'].toLowerCase ();
let symbol = undefined;
if (typeof market === 'undefined') {
let marketId = this.safeString (order, 'product_code');
if (typeof marketId !== 'undefined') {
if (marketId in this.markets_by_id)
market = this.markets_by_id[marketId];
}
}
if (typeof market !== 'undefined')
symbol = market['symbol'];
let fee = undefined;
let feeCost = this.safeFloat (order, 'total_commission');
if (typeof feeCost !== 'undefined') {
fee = {
'cost': feeCost,
'currency': undefined,
'rate': undefined,
};
}
return {
'id': order['child_order_acceptance_id'],
'info': order,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'status': status,
'symbol': symbol,
'type': type,
'side': side,
'price': price,
'cost': cost,
'amount': amount,
'filled': filled,
'remaining': remaining,
'fee': fee,
};
}
async fetchOrders (symbol = undefined, since = undefined, limit = 100, params = {}) {
if (typeof symbol === 'undefined')
throw new ExchangeError (this.id + ' fetchOrders() requires a symbol argument');
await this.loadMarkets ();
let market = this.market (symbol);
let request = {
'product_code': market['id'],
'count': limit,
};
let response = await this.privateGetGetchildorders (this.extend (request, params));
let orders = this.parseOrders (response, market, since, limit);
if (symbol)
orders = this.filterBy (orders, 'symbol', symbol);
return orders;
}
async fetchOpenOrders (symbol = undefined, since = undefined, limit = 100, params = {}) {
params['child_order_state'] = 'ACTIVE';
return this.fetchOrders (symbol, since, limit, params);
}
async fetchClosedOrders (symbol = undefined, since = undefined, limit = 100, params = {}) {
params['child_order_state'] = 'COMPLETED';
return this.fetchOrders (symbol, since, limit, params);
}
async fetchOrder (id, symbol = undefined, params = {}) {
if (typeof symbol === 'undefined')
throw new ExchangeError (this.id + ' fetchOrder() requires a symbol argument');
let orders = await this.fetchOrders (symbol);
let ordersById = this.indexBy (orders, 'id');
if (id in ordersById)
return ordersById[id];
throw new OrderNotFound (this.id + ' No order found with id ' + id);
}
async withdraw (currency, amount, address, tag = undefined, params = {}) {
this.checkAddress (address);
await this.loadMarkets ();
let response = await this.privatePostWithdraw (this.extend ({
'currency_code': currency,
'amount': amount,
// 'bank_account_id': 1234,
}, params));
return {
'info': response,
'id': response['message_id'],
};
}
sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let request = '/' + this.version + '/';
if (api === 'private')
request += 'me/';
request += path;
if (method === 'GET') {
if (Object.keys (params).length)
request += '?' + this.urlencode (params);
}
let url = this.urls['api'] + request;
if (api === 'private') {
this.checkRequiredCredentials ();
let nonce = this.nonce ().toString ();
let auth = [ nonce, method, request ].join ('');
if (Object.keys (params).length) {
if (method !== 'GET') {
body = this.json (params);
auth += body;
}
}
headers = {
'ACCESS-KEY': this.apiKey,
'ACCESS-TIMESTAMP': nonce,
'ACCESS-SIGN': this.hmac (this.encode (auth), this.encode (this.secret)),
'Content-Type': 'application/json',
};
}
return { 'url': url, 'method': method, 'body': body, 'headers': headers };
}
};