mirror of
https://github.com/status-im/autobounty.git
synced 2025-02-27 12:30:42 +00:00
WIP: ether.js integration
This commit is contained in:
parent
a75ec287f0
commit
eeebfc5a1a
34
bot/index.js
34
bot/index.js
@ -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
|
||||||
|
33
index.js
33
index.js
@ -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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user