From eeebfc5a1a20b6862164a016c114a643e693b7a1 Mon Sep 17 00:00:00 2001 From: Jordi Montes Date: Tue, 13 Feb 2018 13:05:39 +0100 Subject: [PATCH] WIP: ether.js integration --- bot/index.js | 34 ++++++++++++++++++++++++++++++++++ index.js | 33 ++------------------------------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/bot/index.js b/bot/index.js index 43c8f02..7c86757 100644 --- a/bot/index.js +++ b/bot/index.js @@ -1,4 +1,9 @@ const winston = require('winston'); + +const ethers = require('ethers'); +const Wallet = ethers.Wallet; +const providers = ethers.providers; + const prices = require('./prices'); const config = require('../config'); 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 = { needsFunding: needsFunding, getAddress: getAddress, getAmount: getAmount, getGasPrice: prices.getGasPrice, + sendTransaction: sendTransaction, log: log, logTransaction: logTransaction, error: error diff --git a/index.js b/index.js index c7c24a6..0d20890 100644 --- a/index.js +++ b/index.js @@ -44,7 +44,7 @@ app.post(`${config.urlEndpoint}`, jsonParser, function (req, res, next) { }); const processRequest = function (req) { - const eth = bot.eth; + const wallet = bot.wallet; const from = config.sourceAddress; const to = bot.getAddress(req); @@ -56,9 +56,8 @@ const processRequest = function (req) { .then(function (results) { let amount = results[0]; let gasPrice = results[1]; - let transaction = sendTransaction(eth, from, to, amount, gasPrice); - transaction + bot.sendTransaction(to, amount, gasPrice) .then(function () { 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 app.listen(port, function () { bot.log('Autobounty listening on port', port);