Skip to content

Instantly share code, notes, and snippets.

@ross-p
Last active August 24, 2023 08:04
Show Gist options
  • Save ross-p/cfa489bb7ed7427e4498058b0d6a5984 to your computer and use it in GitHub Desktop.
Save ross-p/cfa489bb7ed7427e4498058b0d6a5984 to your computer and use it in GitHub Desktop.
Transfer ENTIRE balance from one Ethereum account to another
/**
* Transfer the ENTIRE BALANCE from one account to another.
*
* Before you call this, you must unlock your account:
* personal.unlockAccount(from)
*
* @see https://github.com/ethereum/go-ethereum/issues/1637
* @see https://github.com/ethereum/go-ethereum/issues/2173
*/
function transferEntireBalance(from, to) {
var gas = new BigNumber(21000);
var price = web3.eth.gasPrice; // current average price; or set your own
var balance = eth.getBalance(from);
var value = balance.minus(gas.times(price));
if (value.greaterThan(0)) {
var txn = eth.sendTransaction({from: from, to: to, gasPrice: price, gas: gas, value: value});
console.log(" Transfer", from, "to", to, ":", txn);
return txn;
}
console.log(" Transfer "+ from +" to "+ to +": (No funds available)");
return null;
}
@WhiteRabbitGithub
Copy link

doesnt work on arbitrum 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment