[Ethereum] Is 21,000 Gas Limit Fixed Only for Ethereum

etherfeesgastransactions

I was reading a comment on another post on Ethereum SE and found the following statement:

21,000 is for sending Ether, not anything else.

The user suggested to "just set it to a large enough value (which you are willing to pay in the worst case)"

I'm perplexed. When sending ECR20 tokens (such as USDC and USDT) is the gas limit arbitrary? Or is it fixed? Setting it to an arbitrarily large number and hoping it will get accepted by the network might work but it doesn't address the fundamental question of how gas fees are calculated when sending ECR20 based tokens?

Another user suggested in a different thread that the gas fee is in fact fixed by the creator of the token:

You will have control over the gasPrice however a standard ERC20 token
transfer's gasUsed will be "fixed" (or at least out of your control).

What is the right answer? How should I calculate the gas fee when sending USDT and USDC?

Best Answer

The base number of gas needed for any transaction is 21,000 gas. Like you mentioned, sending Ether from an EOA to an EOA, without any data in the transaction, will always take exactly 21,000 gas.

Any gas on top of that is determined by a few things:

  • Transaction data: any zero byte of data costs 4 gas for example, and any non-zero byte costs 68 gas.
  • Contract execution: the cost of running code on the blockchain. This is determined by the opcodes that are used in the contract execution.

How much gas an ERC-20 token transaction takes depends on the contract implementation. A more complex contract, will result in a higher gas cost. There's no fixed number for token transfers, since the gas cost can depend on many different factors. For example, if someone never received tokens before (balance = 0), the cost for contract execution will be slightly higher because you're changing a number (balance) from zero to a non-zero value.

The cost of each operation is specified in the Ethereum Yellow Paper. To get an estimate of how much gas you need for a transaction, you can use the eth_estimateGas JSON-RPC function.

Related Topic