forked from ccxt/ccxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlakebtc.js
326 lines (309 loc) · 11.8 KB
/
lakebtc.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
'use strict';
// ---------------------------------------------------------------------------
const Exchange = require ('./base/Exchange');
const { ExchangeError, AuthenticationError, BadSymbol, InvalidOrder, InsufficientFunds } = require ('./base/errors');
const Precise = require ('./base/Precise');
// ---------------------------------------------------------------------------
module.exports = class lakebtc extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'lakebtc',
'name': 'LakeBTC',
'countries': [ 'US' ],
'version': 'api_v2',
'rateLimit': 1000,
'has': {
'cancelOrder': true,
'CORS': true,
'createMarketOrder': false,
'createOrder': true,
'fetchBalance': true,
'fetchMarkets': true,
'fetchOrderBook': true,
'fetchTicker': true,
'fetchTickers': true,
'fetchTrades': true,
},
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/28074120-72b7c38a-6660-11e7-92d9-d9027502281d.jpg',
'api': 'https://api.lakebtc.com',
'www': 'https://www.lakebtc.com',
'doc': [
'https://www.lakebtc.com/s/api_v2',
'https://www.lakebtc.com/s/api',
],
},
'api': {
'public': {
'get': [
'bcorderbook',
'bctrades',
'ticker',
],
},
'private': {
'post': [
'buyOrder',
'cancelOrders',
'getAccountInfo',
'getExternalAccounts',
'getOrders',
'getTrades',
'openOrders',
'sellOrder',
],
},
},
'fees': {
'trading': {
'maker': 0.15 / 100,
'taker': 0.2 / 100,
},
},
'exceptions': {
'broad': {
'Signature': AuthenticationError,
'invalid symbol': BadSymbol,
'Volume doit': InvalidOrder,
'insufficient_balance': InsufficientFunds,
},
},
});
}
async fetchMarkets (params = {}) {
const response = await this.publicGetTicker (params);
const result = [];
const keys = Object.keys (response);
for (let i = 0; i < keys.length; i++) {
const id = keys[i];
const market = response[id];
const baseId = id.slice (0, 3);
const quoteId = id.slice (3, 6);
const base = baseId.toUpperCase ();
const quote = quoteId.toUpperCase ();
const symbol = base + '/' + quote;
result.push ({
'id': id,
'symbol': symbol,
'base': base,
'quote': quote,
'baseId': baseId,
'quoteId': quoteId,
'info': market,
'active': undefined,
'precision': this.precision,
'limits': this.limits,
});
}
return result;
}
async fetchBalance (params = {}) {
await this.loadMarkets ();
const response = await this.privatePostGetAccountInfo (params);
const balances = this.safeValue (response, 'balance', {});
const result = { 'info': response };
const currencyIds = Object.keys (balances);
for (let i = 0; i < currencyIds.length; i++) {
const currencyId = currencyIds[i];
const code = this.safeCurrencyCode (currencyId);
const account = this.account ();
account['total'] = this.safeString (balances, currencyId);
result[code] = account;
}
return this.parseBalance (result, false);
}
async fetchOrderBook (symbol, limit = undefined, params = {}) {
await this.loadMarkets ();
const request = {
'symbol': this.marketId (symbol),
};
const response = await this.publicGetBcorderbook (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, 'bid'),
'bidVolume': undefined,
'ask': this.safeNumber (ticker, 'ask'),
'askVolume': undefined,
'vwap': undefined,
'open': undefined,
'close': last,
'last': last,
'previousClose': undefined,
'change': undefined,
'percentage': undefined,
'average': undefined,
'baseVolume': this.safeNumber (ticker, 'volume'),
'quoteVolume': undefined,
'info': ticker,
};
}
async fetchTickers (symbols = undefined, params = {}) {
await this.loadMarkets ();
const response = await this.publicGetTicker (params);
const ids = Object.keys (response);
const result = {};
for (let i = 0; i < ids.length; i++) {
const marketId = ids[i];
const ticker = response[marketId];
const market = this.safeMarket (marketId);
const symbol = market['symbol'];
result[symbol] = this.parseTicker (ticker, market);
}
return this.filterByArray (result, 'symbol', symbols);
}
async fetchTicker (symbol, params = {}) {
await this.loadMarkets ();
const market = this.market (symbol);
const tickers = await this.publicGetTicker (params);
return this.parseTicker (tickers[market['id']], market);
}
parseTrade (trade, market = undefined) {
const timestamp = this.safeTimestamp (trade, 'date');
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));
let symbol = undefined;
if (market !== undefined) {
symbol = market['symbol'];
}
return {
'id': id,
'info': trade,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'symbol': symbol,
'order': undefined,
'type': undefined,
'side': undefined,
'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 = {
'symbol': market['id'],
};
const response = await this.publicGetBctrades (this.extend (request, params));
return this.parseTrades (response, market, since, limit);
}
async createOrder (symbol, type, side, amount, price = undefined, params = {}) {
await this.loadMarkets ();
if (type === 'market') {
throw new ExchangeError (this.id + ' allows limit orders only');
}
const method = 'privatePost' + this.capitalize (side) + 'Order';
const market = this.market (symbol);
const order = {
'params': [ price, amount, market['id'] ],
};
const response = await this[method] (this.extend (order, params));
return {
'info': response,
'id': this.safeString (response, 'id'),
};
}
async cancelOrder (id, symbol = undefined, params = {}) {
await this.loadMarkets ();
const request = {
'params': [ id ],
};
return await this.privatePostCancelOrder (this.extend (request, params));
}
nonce () {
return this.microseconds ();
}
sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let url = this.urls['api'] + '/' + this.version;
if (api === 'public') {
url += '/' + path;
if (Object.keys (params).length) {
url += '?' + this.urlencode (params);
}
} else {
this.checkRequiredCredentials ();
const nonce = this.nonce ();
const nonceAsString = nonce.toString ();
const requestId = this.seconds ();
let queryParams = '';
if ('params' in params) {
const paramsList = params['params'];
const stringParams = [];
for (let i = 0; i < paramsList.length; i++) {
let param = paramsList[i];
if (typeof paramsList !== 'string') {
param = param.toString ();
}
stringParams.push (param);
}
queryParams = stringParams.join (',');
body = {
'method': path,
'params': params['params'],
'id': requestId,
};
} else {
body = {
'method': path,
'params': '',
'id': requestId,
};
}
body = this.json (body);
let query = [
'tonce=' + nonceAsString,
'accesskey=' + this.apiKey,
'requestmethod=' + method.toLowerCase (),
'id=' + requestId.toString (),
'method=' + path,
'params=' + queryParams,
];
query = query.join ('&');
const signature = this.hmac (this.encode (query), this.encode (this.secret), 'sha1');
const auth = this.apiKey + ':' + signature;
const signature64 = this.decode (this.stringToBase64 (auth));
headers = {
'Json-Rpc-Tonce': nonceAsString,
'Authorization': 'Basic ' + signature64,
'Content-Type': 'application/json',
};
}
return { 'url': url, 'method': method, 'body': body, 'headers': headers };
}
handleErrors (code, reason, url, method, headers, body, response, requestHeaders, requestBody) {
if (response === undefined) {
return; // fallback to the default error handler
}
//
// {"error":"Failed to submit order: invalid symbol"}
// {"error":"Failed to submit order: La validation a échoué : Volume doit être supérieur ou égal à 1.0"}
// {"error":"Failed to submit order: insufficient_balance"}
//
const feedback = this.id + ' ' + body;
const error = this.safeString (response, 'error');
if (error !== undefined) {
this.throwBroadlyMatchedException (this.exceptions['broad'], error, feedback);
throw new ExchangeError (feedback); // unknown message
}
}
};