WIP: ether.js integration

This commit is contained in:
Jordi Montes 2018-02-13 13:05:39 +01:00
parent a75ec287f0
commit eeebfc5a1a
2 changed files with 36 additions and 31 deletions

View File

@ -1,4 +1,9 @@
const winston = require('winston'); const winston = require('winston');
const ethers = require('ethers');
const Wallet = ethers.Wallet;
const providers = ethers.providers;
const prices = require('./prices'); const prices = require('./prices');
const config = require('../config'); const config = require('../config');
const github = require('./github'); const github = require('./github');
@ -122,11 +127,40 @@ const error = function (errorMessage) {
} }
const wallet = new Wallet(config.privateKey);
const provider = providers.getDefaultProvider();
const sendTransaction = function (to, amount, gasPrice) {
const transaction = {
nonce: 0,
gasLimit: config.gasLimit,
gasPrice: gasPrice,
to: to,
value: amount,
data: "0x",
// This ensures the transaction cannot be replayed on different networks
chainId: providers.Provider.chainId.homestead
};
const signedTransaction = wallet.sign(transaction);
return new Promise((resolve, reject) => {
provider.sendTransaction(signedTransaction)
.then(function(hash) {
resolve(hash);
}).catch(function(err) {
reject(err);
});
});
}
module.exports = { module.exports = {
needsFunding: needsFunding, needsFunding: needsFunding,
getAddress: getAddress, getAddress: getAddress,
getAmount: getAmount, getAmount: getAmount,
getGasPrice: prices.getGasPrice, getGasPrice: prices.getGasPrice,
sendTransaction: sendTransaction,
log: log, log: log,
logTransaction: logTransaction, logTransaction: logTransaction,
error: error error: error

View File

@ -44,7 +44,7 @@ app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) {
}); });
const processRequest = function (req) { const processRequest = function (req) {
const eth = bot.eth; const wallet = bot.wallet;
const from = config.sourceAddress; const from = config.sourceAddress;
const to = bot.getAddress(req); const to = bot.getAddress(req);
@ -56,9 +56,8 @@ const processRequest = function (req) {
.then(function (results) { .then(function (results) {
let amount = results[0]; let amount = results[0];
let gasPrice = results[1]; let gasPrice = results[1];
let transaction = sendTransaction(eth, from, to, amount, gasPrice);
transaction bot.sendTransaction(to, amount, gasPrice)
.then(function () { .then(function () {
resolve(); resolve();
}) })
@ -73,34 +72,6 @@ const processRequest = function (req) {
}); });
} }
const sendTransaction = function (eth, from, to, amount, gasPrice) {
return new Promise((resolve, reject) => {
if (!config.realTransaction) {
let txID = -1;
bot.logTransaction(txID, from, to, amount, gasPrice);
resolve();
} else {
eth.getTransactionCount(from, (err, nonce) => {
eth.sendTransaction({
from: from,
to: to,
gas: gas,
gasPrice: gasPrice,
value: amount,
nonce,
}, (err, txID) => {
if (!err) {
bot.logTransaction(txID, from, to, amount, gasPrice);
resolve();
} else {
reject(err);
}
});
});
}
});
}
const port = process.env.PORT || 8181 const port = process.env.PORT || 8181
app.listen(port, function () { app.listen(port, function () {
bot.log('Autobounty listening on port', port); bot.log('Autobounty listening on port', port);