-
Notifications
You must be signed in to change notification settings - Fork 1
/
coinmarketcap.R
320 lines (285 loc) · 10.9 KB
/
coinmarketcap.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
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
#' coinmarketcap_api_call
#'
#' @param url the url for your CoinMarketCap API call
#' @param api_key your CoinMarketCap API key
#' @param method "GET" or "POST"
#' @param query your query parameters. The default value is NULL.
#' @param timeout_seconds seconds until the query times out. Default is 60.
#'
#' @return returns data from your CoinMarketCap API call
#' @export
#'
#' @examples
#' \dontrun{
#' url <- "https://pro-api.coinmarketcap.com/v1/cryptocurrency/map"
#' api_key <- "..."
#' query_string <- list(
#' listing_status = "active",
#' start = "1",
#' limit = NULL,
#' sort = "id",
#' symbol = NULL,
#' aux = "platform,first_historical_data,last_historical_data,is_active,status"
#' )
#' data <- coinmarketcap_api_call(url, api_key, 'GET', query = query_string)}
coinmarketcap_api_call <- function(url, api_key, method, query = NULL, timeout_seconds = 60){
tryCatch({
res <- httr::VERB(method
, url
, httr::add_headers('X-CMC_PRO_API_KEY' = api_key)
, httr::content_type("application/octet-stream")
, httr::accept("application/json")
, 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)
})
}
#' coinmarketcap_id_map
#'
#' @param api_key your CoinMarketCap API key
#' @param listing_status you can choose "active", "inactive", or "untracked".
#' Multiple options can be passed if they are comma-separated. Choosing "active"
#' will return only active cryptocurrencies. Choosing "inactive" will return
#' cryptocurrencies which are inactive. Choosing "untracked" will return a list
#' of cryptocurrencies which are listed by CoinMarketCap but do not yet meet
#' their methodology requirements to have tracked markets available. The default
#' is "active".
#' @param start you can use this parameter to offset your first result. The
#' default value is "1".
#' @param limit an optional string value between 1 and 5000 which tells
#' CoinMarketCap how many results to return. The default value is NULL.
#' @param sort the field used to sort your results. The two acceptable values
#' are "id" and "cmc_rank". The default value is "id".
#' @param symbol Optionally pass a comma-separated list of cryptocurrency
#' symbols to return CoinMarketCap IDs for. The default value is NULL.
#' @param aux Optionally specify a comma-separated list of supplemental data
#' fields to return. Pass "platform,first_historical_data,last_historical_data,
#' is_active,status" to include all auxiliary fields. This function will include
#' all auxiliary fields by default.
#' @param timeout_seconds seconds until the query times out. Default is 60.
#'
#' @return returns a dataframe which includes the id mapping for CoinMarketCap
#' cryptocurrencies along with other metadata related to the currencies.
#' @export
#'
#' @examples
#' \dontrun{
#' api_key <- "..."
#' id_map <- coinmarketcap_id_map(api_key)}
coinmarketcap_id_map <- function(api_key
, listing_status = 'active'
, start = '1'
, limit = NULL
, sort = 'id'
, symbol = NULL
, aux = 'platform,first_historical_data,last_historical_data,is_active,status'
, timeout_seconds = 60){
url <- 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/map'
query_string <- list(
listing_status = listing_status
, start = start
, limit = limit
, sort = sort
, symbol = symbol
, aux = aux
)
data <- coinmarketcap_api_call(url, api_key, 'GET', query = query_string, timeout_seconds)
if (is.null(data)) {
warning("Failed to retrieve data from CoinMarketCap API.")
return(NULL)
}
if (!is.null(data$data)) {
return(data$data)
} else {
warning("The response does not contain 'data'.")
return(NULL)
}
}
#' coinmarketcap_metadata
#'
#' @param api_key your CoinMarketCap API key
#' @param id the id of the asset you wish to query. The default value is NULL;
#' however, each request must include either an id, slug, symbol, or contract
#' address. You can also pass multiple comma-separated values.
#' @param slug the slug of the asset you wish to query. The default value is
#' NULL. You can also pass multiple comma-separated values.
#' @param symbol the symbol of the asset you wish to query. The default value is
#' NULL. You can also pass multiple comma-separated values.
#' @param address the contract address of the asset you wish to query.
#' The default calue is NULL. You can also pass multiple comma-separated values.
#' @param aux Optionally specify a comma-separated list of supplemental data
#' fields to return. Pass "urls,logo,description,tags,platform,date_added,
#' notice,status" to include all auxiliary fields. This function will include
#' all auxiliary fields by default.
#' @param timeout_seconds seconds until the query times out. Default is 60.
#'
#' @return returns a list which includes a dataframe for each asset you
#' requested. The dataframe will contain CoinMarketCap metadata for the asset.
#' @export
#'
#' @examples
#' \dontrun{
#' api_key <- "..."
#' metadata <- coinmarketcap_metadata(api_key, symbol = "BTC")}
coinmarketcap_metadata <- function(api_key
, id = NULL
, slug = NULL
, symbol = NULL
, address = NULL
, aux = 'urls,logo,description,tags,platform,date_added,notice,status'
, timeout_seconds = 60){
url <- 'https://pro-api.coinmarketcap.com/v2/cryptocurrency/info'
query_string <- list(
id = id
, slug = slug
, symbol = symbol
, address = address
, aux = aux
)
data <- coinmarketcap_api_call(url, api_key, 'GET', query = query_string, timeout_seconds)
if (is.null(data)) {
warning("Failed to retrieve data from CoinMarketCap API.")
return(NULL)
}
if (!is.null(data$data)) {
return(data$data)
} else {
warning("The response does not contain 'data'.")
return(NULL)
}
}
#' coinmarketcap_airdrop
#'
#' @param api_key your CoinMarketCap API key
#' @param id the unique airdrop id which can be found through the airdrops api.
#' @param timeout_seconds seconds until the query times out. Default is 60.
#'
#' @return returns information about the airdrop for the id you provided.
#' @export
#'
#' @examples
#' \dontrun{
#' api_key <- "..."
#' id <- "10744"
#' airdrop <- coinmarketcap_airdrop(api_key, id)}
coinmarketcap_airdrop <- function(api_key, id, timeout_seconds = 60){
url <- 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/airdrop'
query_string <- list(id = id)
data <- coinmarketcap_api_call(url, api_key, 'GET', query = query_string, timeout_seconds)
if (is.null(data)) {
warning("Failed to retrieve data from CoinMarketCap API.")
return(NULL)
} else {
return(data)
}
}
#' coinmarketcap_categories
#'
#' @param api_key your CoinMarketCap API key
#' @param start you can use this parameter to offset your first result. The
#' default value is "1".
#' @param limit an optional string value between 1 and 5000 which tells
#' CoinMarketCap how many results to return. The default value is NULL.
#' @param id filter categories by one or more asset ids. The default value is
#' NULL. Multiple values must be comma-separated.
#' @param slug filter categories by one or more asset slugs. The default value is
#' NULL. Multiple values must be comma-separated.
#' @param symbol filter categories by one or more asset symbols. The default
#' value is NULL. Multiple values must be comma-separated.
#' @param timeout_seconds seconds until the query times out. Default is 60.
#'
#' @return returns a datafrane with information about CoinMarketCap asset
#' categories.
#' @export
#'
#' @examples
#' \dontrun{
#' api_key <- "..."
#' categories <- coinmarketcap_categories(api_key)}
coinmarketcap_categories <- function(api_key
, start = "1"
, limit = NULL
, id = NULL
, slug = NULL
, symbol = NULL
, timeout_seconds = 60){
url <- 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/categories'
query_string <- list(
start = start
, limit = limit
, id = id
, slug = slug
, symbol = symbol
)
data <- coinmarketcap_api_call(url, api_key, 'GET', query = query_string, timeout_seconds)
if (is.null(data)) {
warning("Failed to retrieve data from CoinMarketCap API.")
return(NULL)
}
if (!is.null(data$data)) {
return(data$data)
} else {
warning("The response does not contain 'data'.")
return(NULL)
}
}
#' coinmarketcap_category
#'
#' @param api_key your CoinMarketCap API key
#' @param id the category id you wish to query.
#' @param start you can use this parameter to offset your first result. The
#' default value is "1".
#' @param limit an optional string value between 1 and 5000 which tells
#' CoinMarketCap how many results to return. The default value is NULL.
#' @param convert Optionally calculate market quotes in up to 120 currencies at
#' once by passing a comma-separated list of cryptocurrency or fiat currency
#' symbols.
#' @param convert_id Optionally calculate market quotes by CoinMarketCap id
#' instead of symbol.
#' @param timeout_seconds seconds until the query times out. Default is 60.
#'
#' @return returns a list with information about the specified category.
#' @export
#'
#' @examples
#' \dontrun{
#' api_key <- "..."
#' id <- "6363a6c9cd197958bb543bf0"
#' category <- coinmarketcap_category(api_key, id)}
coinmarketcap_category <- function(api_key
, id
, start = "1"
, limit = NULL
, convert = NULL
, convert_id = NULL
, timeout_seconds = 60){
url <- 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/category'
query_string <- list(
id = id
, start = start
, limit = limit
, convert = convert
, convert_id = convert_id
)
data <- coinmarketcap_api_call(url, api_key, 'GET', query = query_string, timeout_seconds)
if (is.null(data)) {
warning("Failed to retrieve data from CoinMarketCap API.")
return(NULL)
}
if (!is.null(data$data)) {
return(data$data)
} else {
warning("The response does not contain 'data'.")
return(NULL)
}
}