[Ethereum] Is it possible to empty an Ethereum wallet

gasgas-limitmetamaskwallet-transferwallets

When you send a transaction, you specify a gas price and gas limit. If it doesn't use the entire gas limit, then you get some of the money back. So it would seem to be impossible to know beforehand what the total used gas will be, leaving spare change in every wallet.

I tried to empty my MetaMask wallet. I looked up a reasonable gas price on https://ethgasstation.info/ (7). I entered the total balance to send, MetaMask of course said 'not enough balance' due to the fee. I changed the gas price to 7, copied the maximum fee, rejected the transaction, entered a new transaction with the amount equal to (total balance – maximum fee), and again changed the gas price to 7. After it was sent (a few hours), I was left with $0.09 in my MetaMask wallet! Even though I tried to carefully calculate the exact amount, it seems like would be impossible to empty a wallet.

MetaMask help says MyEtherWallet has a 'send max' feature, but how would that work?

https://support.metamask.io/kb/article/14-sending-max-balance

An easy way to send your full balance in the meanwhile is to export your MetaMask account to MyEtherWallet, which has a "send max" feature, and send to the exchange from there.

Isn't it true that you cannot know the actual gas used ahead of time? If you try to calculate the gas limit exactly, then you risk that it uses up all your gas and your transaction still fails! (I've had that happen before.)

Best Answer

If you're sending the ether to an EOA (externally-owned account, not a contract), then the gas used will always be 21000 gas, so it makes it easy to properly send the maximum. (I've personally done this a few times when consolidating accounts and gotten to exactly a 0 balance.)

If you're sending ether to a contract, it's usually still possible to get the gas exactly right. There's a JSON-RPC method eth_estimateGas which simulates the transaction you're about to make and returns the amount of gas used. I would expect MetaMask to use that value as the gas limit by default, but I'm not sure. Unless the contract consumes a different amount of gas based on some mutable state (and that state changes before your transaction gets mined), this should be exactly the amount of gas your actual transaction uses.

I would guess that MEW's "send max" sends the maximum you can assuming that the full gas limit is reached. So if your gas limit is set higher than the actual gas consumed, then there will be a little ether left over.

You're right that when in doubt, it's probably better to slightly overestimate the amount of gas you need (setting a slightly high gas limit) and then just write off the leftover ether.

Related Topic