[Ethereum] Calculating Ethereum Gas Fees

etheretherscanfeestransactions

How are gas fees calculated in account based model coins like ETH?

Let's say I own 10 ETH addresses with 0 starting balance. I receive 0.1 ETH payment from 10 people – each one sending a payment of 0.1 to one of my 10 ETH addresses (Obviously in 10 separate transactions). The combined total in all 10 addresses I own is now equal to exactly 1 ETH.

I now want to "forward" (consolidate) all 10 payments received to a single ETH address under my control. According to EtherScan it costs approx. 40 gwei (or $0.90) for the transaction to be confirmed in 16 min (slowest confirmation time). Assume I chose to forward all 10 payments using 16 min confirmation time. My impression is that the combined fee should be calculated as 10 * $0.90 (or 1- * 40 gewi * 21,000) which is equal to $9. Is this correct?

If, after forwarding ("consolidating") the entire amount to one address, I now want to transfer that amount (1 ETH) to another address (single output), the fee for this transfer would only be $0.90 cents, correct (since all ETH is now originating from a single address)?

If instead of sending 1 ETH to one address, I split it between two ETH addresses (so 0.5 ETH to each address) the cost is calculated as 2 * $0.90 cents or $1.8 total or is it one fee of $0.90?

Assuming my calculation above is accurate, does the calculation change if I transferred the original 10 * 0.1 payment to the single ETH address via a single batch transaction vs. if they were transferred in 10 different transaction? My impression is that since ETH uses an account based model there's no concept of batch transfers. So whether the original 10 * 0.1 were transferred at one time vs multiple occasions/times – it would not effect the fee I pay when transferring 1 ETH to another address. Is this correct?

I hope the question is not too basic as I searched and could not find a definitive answer.

Best Answer

You are mostly correct in your analysis. Sending ETH from account A to account B will always cost (21000 * gasPrice). If you send ETH from account A to account B and C, you will have to do 2 transactions - A to B, then A to C. So you will pay twice as more fees (2 * 21000 * gasPrice).

Examples:

From A To B, C and D - 3 transactions (A-B, A-C, A-D), so 3 times the fee

From A To B - 1 transaction (A-B), so 1 time the fee

From A and B To C - 2 transactions (A-C, B-C) so 2 times the fee

However, contrary to what I just wrote, it is possible to send ETH from one account to N accounts and pay less than N times the fee per transaction (what you call a batch transfer). To do so, instead of sending the ETH from account A to all the N accounts directly, you must send the ETH from account A to a smart contract (DApp) that will handle the distribution to multiple accounts in a single transaction.

I read about disperse.app that is supposed to do exactly that, but I have not used it myself so do you own due diligence before using this service if you ever do. However, according to this post, you would only save gas on transactions that send ETH to accounts that have already been initialized. For brand new accounts, the gas cost is 25000 instead of the regular 21000 when doing a direct ether transfer. Sending ether from the DApp to existing accounts costs either 5000 or 20000 gas depending on whether the receiving address already has a balance or not, which is better than the regular 21000. I did not test this myself and only read about it in the above post.

You can estimate the gas of calling a DApp function by different methods. If calling the contract from Metamask, Metamask provides a generally accurate estimate of the gas you will pay just before confirming the transaction. If you are using web3.js to call the DApp, you should use the estimateGas() function. In all cases, you have to multiply the gas amount by the gas price in WEI to get the total cost in WEI that you will pay to call the DApp.

Related Topic