Merge pull request #2 from snugghash/master

Listener to consume `issue_comments` webhook, parse and send ETH to address.
This commit is contained in:
Jorge Izquierdo 2017-06-14 14:30:21 +02:00 committed by GitHub
commit 6678064cc0
3 changed files with 69 additions and 21 deletions

View File

@ -2,3 +2,6 @@ FROM node:7-onbuild
ENV PORT 8080 ENV PORT 8080
EXPOSE 8080 EXPOSE 8080
ENV NAME autobounty
ENV STANDARD_BOUNTY 0.001

View File

@ -1,8 +1,19 @@
/*
* 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.
*/
const SignerProvider = require('ethjs-provider-signer'); const SignerProvider = require('ethjs-provider-signer');
const sign = require('ethjs-signer').sign; const sign = require('ethjs-signer').sign;
const Eth = require('ethjs-query'); const Eth = require('ethjs-query');
const address = process.env.ADDRESS const address = process.env.ADDRESS;
const name = process.env.NAME;
const provider = new SignerProvider(process.env.NODE, { const provider = new SignerProvider(process.env.NODE, {
signTransaction: (rawTx, cb) => cb(null, sign(rawTx, process.env.KEY)), signTransaction: (rawTx, cb) => cb(null, sign(rawTx, process.env.KEY)),
@ -12,32 +23,65 @@ const eth = new Eth(provider);
var express = require('express'), var express = require('express'),
cors = require('cors'), cors = require('cors'),
app = express(); app = express(),
bodyParser = require('body-parser'),
jsonParser = bodyParser.json();
app.use(cors()); app.use(cors());
app.get('/address/:address', function(req, res, next){ // Store issue ids and their bounty addresses
eth.getTransactionCount(address, (err, nonce) => { var issueData = {};
eth.sendTransaction({
from: address, // Receive a POST request at the address specified by an env. var.
to: req.params.address, app.post('/' + address.toString(), jsonParser, function(req, res, next){
gas: 100000, if (!req.body)
value: (parseFloat(process.env.AMOUNT) || 1.5) * 1e18, return res.sendStatus(400);
data: '0xde5f72fd', // sha3('faucet()') var commentBody = req.body.comment.body;
nonce, var issueId = req.body.issue.id;
}, (err, txID) => { var namePosition = commentBody.search("@" + name);
if (err) { // Store toAddress from commiteth
console.log('Request failed', err) if (namePosition == -1 && req.body.comment.user.login == 'commiteth') { // TODO no existence check
return res.status(500).json(err) issueData[issueId] = {"toAddress": commentBody.substring(commentBody.search("Contract address:") + 18, commentBody.search("Contract address:") + 60)}
console.log(issueData);
return res.status(204);
}
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;
} }
else { else {
console.log('Successful request:', txID) amount = parseFloat(postNameWords[0]);
res.json({ txID })
} }
}
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 })
}
});
}); });
}) }
}); });
const port = process.env.PORT || 8181 const port = process.env.PORT || 8181
app.listen(port, function(){ app.listen(port, function(){
console.log('Faucet listening on port', port); console.log('Autobounty listening on port', port);
});

View File

@ -1,7 +1,7 @@
{ {
"name": "kovan-faucet", "name": "autobounty",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "Automatically award bounty specified to the address in issue tracker",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "node index.js", "start": "node index.js",
@ -10,6 +10,7 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"body-parser": "^1.17.2",
"cors": "^2.8.1", "cors": "^2.8.1",
"ethjs-provider-signer": "^0.1.4", "ethjs-provider-signer": "^0.1.4",
"ethjs-query": "^0.2.4", "ethjs-query": "^0.2.4",