Last active
August 24, 2023 08:04
-
-
Save ross-p/cfa489bb7ed7427e4498058b0d6a5984 to your computer and use it in GitHub Desktop.
Transfer ENTIRE balance from one Ethereum account to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?