-
Notifications
You must be signed in to change notification settings - Fork 1
/
BitFinex.php
582 lines (475 loc) · 14.2 KB
/
BitFinex.php
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
/**
* Class BitFinex
*/
class BitFinex
{
/**
* @var string BitFinex API Secret
*/
protected $apiSecret;
/**
* @var string BitFinex API Key
*/
protected $apiKey;
/**
* BitFinex default url
*/
private const BITFINEX_API_URL = 'https://api.bitfinex.com';
/**
* Version for the BitFinex API. In case of doubt, leave it at v1.
*/
private const BITFINEX_API_VERSION = 'v1';
/**
* Constants to use on the cURL call
*/
private const PUBLIC_REQUEST = 'public';
private const PRIVATE_REQUEST = 'private';
/**
* BitFinex constructor.
* @param $apiKey string
* @param $apiSecret string
*/
public function __construct($apiKey, $apiSecret)
{
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
}
/**
* Get Book
*
* @param string $symbol
* @param array $data
* @return mixed
*/
public function getBook($symbol = 'BTCUSD', $data = [])
{
$url = $this->generateURL('book', $symbol);
return $this->call(self::PUBLIC_REQUEST, $url, $data);
}
/**
* Get Lendbook - gets the margin funding book.
*
* @param string $currency
* @param array $data
* @return object
*/
public function getLendbook($currency = 'USD', $data = [])
{
$url = $this->generateURL('lendbook', $currency);
return $this->call(self::PUBLIC_REQUEST, $url, $data);
}
/**
* Get lends - List of the most recent funding data for a given currency.
*
* @param string $currency
* @param array $data
* @return object
*/
public function getLends($currency = 'USD', $data = [])
{
$url = $this->generateURL('lends', $currency);
return $this->call(self::PUBLIC_REQUEST, $url, $data);
}
/**
* Get stats - misc stats about the givem symbol
*
* @param string $symbol
* @return object
*/
public function getStats($symbol = 'BTCUSD')
{
$url = $this->generateURL('stats', $symbol);
return $this->call(self::PUBLIC_REQUEST, $url, []);
}
/**
* Get all BitFinex trades
*
* @param string $symbol
* @param array $data
* @return mixed
*/
public function getTrades($symbol = 'BTCUSD', $data = [])
{
$url = $this->generateURL('trades', $symbol);
return $this->call(self::PUBLIC_REQUEST, $url, $data);
}
/**
* Get list of symbols
*
* @return object
*/
public function getSymbols()
{
$url = $this->generateURL('symbols');
return $this->call(self::PUBLIC_REQUEST, $url, []);
}
/**
* Get list of symbols with IDs, public
*
* @return mixed
*/
public function getSymbolsDetails()
{
$url = $this->generateURL('symbols_details');
return $this->call(self::PUBLIC_REQUEST, $url, []);
}
/**
* Gets information on most recent trades from the last 24 hours
*
* @param string $symbol
* @return object
*/
public function getTicker($symbol = 'BTCUSD')
{
$url = $this->generateURL('pubticket', $symbol);
return $this->call(self::PUBLIC_REQUEST, $url, []);
}
/**
* Gets account information
*
* @return mixed
*/
public function getMyAccountInfo()
{
$url = $this->generateURL('account_infos');
$data = [
'request' => str_replace(self::BITFINEX_API_URL, '', $url)
];
return $this->call(self::PRIVATE_REQUEST, $url, $data);
}
/**
* Get all trades
*
* @param string $symbol Name of the symbols
* @param int $limit_trades Optional. Limits the number of trades returned. Default 50.
* @param null $timestamp Optional. Trades made before this timestamp won't be returned.
* @param null $until Optional. Trades made after this timestamp won't be returned.
* @param int $reverse Optional. Revert listing of trades (oldest -> newest). Default is newest -> oldest
* @return mixed
*/
public function getMyTrades($symbol = 'BTCUSD', $limit_trades, $timestamp = null, $until = null, $reverse = 0)
{
$url = $this->generateURL('mytrades');
$data = [
'request' => str_replace(self::BITFINEX_API_URL, '', $url),
'symbol' => $symbol,
'limit_trades' => $limit_trades,
'reverse' => $reverse
];
if ($timestamp) $data['timestamp'] = $timestamp;
if ($until) $data['until'] = $until;
return $this->call(self::PRIVATE_REQUEST, $url, $data);
}
/**
* Get trade history
*
* @param $currency
* @param $wallet
* @param int $limit
* @param null $since
* @param null $until
* @return object
*/
public function getMyHistory($currency, $wallet, $limit = 500, $since = null, $until = null)
{
$url = $this->generateURL('history');
$data = [
'request' => str_replace(self::BITFINEX_API_URL, '', $url),
'currency' => $currency,
'wallet' => $wallet,
'limit' => $limit
];
if ($since) $data['since'] = $since;
if ($until) $data['until'] = $until;
return $this->call(self::PRIVATE_REQUEST, $url, $data);
}
/**
* Get trade history movements
*
* @param string $currency
* @param string $method
* @param int $limit
* @param null $since
* @param null $until
* @return object
*/
public function getMyHistoryMovements($currency = 'USD', $method = 'bitcoin', $limit = 50, $since = null, $until = null)
{
$url = $this->generateURL('history', 'movements');
$data = [
'request' => str_replace(self::BITFINEX_API_URL, '', $url),
'currency' => $currency,
'method' => $method,
'limit' => $limit
];
if ($since) $data['since'] = $since;
if ($until) $data['until'] = $until;
return $this->call(self::PRIVATE_REQUEST, $url, $data);
}
/**
* Get summary
*
* @return mixed
*/
public function getMySummary()
{
$url = $this->generateURL('summary');
$data = [
'request' => str_replace(self::BITFINEX_API_URL, '', $url)
];
return $this->call(self::PRIVATE_REQUEST, $url, $data);
}
/**
* Get balances
*
* @return mixed
*/
public function getMyBalances()
{
$url = $this->generateURL('balances');
$data = [
'request' => str_replace(self::BITFINEX_API_URL, '', $url)
];
return $this->call(self::PRIVATE_REQUEST, $url, $data);
}
/**
* Returns deposit address to make a deposit
*
* @param $method
* @param $walletName
* @param int $renew
* @return object
*/
public function makeDeposit($method, $walletName, $renew = 0)
{
$url = $this->generateURL('deposit', 'new');
$data = [
'request' => str_replace(self::BITFINEX_API_URL, '', $url),
'method' => $method,
'wallet_name' => $walletName,
'renew' => $renew
];
return $this->call(self::PRIVATE_REQUEST, $url, $data);
}
/**
* Submit new order
*
* @param $symbol string Symbol, see list /symbols
* @param $amount float How much you want to buy and sell
* @param $price float Price to buy and/or sell.
* @param $exchange string e.g. "bitfinex"
* @param $side string "buy" or "sell"
* @param $type string market, limit, stop, trailing-stop, fill-or-kill, exchange market, exchange limit, exchange shop, exchange trailing-stop, exchange fill-or-kill
* @param bool $isHidden true if the order should be hidden
* @param bool $isPostOnly true if the order should be post only.
* @param bool $ocoOrder Additional STOP OCO order.
* @param null $buyPriceOco If OCO is true, this represents the value of the OCO to stop
* @return object
*/
public function makeOrder($symbol, $amount, $price, $exchange, $side, $type, $isHidden = false, $isPostOnly = false, $ocoOrder = false, $buyPriceOco = null)
{
$url = $this->generateURL('order', 'new');
$data = [
'request' => str_replace(self::BITFINEX_API_URL, '', $url),
'symbol' => $symbol,
'amount' => $amount,
'price' => $price,
'exchange' => $exchange,
'side' => $side,
'type' => $type,
'is_hidden' => $isHidden,
'is_postonly' => $isPostOnly,
'ocoorder' => $ocoOrder
];
if ($ocoOrder) $data['buy_price_oco'] = $buyPriceOco;
return $this->call(self::PRIVATE_REQUEST, $data, $url);
}
/**
* Submit multiple orders
*
* @param $symbol string Symbol, see list /symbols
* @param $amount float How much you want to buy and sell
* @param $price float Price to buy and/or sell.
* @param $exchange string e.g. "bitfinex"
* @param $side string "buy" or "sell"
* @param $type string market, limit, stop, trailing-stop, fill-or-kill
* @return object
*/
public function makeMultipleOrder($symbol, $amount, $price, $exchange, $side, $type)
{
$url = $this->generateURL('order', 'new/multi');
$data = [
'request' => str_replace(self::BITFINEX_API_URL, '', $url),
'symbol' => $symbol,
'amount' => $amount,
'price' => $price,
'exchange' => $exchange,
'side' => $side,
'type' => $type
];
return $this->call(self::PRIVATE_REQUEST, $data, $url);
}
/**
* Cancel an order
*
* @param $orderId int
* @return object
*/
public function cancelOrder($orderId)
{
$url = $this->generateURL('order', 'cancel');
$data = [
'request' => str_replace(self::BITFINEX_API_URL, '', $url),
'order_id' => $orderId
];
return $this->call(self::PRIVATE_REQUEST, $data, $url);
}
/**
* Cancels multiple orders
*
* @param $orderIds
* @return object
*/
public function cancelMultipleOrders($orderIds)
{
$url = $this->generateURL('order', 'cancel/multi');
$data = [
'request' => str_replace(self::BITFINEX_API_URL, '', $url),
'order_id' => $orderIds
];
return $this->call(self::PRIVATE_REQUEST, $data, $url);
}
/**
* Cancels all orders
*
* @param $orderIds
* @return object
*/
public function cancelAllOrders($orderIds)
{
$url = $this->generateURL('order', 'cancel/all');
$data = [
'request' => str_replace(self::BITFINEX_API_URL, '', $url)
];
return $this->call(self::PRIVATE_REQUEST, $data, $url);
}
/**
* Generates the URL for the API Call
*
* @param $method string
* @param null /array $params
* @return string
*/
private function generateURL($method, $params = null)
{
$parameters = '';
if ($params !== null) {
$parameters = '/';
if (is_array($params)) {
$parameters .= implode('/', $params);
} else {
$parameters .= $params;
}
}
return self::BITFINEX_API_URL . '/' . self::BITFINEX_API_VERSION . "/$method$parameters";
}
/**
* Prepare headers for authentication
*
* @param $data array
* @return array
*/
private function generateHeaders($data)
{
$data['nonce'] = number_format(round(microtime(true) * 100000), 0, '.', '');
$payload = base64_encode(json_encode($data));
$signature = hash_hmac('sha384', $payload, $this->apiSecret);
return [
"X-BFX-APIKEY: $this->apiKey",
"X-BFX-PAYLOAD: $payload",
"X-BFX-SIGNATURE: $signature"
];
}
/**
* Outputs the result of the cURL call
*
* @param $result
* @param bool $isError
* @return mixed
*/
public function output($result, $isError = false)
{
$out = json_decode($result, true);
if ($isError) {
$out['error'] = true;
}
return $out;
}
/**
* Outputs the result of a CURL error
*
* @param $curl
* @return bool
*/
public function error($curl)
{
if ($errno = curl_errno($curl)) {
echo "cURL error {$errno}: " . curl_strerror($errno);
return false;
}
return true;
}
/**
* Returns if there was an error with the call
*
* @param $curl
* @return bool
*/
public function isBitFinexError($curl)
{
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
return ($httpCode !== 200 ? true : false);
}
/**
* Executes the API call
*
* @param $url string
* @param array $data
* @return object/bool
*/
private function call($type, $url, $data = false)
{
$curl = curl_init();
if ($type == self::PRIVATE_REQUEST) {
$headers = $this->generateHeaders($data);
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => ''
]);
if (!$result = curl_exec($curl)) {
return $this->error($curl);
}
return $this->output($result, $this->isBitFinexError($curl));
} elseif ($type == self::PUBLIC_REQUEST) {
$query = '';
if (count($data)) {
$query = '?' . http_build_query($data);
}
$url = $url . $query;
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true
]);
if (!$result = curl_exec($curl)) {
return $this->error($curl);
}
return $this->output($result, $this->isBitFinexError($curl));
}
}
}