WIP: sendTransaction structure and logFunding

This commit is contained in:
Pol.Alvarez@BSC 2018-01-22 16:50:23 +01:00
parent da6f6162dc
commit 918dfe41cc
3 changed files with 104 additions and 62 deletions

View File

@ -1,23 +1,23 @@
const winston = require('winston'); const winston = require('winston');
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 prices = require('./prices'); const prices = require('./prices');
const config = require('../config'); const config = require('../config');
const provider = new SignerProvider(config.signerPath, { const provider = new signerProvider(config.signerPath, {
signTransaction: (rawTx, cb) => cb(null, sign(rawTx, process.env.KEY)), signTransaction: (rawTx, cb) => cb(null, sign(rawTx, process.env.KEY)),
accounts: (cb) => cb(null, [address]), accounts: (cb) => cb(null, [address]),
}); });
const eth = new Eth(provider); const eth = new eth(provider);
const logger = winston.createLogger({ const logger = winston.createLogger({
level: 'info', level: 'info',
format: winston.format.json(), format: winston.format.json(),
transports: [ transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }), new winston.transports.File({ filename: config.logPath + 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'info.log', level: 'info'}), new winston.transports.File({ filename: config.logPath + 'info.log', level: 'info'}),
new winston.transports.File({ filename: 'combined.log' }) new winston.transports.File({ filename: config.logPath + 'combined.log' })
] ]
}); });
@ -45,7 +45,7 @@ const getAddress = function(req) {
} }
const getLabel = function(req) { const getLabel = function(req) {
let labelNames = req.body.issue.labels.map(lableObj => lableObj.name); let labelNames = req.body.issue.labels.map(labelObj => labelObj.name);
labels = labelNames.filter(name => bountyLabels.hasOwnProperty(name)); labels = labelNames.filter(name => bountyLabels.hasOwnProperty(name));
@ -71,19 +71,32 @@ const getAmount = function(req) {
const getGasPrice = function(req) { const getGasPrice = function(req) {
let gasPricePromise = prices.getGasPrice(); let gasPricePromise = prices.getGasPrice();
return gasPricePromise;
}
gasPricePromise // Logging functions
.then((gasPrice) => {return gasPrice})
.catch((err) => {console.log("TODO-ERROR: Failed gas price request throw log error")}); const logFunding = function(txId, from, to, amount, gasPrice){
// Check how to handle errors when promises does not arrive logger.info("\nSuccesfully funded bounty with transaction ", txId);
logger.info(" * From: ", from);
logger.info(" * To: ", to);
logger.info(" * Amount: ", amount);
logger.info(" * Gas Price: ", gasPrice);
logger.info("====================================================");
} }
const log = function(msg) { const log = function(msg) {
logger.info(msg); logger.info(msg);
} }
const error = function(requestBody, errorMessage) {
logger.error("[ERROR] Request processing failed: ", errorMessage);
logger.error("[ERROR] Request body: ", requestBody);
}
module.exports = { module.exports = {
eth: new Eth(provider), eth: new eth(provider),
needsFunding: needsFunding, needsFunding: needsFunding,
getAddress: getAddress, getAddress: getAddress,
getAmount: getAmount, getAmount: getAmount,

View File

@ -1,9 +1,18 @@
module.exports = { module.exports = {
urlEndpoint: "/autobounty/fund", debug: true,
signerPath: "https://ropsten.infura.io", urlEndpoint: '/autobounty/fund',
sourceAddress: "XXXXX", logPath: '../log/',
token: "SNT", signerPath: 'https://ropsten.infura.io',
sourceAddress: 'XXXXX',
token: 'SNT',
gasLimit: 92000, gasLimit: 92000,
priceHour: 35, priceHour: 35,
workHours: {xs: 2000, s: 2500} bountyLabelsHours: {
'bounty-xs': 1,
'bounty-s': 10,
'bounty-m': 100,
'bounty-l': 1000,
'bounty-xl': 10000,
'bounty-xx': 100000
};
} }

View File

@ -12,11 +12,11 @@ const config = require('./config');
const bot = require('./bot'); const bot = require('./bot');
var express = require('express'), var express = require('express'),
cors = require('cors'), cors = require('cors'),
helmet = require('helmet'), helmet = require('helmet'),
app = express(), app = express(),
bodyParser = require('body-parser'), bodyParser = require('body-parser'),
jsonParser = bodyParser.json(); jsonParser = bodyParser.json();
app.use(cors()); app.use(cors());
app.use(helmet()); app.use(helmet());
@ -24,21 +24,37 @@ app.use(helmet());
// 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(`${config.urlEndpoint}`, jsonParser, function(req, res, next) { app.post(`${config.urlEndpoint}`, jsonParser, function(req, res, next) {
// TODO decide how long the delay to start everything should be // TODO decide how long the delay to start everything should be
if (!req.body || !req.body.action) if (!req.body || !req.body.action) {
return res.sendStatus(400); return res.sendStatus(400);
}
if (!bot.needsFunding(req)) if (!bot.needsFunding(req)) {
return res.sendStatus(204); return res.sendStatus(204);
}
//const eth = bot.eth; const eth = bot.eth;
//const from = config.sourceAddress; const from = config.sourceAddress;
//const to = bot.getAddress(req); const to = bot.getAddress(req);
const amount = bot.getAmount(req);
const gasPrice = bot.getGasPrice(); // Asynchronous requests for Gas Price and Amount
bot.log('amount: ' + amount); const amountPromise = bot.getAmount(req);
bot.log('gasPrice: ' + gasPrice); const gasPricePromise = bot.getGasPrice();
/*
eth.getTransactionCount(address, (err, nonce) => { Promise.all([amountPromise, gasPricePromise])
.then(function(amount, gasPrice){
sendTransaction(eth, from, to, amount, gasPrice)
})
.catch(function(error){
bot.error(req.body, error);
});
}
const sendTransaction = function(eth, from, to, amount, gasPrice){
if (!config.debug){
eth.getTransactionCount(from, (err, nonce) => {
eth.sendTransaction({ eth.sendTransaction({
from: from, from: from,
to: to, to: to,
@ -48,20 +64,24 @@ app.post(`${config.urlEndpoint}`, jsonParser, function(req, res, next) {
nonce, nonce,
}, (err, txID) => { }, (err, txID) => {
if (err) { if (err) {
config.log('Request failed', err) bot.error(req.body, err)
return res.status(500).json(err) return res.status(500).json(err)
} }
else { else {
config.log('Successful request:', txID) bot.logFunding(txID, from, to, amount, gasPrice);
res.json({ txID }) res.json({ txID })
} }
}); });
}); });
*/
return res.sendStatus(200); return res.sendStatus(200);
}); } else {
let txId = -1;
bot.logFunding(txID, from, to, amount, gasPrice);
}
}
const port = process.env.PORT || 8181 const port = process.env.PORT || 8181
app.listen(port, function(){ app.listen(port, function(){
console.log('Autobounty listening on port', port); bot.log('Autobounty listening on port', port);
}); });