2017-06-10 20:37:15 +05:30
|
|
|
/*
|
2018-01-22 12:24:19 +01:00
|
|
|
* Bot that receives a POST request (from a GitHub issue comment webhook)
|
|
|
|
* and in case it's a comment that has "@autobounty <decimal> <currency>"
|
|
|
|
* awards that bounty to the address posted earlier in the thread (by the
|
|
|
|
* commiteth bot).
|
|
|
|
* TODO tests
|
|
|
|
* REVIEW parsing, non-persisting storage of addresses, hardcoded string length.
|
|
|
|
* Depends on commiteth version as of 2017-06-10.
|
|
|
|
*/
|
2018-01-19 18:33:27 +01:00
|
|
|
|
2018-01-17 16:48:56 +00:00
|
|
|
const config = require('./config');
|
2018-01-19 18:33:27 +01:00
|
|
|
const bot = require('./bot');
|
2018-01-17 16:48:56 +00:00
|
|
|
|
2017-06-09 12:15:53 +02:00
|
|
|
var express = require('express'),
|
2018-01-23 16:36:34 +01:00
|
|
|
cors = require('cors'),
|
|
|
|
helmet = require('helmet'),
|
|
|
|
app = express(),
|
|
|
|
bodyParser = require('body-parser'),
|
|
|
|
jsonParser = bodyParser.json();
|
2017-03-07 12:24:01 +01:00
|
|
|
|
|
|
|
app.use(cors());
|
2018-01-19 11:42:58 +01:00
|
|
|
app.use(helmet());
|
2018-01-17 16:48:56 +00:00
|
|
|
|
2018-01-22 23:45:09 +01:00
|
|
|
// Receive a POST request at the url specified by an env. var.
|
2018-01-23 16:36:34 +01:00
|
|
|
app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) {
|
2018-01-22 16:50:23 +01:00
|
|
|
if (!req.body || !req.body.action) {
|
2018-01-22 21:21:31 +01:00
|
|
|
bot.error('', 'Wrong format');
|
2018-01-22 16:50:23 +01:00
|
|
|
return res.sendStatus(400);
|
2018-01-22 23:45:09 +01:00
|
|
|
} else if (!bot.needsFunding(req)) {
|
2018-01-22 16:50:23 +01:00
|
|
|
return res.sendStatus(204);
|
|
|
|
}
|
2018-01-19 11:42:58 +01:00
|
|
|
|
2018-01-22 23:45:09 +01:00
|
|
|
setTimeout(function (req, res) {
|
|
|
|
processRequest(req, res);
|
|
|
|
}, config.delayInMiliSeconds);
|
|
|
|
});
|
|
|
|
|
2018-01-23 16:36:34 +01:00
|
|
|
const processRequest = function (req, res) {
|
2018-01-22 16:50:23 +01:00
|
|
|
const eth = bot.eth;
|
|
|
|
const from = config.sourceAddress;
|
|
|
|
const to = bot.getAddress(req);
|
|
|
|
|
|
|
|
// Asynchronous requests for Gas Price and Amount
|
|
|
|
const amountPromise = bot.getAmount(req);
|
|
|
|
const gasPricePromise = bot.getGasPrice();
|
|
|
|
|
|
|
|
Promise.all([amountPromise, gasPricePromise])
|
2018-01-23 16:36:34 +01:00
|
|
|
.then(function (amount, gasPrice) {
|
|
|
|
let transaction = sendTransaction(eth, from, to, amount, gasPrice);
|
|
|
|
|
|
|
|
transaction
|
|
|
|
.then(function () {
|
|
|
|
return res.sendStatus(200);
|
|
|
|
})
|
|
|
|
.catch(function (error) {
|
|
|
|
bot.error(req.body, error);
|
|
|
|
});
|
|
|
|
|
2018-01-22 23:45:09 +01:00
|
|
|
})
|
2018-01-23 16:36:34 +01:00
|
|
|
.catch(function (error) {
|
2018-01-22 23:45:09 +01:00
|
|
|
bot.error(req.body, error);
|
|
|
|
});
|
|
|
|
}
|
2018-01-22 16:50:23 +01:00
|
|
|
|
2018-01-23 16:36:34 +01:00
|
|
|
const sendTransaction = function (eth, from, to, amount, gasPrice) {
|
|
|
|
if (!config.debug) {
|
2018-01-22 16:50:23 +01:00
|
|
|
|
|
|
|
eth.getTransactionCount(from, (err, nonce) => {
|
|
|
|
eth.sendTransaction({
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
gas: gas,
|
|
|
|
gasPrice: gasPrice,
|
|
|
|
value: amount,
|
|
|
|
nonce,
|
|
|
|
}, (err, txID) => {
|
|
|
|
if (err) {
|
|
|
|
bot.error(req.body, err)
|
|
|
|
return res.status(500).json(err)
|
|
|
|
}
|
|
|
|
else {
|
2018-01-22 17:08:23 +01:00
|
|
|
bot.logTransaction(txID, from, to, amount, gasPrice);
|
2018-01-22 16:50:23 +01:00
|
|
|
res.json({ txID })
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
2018-01-22 21:21:31 +01:00
|
|
|
let txID = -1;
|
2018-01-22 17:08:23 +01:00
|
|
|
bot.logTransaction(txID, from, to, amount, gasPrice);
|
2018-01-22 16:50:23 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-07 12:24:01 +01:00
|
|
|
|
|
|
|
const port = process.env.PORT || 8181
|
2018-01-23 16:36:34 +01:00
|
|
|
app.listen(port, function () {
|
2018-01-22 16:50:23 +01:00
|
|
|
bot.log('Autobounty listening on port', port);
|
2017-06-10 23:54:34 +05:30
|
|
|
});
|