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. // 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) {
if (!req.body || !req.body.action) { if (!req.body || !req.body.action) {
bot.error('', 'Wrong format');
return res.sendStatus(400); return res.sendStatus(400);
} else if (!bot.needsFunding(req)) { } else if (!bot.needsFunding(req)) {
return res.sendStatus(204); return res.sendStatus(204);
} }
setTimeout(function (req, res) { setTimeout(() => {
processRequest(req, res); 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); }, config.delayInMiliSeconds);
}); });
const processRequest = function (req, res) { const processRequest = function (req) {
const eth = bot.eth; const eth = bot.eth;
const from = config.sourceAddress; const from = config.sourceAddress;
const to = bot.getAddress(req); const to = bot.getAddress(req);
@ -45,26 +53,32 @@ const processRequest = function (req, res) {
const gasPricePromise = bot.getGasPrice(); const gasPricePromise = bot.getGasPrice();
Promise.all([amountPromise, gasPricePromise]) 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); let transaction = sendTransaction(eth, from, to, amount, gasPrice);
transaction transaction
.then(function () { .then(function () {
return res.sendStatus(200); return res.sendStatus(200);
}) })
.catch(function (error) { .catch(function (err) {
bot.error(req.body, error); reject(err);
}); });
}) })
.catch(function (error) { .catch(function (err) {
bot.error(req.body, error); reject(err);
}); });
} }
const sendTransaction = function (eth, from, to, amount, gasPrice) { 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.getTransactionCount(from, (err, nonce) => {
eth.sendTransaction({ eth.sendTransaction({
from: from, from: from,
@ -74,20 +88,16 @@ const sendTransaction = function (eth, from, to, amount, gasPrice) {
value: amount, value: amount,
nonce, nonce,
}, (err, txID) => { }, (err, txID) => {
if (err) { if (!err) {
bot.error(req.body, err)
return res.status(500).json(err)
}
else {
bot.logTransaction(txID, from, to, amount, gasPrice); bot.logTransaction(txID, from, to, amount, gasPrice);
res.json({ txID }) resolve();
}
});
});
} else { } else {
let txID = -1; reject(err);
bot.logTransaction(txID, from, to, amount, gasPrice);
} }
});
});
}
});
} }
const port = process.env.PORT || 8181 const port = process.env.PORT || 8181