mirror of
https://github.com/status-im/autobounty.git
synced 2025-02-27 12:30:42 +00:00
WIP: sendTransaction structure and logFunding
This commit is contained in:
parent
da6f6162dc
commit
918dfe41cc
45
bot/index.js
45
bot/index.js
@ -1,30 +1,30 @@
|
|||||||
const winston = require('winston');
|
const winston = require('winston');
|
||||||
const SignerProvider = require('ethjs-provider-signer');
|
const signerProvider = require('ethjs-provider-signer');
|
||||||
const sign = require('ethjs-signer').sign;
|
const sign = require('ethjs-signer').sign;
|
||||||
const Eth = require('ethjs-query');
|
const eth = require('ethjs-query');
|
||||||
const prices = require('./prices');
|
const prices = require('./prices');
|
||||||
const config = require('../config');
|
const config = require('../config');
|
||||||
|
|
||||||
const provider = new SignerProvider(config.signerPath, {
|
const provider = new signerProvider(config.signerPath, {
|
||||||
signTransaction: (rawTx, cb) => cb(null, sign(rawTx, process.env.KEY)),
|
signTransaction: (rawTx, cb) => cb(null, sign(rawTx, process.env.KEY)),
|
||||||
accounts: (cb) => cb(null, [address]),
|
accounts: (cb) => cb(null, [address]),
|
||||||
});
|
});
|
||||||
const eth = new Eth(provider);
|
const eth = new eth(provider);
|
||||||
|
|
||||||
const logger = winston.createLogger({
|
const logger = winston.createLogger({
|
||||||
level: 'info',
|
level: 'info',
|
||||||
format: winston.format.json(),
|
format: winston.format.json(),
|
||||||
transports: [
|
transports: [
|
||||||
new winston.transports.File({ filename: 'error.log', level: 'error' }),
|
new winston.transports.File({ filename: config.logPath + 'error.log', level: 'error' }),
|
||||||
new winston.transports.File({ filename: 'info.log', level: 'info'}),
|
new winston.transports.File({ filename: config.logPath + 'info.log', level: 'info'}),
|
||||||
new winston.transports.File({ filename: 'combined.log' })
|
new winston.transports.File({ filename: config.logPath + 'combined.log' })
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
const bountyLabels = {
|
const bountyLabels = {
|
||||||
'bounty-xs': 1,
|
'bounty-xs': 1,
|
||||||
'bounty-s': 10,
|
'bounty-s': 10,
|
||||||
'bounty-m': 100,
|
'bounty-m': 100,
|
||||||
'bounty-l': 1000,
|
'bounty-l': 1000,
|
||||||
'bounty-xl': 10000,
|
'bounty-xl': 10000,
|
||||||
'bounty-xx': 100000
|
'bounty-xx': 100000
|
||||||
@ -45,13 +45,13 @@ const getAddress = function(req) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getLabel = function(req) {
|
const getLabel = function(req) {
|
||||||
let labelNames = req.body.issue.labels.map(lableObj => lableObj.name);
|
let labelNames = req.body.issue.labels.map(labelObj => labelObj.name);
|
||||||
|
|
||||||
labels = labelNames.filter(name => bountyLabels.hasOwnProperty(name));
|
labels = labelNames.filter(name => bountyLabels.hasOwnProperty(name));
|
||||||
|
|
||||||
if (labels.length == 1)
|
if (labels.length == 1)
|
||||||
return labels[0];
|
return labels[0];
|
||||||
|
|
||||||
//log error
|
//log error
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -71,19 +71,32 @@ const getAmount = function(req) {
|
|||||||
|
|
||||||
const getGasPrice = function(req) {
|
const getGasPrice = function(req) {
|
||||||
let gasPricePromise = prices.getGasPrice();
|
let gasPricePromise = prices.getGasPrice();
|
||||||
|
return gasPricePromise;
|
||||||
|
}
|
||||||
|
|
||||||
gasPricePromise
|
// Logging functions
|
||||||
.then((gasPrice) => {return gasPrice})
|
|
||||||
.catch((err) => {console.log("TODO-ERROR: Failed gas price request throw log error")});
|
const logFunding = function(txId, from, to, amount, gasPrice){
|
||||||
// Check how to handle errors when promises does not arrive
|
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("====================================================");
|
||||||
}
|
}
|
||||||
|
|
||||||
const log = function(msg) {
|
const log = function(msg) {
|
||||||
logger.info(msg);
|
logger.info(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const error = function(requestBody, errorMessage) {
|
||||||
|
logger.error("[ERROR] Request processing failed: ", errorMessage);
|
||||||
|
logger.error("[ERROR] Request body: ", requestBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
eth: new Eth(provider),
|
eth: new eth(provider),
|
||||||
needsFunding: needsFunding,
|
needsFunding: needsFunding,
|
||||||
getAddress: getAddress,
|
getAddress: getAddress,
|
||||||
getAmount: getAmount,
|
getAmount: getAmount,
|
||||||
|
@ -1,9 +1,18 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
urlEndpoint: "/autobounty/fund",
|
debug: true,
|
||||||
signerPath: "https://ropsten.infura.io",
|
urlEndpoint: '/autobounty/fund',
|
||||||
sourceAddress: "XXXXX",
|
logPath: '../log/',
|
||||||
token: "SNT",
|
signerPath: 'https://ropsten.infura.io',
|
||||||
|
sourceAddress: 'XXXXX',
|
||||||
|
token: 'SNT',
|
||||||
gasLimit: 92000,
|
gasLimit: 92000,
|
||||||
priceHour: 35,
|
priceHour: 35,
|
||||||
workHours: {xs: 2000, s: 2500}
|
bountyLabelsHours: {
|
||||||
|
'bounty-xs': 1,
|
||||||
|
'bounty-s': 10,
|
||||||
|
'bounty-m': 100,
|
||||||
|
'bounty-l': 1000,
|
||||||
|
'bounty-xl': 10000,
|
||||||
|
'bounty-xx': 100000
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
102
index.js
102
index.js
@ -12,56 +12,76 @@ const config = require('./config');
|
|||||||
const bot = require('./bot');
|
const bot = require('./bot');
|
||||||
|
|
||||||
var express = require('express'),
|
var express = require('express'),
|
||||||
cors = require('cors'),
|
cors = require('cors'),
|
||||||
helmet = require('helmet'),
|
helmet = require('helmet'),
|
||||||
app = express(),
|
app = express(),
|
||||||
bodyParser = require('body-parser'),
|
bodyParser = require('body-parser'),
|
||||||
jsonParser = bodyParser.json();
|
jsonParser = bodyParser.json();
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(helmet());
|
app.use(helmet());
|
||||||
|
|
||||||
// Receive a POST request at the address specified by an env. var.
|
// Receive a POST request at the address specified by an env. var.
|
||||||
app.post(`${config.urlEndpoint}`, jsonParser, function(req, res, next) {
|
app.post(`${config.urlEndpoint}`, jsonParser, function(req, res, next) {
|
||||||
// TODO decide how long the delay to start everything should be
|
// TODO decide how long the delay to start everything should be
|
||||||
if (!req.body || !req.body.action)
|
if (!req.body || !req.body.action) {
|
||||||
return res.sendStatus(400);
|
return res.sendStatus(400);
|
||||||
|
}
|
||||||
|
|
||||||
if (!bot.needsFunding(req))
|
if (!bot.needsFunding(req)) {
|
||||||
return res.sendStatus(204);
|
return res.sendStatus(204);
|
||||||
|
}
|
||||||
|
|
||||||
//const eth = bot.eth;
|
const eth = bot.eth;
|
||||||
//const from = config.sourceAddress;
|
const from = config.sourceAddress;
|
||||||
//const to = bot.getAddress(req);
|
const to = bot.getAddress(req);
|
||||||
const amount = bot.getAmount(req);
|
|
||||||
const gasPrice = bot.getGasPrice();
|
// Asynchronous requests for Gas Price and Amount
|
||||||
bot.log('amount: ' + amount);
|
const amountPromise = bot.getAmount(req);
|
||||||
bot.log('gasPrice: ' + gasPrice);
|
const gasPricePromise = bot.getGasPrice();
|
||||||
/*
|
|
||||||
eth.getTransactionCount(address, (err, nonce) => {
|
Promise.all([amountPromise, gasPricePromise])
|
||||||
eth.sendTransaction({
|
.then(function(amount, gasPrice){
|
||||||
from: from,
|
sendTransaction(eth, from, to, amount, gasPrice)
|
||||||
to: to,
|
})
|
||||||
gas: gas,
|
.catch(function(error){
|
||||||
gasPrice: gasPrice,
|
bot.error(req.body, error);
|
||||||
value: amount,
|
});
|
||||||
nonce,
|
|
||||||
}, (err, txID) => {
|
}
|
||||||
if (err) {
|
|
||||||
config.log('Request failed', err)
|
|
||||||
return res.status(500).json(err)
|
const sendTransaction = function(eth, from, to, amount, gasPrice){
|
||||||
}
|
if (!config.debug){
|
||||||
else {
|
|
||||||
config.log('Successful request:', txID)
|
eth.getTransactionCount(from, (err, nonce) => {
|
||||||
res.json({ txID })
|
eth.sendTransaction({
|
||||||
}
|
from: from,
|
||||||
});
|
to: to,
|
||||||
});
|
gas: gas,
|
||||||
*/
|
gasPrice: gasPrice,
|
||||||
return res.sendStatus(200);
|
value: amount,
|
||||||
});
|
nonce,
|
||||||
|
}, (err, txID) => {
|
||||||
|
if (err) {
|
||||||
|
bot.error(req.body, err)
|
||||||
|
return res.status(500).json(err)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
bot.logFunding(txID, from, to, amount, gasPrice);
|
||||||
|
res.json({ txID })
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.sendStatus(200);
|
||||||
|
} else {
|
||||||
|
let txId = -1;
|
||||||
|
bot.logFunding(txID, from, to, amount, gasPrice);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const port = process.env.PORT || 8181
|
const port = process.env.PORT || 8181
|
||||||
app.listen(port, function(){
|
app.listen(port, function(){
|
||||||
console.log('Autobounty listening on port', port);
|
bot.log('Autobounty listening on port', port);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user