mirror of
https://github.com/status-im/autobounty.git
synced 2025-02-27 20:40:45 +00:00
Betha version only some clean ups and readme needed
This commit is contained in:
parent
2a7bd77783
commit
c05fc588fc
@ -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
|
||||
|
20
bot/index.js
20
bot/index.js
@ -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,12 +64,14 @@ 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) {
|
||||
resolve(bountyLabels[0]);
|
||||
console.log('getLabel promise finishes');
|
||||
resolve(bountyLabels[0]);
|
||||
} else {
|
||||
error(req.body, 'More than 1 label found: [' + labels.length + ']');
|
||||
reject(new Error('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) => {
|
||||
|
@ -24,7 +24,8 @@ const getGasPrice = function() {
|
||||
// safeLowWait returns GWei (10^10 Wei).
|
||||
let jsonBody = JSON.parse(body.join(''));
|
||||
let gasPriceWei = parseInt(jsonBody['safeLowWait']) * Math.pow(10, 10);
|
||||
resolve(gasPriceWei);
|
||||
console.log('getGasPrice finishes')
|
||||
resolve(gasPriceWei);
|
||||
});
|
||||
});
|
||||
// handle connection errors of the request
|
||||
|
@ -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
|
||||
}
|
||||
|
46
index.js
46
index.js
@ -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,30 +52,33 @@ 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];
|
||||
let gasPrice = results[1];
|
||||
let transaction = sendTransaction(eth, from, to, amount, gasPrice);
|
||||
|
||||
Promise.all([amountPromise, gasPricePromise])
|
||||
.then(function (results) {
|
||||
let amount = results[0];
|
||||
let gasPrice = results[1];
|
||||
let transaction = sendTransaction(eth, from, to, amount, gasPrice);
|
||||
transaction
|
||||
.then(function () {
|
||||
resolve();
|
||||
})
|
||||
.catch(function (err) {
|
||||
reject(err);
|
||||
});
|
||||
|
||||
transaction
|
||||
.then(function () {
|
||||
return res.sendStatus(200);
|
||||
})
|
||||
.catch(function (err) {
|
||||
reject(err);
|
||||
});
|
||||
|
||||
})
|
||||
.catch(function (err) {
|
||||
reject(err);
|
||||
})
|
||||
.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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user