1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Fabian Hanisch |
4
|
|
|
* @since 16.07.2017 02:56 |
5
|
|
|
* @version 1.0 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace HanischIt\KrakenApi; |
9
|
|
|
|
10
|
|
|
use HanischIt\KrakenApi\External\HttpClient; |
11
|
|
|
use HanischIt\KrakenApi\Model\AccountBalance\AccountBalanceRequest; |
12
|
|
|
use HanischIt\KrakenApi\Model\AccountBalance\AccountBalanceResponse; |
13
|
|
|
use HanischIt\KrakenApi\Model\AddOrder\AddOrderRequest; |
14
|
|
|
use HanischIt\KrakenApi\Model\AddOrder\AddOrderResponse; |
15
|
|
|
use HanischIt\KrakenApi\Model\Assets\AssetsRequest; |
16
|
|
|
use HanischIt\KrakenApi\Model\Assets\AssetsResponse; |
17
|
|
|
use HanischIt\KrakenApi\Model\CancelOpenOrder\CancelOpenOrderRequest; |
18
|
|
|
use HanischIt\KrakenApi\Model\CancelOpenOrder\CancelOpenOrderResponse; |
19
|
|
|
use HanischIt\KrakenApi\Model\ClosedOrders\ClosedOrdersRequest; |
20
|
|
|
use HanischIt\KrakenApi\Model\ClosedOrders\ClosedOrdersResponse; |
21
|
|
|
use HanischIt\KrakenApi\Model\GetTicker\TickerRequest; |
22
|
|
|
use HanischIt\KrakenApi\Model\GetTicker\TickerResponse; |
23
|
|
|
use HanischIt\KrakenApi\Model\Header; |
24
|
|
|
use HanischIt\KrakenApi\Model\LedgersInfo\LedgersInfoRequest; |
25
|
|
|
use HanischIt\KrakenApi\Model\LedgersInfo\LedgersInfoResponse; |
26
|
|
|
use HanischIt\KrakenApi\Model\OHLCData\OHLCDataRequest; |
27
|
|
|
use HanischIt\KrakenApi\Model\OHLCData\OHLCDataResponse; |
28
|
|
|
use HanischIt\KrakenApi\Model\OpenOrders\OpenOrdersRequest; |
29
|
|
|
use HanischIt\KrakenApi\Model\OpenOrders\OpenOrdersResponse; |
30
|
|
|
use HanischIt\KrakenApi\Model\OrderBook\OrderBookRequest; |
31
|
|
|
use HanischIt\KrakenApi\Model\OrderBook\OrderBookResponse; |
32
|
|
|
use HanischIt\KrakenApi\Model\OrdersInfo\OrdersInfoRequest; |
33
|
|
|
use HanischIt\KrakenApi\Model\OrdersInfo\OrdersInfoResponse; |
34
|
|
|
use HanischIt\KrakenApi\Model\QueryLedgers\QueryLedgersRequest; |
35
|
|
|
use HanischIt\KrakenApi\Model\QueryLedgers\QueryLedgersResponse; |
36
|
|
|
use HanischIt\KrakenApi\Model\RecentTrades\RecentTradesRequest; |
37
|
|
|
use HanischIt\KrakenApi\Model\RecentTrades\RecentTradesResponse; |
38
|
|
|
use HanischIt\KrakenApi\Model\RequestInterface; |
39
|
|
|
use HanischIt\KrakenApi\Model\RequestOptions; |
40
|
|
|
use HanischIt\KrakenApi\Model\ResponseInterface; |
41
|
|
|
use HanischIt\KrakenApi\Model\ServerTime\ServerTimeRequest; |
42
|
|
|
use HanischIt\KrakenApi\Model\ServerTime\ServerTimeResponse; |
43
|
|
|
use HanischIt\KrakenApi\Model\SpreadData\SpreadDataRequest; |
44
|
|
|
use HanischIt\KrakenApi\Model\SpreadData\SpreadDataResponse; |
45
|
|
|
use HanischIt\KrakenApi\Model\TradableAssetPairs\TradableAssetPairsRequest; |
46
|
|
|
use HanischIt\KrakenApi\Model\TradableAssetPairs\TradableAssetPairsResponse; |
47
|
|
|
use HanischIt\KrakenApi\Model\TradeBalance\TradeBalanceRequest; |
48
|
|
|
use HanischIt\KrakenApi\Model\TradeBalance\TradeBalanceResponse; |
49
|
|
|
use HanischIt\KrakenApi\Model\Trades\TradesRequest; |
50
|
|
|
use HanischIt\KrakenApi\Model\Trades\TradesResponse; |
51
|
|
|
use HanischIt\KrakenApi\Model\TradesHistory\TradesHistoryRequest; |
52
|
|
|
use HanischIt\KrakenApi\Model\TradesHistory\TradesHistoryResponse; |
53
|
|
|
use HanischIt\KrakenApi\Model\TradeVolume\TradeVolumeRequest; |
54
|
|
|
use HanischIt\KrakenApi\Model\TradeVolume\TradeVolumeResponse; |
55
|
|
|
use HanischIt\KrakenApi\Service\RequestService\GetRequest; |
56
|
|
|
use HanischIt\KrakenApi\Service\RequestService\Nonce; |
57
|
|
|
use HanischIt\KrakenApi\Service\RequestService\PostRequest; |
58
|
|
|
use HanischIt\KrakenApi\Service\RequestService\Request; |
59
|
|
|
use HanischIt\KrakenApi\Service\RequestService\RequestHeader; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Class KrakenApi |
63
|
|
|
* |
64
|
|
|
* @package HanischIt\KrakenApi |
65
|
|
|
*/ |
66
|
|
|
class KrakenApi |
67
|
|
|
{ |
68
|
|
|
/** |
69
|
|
|
* @var RequestOptions |
70
|
|
|
*/ |
71
|
|
|
private $requestOptions; |
72
|
|
|
/** |
73
|
|
|
* @var Header |
74
|
|
|
*/ |
75
|
|
|
private $header; |
76
|
|
|
/** |
77
|
|
|
* @var Request |
78
|
|
|
*/ |
79
|
|
|
private $request; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* KrakenApi constructor. |
83
|
|
|
* |
84
|
|
|
* @param string $apiKey |
85
|
|
|
* @param string $apiSign |
86
|
|
|
* @param string $version |
87
|
|
|
* @param string $endpoint |
88
|
|
|
*/ |
89
|
20 |
|
public function __construct($apiKey, $apiSign, $version = '0', $endpoint = 'https://api.kraken.com/') |
90
|
|
|
{ |
91
|
20 |
|
$httpClient = new HttpClient(['verify' => false]); |
92
|
20 |
|
$requestHeader = new RequestHeader(); |
93
|
20 |
|
$nonce = new Nonce(); |
94
|
|
|
|
95
|
20 |
|
$this->requestOptions = new RequestOptions($endpoint, $version); |
96
|
20 |
|
$postRequest = new PostRequest($httpClient, $requestHeader, $nonce); |
97
|
20 |
|
$getRequest = new GetRequest($httpClient, $requestHeader); |
98
|
20 |
|
$this->request = new Request($postRequest, $getRequest); |
99
|
20 |
|
$this->header = new Header($apiKey, $apiSign); |
100
|
20 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Request $request |
104
|
|
|
*/ |
105
|
20 |
|
public function setRequest(Request $request) |
106
|
|
|
{ |
107
|
20 |
|
$this->request = $request; |
108
|
20 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return ServerTimeResponse|ResponseInterface |
112
|
|
|
*/ |
113
|
1 |
|
public function getServerTime() |
114
|
|
|
{ |
115
|
1 |
|
$serverTimeRequest = new ServerTimeRequest(); |
116
|
|
|
|
117
|
1 |
|
return $this->doRequest($serverTimeRequest); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return AccountBalanceResponse|ResponseInterface |
122
|
|
|
*/ |
123
|
1 |
|
public function getAccountBalance() |
124
|
|
|
{ |
125
|
1 |
|
$accountBalanceRequest = new AccountBalanceRequest(); |
126
|
|
|
|
127
|
1 |
|
return $this->doRequest($accountBalanceRequest); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $pair |
132
|
|
|
* @param string $type |
133
|
|
|
* @param string $orderType |
134
|
|
|
* @param null|float $price |
135
|
|
|
* @param null|float $volume |
136
|
|
|
* |
137
|
|
|
* @return ResponseInterface|AddOrderResponse |
138
|
|
|
*/ |
139
|
1 |
|
public function addOrder($pair, $type, $orderType, $price = null, $volume = null) |
140
|
|
|
{ |
141
|
1 |
|
$addOrderRequest = new AddOrderRequest($pair, $type, $orderType, $price, $volume); |
142
|
|
|
|
143
|
1 |
|
return $this->doRequest($addOrderRequest); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return ResponseInterface|AssetsResponse |
148
|
|
|
*/ |
149
|
1 |
|
public function getAssets() |
150
|
|
|
{ |
151
|
1 |
|
$assetsRequest = new AssetsRequest(); |
152
|
|
|
|
153
|
1 |
|
return $this->doRequest($assetsRequest); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param array $assetNames |
158
|
|
|
* |
159
|
|
|
* @return ResponseInterface|TickerResponse |
160
|
|
|
*/ |
161
|
1 |
|
public function getTicker(array $assetNames) |
162
|
|
|
{ |
163
|
1 |
|
$tickerRequest = new TickerRequest($assetNames); |
164
|
|
|
|
165
|
1 |
|
return $this->doRequest($tickerRequest); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $assetPair |
170
|
|
|
* @param int|null $count |
171
|
|
|
* |
172
|
|
|
* @return ResponseInterface|OrderBookResponse |
173
|
|
|
*/ |
174
|
1 |
|
public function getOrderBook($assetPair, $count = null) |
175
|
|
|
{ |
176
|
1 |
|
$orderBookRequest = new OrderBookRequest($assetPair, $count); |
177
|
|
|
|
178
|
1 |
|
return $this->doRequest($orderBookRequest); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param bool $trades |
183
|
|
|
* @param null $userref |
|
|
|
|
184
|
|
|
* |
185
|
|
|
* @return ResponseInterface|OpenOrdersResponse |
186
|
|
|
*/ |
187
|
1 |
|
public function getOpenOrders($trades = false, $userref = null) |
188
|
|
|
{ |
189
|
1 |
|
$orderBookRequest = new OpenOrdersRequest($trades, $userref); |
190
|
|
|
|
191
|
1 |
|
return $this->doRequest($orderBookRequest); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param bool $trades |
196
|
|
|
* @param null $userref |
|
|
|
|
197
|
|
|
* @param null|string $start |
198
|
|
|
* @param null|string $end |
199
|
|
|
* @param null|int $ofs |
200
|
|
|
* @param null|string $closetime |
201
|
|
|
* |
202
|
|
|
* @return ClosedOrdersResponse|ResponseInterface |
203
|
|
|
*/ |
204
|
1 |
|
public function getClosedOrders( |
205
|
|
|
$trades = false, |
206
|
|
|
$userref = null, |
207
|
|
|
$start = null, |
208
|
|
|
$end = null, |
209
|
|
|
$ofs = null, |
210
|
|
|
$closetime = null |
211
|
|
|
) { |
212
|
1 |
|
$orderBookRequest = new ClosedOrdersRequest($trades, $userref, $start, $end, $ofs, $closetime); |
213
|
|
|
|
214
|
1 |
|
return $this->doRequest($orderBookRequest); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param string $assetPair |
219
|
|
|
* @param null|string $since |
220
|
|
|
* |
221
|
|
|
* @return ResponseInterface|RecentTradesResponse |
222
|
|
|
*/ |
223
|
1 |
|
public function getRecentTrades($assetPair, $since = null) |
224
|
|
|
{ |
225
|
1 |
|
$recentTradeRequest = new RecentTradesRequest($assetPair, $since); |
226
|
|
|
|
227
|
1 |
|
return $this->doRequest($recentTradeRequest); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param string $assetPair |
232
|
|
|
* @param string $since |
233
|
|
|
* @return ResponseInterface|SpreadDataResponse |
234
|
|
|
*/ |
235
|
1 |
|
public function getSpreadData($assetPair, $since = null) |
236
|
|
|
{ |
237
|
1 |
|
$spreadDataRequest = new SpreadDataRequest($assetPair, $since); |
238
|
|
|
|
239
|
1 |
|
return $this->doRequest($spreadDataRequest); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $info |
244
|
|
|
* @param array|null $assetPairs |
245
|
|
|
* @return ResponseInterface|TradableAssetPairsResponse |
246
|
|
|
*/ |
247
|
1 |
|
public function getTradableAssetPairs($info, array $assetPairs = null) |
248
|
|
|
{ |
249
|
1 |
|
if (null !== $assetPairs) { |
250
|
1 |
|
$assetPairs = implode(',', $assetPairs); |
251
|
1 |
|
} |
252
|
1 |
|
$tradableAssetPairs = new TradableAssetPairsRequest($info, $assetPairs); |
253
|
|
|
|
254
|
1 |
|
return $this->doRequest($tradableAssetPairs); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param string $assetPair |
259
|
|
|
* @param null|int $interval |
260
|
|
|
* @param null|int $since |
261
|
|
|
* @return ResponseInterface|OHLCDataResponse |
262
|
|
|
*/ |
263
|
1 |
|
public function getOHLCData($assetPair, $interval = null, $since = null) |
264
|
|
|
{ |
265
|
1 |
|
$ohlcDataRequest = new OHLCDataRequest($assetPair, $interval, $since); |
266
|
|
|
|
267
|
1 |
|
return $this->doRequest($ohlcDataRequest); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param array|null $txids |
272
|
|
|
* @param bool $trades |
273
|
|
|
* @param null|string $userref |
274
|
|
|
* @return ResponseInterface|OrdersInfoResponse |
275
|
|
|
*/ |
276
|
1 |
|
public function getOrdersInfo(array $txids, $trades = false, $userref = null) |
277
|
|
|
{ |
278
|
1 |
|
$ordersInfoRequest = new OrdersInfoRequest($txids, $trades, $userref); |
279
|
|
|
|
280
|
1 |
|
return $this->doRequest($ordersInfoRequest); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param string $type |
285
|
|
|
* @param bool $trades |
286
|
|
|
* @param null $start |
|
|
|
|
287
|
|
|
* @param null $end |
288
|
|
|
* @param null $ofs |
289
|
|
|
* @return ResponseInterface|TradesHistoryResponse |
290
|
|
|
*/ |
291
|
1 |
|
public function getTradesHistory($type = 'all', $trades = false, $start = null, $end = null, $ofs = null) |
292
|
|
|
{ |
293
|
1 |
|
$tradesHistoryRequest = new TradesHistoryRequest($type, $trades, $start, $end, $ofs); |
294
|
|
|
|
295
|
1 |
|
return $this->doRequest($tradesHistoryRequest); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param string $txid |
300
|
|
|
* @param bool $trades |
301
|
|
|
* @return ResponseInterface|TradesResponse |
302
|
|
|
*/ |
303
|
1 |
|
public function getTrades($txid, $trades = false) |
304
|
|
|
{ |
305
|
1 |
|
$tradesHistoryRequest = new TradesRequest($txid, $trades); |
306
|
|
|
|
307
|
1 |
|
return $this->doRequest($tradesHistoryRequest); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @param string $txid |
312
|
|
|
* @return ResponseInterface|CancelOpenOrderResponse |
313
|
|
|
*/ |
314
|
1 |
|
public function cancelOrder($txid) |
315
|
|
|
{ |
316
|
1 |
|
$cancelOrderRequest = new CancelOpenOrderRequest($txid); |
317
|
|
|
|
318
|
1 |
|
return $this->doRequest($cancelOrderRequest); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @param string|null $aclass |
323
|
|
|
* @param string|null $asset |
324
|
|
|
* @return ResponseInterface|TradeBalanceResponse |
325
|
|
|
*/ |
326
|
1 |
|
public function getTradeBalance($aclass = null, $asset = null) |
327
|
|
|
{ |
328
|
1 |
|
$tradeBalanceRequest = new TradeBalanceRequest($aclass, $asset); |
329
|
|
|
|
330
|
1 |
|
return $this->doRequest($tradeBalanceRequest); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param string $aclass |
335
|
|
|
* @param string $asset |
336
|
|
|
* @param string $type |
337
|
|
|
* @param null|string $start |
338
|
|
|
* @param null|string $end |
339
|
|
|
* @param null|string $ofs |
340
|
|
|
* @return ResponseInterface|LedgersInfoResponse |
341
|
|
|
*/ |
342
|
1 |
|
public function getLedgersInfo( |
343
|
|
|
$aclass = 'currency', |
344
|
|
|
$asset = 'all', |
345
|
|
|
$type = 'all', |
346
|
|
|
$start = null, |
347
|
|
|
$end = null, |
348
|
|
|
$ofs = null |
349
|
|
|
) { |
350
|
1 |
|
$ledgersInfoRequest = new LedgersInfoRequest( |
351
|
1 |
|
$aclass, |
352
|
1 |
|
$asset, |
353
|
1 |
|
$type, |
354
|
1 |
|
$start, |
355
|
1 |
|
$end, |
356
|
1 |
|
$ofs); |
357
|
|
|
|
358
|
1 |
|
return $this->doRequest($ledgersInfoRequest); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* @param string $id |
363
|
|
|
* @return ResponseInterface|QueryLedgersResponse |
364
|
|
|
*/ |
365
|
1 |
|
public function getLedgers($id) |
366
|
|
|
{ |
367
|
1 |
|
$ledgersInfoRequest = new QueryLedgersRequest($id); |
368
|
|
|
|
369
|
1 |
|
return $this->doRequest($ledgersInfoRequest); |
370
|
|
|
|
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @param null|string $pair |
375
|
|
|
* @param null|bool $feeInfo |
376
|
|
|
* @return TradeVolumeResponse|ResponseInterface |
377
|
|
|
*/ |
378
|
1 |
|
public function getTradeVolume($pair = null, $feeInfo = null) |
379
|
|
|
{ |
380
|
1 |
|
$tradeVolumeRequest = new TradeVolumeRequest($pair, $feeInfo); |
381
|
|
|
|
382
|
1 |
|
return $this->doRequest($tradeVolumeRequest); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @param RequestInterface $requestInterface |
387
|
|
|
* |
388
|
|
|
* @return ResponseInterface |
389
|
|
|
*/ |
390
|
20 |
|
private function doRequest(RequestInterface $requestInterface) |
391
|
|
|
{ |
392
|
20 |
|
return $this->request->execute($requestInterface, $this->requestOptions, $this->header); |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|