2017-06-10 15:07:15 +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).
|
2017-06-10 18:24:34 +00:00
|
|
|
* TODO tests
|
2017-06-14 12:34:26 +00:00
|
|
|
* REVIEW parsing, non-persisting storage of addresses, hardcoded string length.
|
2017-06-11 09:54:09 +00:00
|
|
|
* Depends on commiteth version as of 2017-06-10.
|
2017-06-10 15:07:15 +00:00
|
|
|
*/
|
|
|
|
|
2018-01-17 16:48:56 +00:00
|
|
|
const config = require('./config');
|
|
|
|
|
2017-03-07 11:24:01 +00:00
|
|
|
const SignerProvider = require('ethjs-provider-signer');
|
|
|
|
const sign = require('ethjs-signer').sign;
|
|
|
|
const Eth = require('ethjs-query');
|
|
|
|
|
2017-06-11 09:54:09 +00:00
|
|
|
const address = process.env.ADDRESS;
|
|
|
|
const name = process.env.NAME;
|
2017-03-07 11:24:01 +00:00
|
|
|
|
|
|
|
const provider = new SignerProvider(process.env.NODE, {
|
|
|
|
signTransaction: (rawTx, cb) => cb(null, sign(rawTx, process.env.KEY)),
|
|
|
|
accounts: (cb) => cb(null, [address]),
|
|
|
|
});
|
|
|
|
const eth = new Eth(provider);
|
|
|
|
|
2017-06-09 10:15:53 +00:00
|
|
|
var express = require('express'),
|
|
|
|
cors = require('cors'),
|
2017-06-11 06:23:57 +00:00
|
|
|
app = express(),
|
|
|
|
bodyParser = require('body-parser'),
|
|
|
|
jsonParser = bodyParser.json();
|
2017-03-07 11:24:01 +00:00
|
|
|
|
|
|
|
app.use(cors());
|
|
|
|
|
2017-06-11 09:54:09 +00:00
|
|
|
// Store issue ids and their bounty addresses
|
|
|
|
var issueData = {};
|
|
|
|
|
2018-01-17 16:48:56 +00:00
|
|
|
|
2017-06-10 15:07:15 +00:00
|
|
|
// Receive a POST request at the address specified by an env. var.
|
2018-01-17 16:48:56 +00:00
|
|
|
app.post(`${config.webhook.URLEndpoint}`, jsonParser, function(req, res, next){
|
2017-06-11 09:54:09 +00:00
|
|
|
if (!req.body)
|
|
|
|
return res.sendStatus(400);
|
2018-01-17 16:48:56 +00:00
|
|
|
return res.sendStatus(200)
|
|
|
|
/*var commentBody = req.body.comment.body;
|
2017-06-11 09:54:09 +00:00
|
|
|
var issueId = req.body.issue.id;
|
|
|
|
var namePosition = commentBody.search("@" + name);
|
|
|
|
// Store toAddress from commiteth
|
2017-06-14 13:41:46 +00:00
|
|
|
if (namePosition == -1 ) {
|
|
|
|
if (req.body.comment.user.login == 'commiteth') { // TODO no existence check
|
|
|
|
issueData[issueId] = {"toAddress": commentBody.substring(commentBody.search("Contract address:") + 18, commentBody.search("Contract address:") + 60)}
|
|
|
|
console.log(issueData);
|
|
|
|
return res.status(204);
|
|
|
|
}
|
2017-06-11 09:54:09 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
var postNameWords = commentBody.substring(namePosition + 1 + name.length + 1).trim().split(' ');
|
|
|
|
var amount = 0;
|
|
|
|
if (postNameWords.length > 0) {
|
|
|
|
if(postNameWords[0] == "standard") {
|
|
|
|
amount = process.env.STANDARD_BOUNTY;
|
2017-03-10 09:01:43 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-06-11 09:54:09 +00:00
|
|
|
amount = parseFloat(postNameWords[0]);
|
2017-03-10 09:01:43 +00:00
|
|
|
}
|
2017-06-11 09:54:09 +00:00
|
|
|
}
|
|
|
|
console.log("Trying to give " + amount + " ETH to " + issueData[issueId].toAddress + " for issue " + issueId);
|
|
|
|
issueData[issueId].amount = amount;
|
|
|
|
|
|
|
|
// Conduct the transaction
|
|
|
|
eth.getTransactionCount(address, (err, nonce) => {
|
|
|
|
eth.sendTransaction({
|
|
|
|
from: address, // Specified in webhook, secret
|
|
|
|
to: issueData[issueId].toAddress, // Address from earlier in the thread
|
|
|
|
gas: 100000,
|
|
|
|
value: issueData[issueId].amount,
|
|
|
|
nonce,
|
|
|
|
}, (err, txID) => {
|
|
|
|
if (err) {
|
|
|
|
console.log('Request failed', err)
|
|
|
|
return res.status(500).json(err)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log('Successful request:', txID)
|
|
|
|
res.json({ txID })
|
|
|
|
}
|
|
|
|
});
|
2017-03-10 09:01:43 +00:00
|
|
|
});
|
2018-01-17 16:48:56 +00:00
|
|
|
}*/
|
2017-03-07 11:24:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const port = process.env.PORT || 8181
|
|
|
|
app.listen(port, function(){
|
2017-06-10 13:39:53 +00:00
|
|
|
console.log('Autobounty listening on port', port);
|
2017-06-10 18:24:34 +00:00
|
|
|
});
|