forked from ExodusMovement/cryptocompare
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
118 lines (104 loc) · 4.12 KB
/
index.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
'use strict'
/* global fetch */
const baseUrl = 'https://min-api.cryptocompare.com/data/'
function fetchJSON (url) {
return fetch(url)
.then(res => res.json())
.then(body => {
if (body.Response === 'Error') throw body.Message
return body
})
}
function price (fsym, tsyms, options) {
options = options || {}
let url = `${baseUrl}price?fsym=${fsym}&tsyms=${tsyms}`
if (options.exchanges) url += `&e=${options.exchanges}`
if (options.tryConversion === false) url += '&tryConversion=false'
return fetchJSON(url)
}
function priceMulti (fsyms, tsyms, options) {
options = options || {}
let url = `${baseUrl}pricemulti?fsyms=${fsyms}&tsyms=${tsyms}`
if (options.exchanges) url += `&e=${options.exchanges}`
if (options.tryConversion === false) url += '&tryConversion=false'
return fetchJSON(url)
}
function priceFull (fsyms, tsyms, options) {
options = options || {}
let url = `${baseUrl}pricemultifull?fsyms=${fsyms}&tsyms=${tsyms}`
if (options.exchanges) url += `&e=${options.exchanges}`
if (options.tryConversion === false) url += '&tryConversion=false'
// We want the RAW data, not the DISPLAY data:
return fetchJSON(url).then(result => result.RAW)
}
function priceHistorical (fsym, tsyms, time, options) {
options = options || {}
time = dateToTimestamp(time)
let url = `${baseUrl}pricehistorical?fsym=${fsym}&tsyms=${tsyms}&ts=${time}`
if (options.exchanges) url += `&e=${options.exchanges}`
if (options.tryConversion === false) url += '&tryConversion=false'
// The API returns json with an extra layer of nesting, so remove it
return fetchJSON(url).then(result => result[fsym])
}
function generateAvg (fsym, tsym, markets, tryConversion) {
let url = `${baseUrl}generateAvg?fsym=${fsym}&tsym=${tsym}&markets=${markets}`
if (tryConversion === false) url += '&tryConversion=false'
return fetchJSON(url).then(result => result.RAW)
}
function topPairs (fsym, limit) {
let url = `${baseUrl}top/pairs?fsym=${fsym}`
if (limit) url += `&limit=${limit}`
return fetchJSON(url).then(result => result.Data)
}
function topExchanges (fsym, tsym, limit) {
let url = `${baseUrl}top/exchanges?fsym=${fsym}&tsym=${tsym}`
if (limit) url += `&limit=${limit}`
return fetchJSON(url).then(result => result.Data)
}
function histoDay (fsym, tsym, options) {
options = options || {}
if (options.timestamp) options.timestamp = dateToTimestamp(options.timestamp)
let url = `${baseUrl}histoday?fsym=${fsym}&tsym=${tsym}`
if (options.limit === 'none') url += '&allData=true'
else if (options.limit) url += `&limit=${options.limit}`
if (options.tryConversion === false) url += '&tryConversion=false'
if (options.aggregate) url += `&aggregate=${options.aggregate}`
if (options.timestamp) url += `&toTs=${options.timestamp}`
return fetchJSON(url).then(result => result.Data)
}
function histoHour (fsym, tsym, options) {
options = options || {}
if (options.timestamp) options.timestamp = dateToTimestamp(options.timestamp)
let url = `${baseUrl}histohour?fsym=${fsym}&tsym=${tsym}`
if (options.limit) url += `&limit=${options.limit}`
if (options.tryConversion === false) url += '&tryConversion=false'
if (options.aggregate) url += `&aggregate=${options.aggregate}`
if (options.timestamp) url += `&toTs=${options.timestamp}`
return fetchJSON(url).then(result => result.Data)
}
function histoMinute (fsym, tsym, options) {
options = options || {}
if (options.timestamp) options.timestamp = dateToTimestamp(options.timestamp)
let url = `${baseUrl}histominute?fsym=${fsym}&tsym=${tsym}`
if (options.limit) url += `&limit=${options.limit}`
if (options.tryConversion === false) url += '&tryConversion=false'
if (options.aggregate) url += `&aggregate=${options.aggregate}`
if (options.timestamp) url += `&toTs=${options.timestamp}`
return fetchJSON(url).then(result => result.Data)
}
function dateToTimestamp (date) {
if (!(date instanceof Date)) throw new Error('timestamp must be an instance of Date.')
return Math.floor(date.getTime() / 1000)
}
module.exports = {
price,
priceMulti,
priceFull,
priceHistorical,
generateAvg,
topPairs,
topExchanges,
histoDay,
histoHour,
histoMinute
}