0

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.

1
  • Using half of ether for gas is probably too generous. 21000 * web3.eth.getGasPrice() would be enough. Commented Apr 8, 2019 at 14:51

2 Answers 2

0
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()...).

1
0
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
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.