transactions – What Determines Transaction Costs in Remix – Comprehensive Breakdown

feesgasremixtransactions

I've reviewed several related SE questions on this topic and I haven't seen an answer. In Remix, I've created two separate .sol files. The files are names file1.sol and file2.sol. The code for the two files looks like this:

file1.sol

pragma solidity ^0.4.0;

contract MyFirstContract {

}

file2.sol

pragma solidity ^0.4.0;

contract MyOtherContract {

}

When I review the transaction and execution costs, I'm slightly confused. The part that cons

          | transaction cost | execution cost |
file1.sol | 68926 gas        | 10862 gas      |
file2.sol | 68990 gas        | 10862 gas      |

Why are the transaction costs different when the contracts are virtually the same? At first, I suspected there was an added space or something else in the contract. However, I verified that wasn't the case. Why are there different transaction costs across these two contracts?

Best Answer

I can reproduce this with compiler version 0.4.12+commit.194ff033.Emscripten.clang (but, in my case, the other way round - file1.sol has a deployment cost of 68990, file2.sol has a deployment cost of 68926.)

There is a 64 gas difference in the cost between the two deployments. The reason for this is that, although the deployment code is exactly the same length (82 bytes), one of the sets of bytecodes has a zero byte in a place where the other has a non-zero byte.

When transmitting a transaction to the blockchain, the gas cost per byte of calldata is 68 for non-zero bytes, and 4 for zero bytes (Appendix G of the Yellow paper, G_txdatanonzero vs. G_txdatazero). Hence the 68 - 4 = 64 gas difference between your codes.

The different byte actually occurs in the appended contract metadata section, not in the deployment code or the deployed code itself which are identical for both your examples as you would expect. The following are the metadata sections for the contracts - the metadata is generated automatically by the compiler and comprises a Swarm hash of the contents of the source file plus some other bits and pieces:

a165627a7a72305820176535c2c0a4a3ac4a26111bf3f9aa4ab0fa5db14d592f8574bf15f1f42f46420029
a165627a7a723058204e0a27613ab151031db65041c0e2ff84bf88842fd7b2cc00163bc9af9acd05230029
                          zero/non-zero byte difference is here ^^

So, with two near identical source files you have some chance of there being a difference in deployment cost due to this, and it is compiler version dependent since the compiler version is embedded in the data used to create the Swarm hash.

Related Topic