-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpbitAPI.php
More file actions
356 lines (297 loc) · 10.3 KB
/
UpbitAPI.php
File metadata and controls
356 lines (297 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
use Exception;
use Firebase\JWT\JWT;
/**
* Upbit Api
* Upbit 서버와 통신을 하는 메소드로 구성
*/
class UpbitApi
{
const POST = "POST";
const GET = "GET";
const PUT = "PUT";
const DELETE = "DELETE";
const HTTP_CODE_200 = "200";
const SIDE_BID = "bid"; // 매수
const SIDE_ASK = "ask"; // 매도
const ORD_TYPE_LIMIT = "limit"; // 지정가 주문
const ORD_TYPE_PRICE = "price"; // 시장가 주문 (매수)
const ORD_TYPE_MARKET = "market"; // 시장가 주문 (매도)
private $accessKey;
private $secretKey;
private \GuzzleHttp\Client $client;
/**
* 생성자
*/
public function __construct(string $accessKey, string $secretKey)
{
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
$this->client = new \GuzzleHttp\Client(['base_uri' => 'https://api.upbit.com/v1/']);
}
/**
* 마켓 코드 조회
*
* @return array
*/
public function getMarketList(): array
{
$uri = "market/all?isDetails=false";
return $this->send(self::GET, $uri);
}
/**
* 분 Candle 정보 조회
*
* @param int $unit 분 단위 1 3 5 15 10 30 60 240 분 캔들 조회
* @param string $market 마켓 코드
* @param string|null $to 마지막 캔들 시각 포맷 (yyyy-MM-dd'T'HH:mm:ss'Z') 또는 (yyyy-MM-dd HH:mm:ss), 빈 값 요청시 가장 최근 캔들
* @param int|null $count 캔들 갯수(최대 200개)
* @return array
*/
public function getCandlesMinutes(int $unit, string $market, ?string $to = null, ?int $count = null): array
{
$uri = "candles/minutes/{$unit}?market={$market}";
$uri .= $to ? "&to={$to}" : "";
$uri .= $count ? "&count={$count}" : "";
return $this->send(self::GET, $uri);
}
/**
* 일 Candle 정보 조회
*
* @param string $market 마켓 코드
* @param integer|null $count 캔들 갯수(최대 200개)
* @param string|null $to 마지막 캔들 시각 포맷 (yyyy-MM-dd'T'HH:mm:ss'Z') 또는 (yyyy-MM-dd HH:mm:ss), 빈 값 요청시 가장 최근 캔들
* @param string|null $convertingPriceUnit 종가 환산 화폐 단위 (생략 가능 KRW로 명시할 시 원화 환산 가격을 반환)
* @return array
*/
public function getCandlesDays(string $market, ?int $count = null, ?string $to = null, ?string $convertingPriceUnit): array
{
$uri = "candles/days?market={$market}";
$uri .= $count ? "&count={$count}" : "";
$uri .= $to ? "&to={$to}" : "";
$uri .= $convertingPriceUnit ? "&convertingPriceUnit={$convertingPriceUnit}" : "";
return $this->send(self::GET, $uri);
}
/**
* 주 Candle 정보 조회
*
* @param string $market 마켓 코드
* @param integer|null $count 캔들 갯수(최대 200개)
* @param string|null $to 마지막 캔들 시각 포맷 (yyyy-MM-dd'T'HH:mm:ss'Z') 또는 (yyyy-MM-dd HH:mm:ss), 빈 값 요청시 가장 최근 캔들
* @return array
*/
public function getCandlesWeeks(string $market, ?int $count = null, ?string $to = null): array
{
$uri = "candles/weeks?market={$market}";
$uri .= $count ? "&count={$count}" : "";
$uri .= $to ? "&to={$to}" : "";
return $this->send(self::GET, $uri);
}
/**
* 월 Candle 정보 조회
*
* @param string $market 마켓 코드
* @param integer|null $count 캔들 갯수(최대 200개)
* @param string|null $to 마지막 캔들 시각 포맷 (yyyy-MM-dd'T'HH:mm:ss'Z') 또는 (yyyy-MM-dd HH:mm:ss), 빈 값 요청시 가장 최근 캔들
* @return array
*/
public function getCandlesMonths(string $market, ?int $count = null, ?string $to = null): array
{
$uri = "candles/months?market={$market}";
$uri .= $count ? "&count={$count}" : "";
$uri .= $to ? "&to={$to}" : "";
return $this->send(self::GET, $uri);
}
/**
* 계좌정보 조회
*
* @return array
*/
public function getAccounts(): array
{
$uri = "accounts";
return $this->send(self::GET, $uri);
}
/**
* 주문
*
* @param string $market
* @param string $side
* @param string $ord_type
* @param float|null $volume
* @param float|null $price
* @param string|null $identifier
* @return array
*/
public function order(string $market, string $side, string $ord_type, ?float $volume = null, ?float $price = null, ?string $identifier = null): array
{
$uri = "orders";
// 검증
if ($side == self::SIDE_BID && $ord_type == self::ORD_TYPE_MARKET) {
throw new Exception("매수 주문 인 경우 ord_type 이 market이 될 수 없습니다.");
} else if ($side == self::SIDE_ASK && $ord_type == self::ORD_TYPE_PRICE) {
throw new Exception("매도 주문 인 경우 ord_type 이 price가 될 수 없습니다.");
}
if ($side == self::SIDE_BID && $ord_type == self::ORD_TYPE_PRICE && (empty($price) || $price == 0)) {
throw new Exception("시장가 매수 주문 인 경우 price 의 값이 필요로 합니다.");
} else if ($side == self::SIDE_ASK && $ord_type == self::ORD_TYPE_MARKET && (empty($volume) || $volume == 0)) {
throw new Exception("시장가 매도 주문 인 경우 volume 의 값이 필요로 합니다.");
}
if ($identifier == null) {
$identifier = "{$market}_{$side}_{$ord_type}_" . date("Ymdhis");
}
$queryParam = [
"market" => $market,
"side" => $side,
"volume" => $volume ?? null,
"price" => $price ?? null,
"ord_type" => $ord_type,
"identifier" => $identifier,
];
return $this->send(self::POST, $uri, $queryParam);
}
/**
* 주문 취소
*
* @param string|null $uuid
* @param string|null $identifier
* @return array
*/
public function orderCancel(?string $uuid = null, ?string $identifier = null): array
{
$uri = "order";
// 검증
if ($uuid == null && $identifier == null) {
throw new Exception("uuid 또는 identifier 둘 중에 하나는 필요로 합니다.");
}
$queryParam = [
"uuid" => $uuid,
"identifier" => $identifier,
];
return $this->send(self::DELETE, $uri, $queryParam);
}
/**
* 주문 가능 정보
*
* @param string $market
* @return array
*/
public function orderChance(string $market): array
{
$uri = "orders/chance";
$queryParam = [
"market" => $market,
];
return $this->send(self::GET, $uri, $queryParam);
}
/**
* 주문 단건 조회
*
* @param string|null $uuid
* @param string|null $identifier
* @return array
*/
public function orderInfo(?string $uuid = null, ?string $identifier = null): array
{
$uri = "order";
// 검증
if ($uuid == null && $identifier == null) {
throw new Exception("uuid 또는 identifier 둘 중에 하나는 필요로 합니다.");
}
$queryParam = [
"uuid" => $uuid,
"identifier" => $identifier,
];
return $this->send(self::GET, $uri, $queryParam);
}
/**
* 주문 내역 조회
*
* @param string|null $market
* @param array|null $uuids
* @param array|null $identifiers
* @param string|null $state
* @param array|null $states
* @param integer|null $page
* @param integer|null $limit
* @param string|null $order_by
* @return array
*/
public function orderList(?string $market = null, ?array $uuids = null, ?array $identifiers = null, ?string $state = null, ?array $states = null, ?int $page = null, ?int $limit = null, ?string $order_by = null): array
{
$uri = "orders";
$queryParam = [
"market" => $market,
"uuids" => $uuids,
"identifiers" => $identifiers,
"state" => $state,
"states" => $states,
"page" => $page,
"limit" => $limit,
"order_by" => $order_by,
];
// 값이 없으면 키를 제거해준다.
$queryParam = array_filter($queryParam);
print_r($queryParam);
if (empty($queryParam)) {
$queryParam = null;
}
return $this->send(self::GET, $uri, $queryParam);
}
/**
* UPBIT 토큰 만드는 함수
*
* @param array|null $queryParam
* @return string
*/
private function makeToken(?array $queryParam = null): string
{
$addPayload = [];
if ($queryParam !== null) {
$queryString = http_build_query($queryParam);
$query_hash = hash('sha512', $queryString, false);
$addPayload = [
"query_hash" => $query_hash,
"query_hash_alg" => "SHA512",
];
}
$payload = [
"access_key" => $this->accessKey,
"nonce" => "SSR_" . uniqid(),
];
$mergePayload = array_merge($payload, $addPayload);
$token = JWT::encode($mergePayload, $this->secretKey);
return $token;
}
/**
* UPBIT API 서버와 통신
*
* @param string $method
* @param string $uri
* @param array|null $param
* @return array
*/
private function send(string $method = self::GET, string $uri, ?array $param = null): array
{
$token = $this->makeToken($param);
$form_params = array();
if ($param !== null) {
$form_params = array('form_params' => $param);
}
// basic option
$options = array(
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer " . $token,
],
);
if (!empty($form_params)) {
$options = array_merge($options, $form_params);
}
$response = $this->client->request($method, $uri, $options);
if ($response->getStatusCode() != self::HTTP_CODE_200) {
throw new Exception("Resopnse Code is not 200", 0);
}
return json_decode($response->getBody(), true);
}
}