6

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

2 Answers 2

4

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.

8
  • So sending Ether is 21,000 no matter how many inputs? Bitcoin transactions with many inputs have higher transaction fees. Yes, I ran out of gas before sending to a contract. It used about 6 million gas. How do you convert from gas to Ether? I think a gas/GWEI is the same as a satoshi in Bitcoin, but I'm not sure.
    – Chloe
    Jan 15, 2018 at 19:18
  • 1
    I'm not sure what you mean by "how many inputs." A simple transfer of ether (regardless of the amount) costs 21,000 gas. That's the minimum cost of a transaction. Gas * gasPrice = how much the transaction will cost. A gwei is a billionth of an ether. See etherconverter.online for an easy conversion tool.
    – user19510
    Jan 15, 2018 at 19:20
  • Before there was ethereum, there was a blockchain based currency called bitcoin. Each bitcoin address can contain a public key and a private key. A bitcoin balance may be spread across many different addresses, all summed up. When you spend a bitcoin, you create a transaction with the address containing the value you wish to send. If that address doesn't have enough, then you send multiple addresses. Sometimes after receiving many thousands of payments, such as a merchant, you may have thousands of addresses. Sending a large transaction may use up many addresses and result in a large block of
    – Chloe
    Jan 15, 2018 at 19:57
  • Ethereum has no such concept.
    – user19510
    Jan 15, 2018 at 19:59
  • 2
    Ethereum doesn't have a concept of multiple inputs. So a transaction is always from a single address to a single address. Ethereum does, however, have smart contracts, which can do all sorts of things (including storing data, sending ether, and deploying new contracts). The gas cost of sending a transaction to a smart contract varies depending on how much work that smart contract does.
    – user19510
    Jan 15, 2018 at 20:03
2

Transferring funds to another account costs 21000 gas. So to send the entire amount, set the gas limit to 21000 and multiply it with whatever gas price you specified. Then subtract this from the balance of the account. This way there should be no funds left in your account and you can keep your peace :)

2
  • It is 21000 always? No matter how many inputs? Bitcoin transactions with many inputs have higher transaction fees. How do you convert gas to ether? I think gas/GWEI is the same as a satoshi in Bitcoin, but I'm not sure.
    – Chloe
    Jan 15, 2018 at 19:13
  • I'm guessing this is harder to do with EIP-1559 transactions that use max-fee/max-pri values for gas. But I guess the eth you save (thanks to EIP-1559) will just have to stay in the wallet you are emptying forever, instead of going to the miners. <shrug>
    – Donn Lee
    Mar 29, 2022 at 23:08

Your Answer

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

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