|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IndependentReserve; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
6
|
|
|
use GuzzleHttp\Message\Response; |
|
7
|
|
|
use IndependentReserve\Object\FxRate; |
|
8
|
|
|
use IndependentReserve\Object\MarketSummary; |
|
9
|
|
|
use IndependentReserve\Object\OrderBook; |
|
10
|
|
|
use IndependentReserve\Object\RecentTrades; |
|
11
|
|
|
use IndependentReserve\Object\TradeHistorySummary; |
|
12
|
|
|
use stdClass; |
|
13
|
|
|
|
|
14
|
|
|
class PublicClient |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var GuzzleClient |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $client; |
|
20
|
|
|
|
|
21
|
14 |
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
14 |
|
$this->client = new GuzzleClient(); |
|
24
|
14 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $url |
|
28
|
|
|
* @return mixed |
|
29
|
|
|
*/ |
|
30
|
9 |
|
protected function get($url) |
|
31
|
|
|
{ |
|
32
|
|
|
/** @noinspection PhpVoidFunctionResultUsedInspection */ |
|
33
|
|
|
/** @var Response $response */ |
|
34
|
9 |
|
$response = $this->client->get($url); |
|
35
|
9 |
|
return json_decode($response->getBody()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $endpoint The public endpoint name. |
|
40
|
|
|
* @param array $params Optional named parameters. |
|
41
|
|
|
* @param string $visibility `Public` or `Private`. |
|
42
|
|
|
* @param string $method |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
13 |
|
public function getEndpoint($endpoint, array $params = array(), $visibility = 'Public', |
|
46
|
|
|
$method = 'GET') |
|
47
|
|
|
{ |
|
48
|
13 |
|
$url = $this->getEndpointUrl($endpoint, $visibility); |
|
49
|
13 |
|
if ('GET' === $method) { |
|
50
|
9 |
|
$query = http_build_query($params); |
|
51
|
9 |
|
return $this->get("$url?$query"); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** @var \GuzzleHttp\Message\Response $response */ |
|
55
|
|
|
/** @noinspection PhpVoidFunctionResultUsedInspection */ |
|
56
|
4 |
|
$response = $this->client->post($url, [ 'json' => $params ]); |
|
57
|
4 |
|
return $response->getBody()->getContents(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
13 |
|
public function getEndpointUrl($endpoint, $visibility) { |
|
61
|
13 |
|
return "https://api.independentreserve.com/$visibility/$endpoint"; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns a list of valid primary currency codes. These are the digital currencies which can be |
|
66
|
|
|
* traded on Independent Reserve. |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function getValidPrimaryCurrencyCodes() |
|
70
|
|
|
{ |
|
71
|
1 |
|
return $this->getEndpoint('GetValidPrimaryCurrencyCodes'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns a list of valid secondary currency codes. These are the fiat currencies which are |
|
76
|
|
|
* supported by Independent Reserve for trading purposes. |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function getValidSecondaryCurrencyCodes() |
|
80
|
|
|
{ |
|
81
|
1 |
|
return $this->getEndpoint('GetValidSecondaryCurrencyCodes'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns a list of valid limit order types which can be placed onto the Independent Reserve |
|
86
|
|
|
* exchange platform. |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function getGetValidLimitOrderTypes() |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->getEndpoint('GetValidLimitOrderTypes'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns a list of valid market order types which can be placed onto the Independent Reserve |
|
96
|
|
|
* exchange platform. |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function getValidMarketOrderTypes() |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->getEndpoint('GetValidMarketOrderTypes'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns a current snapshot of the Independent Reserve market for a given currency pair. |
|
106
|
|
|
* @param string $primaryCurrencyCode The digital currency for which to retrieve market summary. |
|
107
|
|
|
* Must be a valid primary currency, which can be checked via the |
|
108
|
|
|
* getValidPrimaryCurrencyCodes() method. |
|
109
|
|
|
* @param string $secondaryCurrencyCode The fiat currency in which to retrieve market summary. |
|
110
|
|
|
* Must be a valid secondary currency, which can be checked via the |
|
111
|
|
|
* getValidSecondaryCurrencyCodes() method. |
|
112
|
|
|
* @return MarketSummary |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function getMarketSummary($primaryCurrencyCode, $secondaryCurrencyCode) |
|
115
|
|
|
{ |
|
116
|
1 |
|
return MarketSummary::createFromObject($this->getEndpoint('GetMarketSummary', [ |
|
117
|
1 |
|
'primaryCurrencyCode' => $primaryCurrencyCode, |
|
118
|
1 |
|
'secondaryCurrencyCode' => $secondaryCurrencyCode, |
|
119
|
1 |
|
])); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns the Order Book for a given currency pair. |
|
124
|
|
|
* @param string $primaryCurrencyCode The digital currency for which to retrieve order book. |
|
125
|
|
|
* Must be a valid primary currency, which can be checked via the |
|
126
|
|
|
* getValidPrimaryCurrencyCodes() method. |
|
127
|
|
|
* @param string $secondaryCurrencyCode The fiat currency in which to retrieve order book. Must |
|
128
|
|
|
* be a valid secondary currency, which can be checked via the |
|
129
|
|
|
* getValidSecondaryCurrencyCodes() method. |
|
130
|
|
|
* @return OrderBook |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function getOrderBook($primaryCurrencyCode, $secondaryCurrencyCode) |
|
133
|
|
|
{ |
|
134
|
1 |
|
return OrderBook::createFromObject($this->getEndpoint('GetOrderBook', [ |
|
135
|
1 |
|
'primaryCurrencyCode' => $primaryCurrencyCode, |
|
136
|
1 |
|
'secondaryCurrencyCode' => $secondaryCurrencyCode, |
|
137
|
1 |
|
])); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Returns summarised historical trading data for a given currency pair. Data is summarised into |
|
142
|
|
|
* 1 hour intervals. |
|
143
|
|
|
* @note This method caches return values for 30 minutes. Calling it more than once per 30 |
|
144
|
|
|
* minutes will result in cached data being returned. |
|
145
|
|
|
* @param string $primaryCurrencyCode The digital currency for which to retrieve the trade |
|
146
|
|
|
* history summary. Must be a valid primary currency, which can be checked via the |
|
147
|
|
|
* getValidPrimaryCurrencyCodes() method. |
|
148
|
|
|
* @param string $secondaryCurrencyCode The fiat currency in which to retrieve the trade history |
|
149
|
|
|
* summary. Must be a valid secondary currency, which can be checked via the |
|
150
|
|
|
* getValidSecondaryCurrencyCodes() method. |
|
151
|
|
|
* @param int $numberOfHoursInThePastToRetrieve How many past hours of historical summary data |
|
152
|
|
|
* to retrieve (maximum is 240). |
|
153
|
|
|
* @return TradeHistorySummary |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function getTradeHistorySummary($primaryCurrencyCode, $secondaryCurrencyCode, |
|
156
|
|
|
$numberOfHoursInThePastToRetrieve) |
|
157
|
|
|
{ |
|
158
|
1 |
|
return TradeHistorySummary::createFromObject($this->getEndpoint('GetTradeHistorySummary', [ |
|
159
|
1 |
|
'primaryCurrencyCode' => $primaryCurrencyCode, |
|
160
|
1 |
|
'secondaryCurrencyCode' => $secondaryCurrencyCode, |
|
161
|
1 |
|
'numberOfHoursInThePastToRetrieve' => $numberOfHoursInThePastToRetrieve, |
|
162
|
1 |
|
])); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Returns a list of most recently executed trades for a given currency pair. |
|
167
|
|
|
* @note This method caches return values for 1 second. Calling it more than once per second |
|
168
|
|
|
* will result in cached data being returned. |
|
169
|
|
|
* @param string $primaryCurrencyCode The digital currency for which to retrieve recent trades. |
|
170
|
|
|
* Must be a valid primary currency, which can be checked via the |
|
171
|
|
|
* getValidPrimaryCurrencyCodes() method. |
|
172
|
|
|
* @param string $secondaryCurrencyCode The fiat currency in which to retrieve recent trades. |
|
173
|
|
|
* Must be a valid secondary currency, which can be checked via the |
|
174
|
|
|
* getValidPrimaryCurrencyCodes() method. |
|
175
|
|
|
* @param integer $numberOfRecentTradesToRetrieve How many recent trades to retrieve (maximum |
|
176
|
|
|
* is 50). |
|
177
|
|
|
* @return RecentTrades |
|
178
|
|
|
*/ |
|
179
|
1 |
|
public function getRecentTrades($primaryCurrencyCode, $secondaryCurrencyCode, |
|
180
|
|
|
$numberOfRecentTradesToRetrieve) |
|
181
|
|
|
{ |
|
182
|
1 |
|
return RecentTrades::createFromObject($this->getEndpoint('GetRecentTrades', [ |
|
183
|
1 |
|
'primaryCurrencyCode' => $primaryCurrencyCode, |
|
184
|
1 |
|
'secondaryCurrencyCode' => $secondaryCurrencyCode, |
|
185
|
1 |
|
'numberOfRecentTradesToRetrieve' => $numberOfRecentTradesToRetrieve, |
|
186
|
1 |
|
])); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Returns a list of exchange rates used by Independent Reserve when depositing funds or |
|
191
|
|
|
* withdrawing funds from accounts. |
|
192
|
|
|
* @note The rates represent the amount of Currency Code B that can be bought with 1 unit of |
|
193
|
|
|
* Currency Code A. |
|
194
|
|
|
* @note This method caches return values for 1 minute. Calling it more than once per minute |
|
195
|
|
|
* will result in cached data being returned. |
|
196
|
|
|
* @return FxRate[] |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getFxRates() |
|
199
|
|
|
{ |
|
200
|
1 |
|
return array_map(function (stdClass $object) { |
|
201
|
1 |
|
return FxRate::createFromObject($object); |
|
202
|
1 |
|
}, $this->getEndpoint('GetFxRates')); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|