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:
Pedro Pombeiro 2018-03-20 14:39:30 +01:00
parent 638358e802
commit 717b46ec52
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
4 changed files with 43 additions and 75 deletions

View File

@ -1,31 +1,11 @@
'use strict' 'use strict'
const axios = require('axios')
async function getGasPrice () { async function getGasPrice () {
const url = 'https://ethgasstation.info/json/ethgasAPI.json' const response = await axios.get('https://ethgasstation.info/json/ethgasAPI.json')
// return new pending promise const gasPriceWei = Math.trunc(parseFloat(response.data.safeLowWait) * Math.pow(10, 10))
return new Promise((resolve, reject) => { return gasPriceWei
// 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))
})
} }
async function getTokenPrice (token) { async function getTokenPrice (token) {
@ -34,30 +14,9 @@ async function getTokenPrice (token) {
} }
const currency = 'USD' const currency = 'USD'
const url = `https://min-api.cryptocompare.com/data/price?fsym=${token}&tsyms=${currency}` const response = await axios.get(`https://min-api.cryptocompare.com/data/price?fsym=${token}&tsyms=${currency}`)
// return new pending promise const tokenPrice = parseFloat(response.data[currency])
return new Promise((resolve, reject) => { return tokenPrice
// 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))
})
} }
module.exports = { module.exports = {

View File

@ -22,6 +22,8 @@ app.use(helmet())
// Receive a POST request at the url specified by an env. var. // Receive a POST request at the url specified by an env. var.
app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) { app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) {
bot.info(`Handling ${req.body.issue.url}`)
if (!req.body || !req.body.action) { if (!req.body || !req.body.action) {
return res.sendStatus(400) return res.sendStatus(400)
} else if (!bot.needsFunding(req)) { } else if (!bot.needsFunding(req)) {

57
package-lock.json generated
View File

@ -159,6 +159,15 @@
"lodash": "4.17.5" "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": { "babel-code-frame": {
"version": "6.26.0", "version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", "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", "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-1.1.0.tgz",
"integrity": "sha512-VOMDtYPwLbIncTxNoSzRyvaMxtXmLWLUqr8k5AfC1BzLk34HvBXaQX8snOwQZ4c0aX8aSERqtJSiI9/m2u5kuA==" "integrity": "sha512-VOMDtYPwLbIncTxNoSzRyvaMxtXmLWLUqr8k5AfC1BzLk34HvBXaQX8snOwQZ4c0aX8aSERqtJSiI9/m2u5kuA=="
}, },
"bignumber.js": {
"version": "git+https://github.com/debris/bignumber.js.git#94d7146671b9719e00a09c29b01a691bc85048c2"
},
"bn.js": { "bn.js": {
"version": "4.11.8", "version": "4.11.8",
"resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.8.tgz", "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.8.tgz",
@ -508,11 +514,6 @@
"which": "1.3.0" "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": { "dasherize": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/dasherize/-/dasherize-2.0.0.tgz", "resolved": "https://registry.npmjs.org/dasherize/-/dasherize-2.0.0.tgz",
@ -1179,6 +1180,24 @@
"write": "0.2.1" "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": { "forwarded": {
"version": "0.1.2", "version": "0.1.2",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",
@ -1481,6 +1500,11 @@
"integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=", "integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=",
"dev": true "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": { "is-builtin-module": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-1.0.0.tgz", "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", "resolved": "https://registry.npmjs.org/url-template/-/url-template-2.0.8.tgz",
"integrity": "sha1-/FZaPMy/93MMd19WQflVV5FDnyE=" "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": { "util-deprecate": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "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", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" "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": { "which": {
"version": "1.3.0", "version": "1.3.0",
"resolved": "https://registry.npmjs.org/which/-/which-1.3.0.tgz", "resolved": "https://registry.npmjs.org/which/-/which-1.3.0.tgz",

View File

@ -12,6 +12,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@octokit/rest": "^15.2.5", "@octokit/rest": "^15.2.5",
"axios": "^0.18.0",
"body-parser": "^1.17.2", "body-parser": "^1.17.2",
"cors": "^2.8.1", "cors": "^2.8.1",
"ethers": "^3.0", "ethers": "^3.0",
@ -21,7 +22,6 @@
"express": "^4.15.2", "express": "^4.15.2",
"helmet": "^3.9.0", "helmet": "^3.9.0",
"lodash": "^4.17.4", "lodash": "^4.17.4",
"web3": "^0.18.2",
"winston": "^3.0.0-rc3" "winston": "^3.0.0-rc3"
}, },
"devDependencies": { "devDependencies": {