Betha version only some clean ups and readme needed

This commit is contained in:
Jordi Montes 2018-01-25 00:01:57 +00:00
parent 2a7bd77783
commit c05fc588fc
5 changed files with 57 additions and 25 deletions

View File

@ -8,6 +8,8 @@ const config = require('../config');
const getLabelsURL = function (req) {
let url = req.body.issue.labels_url;
// Make the URL generic removing the name of the label
console.log(url);
console.log('clean url' + url.replace('{name}', ''));
return url.replace('{/name}', '');
}
@ -18,15 +20,19 @@ const getLabelsMock = function (req) {
// Returns all the bounty labelNames of a given issue (Github API v3)
const getLabels = function (req) {
console.log('starting getLabels from github...');
if (config.debug) {
return getLabelsMock(req);
} else {
let path = getLabelsURL(req).remplace('api.github.com', '');
console.log('getting path for asking labels...');
let path = getLabelsURL(req).replace('https://api.github.com', '');
console.log('path: ' + path);
const options = {
hostname: 'api.github.com',
path: path,
headers: { 'User-Agent': 'kafkasl' }
};
console.log('Github url: ' + options.hostname+path)
return new Promise((resolve, reject) => {
const request = https.get(options, (response) => {
// handle http errors

View File

@ -10,6 +10,7 @@ const logger = winston.createLogger({
transports: [
new winston.transports.File({ filename: config.logPath + 'error.log', level: 'error' }),
new winston.transports.File({ filename: config.logPath + 'info.log', level: 'info' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
@ -19,10 +20,24 @@ const needsFunding = function (req) {
return false
} else if (req.body.comment.user.login !== config.githubUsername) {
return false
} else if (!hasAddress(req)) {
console.log('does not have address');
return false;
}
return true
}
const hasAddress = function(req) {
let commentBody = req.body.comment.body;
console.log(commentBody);
console.log(commentBody.search('Contract address:'));
if (commentBody.search('Contract address:') === -1) {
return false;
} else {
return true;
}
}
const getAddress = function (req) {
let commentBody = req.body.comment.body;
return commentBody.substring(commentBody.search("Contract address:") + 18, commentBody.search("Contract address:") + 60)
@ -49,11 +64,13 @@ const getLabel = function (req) {
if (config.debug) {
return getLabelMock(req);
}
console.log('starting getLabel...');
return new Promise((resolve, reject) => {
github.getLabels(req)
.then(labels => {
let bountyLabels = labels.filter(name => config.bountyLabels.hasOwnProperty(name));
if (bountyLabels.length === 1) {
console.log('getLabel promise finishes');
resolve(bountyLabels[0]);
} else {
error(req.body, 'More than 1 label found: [' + labels.length + ']');
@ -82,6 +99,7 @@ const getAmount = function (req) {
let label = values[0];
let tokenPrice = values[1];
let amountToPayDollar = config.priceHour * config.bountyLabels[label];
console.log('getAmount finishes');
resolve(amountToPayDollar / tokenPrice);
})
.catch((err) => {

View File

@ -24,6 +24,7 @@ const getGasPrice = function() {
// safeLowWait returns GWei (10^10 Wei).
let jsonBody = JSON.parse(body.join(''));
let gasPriceWei = parseInt(jsonBody['safeLowWait']) * Math.pow(10, 10);
console.log('getGasPrice finishes')
resolve(gasPriceWei);
});
});

View File

@ -32,5 +32,8 @@ module.exports = {
bountyLabels: {},
// username for the bot which has to comment for starting the process (e.g. status-bounty-)
githubUsername: ''
githubUsername: '',
// Activate real transactions
realTransaction: false
}

View File

@ -28,19 +28,20 @@ app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) {
} else if (!bot.needsFunding(req)) {
return res.sendStatus(204);
}
console.log('new req to process:' + req.body);
setTimeout(() => {
processRequest(req)
.then(() => {
return res.sendStatus(200);
console.log('Well funded');
})
.catch((err) => {
bot.error('Error funding issue: ' + req.body.issue.url);
bot.error('error: ' + err);
bot.error('dump: ' + req);
return res.sendStatus(204);
});
}, config.delayInMiliSeconds);
return res.sendStatus(200);
});
const processRequest = function (req) {
@ -51,7 +52,8 @@ const processRequest = function (req) {
// Asynchronous requests for Gas Price and Amount
const amountPromise = bot.getAmount(req);
const gasPricePromise = bot.getGasPrice();
console.log('processingRequest...');
return new Promise((resolve, reject) => {
Promise.all([amountPromise, gasPricePromise])
.then(function (results) {
let amount = results[0];
@ -60,7 +62,7 @@ const processRequest = function (req) {
transaction
.then(function () {
return res.sendStatus(200);
resolve();
})
.catch(function (err) {
reject(err);
@ -70,11 +72,13 @@ const processRequest = function (req) {
.catch(function (err) {
reject(err);
});
});
}
const sendTransaction = function (eth, from, to, amount, gasPrice) {
console.log('sending transaction...');
return new Promise((resolve, reject) => {
if (config.debug) {
if (!config.realTransaction) {
let txID = -1;
bot.logTransaction(txID, from, to, amount, gasPrice);
resolve();