forked from mariodian/bitfinex-auto-lend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitfinex.php
211 lines (152 loc) · 3.86 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
<?php
class Bitfinex
{
private $api_key;
private $api_secret;
private $api_version;
private $base_url = 'https://api.bitfinex.com';
public function __construct($api_key, $api_secret, $api_version = 'v1')
{
$this->api_key = $api_key;
$this->api_secret = $api_secret;
$this->api_version = $api_version;
}
public function new_offer($currency, $amount, $rate, $period, $direction = "lend")
{
$request = '/' . $this->api_version . '/offer/new';
$data = array(
'request' => $request,
'currency' => $currency,
'amount' => $amount,
'rate' => $rate,
'period' => $period,
'direction' => $direction
);
return $this->send_signed_request($data);
}
public function cancel_offer($offer_id)
{
$request = '/' . $this->api_version . '/offer/cancel';
$data = array(
'request' => $request,
'offer_id' => $offer_id
);
return $this->send_signed_request($data);
}
public function get_offer($offer_id)
{
$request = '/' . $this->api_version . '/offer/status';
$data = array(
'request' => $request,
'offer_id' => $offer_id
);
return $this->send_signed_request($data);
}
public function get_trades()
{
$request = '/' . $this->api_version . '/trades/btcusd';
$data = array(
'request' => $request,
'options' => array()
);
return $this->send_signed_request($data);
}
public function get_offers()
{
$request = '/' . $this->api_version . '/offers';
$data = array(
'request' => $request,
'options' => array()
);
return $this->send_signed_request($data);
}
public function get_credits()
{
$request = '/' . $this->api_version . '/credits';
$data = array(
'request' => $request,
'options' => array()
);
return $this->send_signed_request($data);
}
public function get_balances()
{
$request = '/' . $this->api_version . '/balances';
$data = array(
'request' => $request,
'options' => array()
);
return $this->send_signed_request($data);
}
public function get_history($currency, $limit = 1, $wallet = 'trading')
{
$request = '/' . $this->api_version . '/history';
$data = array(
'request' => $request,
'currency' => $currency,
'limit' => $limit,
'wallet' => $wallet
);
return $this->send_signed_request($data);
}
public function get_lendbook($symbol = "usd")
{
$request = '/v1/lendbook/' . $symbol;
return $this->send_unsigned_request($request);
}
public function get_lends($symbol = "usd")
{
$request = '/v1/lends/' . $symbol;
return $this->send_unsigned_request($request);
}
public function get_ticker($symbol = "btcusd")
{
$request = '/' . $this->api_version . '/ticker/' . $symbol;
return $this->send_unsigned_request($request);
}
private function prepare_header($data)
{
$data['nonce'] = (string) number_format(round(microtime(true) * 100000), 0, '.', '');
$payload = base64_encode(json_encode($data));
$signature = hash_hmac('sha384', $payload, $this->api_secret);
return array(
'X-BFX-APIKEY: ' . $this->api_key,
'X-BFX-PAYLOAD: ' . $payload,
'X-BFX-SIGNATURE: ' . $signature
);
}
private function send_signed_request($data)
{
$ch = curl_init();
$url = $this->base_url . $data['request'];
$headers = $this->prepare_header($data);
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => ""
));
if (!$result = curl_exec($ch)) {
return false;
} else {
return json_decode($result, true);
}
}
private function send_unsigned_request($request)
{
$ch = curl_init();
$url = $this->base_url . $request;
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
));
if (!$result = curl_exec($ch)) {
return false;
} else {
return json_decode($result, true);
}
}
}