autobounty/bot/index.js

174 lines
4.9 KiB
JavaScript
Raw Normal View History

2018-01-22 12:24:19 +01:00
const winston = require('winston');
2018-02-13 13:05:39 +01:00
const ethers = require('ethers');
const Wallet = ethers.Wallet;
const providers = ethers.providers;
2018-01-20 17:34:11 +01:00
const prices = require('./prices');
2018-01-20 17:32:11 +01:00
const config = require('../config');
2018-01-22 21:21:31 +01:00
const github = require('./github');
2018-01-22 12:24:19 +01:00
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: './log/error.log', level: 'error' }),
new winston.transports.File({ filename: './log/info.log', level: 'info' }),
// new winston.transports.File({ filename: './log/combined.log' })
]
2018-01-22 12:24:19 +01:00
});
2018-01-20 13:51:57 +01:00
const needsFunding = function (req) {
2018-01-23 14:39:12 +01:00
if (req.body.action !== 'created' || !req.body.hasOwnProperty('comment')) {
return false
} else if (req.body.comment.user.login !== config.githubUsername) {
2018-01-23 14:39:12 +01:00
return false
} else if (!hasAddress(req)) {
return false;
2018-01-23 14:39:12 +01:00
}
return true
}
2018-01-25 01:06:50 +01:00
const hasAddress = function (req) {
let commentBody = req.body.comment.body;
if (commentBody.search('Contract address:') === -1) {
return false;
} else {
return true;
}
}
const getAddress = function (req) {
2018-01-20 14:15:49 +01:00
let commentBody = req.body.comment.body;
return commentBody.substring(commentBody.search("Contract address:") + 18, commentBody.search("Contract address:") + 60)
}
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 01:06:50 +01: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 17:05:42 +01:00
});
}
2018-01-25 01:06:50 +01:00
const getAmountMock = function (req) {
2018-01-22 17:05:42 +01:00
return new Promise((resolve, reject) => {
resolve(10);
});
}
const getAmount = function (req) {
return new Promise((resolve, reject) => {
let labelPromise = getLabel(req);
2018-01-22 17:05:42 +01:00
let tokenPricePromise = prices.getTokenPrice(config.token);
2018-01-22 21:21:31 +01:00
Promise.all([labelPromise, tokenPricePromise])
.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 17:05:42 +01:00
});
}
// Logging functions
const logTransaction = function (tx) {
logger.info("[OK] Succesfully funded bounty with transaction " + tx.hash);
logger.info(" * From: " + tx.from);
2018-01-22 21:21:31 +01:00
logger.info(" * To: " + to);
logger.info(" * Amount: " + amount);
logger.info(" * Gas Price: " + tx.gasPrice);
logger.info("====================================================");
}
2018-02-13 11:43:28 +01:00
const info = function (msg) {
2018-01-22 12:24:19 +01:00
logger.info(msg);
}
const error = function (errorMessage) {
2018-01-22 21:21:31 +01:00
logger.error("[ERROR] Request processing failed: " + errorMessage);
}
2018-02-13 13:05:39 +01:00
2018-02-27 11:10:17 +01:00
2018-02-27 11:24:56 +01:00
const sendTransaction = function (to, amount, gasPrice) {
2018-02-27 11:10:17 +01:00
console.log("Creating wallet with PK: ", config.privateKey);
const wallet = new Wallet(config.privateKey);
2018-02-27 11:24:56 +01:00
wallet.provider = ethers.providers.getDefaultProvider('ropsten');
2018-02-27 11:10:17 +01:00
2018-02-13 13:05:39 +01:00
const transaction = {
nonce: 0,
gasLimit: config.gasLimit,
gasPrice: gasPrice,
to: to,
value: ethers.utils.parseEther(amount),
2018-02-27 11:10:17 +01:00
// data: "0x",
2018-02-13 13:05:39 +01:00
// This ensures the transaction cannot be replayed on different networks
2018-02-27 11:24:56 +01:00
chainId: 3 // ropsten
2018-02-13 13:05:39 +01:00
};
2018-02-23 17:36:03 +01:00
2018-02-13 13:05:39 +01:00
const signedTransaction = wallet.sign(transaction);
2018-02-23 17:36:03 +01:00
2018-02-13 13:05:39 +01:00
return new Promise((resolve, reject) => {
2018-02-27 11:10:17 +01:00
wallet.sendTransaction(signedTransaction)
2018-02-13 13:05:39 +01:00
.then(function(hash) {
2018-02-27 11:32:12 +01:00
logTransaction(hash, config.sourceAddress, to, amount, gasPrice);
2018-02-13 13:05:39 +01:00
resolve(hash);
}).catch(function(err) {
reject(err);
});
2018-02-23 17:36:03 +01:00
});
2018-02-13 13:05:39 +01:00
}
module.exports = {
needsFunding: needsFunding,
getAddress: getAddress,
getAmount: getAmount,
2018-01-22 16:49:41 +01:00
getGasPrice: prices.getGasPrice,
2018-02-13 13:05:39 +01:00
sendTransaction: sendTransaction,
2018-02-13 11:45:55 +01:00
info: info,
2018-01-22 17:08:23 +01:00
logTransaction: logTransaction,
error: error
}