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