2018-01-22 11:24:19 +00:00
|
|
|
const winston = require('winston');
|
2018-01-22 15:50:23 +00:00
|
|
|
const signerProvider = require('ethjs-provider-signer');
|
2018-01-19 17:33:27 +00:00
|
|
|
const sign = require('ethjs-signer').sign;
|
2018-01-22 15:50:23 +00:00
|
|
|
const eth = require('ethjs-query');
|
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-19 17:33:27 +00:00
|
|
|
|
2018-01-22 15:50:23 +00:00
|
|
|
const provider = new signerProvider(config.signerPath, {
|
2018-01-19 17:33:27 +00:00
|
|
|
signTransaction: (rawTx, cb) => cb(null, sign(rawTx, process.env.KEY)),
|
|
|
|
accounts: (cb) => cb(null, [address]),
|
|
|
|
});
|
2018-01-22 15:50:23 +00:00
|
|
|
const eth = new eth(provider);
|
2018-01-19 17:33:27 +00:00
|
|
|
|
2018-01-22 11:24:19 +00:00
|
|
|
const logger = winston.createLogger({
|
|
|
|
level: 'info',
|
|
|
|
format: winston.format.json(),
|
|
|
|
transports: [
|
2018-01-22 15:50:23 +00:00
|
|
|
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 + 'combined.log' })
|
2018-01-22 11:24:19 +00:00
|
|
|
]
|
|
|
|
});
|
2018-01-19 17:33:27 +00:00
|
|
|
|
2018-01-20 13:15:49 +00:00
|
|
|
const bountyLabels = {
|
2018-01-22 15:50:23 +00:00
|
|
|
'bounty-xs': 1,
|
2018-01-20 13:15:49 +00:00
|
|
|
'bounty-s': 10,
|
2018-01-22 15:50:23 +00:00
|
|
|
'bounty-m': 100,
|
2018-01-20 13:15:49 +00:00
|
|
|
'bounty-l': 1000,
|
|
|
|
'bounty-xl': 10000,
|
|
|
|
'bounty-xx': 100000
|
|
|
|
};
|
2018-01-20 12:51:57 +00:00
|
|
|
|
2018-01-19 17:33:27 +00:00
|
|
|
const needsFunding = function(req) {
|
2018-01-20 16:26:49 +00:00
|
|
|
if (req.body.action !== 'created' || !req.body.hasOwnProperty('comment'))
|
2018-01-19 17:33:27 +00:00
|
|
|
return false
|
2018-01-20 13:15:49 +00:00
|
|
|
//else if (req.comment.user.login !== 'status-open-bounty')
|
|
|
|
// return false
|
2018-01-19 17:33:27 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
const getAddress = function(req) {
|
2018-01-20 13:15:49 +00:00
|
|
|
let commentBody = req.body.comment.body;
|
2018-01-19 17:33:27 +00:00
|
|
|
return commentBody.substring(commentBody.search("Contract address:") + 18, commentBody.search("Contract address:") + 60)
|
|
|
|
}
|
|
|
|
|
2018-01-20 13:15:49 +00:00
|
|
|
const getLabel = function(req) {
|
2018-01-22 15:50:23 +00:00
|
|
|
let labelNames = req.body.issue.labels.map(labelObj => labelObj.name);
|
2018-01-20 13:15:49 +00:00
|
|
|
|
|
|
|
labels = labelNames.filter(name => bountyLabels.hasOwnProperty(name));
|
|
|
|
|
|
|
|
if (labels.length == 1)
|
|
|
|
return labels[0];
|
2018-01-22 15:50:23 +00:00
|
|
|
|
2018-01-20 13:15:49 +00:00
|
|
|
//log error
|
|
|
|
return 0;
|
2018-01-19 17:33:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const getAmount = function(req) {
|
2018-01-20 16:34:11 +00:00
|
|
|
let tokenPricePromise = prices.getTokenPrice(config.token);
|
2018-01-19 17:33:27 +00:00
|
|
|
|
|
|
|
let label = getLabel(req);
|
|
|
|
let amountToPayDollar = config.priceHour * config.workHours[label];
|
|
|
|
|
|
|
|
tokenPricePromise
|
2018-01-20 12:51:57 +00:00
|
|
|
.then((tokenPrice) => {return tokenPrice * config.amountToPayInDollars} )
|
|
|
|
.catch((err) => {console.log("TODO-ERROR: Failed token price request throw log error")});
|
2018-01-19 17:33:27 +00:00
|
|
|
// Check how to handle errors when promises does not arrive
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const getGasPrice = function(req) {
|
2018-01-20 16:34:11 +00:00
|
|
|
let gasPricePromise = prices.getGasPrice();
|
2018-01-22 15:50:23 +00:00
|
|
|
return gasPricePromise;
|
|
|
|
}
|
2018-01-19 17:33:27 +00:00
|
|
|
|
2018-01-22 15:50:23 +00:00
|
|
|
// Logging functions
|
|
|
|
|
|
|
|
const logFunding = function(txId, from, to, amount, gasPrice){
|
|
|
|
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("====================================================");
|
2018-01-19 17:33:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 11:24:19 +00:00
|
|
|
const log = function(msg) {
|
|
|
|
logger.info(msg);
|
2018-01-19 17:33:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 15:50:23 +00:00
|
|
|
const error = function(requestBody, errorMessage) {
|
|
|
|
logger.error("[ERROR] Request processing failed: ", errorMessage);
|
|
|
|
logger.error("[ERROR] Request body: ", requestBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-19 17:33:27 +00:00
|
|
|
module.exports = {
|
2018-01-22 15:50:23 +00:00
|
|
|
eth: new eth(provider),
|
2018-01-19 17:33:27 +00:00
|
|
|
needsFunding: needsFunding,
|
|
|
|
getAddress: getAddress,
|
|
|
|
getAmount: getAmount,
|
|
|
|
getGasPrice: getGasPrice,
|
|
|
|
log: log
|
|
|
|
}
|