2018-01-22 11:24:19 +00:00
|
|
|
const winston = require('winston');
|
2018-01-20 16:34:11 +00:00
|
|
|
const prices = require('./prices');
|
2018-01-20 16:32:11 +00:00
|
|
|
const config = require('../config');
|
2018-01-22 20:21:31 +00:00
|
|
|
const github = require('./github');
|
2018-01-19 17:33:27 +00:00
|
|
|
|
|
|
|
|
2018-01-22 11:24:19 +00:00
|
|
|
const logger = winston.createLogger({
|
2018-01-24 22:37:39 +00:00
|
|
|
level: 'info',
|
|
|
|
format: winston.format.json(),
|
|
|
|
transports: [
|
|
|
|
new winston.transports.File({ filename: config.logPath + 'error.log', level: 'error' }),
|
|
|
|
new winston.transports.File({ filename: config.logPath + 'info.log', level: 'info' }),
|
2018-01-25 00:06:50 +00:00
|
|
|
new winston.transports.File({ filename: 'combined.log' })
|
2018-01-24 22:37:39 +00:00
|
|
|
]
|
2018-01-22 11:24:19 +00:00
|
|
|
});
|
2018-01-19 17:33:27 +00:00
|
|
|
|
2018-01-20 12:51:57 +00:00
|
|
|
|
2018-01-24 22:37:39 +00:00
|
|
|
const needsFunding = function (req) {
|
2018-01-23 13:39:12 +00:00
|
|
|
if (req.body.action !== 'created' || !req.body.hasOwnProperty('comment')) {
|
2018-01-19 17:33:27 +00:00
|
|
|
return false
|
2018-01-24 22:37:39 +00:00
|
|
|
} else if (req.body.comment.user.login !== config.githubUsername) {
|
2018-01-23 13:39:12 +00:00
|
|
|
return false
|
2018-01-25 00:01:57 +00:00
|
|
|
} else if (!hasAddress(req)) {
|
|
|
|
return false;
|
2018-01-23 13:39:12 +00:00
|
|
|
}
|
2018-01-19 17:33:27 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-01-25 00:06:50 +00:00
|
|
|
const hasAddress = function (req) {
|
2018-01-25 00:01:57 +00:00
|
|
|
let commentBody = req.body.comment.body;
|
|
|
|
if (commentBody.search('Contract address:') === -1) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 22:37:39 +00:00
|
|
|
const getAddress = function (req) {
|
2018-01-20 13:15:49 +00:00
|
|
|
let commentBody = req.body.comment.body;
|
2018-01-24 22:37:39 +00:00
|
|
|
return commentBody.substring(commentBody.search("Contract address:") + 18, commentBody.search("Contract address:") + 60)
|
2018-01-19 17:33:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 22:37:39 +00:00
|
|
|
const getLabelMock = function (req) {
|
|
|
|
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]);
|
|
|
|
} else {
|
|
|
|
reject('Error getting bounty labels: ' + bountyLabels);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const getLabel = function (req) {
|
|
|
|
if (config.debug) {
|
|
|
|
return getLabelMock(req);
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
github.getLabels(req)
|
2018-01-25 00:06:50 +00:00
|
|
|
.then(labels => {
|
|
|
|
let bountyLabels = labels.filter(name => config.bountyLabels.hasOwnProperty(name));
|
|
|
|
if (bountyLabels.length === 1) {
|
|
|
|
resolve(bountyLabels[0]);
|
|
|
|
} else {
|
|
|
|
reject('Error getting bounty labels');
|
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
2018-01-22 16:05:42 +00:00
|
|
|
});
|
2018-01-19 17:33:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 00:06:50 +00:00
|
|
|
const getAmountMock = function (req) {
|
2018-01-22 16:05:42 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-01-24 22:37:39 +00:00
|
|
|
resolve(10);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const getAmount = function (req) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let labelPromise = getLabel(req);
|
2018-01-22 16:05:42 +00:00
|
|
|
let tokenPricePromise = prices.getTokenPrice(config.token);
|
2018-01-19 17:33:27 +00:00
|
|
|
|
2018-01-22 20:21:31 +00:00
|
|
|
Promise.all([labelPromise, tokenPricePromise])
|
2018-01-24 22:37:39 +00:00
|
|
|
.then(function (values) {
|
|
|
|
let label = values[0];
|
|
|
|
let tokenPrice = values[1];
|
|
|
|
let amountToPayDollar = config.priceHour * config.bountyLabels[label];
|
|
|
|
resolve(amountToPayDollar / tokenPrice);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
2018-01-22 16:05:42 +00:00
|
|
|
});
|
2018-01-19 17:33:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-22 15:50:23 +00:00
|
|
|
// Logging functions
|
|
|
|
|
2018-01-24 22:37:39 +00:00
|
|
|
const logTransaction = function (txId, from, to, amount, gasPrice) {
|
2018-01-22 20:21:31 +00:00
|
|
|
logger.info("[OK] Succesfully funded bounty with transaction " + txId);
|
|
|
|
logger.info(" * From: " + from);
|
|
|
|
logger.info(" * To: " + to);
|
|
|
|
logger.info(" * Amount: " + amount);
|
2018-01-24 22:37:39 +00:00
|
|
|
logger.info(" * Gas Price: " + gasPrice);
|
2018-01-22 15:50:23 +00:00
|
|
|
logger.info("====================================================");
|
2018-01-19 17:33:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 22:37:39 +00:00
|
|
|
const log = function (msg) {
|
2018-01-22 11:24:19 +00:00
|
|
|
logger.info(msg);
|
2018-01-19 17:33:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 22:37:39 +00:00
|
|
|
const error = function (errorMessage) {
|
2018-01-22 20:21:31 +00:00
|
|
|
logger.error("[ERROR] Request processing failed: " + errorMessage);
|
2018-01-22 15:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-19 17:33:27 +00:00
|
|
|
module.exports = {
|
|
|
|
needsFunding: needsFunding,
|
|
|
|
getAddress: getAddress,
|
|
|
|
getAmount: getAmount,
|
2018-01-22 15:49:41 +00:00
|
|
|
getGasPrice: prices.getGasPrice,
|
2018-01-22 16:08:23 +00:00
|
|
|
log: log,
|
|
|
|
logTransaction: logTransaction,
|
|
|
|
error: error
|
2018-01-19 17:33:27 +00:00
|
|
|
}
|