autobounty/bot/index.js

103 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-01-22 11:24:19 +00:00
const winston = require('winston');
const signerProvider = require('ethjs-provider-signer');
const sign = require('ethjs-signer').sign;
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');
const provider = new signerProvider(config.signerPath, {
signTransaction: (rawTx, cb) => cb(null, sign(rawTx, process.env.KEY)),
accounts: (cb) => cb(null, [address]),
});
const eth = new eth(provider);
2018-01-22 11:24:19 +00:00
const logger = winston.createLogger({
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'}),
new winston.transports.File({ filename: config.logPath + 'combined.log' })
2018-01-22 11:24:19 +00:00
]
});
2018-01-20 12:51:57 +00:00
const needsFunding = function(req) {
2018-01-20 16:26:49 +00:00
if (req.body.action !== 'created' || !req.body.hasOwnProperty('comment'))
return false
2018-01-20 13:15:49 +00:00
//else if (req.comment.user.login !== 'status-open-bounty')
// return false
return true
}
const getAddress = function(req) {
2018-01-20 13:15:49 +00:00
let commentBody = req.body.comment.body;
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 16:05:42 +00:00
return github.getLabels(req).then(labels => {
if (labels.length === 1) {
resolve(labels[0]);
} else {
2018-01-22 15:49:41 +00:00
// TODO: Handle error
2018-01-22 16:05:42 +00:00
}
}).catch(err => {
// TODO: Handle error
});
}
const getAmount = function(req) {
2018-01-22 16:05:42 +00:00
return new Promise((resolve, reject) => {
let labelPromise = getLabel(req);
let tokenPricePromise = prices.getTokenPrice(config.token);
Promise.all([labelPromise, tokenPricePromise]).then(function(values) {
let label = values[0];
let tockenPrice = values[1];
let amountToPayDollar = config.priceHour * config.workHours[label];
2018-01-22 16:05:42 +00:00
reslove(config.amountToPayDollar/tockenPrice);
}).catch(error => {
// TODO: Handle error
});
});
}
const getGasPrice = function(req) {
2018-01-20 16:34:11 +00:00
let gasPricePromise = prices.getGasPrice();
return gasPricePromise;
}
// Logging functions
2018-01-22 16:08:23 +00:00
const logTransaction = function(txId, from, to, amount, gasPrice){
logger.info("\n[OK] Succesfully 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-22 11:24:19 +00:00
const log = function(msg) {
logger.info(msg);
}
2018-01-22 16:08:23 +00:00
const error = function(requestInfo, errorMessage) {
logger.error("[ERROR] Request processing failed: ", errorMessage);
2018-01-22 16:08:23 +00:00
logger.error("[ERROR] Request body: ", requestInfo);
}
module.exports = {
eth: new eth(provider),
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
}