Introduce `axios` npm package to replace low-level HTTPS fetch logic. Remove unused web3 package
- Fix fractional part gas price being truncated when converting to wei
This commit is contained in:
parent
638358e802
commit
717b46ec52
|
@ -1,31 +1,11 @@
|
|||
'use strict'
|
||||
|
||||
const axios = require('axios')
|
||||
|
||||
async function getGasPrice () {
|
||||
const url = 'https://ethgasstation.info/json/ethgasAPI.json'
|
||||
// return new pending promise
|
||||
return new Promise((resolve, reject) => {
|
||||
// select http or https module, depending on reqested url
|
||||
const lib = url.startsWith('https') ? require('https') : require('http')
|
||||
const request = lib.get(url, (response) => {
|
||||
// handle http errors
|
||||
if (response.statusCode < 200 || response.statusCode > 299) {
|
||||
reject(new Error(`Failed to load page, status code: ${response.statusCode}`))
|
||||
}
|
||||
// temporary data holder
|
||||
const body = []
|
||||
// on every content chunk, push it to the data array
|
||||
response.on('data', (chunk) => body.push(chunk))
|
||||
// we are done, resolve promise with those joined chunks
|
||||
response.on('end', () => {
|
||||
// safeLowWait returns GWei (10^10 Wei).
|
||||
const jsonBody = JSON.parse(body.join(''))
|
||||
const gasPriceWei = parseInt(jsonBody['safeLowWait']) * Math.pow(10, 10)
|
||||
resolve(gasPriceWei)
|
||||
})
|
||||
})
|
||||
// handle connection errors of the request
|
||||
request.on('error', (err) => reject(err))
|
||||
})
|
||||
const response = await axios.get('https://ethgasstation.info/json/ethgasAPI.json')
|
||||
const gasPriceWei = Math.trunc(parseFloat(response.data.safeLowWait) * Math.pow(10, 10))
|
||||
return gasPriceWei
|
||||
}
|
||||
|
||||
async function getTokenPrice (token) {
|
||||
|
@ -34,30 +14,9 @@ async function getTokenPrice (token) {
|
|||
}
|
||||
|
||||
const currency = 'USD'
|
||||
const url = `https://min-api.cryptocompare.com/data/price?fsym=${token}&tsyms=${currency}`
|
||||
// return new pending promise
|
||||
return new Promise((resolve, reject) => {
|
||||
// select http or https module, depending on reqested url
|
||||
const lib = url.startsWith('https') ? require('https') : require('http')
|
||||
const request = lib.get(url, (response) => {
|
||||
// handle http errors
|
||||
if (response.statusCode < 200 || response.statusCode > 299) {
|
||||
reject(new Error(`Failed to load page, status code: ${response.statusCode}`))
|
||||
}
|
||||
// temporary data holder
|
||||
const body = []
|
||||
// on every content chunk, push it to the data array
|
||||
response.on('data', (chunk) => body.push(chunk))
|
||||
// we are done, resolve promise with those joined chunks
|
||||
response.on('end', () => {
|
||||
const jsonBody = JSON.parse(body.join(''))
|
||||
const tokenPrice = parseFloat(jsonBody[currency])
|
||||
resolve(tokenPrice)
|
||||
})
|
||||
})
|
||||
// handle connection errors of the request
|
||||
request.on('error', (err) => reject(err))
|
||||
})
|
||||
const response = await axios.get(`https://min-api.cryptocompare.com/data/price?fsym=${token}&tsyms=${currency}`)
|
||||
const tokenPrice = parseFloat(response.data[currency])
|
||||
return tokenPrice
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
2
index.js
2
index.js
|
@ -22,6 +22,8 @@ app.use(helmet())
|
|||
|
||||
// Receive a POST request at the url specified by an env. var.
|
||||
app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) {
|
||||
bot.info(`Handling ${req.body.issue.url}`)
|
||||
|
||||
if (!req.body || !req.body.action) {
|
||||
return res.sendStatus(400)
|
||||
} else if (!bot.needsFunding(req)) {
|
||||
|
|
|
@ -159,6 +159,15 @@
|
|||
"lodash": "4.17.5"
|
||||
}
|
||||
},
|
||||
"axios": {
|
||||
"version": "0.18.0",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.18.0.tgz",
|
||||
"integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=",
|
||||
"requires": {
|
||||
"follow-redirects": "1.4.1",
|
||||
"is-buffer": "1.1.6"
|
||||
}
|
||||
},
|
||||
"babel-code-frame": {
|
||||
"version": "6.26.0",
|
||||
"resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz",
|
||||
|
@ -205,9 +214,6 @@
|
|||
"resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-1.1.0.tgz",
|
||||
"integrity": "sha512-VOMDtYPwLbIncTxNoSzRyvaMxtXmLWLUqr8k5AfC1BzLk34HvBXaQX8snOwQZ4c0aX8aSERqtJSiI9/m2u5kuA=="
|
||||
},
|
||||
"bignumber.js": {
|
||||
"version": "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"
|
||||
},
|
||||
"bn.js": {
|
||||
"version": "4.11.8",
|
||||
"resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.8.tgz",
|
||||
|
@ -508,11 +514,6 @@
|
|||
"which": "1.3.0"
|
||||
}
|
||||
},
|
||||
"crypto-js": {
|
||||
"version": "3.1.8",
|
||||
"resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-3.1.8.tgz",
|
||||
"integrity": "sha1-cV8HC/YBTyrpkqmLOSkli3E/CNU="
|
||||
},
|
||||
"dasherize": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/dasherize/-/dasherize-2.0.0.tgz",
|
||||
|
@ -1179,6 +1180,24 @@
|
|||
"write": "0.2.1"
|
||||
}
|
||||
},
|
||||
"follow-redirects": {
|
||||
"version": "1.4.1",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.4.1.tgz",
|
||||
"integrity": "sha512-uxYePVPogtya1ktGnAAXOacnbIuRMB4dkvqeNz2qTtTQsuzSfbDolV+wMMKxAmCx0bLgAKLbBOkjItMbbkR1vg==",
|
||||
"requires": {
|
||||
"debug": "3.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"debug": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
|
||||
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
|
||||
"requires": {
|
||||
"ms": "2.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"forwarded": {
|
||||
"version": "0.1.2",
|
||||
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",
|
||||
|
@ -1481,6 +1500,11 @@
|
|||
"integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=",
|
||||
"dev": true
|
||||
},
|
||||
"is-buffer": {
|
||||
"version": "1.1.6",
|
||||
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz",
|
||||
"integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w=="
|
||||
},
|
||||
"is-builtin-module": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-1.0.0.tgz",
|
||||
|
@ -2485,11 +2509,6 @@
|
|||
"resolved": "https://registry.npmjs.org/url-template/-/url-template-2.0.8.tgz",
|
||||
"integrity": "sha1-/FZaPMy/93MMd19WQflVV5FDnyE="
|
||||
},
|
||||
"utf8": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/utf8/-/utf8-2.1.2.tgz",
|
||||
"integrity": "sha1-H6DZJw6b6FDZsFAn9jUZv0ZFfZY="
|
||||
},
|
||||
"util-deprecate": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
|
||||
|
@ -2521,18 +2540,6 @@
|
|||
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
|
||||
"integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw="
|
||||
},
|
||||
"web3": {
|
||||
"version": "0.18.4",
|
||||
"resolved": "https://registry.npmjs.org/web3/-/web3-0.18.4.tgz",
|
||||
"integrity": "sha1-gewXhBRUkfLqqJVbMcBgSeB8Xn0=",
|
||||
"requires": {
|
||||
"bignumber.js": "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2",
|
||||
"crypto-js": "3.1.8",
|
||||
"utf8": "2.1.2",
|
||||
"xhr2": "0.1.3",
|
||||
"xmlhttprequest": "1.8.0"
|
||||
}
|
||||
},
|
||||
"which": {
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmjs.org/which/-/which-1.3.0.tgz",
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@octokit/rest": "^15.2.5",
|
||||
"axios": "^0.18.0",
|
||||
"body-parser": "^1.17.2",
|
||||
"cors": "^2.8.1",
|
||||
"ethers": "^3.0",
|
||||
|
@ -21,7 +22,6 @@
|
|||
"express": "^4.15.2",
|
||||
"helmet": "^3.9.0",
|
||||
"lodash": "^4.17.4",
|
||||
"web3": "^0.18.2",
|
||||
"winston": "^3.0.0-rc3"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
Loading…
Reference in New Issue