autobounty/index.js

79 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-06-10 15:07:15 +00:00
/*
2018-01-22 11:24:19 +00: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-17 16:48:56 +00:00
const config = require('./config');
const bot = require('./bot');
2018-01-17 16:48:56 +00:00
2017-06-09 10:15:53 +00:00
var express = require('express'),
2018-01-23 15:36:34 +00:00
cors = require('cors'),
helmet = require('helmet'),
app = express(),
bodyParser = require('body-parser'),
jsonParser = bodyParser.json();
2017-03-07 11:24:01 +00:00
app.use(cors());
2018-01-19 10:42:58 +00:00
app.use(helmet());
2018-01-17 16:48:56 +00:00
// Receive a POST request at the url specified by an env. var.
2018-01-23 15:36:34 +00:00
app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) {
if (!req.body || !req.body.action) {
return res.sendStatus(400);
} else if (!bot.needsFunding(req)) {
return res.sendStatus(204);
}
setTimeout(() => {
processRequest(req)
.then(() => {
2018-01-25 12:02:47 +00:00
bot.info('issue well funded: ' + res.body.issue.url);
})
.catch((err) => {
bot.error('Error funding issue: ' + req.body.issue.url);
bot.error('error: ' + err);
bot.error('dump: ' + req);
});
}, config.delayInMiliSeconds);
return res.sendStatus(200);
});
const processRequest = function (req) {
2018-02-13 12:05:39 +00:00
const wallet = bot.wallet;
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();
return new Promise((resolve, reject) => {
2018-01-25 12:02:47 +00:00
Promise.all([amountPromise, gasPricePromise])
.then(function (results) {
let amount = results[0];
let gasPrice = results[1];
2018-02-13 12:05:39 +00:00
bot.sendTransaction(to, amount, gasPrice)
.then(function () {
resolve();
})
.catch(function (err) {
reject(err);
});
2018-01-23 15:36:34 +00:00
})
.catch(function (err) {
reject(err);
});
2018-01-25 12:02:47 +00:00
});
}
2017-03-07 11:24:01 +00:00
const port = process.env.PORT || 8181
2018-01-23 15:36:34 +00:00
app.listen(port, function () {
bot.log('Autobounty listening on port', port);
2017-06-10 18:24:34 +00:00
});