-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb_upbit_lib.js
294 lines (251 loc) · 7.62 KB
/
b_upbit_lib.js
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
//https://docs.upbit.com/v1.0.3/
const rp = require("request-promise")
const sign = require("jsonwebtoken").sign
const queryEncode = require("querystring").encode
async function request(url, qs, token, method) {
if (!method) {
method = 'GET'
}
let options = {
method: method,
url: url,
json: true,
transform: function (body, response) {
let remain_min = 0
let remain_sec = 0
if (response.headers && response.headers['remaining-req']) {
let items = response.headers['remaining-req'].split(';')
for (let item of items) {
let [key, val] = item.split('=')
if (key.trim()=='min') {
remain_min = parseInt(val.trim())
} else if (key.trim()=='sec') {
remain_sec = parseInt(val.trim())
}
}
}
return {
'success': true,
'remain_min': remain_min,
'remain_sec': remain_sec,
'data': body
}
}
}
if (method=='POST') {
options.json = qs
} else {
options.qs = qs
}
if (token) {
options.headers = {Authorization: `Bearer ${token}`}
}
let result = {'success': false, 'message': null, 'name': null}
try {
result = await rp(options)
} catch(e) {
result.data = null
if (e.error.error) {
result.message = e.error.error.message
result.name = e.error.error.name
} else {
result.message = e.message
}
}
return result
}
//전체 계좌 조회
async function accounts() {
const url = 'https://api.upbit.com/v1/accounts'
const payload = {
access_key: this.accessKey,
nonce: (new Date).getTime(),
}
const token = sign(payload, this.secretKey)
let result = await request(url, {}, token)
return result
}
// 주문 리스트
async function order_list(market, state, uuids, page) {
//market: null KRW-BTC
//state: wait done
const url = 'https://api.upbit.com/v1/orders'
let qs = {state:state, page:page}
if (market) qs['market'] = market
if (uuids) qs['uuids'] = uuids
const query = queryEncode(qs)
const payload = {
access_key: this.accessKey,
nonce: (new Date).getTime(),
query: query
}
const token = sign(payload, this.secretKey)
let result = await request(url, qs, token)
return result
}
// 주문(매수)
async function order_bid(market, volume, price) {
//market: KRW-BTC
const url = 'https://api.upbit.com/v1/orders'
let qs = {market:market, side:'bid', volume:volume, price:price, ord_type:'limit'}
const query = queryEncode(qs)
const payload = {
access_key: this.accessKey,
nonce: (new Date).getTime(),
query: query
}
const token = sign(payload, this.secretKey)
let result = await request(url, qs, token, 'POST')
return result
}
// 주문(매도)
async function order_ask(market, volume, price) {
//market: KRW-BTC
const url = 'https://api.upbit.com/v1/orders'
let qs = {market:market, side:'ask', volume:volume, price:price, ord_type:'limit'}
const query = queryEncode(qs)
const payload = {
access_key: this.accessKey,
nonce: (new Date).getTime(),
query: query
}
const token = sign(payload, this.secretKey)
let result = await request(url, qs, token, 'POST')
return result
}
// 주문 상세
async function order_detail(uuid) {
const url = 'https://api.upbit.com/v1/order'
let qs = {uuid:uuid}
const query = queryEncode(qs)
const payload = {
access_key: this.accessKey,
nonce: (new Date).getTime(),
query: query
}
const token = sign(payload, this.secretKey)
let result = await request(url, qs, token)
return result
}
// 주문 취소
async function order_delete(uuid) {
const url = 'https://api.upbit.com/v1/order'
let qs = {uuid:uuid}
const query = queryEncode(qs)
const payload = {
access_key: this.accessKey,
nonce: (new Date).getTime(),
query: query
}
const token = sign(payload, this.secretKey)
let result = await request(url, qs, token, 'DELETE')
return result
}
// 주문 가능 정보
async function order_chance(market) {
const url = 'https://api.upbit.com/v1/orders/chance'
let qs = {market:market}
const query = queryEncode(qs)
const payload = {
access_key: this.accessKey,
nonce: (new Date).getTime(),
query: query
}
const token = sign(payload, this.secretKey)
let result = await request(url, qs, token)
return result
}
// 시세종목정보
async function market_all() {
const url = 'https://api.upbit.com/v1/market/all'
let result = await request(url)
return result
}
// 분 캔들
async function market_minute(market, unit, to, count) {
//unit: 1, 3, 5, 15, 10, 30, 60, 240
//to: yyyy-MM-dd'T'HH:mm:ssXXX
const url = 'https://api.upbit.com/v1/candles/minutes/'+unit
let qs = {market:market}
if (to) qs.to = to
if (count) qs.count = count
let result = await request(url, qs)
return result
}
// 일 캔들
async function market_day(market, to, count) {
//to: yyyy-MM-dd'T'HH:mm:ssXXX
const url = 'https://api.upbit.com/v1/candles/days'
let qs = {market:market}
if (to) qs.to = to
if (count) qs.count = count
let result = await request(url, qs)
return result
}
// 주 캔들
async function market_week(market, to, count) {
//to: yyyy-MM-dd'T'HH:mm:ssXXX
const url = 'https://api.upbit.com/v1/candles/weeks'
let qs = {market:market}
if (to) qs.to = to
if (count) qs.count = count
let result = await request(url, qs)
return result
}
// 월 캔들
async function market_month(market, to, count) {
//to: yyyy-MM-dd'T'HH:mm:ssXXX
const url = 'https://api.upbit.com/v1/candles/months'
let qs = {market:market}
if (to) qs.to = to
if (count) qs.count = count
let result = await request(url, qs)
return result
}
// 채결 정보
async function market_trade_tick(market, to, count) {
//to: yyyy-MM-dd'T'HH:mm:ssXXX
const url = 'https://api.upbit.com/v1/trades/ticks'
let qs = {market:market}
if (to) qs.to = to
if (count) qs.count = count
let result = await request(url, qs)
return result
}
// 시세 Ticker
async function market_ticker(markets) {
// markets: KRW-BTC,KRW-ETH
const url = 'https://api.upbit.com/v1/ticker'
let qs = {markets:markets}
let result = await request(url, qs)
return result
}
// 호가 정보
async function trade_orderbook(markets) {
// markets: KRW-BTC,KRW-ETH
const url = 'https://api.upbit.com/v1/orderbook'
let qs = {markets:markets}
let result = await request(url, qs)
return result
}
// class Upbit
function Upbit(s, a) {
this.secretKey = s
this.accessKey = a
}
Upbit.prototype.accounts = accounts
Upbit.prototype.order_list = order_list
Upbit.prototype.order_bid = order_bid
Upbit.prototype.order_ask = order_ask
Upbit.prototype.order_detail = order_detail
Upbit.prototype.order_delete = order_delete
Upbit.prototype.order_chance = order_chance
Upbit.prototype.market_all = market_all
Upbit.prototype.market_minute = market_minute
Upbit.prototype.market_day = market_day
Upbit.prototype.market_week = market_week
Upbit.prototype.market_month = market_month
Upbit.prototype.market_trade_tick = market_trade_tick
Upbit.prototype.market_ticker = market_ticker
Upbit.prototype.trade_orderbook = trade_orderbook
module.exports = Upbit