-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproviders.js
212 lines (195 loc) · 6.36 KB
/
providers.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
const axios = require('axios')
const cache = require('./cache')
// get bitcoin's average price against various fiat currencies
exports.BTCCoingecko = function(url, [...currencies]) {
const output = {}
const cacheRef = '_cachedCoingeckoFor_' + currencies
return new Promise(resolve => {
cache.get(cacheRef, function(error, data) {
if (error) throw error
// if the data is in cache, return that
if (!!data) {
resolve(JSON.parse(data))
} else {
axios.get(url + '&vs_currencies=' + currencies.join())
.then(async result => {
// for each currency passed into this function, we add a key/value to output (ex. USD: 6500.12345)
for (var currency of currencies) {
// use the "last" price from bitcoinaverage to give us the most recent exchange rate
output[currency] = result.data['bitcoin'][currency.toLowerCase()]
}
// set the cache for this response and save for 60 seconds
cache.setex(cacheRef, 60, JSON.stringify(output));
// resolve an object containing all requested currencies
resolve(output)
})
.catch(error => {
console.log(`Error: ${error}`)
resolve(error)
})
}
})
})
}
// get dash's average trading price from various exchanges
exports.DASHCryptoCompareAvg = function(url) {
let output = {}
const cacheRef = '_cachedCryptoCompareAvg'
return new Promise(resolve => {
cache.get(cacheRef, function(error, data) {
if (error) throw error
// if the data is in cache, return that
if (!!data) {
resolve(JSON.parse(data))
console.log('Grabbed _cachedCryptoCompareAvg')
} else {
axios.get(url)
.then(async result => {
// the output should equal the average price (ex. 0.02528)
output = parseFloat(result.data.RAW.PRICE)
// set the cache for this response and save for 60 seconds
cache.setex(cacheRef, 60, JSON.stringify(output))
// resolve an object containing all requested currencies
resolve(output)
})
.catch(error => {
console.log(`Error: ${error}`)
resolve(error)
})
}
})
})
}
// get the current DASH trading price from Poloniex
exports.DASHPoloniex = function(url) {
const cacheRef = '_cachedPoloniexDash'
return new Promise(resolve => {
cache.get(cacheRef, function(error, data) {
if (error) throw error
// if the data is in cache, return that
if (!!data) {
resolve(JSON.parse(data))
console.log('Grabbed _cachedPoloniexDash')
} else {
axios.get(url)
.then(result => {
let total = 0
let amount = 0
// loop through the results and get the total BTC traded, and the amount of DASH traded
for (var i = 0; i < result.data.length; i++) {
total += parseFloat(result.data[i].total)
amount += parseFloat(result.data[i].amount)
}
// get the average price paid for the last 200 trades
let average = total / amount
// set the cache for this response and save for 60 seconds
cache.setex(cacheRef, 60, JSON.stringify(average))
resolve(average)
})
.catch(error => {
console.log(`Error: ${error}`)
resolve(false)
})
}
})
})
}
// get the current BTC/DASH price - we grab the "last" price from bitcoinaverage to get the most recent exchange rate
exports.CoingeckoDashBtc = function (url) {
const cacheRef = '_cachedDashBTC'
return new Promise(resolve => {
cache.get(cacheRef, function(error, data) {
if (error) throw error
// if the data is in cache, return that
if (!!data) {
resolve(JSON.parse(data))
console.log('Grabbed _cachedDashBTC')
} else {
axios.get(url)
.then(result => {
const last = result.data['dash']['btc']
// set the cache for this response and save for 60 seconds
cache.setex(cacheRef, 60, JSON.stringify(last))
resolve(last)
})
.catch(error => {
console.log(`Error: ${error}`)
resolve(error)
})
}
})
})
}
//get the current BTC/VES price from LocalBitcoins API
exports.BTCLocalBitcoinsVes = function (url) {
const cacheRef = '_cachedDashLocalBitcoinsVes'
return new Promise(resolve => {
cache.get(cacheRef, function (error, data) {
if (error) throw error
// if the data is in cache, return that
if (!!data) {
resolve(JSON.parse(data))
console.log(`Grabbed _${cacheRef}`)
} else {
axios.get(url)
.then(result => {
const btcVes = parseFloat(result.data['VES']['rates']['last'])
// set the cache for this response and save for 60 seconds
cache.setex(cacheRef, 60, btcVes)
resolve(btcVes)
}).catch(error => {
console.log(`Error: ${error}`)
resolve(error)
})
}
})
})
}
// get invoice number from CoinText
exports.invoice = function(address, amount) {
return new Promise(resolve => {
const data = {
'address': address,
'amount': amount,
'network': 'dash',
'api_key': process.env.API_KEY
}
const headers = {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
}
axios.post('https://pos-api.cointext.io/create_invoice/', data, headers)
.then((res) => {
console.log(res.data)
resolve(res.data.paymentId)
})
.catch((err) => {
console.log(err)
resolve(err)
})
})
}
// get invoice number from DashText
exports.dashText = function(address, amount) {
return new Promise(resolve => {
const token = process.env.DASH_TEXT
const data = `address=${address}&amount=${amount}&token=${token}`
const headers = {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
}
axios.post('https://api.dashtext.io/apibuy.php', data)
.then((res) => {
console.log(res.data)
resolve(res.data.code)
})
.catch((err) => {
console.log(err)
resolve(err)
})
})
}