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