mirror of
https://github.com/status-im/autobounty.git
synced 2025-02-28 04:50:41 +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) {
|
const getLabelsURL = function (req) {
|
||||||
let url = req.body.issue.labels_url;
|
let url = req.body.issue.labels_url;
|
||||||
// Make the URL generic removing the name of the label
|
// Make the URL generic removing the name of the label
|
||||||
|
console.log(url);
|
||||||
|
console.log('clean url' + url.replace('{name}', ''));
|
||||||
return 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)
|
// Returns all the bounty labelNames of a given issue (Github API v3)
|
||||||
const getLabels = function (req) {
|
const getLabels = function (req) {
|
||||||
|
console.log('starting getLabels from github...');
|
||||||
if (config.debug) {
|
if (config.debug) {
|
||||||
return getLabelsMock(req);
|
return getLabelsMock(req);
|
||||||
} else {
|
} 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 = {
|
const options = {
|
||||||
hostname: 'api.github.com',
|
hostname: 'api.github.com',
|
||||||
path: path,
|
path: path,
|
||||||
headers: { 'User-Agent': 'kafkasl' }
|
headers: { 'User-Agent': 'kafkasl' }
|
||||||
};
|
};
|
||||||
|
console.log('Github url: ' + options.hostname+path)
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const request = https.get(options, (response) => {
|
const request = https.get(options, (response) => {
|
||||||
// handle http errors
|
// handle http errors
|
||||||
|
20
bot/index.js
20
bot/index.js
@ -10,6 +10,7 @@ const logger = winston.createLogger({
|
|||||||
transports: [
|
transports: [
|
||||||
new winston.transports.File({ filename: config.logPath + 'error.log', level: 'error' }),
|
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: config.logPath + 'info.log', level: 'info' }),
|
||||||
|
new winston.transports.File({ filename: 'combined.log' })
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -19,10 +20,24 @@ const needsFunding = function (req) {
|
|||||||
return false
|
return false
|
||||||
} else if (req.body.comment.user.login !== config.githubUsername) {
|
} else if (req.body.comment.user.login !== config.githubUsername) {
|
||||||
return false
|
return false
|
||||||
|
} else if (!hasAddress(req)) {
|
||||||
|
console.log('does not have address');
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return true
|
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) {
|
const getAddress = function (req) {
|
||||||
let commentBody = req.body.comment.body;
|
let commentBody = req.body.comment.body;
|
||||||
return commentBody.substring(commentBody.search("Contract address:") + 18, commentBody.search("Contract address:") + 60)
|
return commentBody.substring(commentBody.search("Contract address:") + 18, commentBody.search("Contract address:") + 60)
|
||||||
@ -49,12 +64,14 @@ const getLabel = function (req) {
|
|||||||
if (config.debug) {
|
if (config.debug) {
|
||||||
return getLabelMock(req);
|
return getLabelMock(req);
|
||||||
}
|
}
|
||||||
|
console.log('starting getLabel...');
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
github.getLabels(req)
|
github.getLabels(req)
|
||||||
.then(labels => {
|
.then(labels => {
|
||||||
let bountyLabels = labels.filter(name => config.bountyLabels.hasOwnProperty(name));
|
let bountyLabels = labels.filter(name => config.bountyLabels.hasOwnProperty(name));
|
||||||
if (bountyLabels.length === 1) {
|
if (bountyLabels.length === 1) {
|
||||||
resolve(bountyLabels[0]);
|
console.log('getLabel promise finishes');
|
||||||
|
resolve(bountyLabels[0]);
|
||||||
} else {
|
} else {
|
||||||
error(req.body, 'More than 1 label found: [' + labels.length + ']');
|
error(req.body, 'More than 1 label found: [' + labels.length + ']');
|
||||||
reject(new Error('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 label = values[0];
|
||||||
let tokenPrice = values[1];
|
let tokenPrice = values[1];
|
||||||
let amountToPayDollar = config.priceHour * config.bountyLabels[label];
|
let amountToPayDollar = config.priceHour * config.bountyLabels[label];
|
||||||
|
console.log('getAmount finishes');
|
||||||
resolve(amountToPayDollar / tokenPrice);
|
resolve(amountToPayDollar / tokenPrice);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
@ -24,7 +24,8 @@ const getGasPrice = function() {
|
|||||||
// safeLowWait returns GWei (10^10 Wei).
|
// safeLowWait returns GWei (10^10 Wei).
|
||||||
let jsonBody = JSON.parse(body.join(''));
|
let jsonBody = JSON.parse(body.join(''));
|
||||||
let gasPriceWei = parseInt(jsonBody['safeLowWait']) * Math.pow(10, 10);
|
let gasPriceWei = parseInt(jsonBody['safeLowWait']) * Math.pow(10, 10);
|
||||||
resolve(gasPriceWei);
|
console.log('getGasPrice finishes')
|
||||||
|
resolve(gasPriceWei);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// handle connection errors of the request
|
// handle connection errors of the request
|
||||||
|
@ -32,5 +32,8 @@ module.exports = {
|
|||||||
bountyLabels: {},
|
bountyLabels: {},
|
||||||
|
|
||||||
// username for the bot which has to comment for starting the process (e.g. status-bounty-)
|
// 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)) {
|
} else if (!bot.needsFunding(req)) {
|
||||||
return res.sendStatus(204);
|
return res.sendStatus(204);
|
||||||
}
|
}
|
||||||
|
console.log('new req to process:' + req.body);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
processRequest(req)
|
processRequest(req)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return res.sendStatus(200);
|
console.log('Well funded');
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
bot.error('Error funding issue: ' + req.body.issue.url);
|
bot.error('Error funding issue: ' + req.body.issue.url);
|
||||||
bot.error('error: ' + err);
|
bot.error('error: ' + err);
|
||||||
bot.error('dump: ' + req);
|
bot.error('dump: ' + req);
|
||||||
return res.sendStatus(204);
|
|
||||||
});
|
});
|
||||||
}, config.delayInMiliSeconds);
|
}, config.delayInMiliSeconds);
|
||||||
|
|
||||||
|
return res.sendStatus(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
const processRequest = function (req) {
|
const processRequest = function (req) {
|
||||||
@ -51,30 +52,33 @@ const processRequest = function (req) {
|
|||||||
// Asynchronous requests for Gas Price and Amount
|
// Asynchronous requests for Gas Price and Amount
|
||||||
const amountPromise = bot.getAmount(req);
|
const amountPromise = bot.getAmount(req);
|
||||||
const gasPricePromise = bot.getGasPrice();
|
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])
|
transaction
|
||||||
.then(function (results) {
|
.then(function () {
|
||||||
let amount = results[0];
|
resolve();
|
||||||
let gasPrice = results[1];
|
})
|
||||||
let transaction = sendTransaction(eth, from, to, amount, gasPrice);
|
.catch(function (err) {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
|
||||||
transaction
|
})
|
||||||
.then(function () {
|
.catch(function (err) {
|
||||||
return res.sendStatus(200);
|
reject(err);
|
||||||
})
|
});
|
||||||
.catch(function (err) {
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
})
|
|
||||||
.catch(function (err) {
|
|
||||||
reject(err);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const sendTransaction = function (eth, from, to, amount, gasPrice) {
|
const sendTransaction = function (eth, from, to, amount, gasPrice) {
|
||||||
|
console.log('sending transaction...');
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (config.debug) {
|
if (!config.realTransaction) {
|
||||||
let txID = -1;
|
let txID = -1;
|
||||||
bot.logTransaction(txID, from, to, amount, gasPrice);
|
bot.logTransaction(txID, from, to, amount, gasPrice);
|
||||||
resolve();
|
resolve();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user