-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathClient.php
executable file
·356 lines (313 loc) · 10.4 KB
/
Client.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
<?php
/**
* Bittrex API wrapper class
* @author Edson Medina <edsonmedina@gmail.com>
*/
class Client
{
private $baseUrl;
private $apiVersion = 'v1.1';
private $apiKey;
private $apiSecret;
public function __construct ($apiKey, $apiSecret)
{
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
$this->baseUrl = 'https://bittrex.com/api/'.$this->apiVersion.'/';
}
/**
* Invoke API
* @param string $method API method to call
* @param array $params parameters
* @param bool $apiKey use apikey or not
* @return object
*/
private function call ($method, $params = array(), $apiKey = false)
{
$uri = $this->baseUrl.$method;
if ($apiKey == true) {
$params['apikey'] = $this->apiKey;
$params['nonce'] = time();
}
if (!empty($params)) {
$uri .= '?'.http_build_query($params);
}
$sign = hash_hmac ('sha512', $uri, $this->apiSecret);
$ch = curl_init ($uri);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('apisign: '.$sign));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$answer = json_decode($result);
if ($answer->success == false) {
throw new \Exception ($answer->message);
}
return $answer->result;
}
/**
* Get the open and available trading markets at Bittrex along with other meta data.
* @return array
*/
public function getMarkets ()
{
return $this->call ('public/getmarkets');
}
/**
* Get all supported currencies at Bittrex along with other meta data.
* @return array
*/
public function getCurrencies ()
{
return $this->call ('public/getcurrencies');
}
/**
* Get the current tick values for a market.
* @param string $market literal for the market (ex: BTC-LTC)
* @return array
*/
public function getTicker ($market)
{
return $this->call ('public/getticker', array('market' => $market));
}
/**
* Get the last 24 hour summary of all active exchanges
* @return array
*/
public function getMarketSummaries ()
{
return $this->call ('public/getmarketsummaries');
}
/**
* Get the last 24 hour summary of all active exchanges
* @param string $market literal for the market (ex: BTC-LTC)
* @return array
*/
public function getMarketSummary ($market)
{
return $this->call ('public/getmarketsummary', array('market' => $market));
}
/**
* Get the orderbook for a given market
* @param string $market literal for the market (ex: BTC-LTC)
* @param string $type "buy", "sell" or "both" to identify the type of orderbook to return
* @param integer $depth how deep of an order book to retrieve. Max is 50.
* @return array
*/
public function getOrderBook ($market, $type, $depth = 20)
{
$params = array (
'market' => $market,
'type' => $type,
'depth' => $depth
);
return $this->call ('public/getorderbook', $params);
}
/**
* Get the latest trades that have occured for a specific market
* @param string $market literal for the market (ex: BTC-LTC)
* @param integer $count number of entries to return. Max is 50.
* @return array
*/
public function getMarketHistory ($market, $count = 20)
{
$params = array (
'market' => $market,
'count' => $count
);
return $this->call ('public/getmarkethistory', $params);
}
/**
* Place a limit buy order in a specific market.
* Make sure you have the proper permissions set on your API keys for this call to work
* @param string $market literal for the market (ex: BTC-LTC)
* @param float $quantity the amount to purchase
* @param float $rate the rate at which to place the order
* @return array
*/
public function buyLimit ($market, $quantity, $rate)
{
$params = array (
'market' => $market,
'quantity' => $quantity,
'rate' => $rate
);
return $this->call ('market/buylimit', $params, true);
}
/**
* Place a buy order in a specific market.
* Make sure you have the proper permissions set on your API keys for this call to work
* @param string $market literal for the market (ex: BTC-LTC)
* @param float $quantity the amount to purchase
* @return array
*/
public function buyMarket ($market, $quantity)
{
$params = array (
'market' => $market,
'quantity' => $quantity
);
return $this->call ('market/buymarket', $params, true);
}
/**
* Place a limit sell order in a specific market.
* Make sure you have the proper permissions set on your API keys for this call to work
* @param string $market literal for the market (ex: BTC-LTC)
* @param float $quantity the amount to sell
* @param float $rate the rate at which to place the order
* @return array
*/
public function sellLimit ($market, $quantity, $rate)
{
$params = array (
'market' => $market,
'quantity' => $quantity,
'rate' => $rate
);
return $this->call ('market/selllimit', $params, true);
}
/**
* Place a sell order in a specific market.
* Make sure you have the proper permissions set on your API keys for this call to work
* @param string $market literal for the market (ex: BTC-LTC)
* @param float $quantity the amount to sell
* @return array
*/
public function sellMarket ($market, $quantity)
{
$params = array (
'market' => $market,
'quantity' => $quantity
);
return $this->call ('market/sellmarket', $params, true);
}
/**
* Cancel a buy or sell order
* @param string $uuid id of sell or buy order
* @return array
*/
public function cancel ($uuid)
{
$params = array ('uuid' => $uuid);
return $this->call ('market/cancel', $params, true);
}
/**
* Get all orders that you currently have opened. A specific market can be requested
* @param string $market literal for the market (ex: BTC-LTC)
* @return array
*/
public function getOpenOrders ($market = null)
{
$params = array ('market' => $market);
return $this->call ('market/getopenorders', $params, true);
}
/**
* Retrieve all balances from your account
* @return array
*/
public function getBalances ()
{
return $this->call ('account/getbalances', array(), true);
}
/**
* Retrieve the balance from your account for a specific currency
* @param string $currency literal for the currency (ex: LTC)
* @return array
*/
public function getBalance ($currency)
{
$params = array ('currency' => $currency);
return $this->call ('account/getbalance', $params, true);
}
/**
* Retrieve or generate an address for a specific currency. If one
* does not exist, the call will fail and return ADDRESS_GENERATING
* until one is available.
* @param string $currency literal for the currency (ex: LTC)
* @return array
*/
public function getDepositAddress ($currency)
{
$params = array ('currency' => $currency);
return $this->call ('account/getdepositaddress', $params, true);
}
/**
* Withdraw funds from your account. note: please account for txfee.
* @param string $currency literal for the currency (ex: LTC)
* @param float $quantity the quantity of coins to withdraw
* @param float $address the address where to send the funds
* @param float $paymentid (optional) used for CryptoNotes/BitShareX/Nxt optional field (memo/paymentid)
* @return array
*/
public function withdraw ($currency, $quantity, $address, $paymentid = null)
{
$params = array (
'currency' => $currency,
'quantity' => $quantity,
'address' => $address,
);
if ($paymentid) {
$params['paymentid'] = $paymentid;
}
return $this->call ('account/withdraw', $params, true);
}
/**
* Retrieve a single order by uuid
* @param string $uuid the uuid of the buy or sell order
* @return array
*/
public function getOrder ($uuid)
{
$params = array ('uuid' => $uuid);
return $this->call ('account/getorder', $params, true);
}
/**
* Retrieve your order history
* @param string $market (optional) a string literal for the market (ie. BTC-LTC). If ommited, will return for all markets
* @param integer $count (optional) the number of records to return
* @return array
*/
public function getOrderHistory ($market = null, $count = null)
{
$params = array ();
if ($market) {
$params['market'] = $market;
}
if ($count) {
$params['count'] = $count;
}
return $this->call ('account/getorderhistory', $params, true);
}
/**
* Retrieve your withdrawal history
* @param string $currency (optional) a string literal for the currecy (ie. BTC). If omitted, will return for all currencies
* @param integer $count (optional) the number of records to return
* @return array
*/
public function getWithdrawalHistory ($currency = null, $count = null)
{
$params = array ();
if ($currency) {
$params['currency'] = $currency;
}
if ($count) {
$params['count'] = $count;
}
return $this->call ('account/getwithdrawalhistory', $params, true);
}
/**
* Retrieve your deposit history
* @param string $currency (optional) a string literal for the currecy (ie. BTC). If omitted, will return for all currencies
* @param integer $count (optional) the number of records to return
* @return array
*/
public function getDepositHistory ($currency = null, $count = null)
{
$params = array ();
if ($currency) {
$params['currency'] = $currency;
}
if ($count) {
$params['count'] = $count;
}
return $this->call ('account/getdeposithistory', $params, true);
}
}