2018-01-19 17:33:27 +00:00
|
|
|
"use strict"
|
|
|
|
|
2018-03-19 21:37:38 +00:00
|
|
|
const https = require("https")
|
|
|
|
const config = require("../config")
|
2018-01-24 22:37:39 +00:00
|
|
|
|
2018-01-19 17:33:27 +00:00
|
|
|
|
2018-01-25 00:06:50 +00:00
|
|
|
const getGasPrice = function () {
|
2018-03-19 21:37:38 +00:00
|
|
|
const url = 'https://ethgasstation.info/json/ethgasAPI.json'
|
2018-01-19 17:33:27 +00:00
|
|
|
// return new pending promise
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// select http or https module, depending on reqested url
|
2018-03-19 21:37:38 +00:00
|
|
|
const lib = url.startsWith('https') ? require('https') : require('http')
|
2018-01-19 17:33:27 +00:00
|
|
|
const request = lib.get(url, (response) => {
|
|
|
|
// handle http errors
|
|
|
|
if (response.statusCode < 200 || response.statusCode > 299) {
|
2018-03-19 21:37:38 +00:00
|
|
|
reject('Failed to load page, status code: ' + response.statusCode)
|
2018-01-19 17:33:27 +00:00
|
|
|
}
|
|
|
|
// temporary data holder
|
2018-03-19 21:37:38 +00:00
|
|
|
const body = []
|
2018-01-19 17:33:27 +00:00
|
|
|
// on every content chunk, push it to the data array
|
2018-03-19 21:37:38 +00:00
|
|
|
response.on('data', (chunk) => body.push(chunk))
|
2018-01-19 17:33:27 +00:00
|
|
|
// we are done, resolve promise with those joined chunks
|
|
|
|
response.on('end', () => {
|
|
|
|
// safeLowWait returns GWei (10^10 Wei).
|
2018-03-19 21:40:36 +00:00
|
|
|
const jsonBody = JSON.parse(body.join(''))
|
|
|
|
const gasPriceWei = parseInt(jsonBody['safeLowWait']) * Math.pow(10, 10)
|
2018-03-19 21:37:38 +00:00
|
|
|
resolve(gasPriceWei)
|
|
|
|
})
|
|
|
|
})
|
2018-01-19 17:33:27 +00:00
|
|
|
// handle connection errors of the request
|
2018-03-19 21:37:38 +00:00
|
|
|
request.on('error', (err) => reject(err))
|
2018-01-19 17:33:27 +00:00
|
|
|
})
|
2018-03-19 21:37:38 +00:00
|
|
|
}
|
2018-01-19 17:33:27 +00:00
|
|
|
|
2018-01-25 00:06:50 +00:00
|
|
|
const getTokenPriceMock = function () {
|
2018-03-19 21:37:38 +00:00
|
|
|
return new Promise((resolve, reject) => resolve(0.35))
|
2018-01-24 22:37:39 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 00:06:50 +00:00
|
|
|
const getTokenPrice = function (token) {
|
2018-01-24 22:37:39 +00:00
|
|
|
if (config.debug) {
|
2018-03-19 21:37:38 +00:00
|
|
|
return getTokenPriceMock()
|
2018-01-24 22:37:39 +00:00
|
|
|
}
|
2018-01-19 17:33:27 +00:00
|
|
|
const currency = 'USD'
|
2018-03-19 21:37:38 +00:00
|
|
|
const url = 'https://min-api.cryptocompare.com/data/price?fsym=' + token + '&tsyms=' + currency
|
2018-01-19 17:33:27 +00:00
|
|
|
// return new pending promise
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// select http or https module, depending on reqested url
|
2018-03-19 21:37:38 +00:00
|
|
|
const lib = url.startsWith('https') ? require('https') : require('http')
|
2018-01-19 17:33:27 +00:00
|
|
|
const request = lib.get(url, (response) => {
|
|
|
|
// handle http errors
|
|
|
|
if (response.statusCode < 200 || response.statusCode > 299) {
|
2018-03-19 21:37:38 +00:00
|
|
|
reject('Failed to load page, status code: ' + response.statusCode)
|
2018-01-19 17:33:27 +00:00
|
|
|
}
|
|
|
|
// temporary data holder
|
2018-03-19 21:37:38 +00:00
|
|
|
const body = []
|
2018-01-19 17:33:27 +00:00
|
|
|
// on every content chunk, push it to the data array
|
2018-03-19 21:37:38 +00:00
|
|
|
response.on('data', (chunk) => body.push(chunk))
|
2018-01-19 17:33:27 +00:00
|
|
|
// we are done, resolve promise with those joined chunks
|
|
|
|
response.on('end', () => {
|
2018-03-19 21:40:36 +00:00
|
|
|
const jsonBody = JSON.parse(body.join(''))
|
|
|
|
const tokenPrice = parseFloat(jsonBody[currency])
|
2018-03-19 21:37:38 +00:00
|
|
|
resolve(tokenPrice)
|
|
|
|
})
|
|
|
|
})
|
2018-01-19 17:33:27 +00:00
|
|
|
// handle connection errors of the request
|
|
|
|
request.on('error', (err) => reject(err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2018-01-20 16:34:11 +00:00
|
|
|
getGasPrice: getGasPrice,
|
2018-01-25 00:06:50 +00:00
|
|
|
getTokenPrice: getTokenPrice
|
2018-01-19 17:33:27 +00:00
|
|
|
}
|