1

It seems that different contract interactions cost different amounts of gas. For example, minting an NFT costs much more gas than the action of transferring some ether to another wallet.

Are these gas cost differences between different contract interactions due to the different lengths of code of their respective function calls? And will making a code more efficient cause gas for these interactions to cost less?

1 Answer 1

3

The minimum gas value for a transaction is 21000 (e.g. simple Ethereum transfer). Any additional (such as calculations / read /store /... operation) will incur additional costs. These operations should be priced according to the cost they impose on node operators. From the Whitepaper:

The intent of the fee system is to require an attacker to pay proportionately for every resource that they consume, including computation, bandwidth and storage; hence, any transaction that leads to the network consuming a greater amount of any of these resources must have a gas fee roughly proportional to the increment.

The main cost drivers for regular transactions are the cost of storing data in the chain (SSTORE with 5000/20000 gas) and reading data from the blockchain (SLOAD with 100/2100 gas). Below are some ressources about EVM Opcodes:

  1. EVM Opcodes: Description
  2. EVM Opcodes: Cost

In your example, transferring an NFT requires multiple writes to storage (e.g. when the owner of the token is updated). This makes this transaction much more expensive than a normal Ethereum transfer.

The gas cost of operations (opcodes) are repriced periodically (e.g. to avoid spamming). https://eips.ethereum.org/EIPS/eip-2929

2
  • Thanks alot. Could you also speak on how the length of the solidity code (.sol) itself plays into the gas fee of contract interactions?
    – user610620
    Nov 29, 2021 at 18:18
  • 1
    The size of the contract has no direct impact on the gas cost. However, there are some minor savings by putting frequently used functions at the top of the file and using fewer internal functions, but imo these savings are tiny compared to a reduction in SSTORES / SREADS + have their own drawbacks, so they are almost never worth it I can recommend the following tutorial for the development of a gas efficient ERC-20: youtube.com/… Nov 30, 2021 at 11:27

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.