Add parsing amount, storage of address from thread
Amount is parsed from comment having "@NAME" where name is env. variable. Address from commiteth's comment, stored in a global variable. Destroyed when reinstantiating container. (fixes aragon/autobounty#1) Revert Dockerfile change Revert index.js whitespace
This commit is contained in:
parent
3932bbbe10
commit
1b7252ed26
|
@ -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
|
||||||
|
|
42
index.js
42
index.js
|
@ -4,13 +4,16 @@
|
||||||
* awards that bounty to the address posted earlier in the thread (by the
|
* awards that bounty to the address posted earlier in the thread (by the
|
||||||
* commiteth bot).
|
* commiteth bot).
|
||||||
* TODO tests
|
* 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)),
|
||||||
|
@ -24,17 +27,45 @@ var express = require('express'),
|
||||||
bodyParser = require('body-parser'),
|
bodyParser = require('body-parser'),
|
||||||
jsonParser = bodyParser.json();
|
jsonParser = bodyParser.json();
|
||||||
|
|
||||||
app.use(jsonParser);
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
|
// Store issue ids and their bounty addresses
|
||||||
|
var issueData = {};
|
||||||
|
|
||||||
// Receive a POST request at the address specified by an env. var.
|
// Receive a POST request at the address specified by an env. var.
|
||||||
app.post('/address/:address', function(req, res, next){
|
app.post('/address/:address', jsonParser, function(req, res, next){
|
||||||
|
if (!req.body)
|
||||||
|
return res.sendStatus(400);
|
||||||
|
var commentBody = req.body.comment.body;
|
||||||
|
var issueId = req.body.issue.id;
|
||||||
|
var namePosition = commentBody.search("@" + name);
|
||||||
|
// Store toAddress from commiteth
|
||||||
|
if (namePosition == -1) {
|
||||||
|
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 {
|
||||||
|
amount = parseFloat(postNameWords[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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.getTransactionCount(address, (err, nonce) => {
|
||||||
eth.sendTransaction({
|
eth.sendTransaction({
|
||||||
from: address, // Specified in webhook, secret
|
from: address, // Specified in webhook, secret
|
||||||
to: req.params.address, // TODO replace with address from earlier in the thread
|
to: issueData[issueId].toAddress, // Address from earlier in the thread
|
||||||
gas: 100000,
|
gas: 100000,
|
||||||
value: (parseFloat(process.env.AMOUNT) || 1.5) * 1e18, // TODO replace with parsed amount from comments
|
value: issueData[issueId].amount,
|
||||||
data: '0xde5f72fd', // sha3('faucet()')
|
data: '0xde5f72fd', // sha3('faucet()')
|
||||||
nonce,
|
nonce,
|
||||||
}, (err, txID) => {
|
}, (err, txID) => {
|
||||||
|
@ -48,6 +79,7 @@ app.post('/address/:address', function(req, res, next){
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const port = process.env.PORT || 8181
|
const port = process.env.PORT || 8181
|
||||||
|
|
Loading…
Reference in New Issue