Does anyone have a script that can be used to transfer all ether found in an address to another address?
The script will constantly check for any incoming ether and make an automatic transaction, use half of the ether for gas.
const ETH_URL = ... //ex: http://localhost:8545
const TIMEOUT = 15000; //blocktime
const ADDRESS_FROM = ...
const ADDRESS_TO = ...
const TX_COST = 21000;
const web3 = new web3(ETH_URL);
setInterval(async () => {
const balance = await web3.eth.getBalance(ADDRESS_FROM);
if(balance > TX_COST) {
await web3.sendTransaction({to: ADDRESS_TO, from: ADDRESS_FROM, value: balance - TX_COST});
}
}), TIMEOUT);
I havent tried async function in setInterval()
. If it doesnt work replace by promises (then()...).
var from = ...;
var to = ...;
var fromBalance = web3.utils.toBN (web3.eth.getBalance (from));
var gasPrice = web3.utils.toBN (web3.eth.getGasPrice ());
var fee = gasPrice.mul (web3.utils.toBN (21000));
if (fromBalance.gte (fee)) {
web3.eth.sendTransaction ({
from: from,
to: to,
value: fromBalance.sub (fee),
gas: 21000,
gasPrice: gasPrice
});
21000 * web3.eth.getGasPrice()
would be enough.