autobounty/index.js

54 lines
1.6 KiB
JavaScript
Raw Normal View History

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-10 15:07:15 +00:00
*/
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');
const address = process.env.ADDRESS
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'),
app = express();
2017-03-07 11:24:01 +00:00
app.use(cors());
2017-06-10 15:07:15 +00:00
// Receive a POST request at the address specified by an env. var.
app.post('/address/:address', function(req, res, next){
2017-03-10 09:01:43 +00:00
eth.getTransactionCount(address, (err, nonce) => {
eth.sendTransaction({
2017-06-10 15:07:15 +00:00
from: address, // Specified in webhook, secret
to: req.params.address, // TODO replace with address from earlier in the thread
2017-03-10 09:01:43 +00:00
gas: 100000,
2017-06-10 15:07:15 +00:00
value: (parseFloat(process.env.AMOUNT) || 1.5) * 1e18, // TODO replace with parsed amount from comments
2017-03-10 09:01:43 +00:00
data: '0xde5f72fd', // sha3('faucet()')
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-06-10 18:24:34 +00:00
});
2017-03-07 11:24:01 +00:00
});
const port = process.env.PORT || 8181
app.listen(port, function(){
console.log('Autobounty listening on port', port);
2017-06-10 18:24:34 +00:00
});