beta version: proposed request process working

This commit is contained in:
Jordi Montes 2018-01-24 23:36:30 +01:00
parent cc690f784d
commit e1fd3c8251
1 changed files with 43 additions and 33 deletions

View File

@ -24,18 +24,26 @@ app.use(helmet());
// Receive a POST request at the url specified by an env. var.
app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) {
if (!req.body || !req.body.action) {
bot.error('', 'Wrong format');
return res.sendStatus(400);
} else if (!bot.needsFunding(req)) {
return res.sendStatus(204);
}
setTimeout(function (req, res) {
processRequest(req, res);
setTimeout(() => {
processRequest(req)
.then(() => {
return res.sendStatus(200);
})
.catch((err) => {
bot.error('Error funding issue: ' + req.body.issue.url);
bot.error('error: ' + err);
bot.error('dump: ' + req);
return res.sendStatus(204);
});
}, config.delayInMiliSeconds);
});
const processRequest = function (req, res) {
const processRequest = function (req) {
const eth = bot.eth;
const from = config.sourceAddress;
const to = bot.getAddress(req);
@ -45,26 +53,32 @@ const processRequest = function (req, res) {
const gasPricePromise = bot.getGasPrice();
Promise.all([amountPromise, gasPricePromise])
.then(function (amount, gasPrice) {
.then(function (results) {
let amount = results[0];
let gasPrice = results[1];
let transaction = sendTransaction(eth, from, to, amount, gasPrice);
transaction
.then(function () {
return res.sendStatus(200);
})
.catch(function (error) {
bot.error(req.body, error);
.catch(function (err) {
reject(err);
});
})
.catch(function (error) {
bot.error(req.body, error);
.catch(function (err) {
reject(err);
});
}
const sendTransaction = function (eth, from, to, amount, gasPrice) {
if (!config.debug) {
return new Promise((resolve, reject) => {
if (config.debug) {
let txID = -1;
bot.logTransaction(txID, from, to, amount, gasPrice);
resolve();
} else {
eth.getTransactionCount(from, (err, nonce) => {
eth.sendTransaction({
from: from,
@ -74,20 +88,16 @@ const sendTransaction = function (eth, from, to, amount, gasPrice) {
value: amount,
nonce,
}, (err, txID) => {
if (err) {
bot.error(req.body, err)
return res.status(500).json(err)
}
else {
if (!err) {
bot.logTransaction(txID, from, to, amount, gasPrice);
res.json({ txID })
}
});
});
resolve();
} else {
let txID = -1;
bot.logTransaction(txID, from, to, amount, gasPrice);
reject(err);
}
});
});
}
});
}
const port = process.env.PORT || 8181