1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Poloniex PHP SDK. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2017-2018 Chasovskih Grisha <[email protected]> |
9
|
|
|
* @license https://github.com/signulls/poloniex-php-sdk/blob/master/LICENSE MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Poloniex\Api; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\RequestOptions; |
15
|
|
|
use Poloniex\Response\PublicApi\{ |
16
|
|
|
ChartData, |
17
|
|
|
Currency, |
18
|
|
|
DayVolume, |
19
|
|
|
LoanOrders, |
20
|
|
|
OrderBook, |
21
|
|
|
Ticker, |
22
|
|
|
TradeHistory |
23
|
|
|
}; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* There are six public methods, all of which take HTTP GET requests and return output in JSON format. |
27
|
|
|
* |
28
|
|
|
* @author Grisha Chasovskih <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class PublicApi extends AbstractApi |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function request(string $command, array $params = []): array |
36
|
|
|
{ |
37
|
|
|
$this->options = [RequestOptions::QUERY => array_merge($params, ['command' => $command])]; |
38
|
|
|
|
39
|
|
|
return parent::request($command, $params); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the ticker for all markets |
44
|
|
|
* Keys of array are pairs like 'ETH_CVC' |
45
|
|
|
* |
46
|
|
|
* @return Ticker[] |
47
|
|
|
*/ |
48
|
|
|
public function returnTicker(): array |
49
|
|
|
{ |
50
|
|
|
$tickers = []; |
51
|
|
|
foreach ($this->request('returnTicker') as $pair => $response) { |
52
|
|
|
/* @var $ticker Ticker */ |
53
|
|
|
$ticker = $this->factory(Ticker::class, $response); |
54
|
|
|
$ticker->pair = $pair; |
55
|
|
|
|
56
|
|
|
$tickers[$pair] = $ticker; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $tickers; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the 24-hour volume for all markets, plus totals for primary currencies. |
64
|
|
|
* |
65
|
|
|
* @return DayVolume |
66
|
|
|
*/ |
67
|
|
|
public function return24hVolume(): DayVolume |
68
|
|
|
{ |
69
|
|
|
$response = new DayVolume(); |
70
|
|
|
|
71
|
|
|
foreach ($this->request('return24hVolume') as $pair => $contents) { |
72
|
|
|
if (strpos($pair, 'total') !== false) { |
73
|
|
|
$response->total[str_replace('total', '', $pair)] = $contents; |
74
|
|
|
|
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$response->volume[$pair] = $contents; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $response; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the order book for a given market, as well as a sequence number for use |
86
|
|
|
* with the Push API and an indicator specifying whether the market is frozen. |
87
|
|
|
* |
88
|
|
|
* @param string $currencyPair |
89
|
|
|
* @param int $depth example: 10 |
90
|
|
|
* |
91
|
|
|
* @return OrderBook |
92
|
|
|
*/ |
93
|
|
|
public function returnOrderBook(string $currencyPair, int $depth = null): OrderBook |
94
|
|
|
{ |
95
|
|
|
$this->throwExceptionIf($currencyPair === 'all', 'Please use PublicApi::allOrderBook method'); |
96
|
|
|
|
97
|
|
|
/* @var $orderBook OrderBook */ |
98
|
|
|
$orderBook = $this->factory(OrderBook::class, $this->request('returnOrderBook', [ |
99
|
|
|
'currencyPair' => strtoupper($currencyPair), |
100
|
|
|
'depth' => $depth |
101
|
|
|
])); |
102
|
|
|
|
103
|
|
|
return $orderBook; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the order books of all markets |
108
|
|
|
* |
109
|
|
|
* @param int $depth example: 10 |
110
|
|
|
* @return OrderBook[] |
111
|
|
|
*/ |
112
|
|
|
public function returnAllOrderBook(int $depth = null): array |
113
|
|
|
{ |
114
|
|
|
$orderBooks = []; |
115
|
|
|
foreach ($this->request('returnOrderBook', [ |
116
|
|
|
'currencyPair' => 'all', |
117
|
|
|
'depth' => $depth, |
118
|
|
|
]) as $currencyPair => $data) { |
119
|
|
|
$orderBooks[$currencyPair] = $this->factory(OrderBook::class, $data); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $orderBooks; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the past 200 trades for a given market, or up to 50,000 trades between a range |
127
|
|
|
* specified in UNIX timestamps by the "start" and "end" GET parameters. |
128
|
|
|
* |
129
|
|
|
* @param string $currencyPair |
130
|
|
|
* @param int $start 1410158341 |
131
|
|
|
* @param int $end 1410499372 |
132
|
|
|
* |
133
|
|
|
* @return TradeHistory[] |
134
|
|
|
*/ |
135
|
|
|
public function returnTradeHistory(string $currencyPair, int $start = null, int $end = null): array |
136
|
|
|
{ |
137
|
|
|
$tradeHistories = []; |
138
|
|
|
foreach ($this->request('returnTradeHistory', [ |
139
|
|
|
'currencyPair' => strtoupper($currencyPair), |
140
|
|
|
'start' => $start ?: time(), |
141
|
|
|
'end' => $end, |
142
|
|
|
]) as $response) { |
143
|
|
|
$tradeHistories[] = $this->factory(TradeHistory::class, $response); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $tradeHistories; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns candlestick chart data. |
151
|
|
|
* |
152
|
|
|
* Required GET parameters are: "currencyPair", "period" (candlestick period in seconds; valid |
153
|
|
|
* values are 300, 900, 1800, 7200, 14400, and 86400), "start", and "end". "Start" and "end" are |
154
|
|
|
* given in UNIX timestamp format and used to specify the date range for the data returned. |
155
|
|
|
* |
156
|
|
|
* @param string $currencyPair |
157
|
|
|
* @param int $start example: 1405699200 |
158
|
|
|
* @param int $end example: 9999999999 |
159
|
|
|
* @param int $period example: 300 |
160
|
|
|
* |
161
|
|
|
* @return ChartData[] |
162
|
|
|
*/ |
163
|
|
|
public function returnChartData(string $currencyPair, int $start = null, int $end = null, int $period = 300): array |
164
|
|
|
{ |
165
|
|
|
$this->throwExceptionIf( |
166
|
|
|
!\in_array($period, [300, 900, 1800, 7200, 14400, 86400], true), |
167
|
|
|
sprintf('Invalid period %d given.', $period) |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$chartData = []; |
171
|
|
|
foreach ($this->request('returnChartData', [ |
172
|
|
|
'currencyPair' => strtoupper($currencyPair), |
173
|
|
|
'start' => $start ?: time() - 60 * 60, |
174
|
|
|
'end' => $end ?: time(), |
175
|
|
|
'period' => $period, |
176
|
|
|
]) as $response) { |
177
|
|
|
$chartData[] = $this->factory(ChartData::class, $response); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $chartData; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Returns information about currencies. |
185
|
|
|
* NOTE: keys of array are 3-digit name of currency |
186
|
|
|
* |
187
|
|
|
* @return Currency[] |
188
|
|
|
*/ |
189
|
|
|
public function returnCurrencies(): array |
190
|
|
|
{ |
191
|
|
|
$currencies = []; |
192
|
|
|
foreach ($this->request('returnCurrencies') as $key => $response) { |
193
|
|
|
/* @var $currency Currency */ |
194
|
|
|
$currencies[$key] = $this->factory(Currency::class, $response); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $currencies; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Returns the list of loan offers and demands for a given currency, specified by the "currency" GET parameter. |
202
|
|
|
* |
203
|
|
|
* @param string $currency 3-digit currency |
204
|
|
|
* @return LoanOrders |
205
|
|
|
*/ |
206
|
|
|
public function returnLoanOrders(string $currency): LoanOrders |
207
|
|
|
{ |
208
|
|
|
/* @var $loanOrders LoanOrders */ |
209
|
|
|
$loanOrders = $this->factory( |
210
|
|
|
LoanOrders::class, |
211
|
|
|
$this->request('returnLoanOrders', compact('currency')) |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
return $loanOrders; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
protected function setup(): void |
221
|
|
|
{ |
222
|
|
|
$this->method = 'GET'; |
223
|
|
|
$this->uri = 'public'; |
224
|
|
|
} |
225
|
|
|
} |