-
Notifications
You must be signed in to change notification settings - Fork 1
/
kucoin.R
288 lines (262 loc) · 8.7 KB
/
kucoin.R
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
#' kucoin_time
#'
#' @return returns a timestamp formatted in the way it is required in order to
#' make an API call to Kucoin.
#' @export
#'
#' @examples
#' kucoin_time()
kucoin_time <- function() {
old <- options()
on.exit(options(old))
op <- options(digits.secs=0)
tm <- as.POSIXlt(Sys.time(), "UTC")
formatted_time <- round(as.numeric(as.POSIXct(tm))) * 1000
return(as.character(formatted_time))
}
#' kucoin_signature
#'
#' @param api_secret your Kucoin API secret
#' @param time a timestamp string formatted the way Kucoin requires. This can be
#' created with the "kucoin_time" function.
#' @param method "GET" or "POST"
#' @param path the endpoint you are using to make an API call.
#' @param body needs to be a json string which matches url parameters. Use a
#' blank string if not applicable.
#'
#' @return returns a signature for use in you Kucoin API calls.
#' @export
#'
#' @examples
#' \dontrun{
#' api_secret <- "..."
#' time <- kucoin_time()
#' method <- "GET"
#' path <- "/api/v1/sub/user"
#' body <- ""
#' sig <- kucoin_signature(api_secret, time, method, path, body)}
kucoin_signature <- function(api_secret, time, method, path, body) {
api_secret <- stringi::stri_enc_toutf8(api_secret)
message <- paste(time, method, path, body, sep = '')
sig <- digest::hmac(api_secret
, message
, "sha256"
, serialize = FALSE
, raw = TRUE
)
encoded_sig <- openssl::base64_encode(sig)
return(encoded_sig)
}
#' kucoin_api_call
#'
#' @param url the full url for your Kucoin API call
#' @param method "GET" or "POST"
#' @param api_key your Kucoin API key
#' @param sig signature for use in your Kucoin API call. This can be generated
#' with the "kucoin_signature" function.
#' @param time a timestamp string formatted the way Kucoin requires. This can be
#' created with the "kucoin_time" function.
#' @param passphrase the passphrase you created when you created your Kucoin API
#' key.
#' @param version your API key version. This can be retrieved from your Kucoin
#' API console.
#' @param api_secret your Kucoin API secret.
#' @param query a named list containing your query parameters.
#' @param timeout_seconds seconds until the query times out. Default is 60.
#'
#' @return returns the data from your Kucoin API call.
#' @export
#'
#' @examples
#' \dontrun{
#' url <- "https://api.kucoin.com/api/v1/sub/user"
#' api_key <- "..."
#' api_secret <- "..."
#' time <- kucoin_time()
#' method <- "GET"
#' path <- "/api/v1/sub/user"
#' body <- ""
#' sig <- kucoin_signature(api_secret, time, method, path, body)
#' passphrase <- "..."
#' version <- "2"
#'
#' accounts <- kucoin_api_call(url, method, api_key, sig, time, passphrase,
#' version, api_secret)}
kucoin_api_call <- function(url
, method
, api_key
, sig
, time
, passphrase
, version
, api_secret
, query = NULL
, timeout_seconds = 60){
if (version == '2') {
api_secret <- stringi::stri_enc_toutf8(api_secret)
passphrase <- stringi::stri_enc_toutf8(passphrase)
passphrase <- digest::hmac(api_secret
, passphrase
, "sha256"
, serialize = FALSE
, raw = TRUE)
passphrase <- openssl::base64_encode(passphrase)
}
tryCatch({
res <- httr::VERB(method
, url
, httr::accept("application/json")
, httr::add_headers('KC-API-KEY' = api_key
, 'KC-API-SIGN' = sig
, 'KC-API-TIMESTAMP' = time
, 'KC-API-PASSPHRASE' = passphrase
, 'KC-API-KEY-VERSION' = version
)
, httr::timeout(timeout_seconds)
, query = query
)
if (res$status_code == 200) {
data <- jsonlite::fromJSON(rawToChar(res$content))
return(data)
} else {
stop(paste("API call failed with status code", res$status_code))
}
}, error = function(e) {
message <- paste("Error during API call:", e$message)
warning(message)
return(NULL)
})
}
#' kucoin_subaccounts
#'
#' @param api_key your Kucoin API key.
#' @param api_secret your Kucoin API secret.
#' @param passphrase the passphrase you created when you created your Kucoin API
#' key.
#' @param version your API key version. This can be retrieved from your Kucoin
#' API console. The default value is "2".
#' @param timeout_seconds seconds until the query times out. Default is 60.
#'
#' @return returns a list containing your Kucoin sub-accounts.
#' @export
#'
#' @examples
#' \dontrun{
#' api_key <- "..."
#' api_secret <- "..."
#' passphrase <- "..."
#' accounts <- kucoin_subaccounts(api_key, api_secret, passphrase)}
kucoin_subaccounts <- function(api_key, api_secret, passphrase, version = "2", timeout_seconds = 60){
url <- "https://api.kucoin.com/api/v1/sub/user"
time <- kucoin_time()
method <- "GET"
path <- "/api/v1/sub/user"
body <- ""
sig <- kucoin_signature(api_secret, time, method, path, body)
data <- kucoin_api_call(url
, method
, api_key
, sig
, time
, passphrase
, version
, api_secret
, timeout_seconds = timeout_seconds)
if (is.null(data)) {
warning("Failed to retrieve data from Kucoin API.")
return(NULL)
}
if (!is.null(data$data)) {
return(data$data)
} else {
warning("The response does not contain 'data'.")
return(NULL)
}
}
#' kucoin_accounts
#'
#' @param api_key your Kucoin API key.
#' @param api_secret your Kucoin API secret.
#' @param passphrase the passphrase you created when you created your Kucoin API
#' key.
#' @param version your API key version. This can be retrieved from your Kucoin
#' API console. The default value is "2".
#' @param timeout_seconds seconds until the query times out. Default is 60.
#'
#' @return returns a dataframe containing your Kucoin accounts and balances.
#' @export
#'
#' @examples
#' \dontrun{
#' api_key <- "..."
#' api_secret <- "..."
#' passphrase <- "..."
#' accounts <- kucoin_accounts(api_key, api_secret, passphrase)}
kucoin_accounts <- function(api_key
, api_secret
, passphrase
, version = '2'
, timeout_seconds = 60){
url <- 'https://api.kucoin.com/api/v1/accounts'
time <- kucoin_time()
method <- 'GET'
path <- '/api/v1/accounts'
body <- ''
sig <- kucoin_signature(api_secret, time, method, path, body)
data <- kucoin_api_call(url
, method
, api_key
, sig
, time
, passphrase
, version
, api_secret
, timeout_seconds = timeout_seconds)
if (is.null(data)) {
warning("Failed to retrieve data from Kucoin API.")
return(NULL)
}
if (!is.null(data$data)) {
return(data$data)
} else {
warning("The response does not contain 'data'.")
return(NULL)
}
}
#' kucoin_symbols_list
#'
#' @param market optionally provide a market to filter on. This function will
#' return all markets by default.
#' @param timeout_seconds seconds until the query times out. Default is 60.
#'
#' @return returns a dataframe containing information about trading symbols
#' @export
#'
#' @examples
#' kucoin_symbols_list('btc', 4.5)
kucoin_symbols_list <- function(market = NULL, timeout_seconds = 60){
query <- list(market = toupper(market))
tryCatch({
res <- httr::VERB('GET'
, 'https://api.kucoin.com/api/v2/symbols'
, httr::accept("application/json")
, httr::timeout(timeout_seconds)
, query = query
)
if (res$status_code == 200) {
data <- jsonlite::fromJSON(rawToChar(res$content))
if (!is.null(data$data)) {
return(data$data)
} else {
warning("The response does not contain 'data'.")
return(NULL)
}
} else {
stop(paste("API call failed with status code", res$status_code))
}
}, error = function(e) {
message <- paste("Error during API call:", e$message)
warning(message)
return(NULL)
})
}