Skip to content

Instantly share code, notes, and snippets.

@ross-p
Last active August 24, 2023 08:04
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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;
}
@willowTank
Copy link

hello ross. How are you?
-- var gas = new BigNumber(21000);
What do you mean by 21000?
I am a beginner of ethereum.
Can you guide me for it?

@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