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,49 +53,51 @@ 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) {
eth.getTransactionCount(from, (err, nonce) => { let txID = -1;
eth.sendTransaction({ bot.logTransaction(txID, from, to, amount, gasPrice);
from: from, resolve();
to: to, } else {
gas: gas, eth.getTransactionCount(from, (err, nonce) => {
gasPrice: gasPrice, eth.sendTransaction({
value: amount, from: from,
nonce, to: to,
}, (err, txID) => { gas: gas,
if (err) { gasPrice: gasPrice,
bot.error(req.body, err) value: amount,
return res.status(500).json(err) nonce,
} }, (err, txID) => {
else { if (!err) {
bot.logTransaction(txID, from, to, amount, gasPrice); bot.logTransaction(txID, from, to, amount, gasPrice);
res.json({ txID }) resolve();
} } else {
reject(err);
}
});
}); });
}); }
} else { });
let txID = -1;
bot.logTransaction(txID, from, to, amount, gasPrice);
}
} }
const port = process.env.PORT || 8181 const port = process.env.PORT || 8181