1

I transferred ETH 3 times to a smart contract which needs 48734 actual gas units to execute.

It took 68932 units for 1st tx and 48734 for remaining 2. Why it took more gas for the first tx than latter 2?

Link to 2nd and 3rd tx.

  • 2
    You called a smart contract method, maybe the first time it has to initialize some value for your address which can modify the amount of gas used. – megaturbo Jan 30 '18 at 12:18
2

Most of that is probably storage. One of the most expensive operations, at 20k gas, is allocating a new storage slot. So when a smart contract sets a storage slot from zero to non-zero, the sender of the tx is charged 20k gas. Similarly, when a storage slot is set from a non-zero to a non-zero (updating an already allocated storage slot), thea sender of the tx is charged 5k. This often means that for a lot of systems the user is charged more for a first time invocation, especially with token contracts because it has to initialize the users balance.

Aside from that, normal branching can often cause a few hundred to several thousand in difference. Also, a common difference is in the tx data. Tx data is charged 64 gas for non-zero bytes and only 4 gas for zero bytes, so if two people call the same function but my parameters have more zero bytes, then it's cheaper for me.

More information on opcode pricing can be found in the yellowpaper on pages 20 and 21.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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