Structure refactor, API calls created with missing error handling
This commit is contained in:
parent
19e3df1938
commit
73aca304a5
|
@ -1,2 +1,4 @@
|
|||
node_modules
|
||||
npm-debug.log
|
||||
|
||||
.ssh
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
const SignerProvider = require('ethjs-provider-signer');
|
||||
const sign = require('ethjs-signer').sign;
|
||||
const Eth = require('ethjs-query');
|
||||
const Prices = require('./prices');
|
||||
const Config = require('./config');
|
||||
|
||||
|
||||
const provider = new SignerProvider(Config.signerPath, {
|
||||
signTransaction: (rawTx, cb) => cb(null, sign(rawTx, process.env.KEY)),
|
||||
accounts: (cb) => cb(null, [address]),
|
||||
});
|
||||
const eth = new Eth(provider);
|
||||
|
||||
|
||||
const needsFunding = function(req) {
|
||||
if (req.action !== 'created' || !req.hasOwnProperty('comment'))
|
||||
return false
|
||||
else if (req.comment.user.login !== 'status-open-bounty')
|
||||
return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
const getAddress = function(req) {
|
||||
commentBody = req.body.comment.body;
|
||||
return commentBody.substring(commentBody.search("Contract address:") + 18, commentBody.search("Contract address:") + 60)
|
||||
}
|
||||
|
||||
const getLabel = function(req){
|
||||
return "XS";
|
||||
}
|
||||
|
||||
const getAmount = function(req) {
|
||||
let tokenPricePromise = Prices.getTokenPrice(Config.token);
|
||||
|
||||
let label = getLabel(req);
|
||||
let amountToPayDollar = config.priceHour * config.workHours[label];
|
||||
|
||||
tokenPricePromise
|
||||
.then((tokenPrice) => return tokenPrice * config.amountToPayInDollars )
|
||||
.catch((err) => console.log("TODO-ERROR: Failed token price request throw log error");
|
||||
// Check how to handle errors when promises does not arrive
|
||||
|
||||
}
|
||||
|
||||
const getGasPrice = function(req) {
|
||||
let gasPricePromise = Prices.getGasPrice();
|
||||
|
||||
gasPricePromise
|
||||
.then((gasPrice) => return gasPrice)
|
||||
.catch((err) => console.log("TODO-ERROR: Failed gas price request throw log error");
|
||||
// Check how to handle errors when promises does not arrive
|
||||
}
|
||||
|
||||
const log = function() {
|
||||
console.log(arguments);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
eth: new Eth(provider),
|
||||
needsFunding: needsFunding,
|
||||
getAddress: getAddress,
|
||||
getAmount: getAmount,
|
||||
getGasPrice: getGasPrice,
|
||||
log: log
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
"use strict"
|
||||
|
||||
const https = require("https");
|
||||
|
||||
const getGasPrice = function() {
|
||||
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).
|
||||
let jsonBody = JSON.parse(body.join(''));
|
||||
let gasPriceWei = parseInt(jsonBody['safeLowWait']) * Math.pow(10, 10);
|
||||
resolve(gasPriceWei);
|
||||
});
|
||||
});
|
||||
// handle connection errors of the request
|
||||
request.on('error', (err) => reject(err))
|
||||
})
|
||||
};
|
||||
|
||||
const getTokenPrice = function(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', () => {
|
||||
let jsonBody = JSON.parse(body.join(''));
|
||||
let tokenPrice = parseFloat(jsonBody[currency]);
|
||||
resolve(tokenPrice);
|
||||
});
|
||||
});
|
||||
// handle connection errors of the request
|
||||
request.on('error', (err) => reject(err))
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getGasPriceInWei: getGasPrice,
|
||||
getTokenPrice : getTokenPrice
|
||||
}
|
|
@ -1,41 +1,9 @@
|
|||
const SignerProvider = require('ethjs-provider-signer');
|
||||
const sign = require('ethjs-signer').sign;
|
||||
const Eth = require('ethjs-query');
|
||||
|
||||
const provider = new SignerProvider(process.env.NODE, {
|
||||
signTransaction: (rawTx, cb) => cb(null, sign(rawTx, process.env.KEY)),
|
||||
accounts: (cb) => cb(null, [address]),
|
||||
});
|
||||
const eth = new Eth(provider);
|
||||
|
||||
|
||||
const needsFunding = function(req) {
|
||||
if (req.action !== 'created' || !req.hasOwnProperty('comment'))
|
||||
return false
|
||||
else if (req.comment.user.login !== 'status-open-bounty')
|
||||
return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
const getAddress = function(req) {
|
||||
commentBody = req.body.comment.body;
|
||||
return commentBody.substring(commentBody.search("Contract address:") + 18, commentBody.search("Contract address:") + 60)
|
||||
}
|
||||
|
||||
const getAmount = function(req) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const log = function() {
|
||||
console.log(arguments);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
webhook: {URLEndpoint: "/autobounty/fund"},
|
||||
eth: new Eth(provider),
|
||||
needsFunding: needsFunding,
|
||||
getAddress: getAddress,
|
||||
getAmount: getAmount,
|
||||
log: log
|
||||
urlEndpoint: "/autobounty/fund",
|
||||
signerPath: "https://ropsten.infura.io",
|
||||
sourceAddress: "XXXXX",
|
||||
token: "SNT",
|
||||
gasLimit: 92000,
|
||||
priceHour: 35,
|
||||
workHours: {xs: 2000, s: 2500
|
||||
}
|
||||
|
|
24
index.js
24
index.js
|
@ -7,8 +7,9 @@
|
|||
* REVIEW parsing, non-persisting storage of addresses, hardcoded string length.
|
||||
* Depends on commiteth version as of 2017-06-10.
|
||||
*/
|
||||
https://duckduckgo.com/
|
||||
|
||||
const config = require('./config');
|
||||
const bot = require('./bot');
|
||||
|
||||
var express = require('express'),
|
||||
cors = require('cors'),
|
||||
|
@ -21,23 +22,26 @@ app.use(cors());
|
|||
app.use(helmet());
|
||||
|
||||
// Receive a POST request at the address specified by an env. var.
|
||||
app.post(`${config.webhook.URLEndpoint}`, jsonParser, function(req, res, next) {
|
||||
app.post(`${config.urlEndpoint}`, jsonParser, function(req, res, next) {
|
||||
// TODO decide how long the delay to start everything should be
|
||||
if (!req.body || !req.body.action)
|
||||
return res.sendStatus(400);
|
||||
|
||||
if (!config.needsFunding(req))
|
||||
return res.sendStatus(204);
|
||||
|
||||
const eth = config.eth;
|
||||
const address = config.address;
|
||||
const toAddress = config.getAddress(req);
|
||||
const eth = bot.eth;
|
||||
const from = config.sourceAddress;
|
||||
const to = bot.getAddress(req);
|
||||
const amount = config.getAmount(req);
|
||||
|
||||
const gasPrice = bot.getGasPrice();
|
||||
|
||||
eth.getTransactionCount(address, (err, nonce) => {
|
||||
eth.sendTransaction({
|
||||
from: address,
|
||||
to: toAddress, // Address from earlier in the thread
|
||||
gas: 100000,
|
||||
from: from,
|
||||
to: to,
|
||||
gas: gas,
|
||||
gasPrice: gasPrice,
|
||||
value: amount,
|
||||
nonce,
|
||||
}, (err, txID) => {
|
||||
|
@ -49,7 +53,7 @@ app.post(`${config.webhook.URLEndpoint}`, jsonParser, function(req, res, next) {
|
|||
config.log('Successful request:', txID)
|
||||
res.json({ txID })
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const port = process.env.PORT || 8181
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
"dependencies": {
|
||||
"body-parser": "^1.17.2",
|
||||
"cors": "^2.8.1",
|
||||
"eslint": "^4.15.0",
|
||||
"ethjs-provider-signer": "^0.1.4",
|
||||
"ethjs-query": "^0.2.4",
|
||||
"ethjs-signer": "^0.1.1",
|
||||
|
|
Loading…
Reference in New Issue