2

Consider the following contract written with solidity in remix.ethereum.org

// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.2 <0.9.0;

contract Sum {

        uint public sum;
        uint private first;
        uint private second;

     function setNumbers(uint a, uint b)public {

                   first=a;
                   second=b;
       }

     function add() public{

                    sum= first+second;
       }
    }

After the deploy, i have this from Metamask:Metamask info

And the bytecode from remix (Total 536 bytes)

0x608060405234801561001057600080fd5b506101f8806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806307d01c3a146100465780634f2be91f14610064578063a378c8981461006e575b600080fd5b61004e61008a565b60405161005b91906100d3565b60405180910390f35b61006c610090565b005b6100886004803603810190610083919061011f565b6100a8565b005b60005481565b6002546001546100a0919061018e565b600081905550565b81600181905550806002819055505050565b6000819050919050565b6100cd816100ba565b82525050565b60006020820190506100e860008301846100c4565b92915050565b600080fd5b6100fc816100ba565b811461010757600080fd5b50565b600081359050610119816100f3565b92915050565b60008060408385031215610136576101356100ee565b5b60006101448582860161010a565b92505060206101558582860161010a565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610199826100ba565b91506101a4836100ba565b92508282019050808211156101bc576101bb61015f565b5b9291505056fea264697066735822122066bf42d24b9ddb1454ff5a1a748bee45d9f6ca583f4ee7e2bda77c1715968e5064736f6c63430008120033

Now, what I would like is to find the gas units used, not directly from remix or metamask but to know exactly for what it was used. I found out from different sources that:

  1. Every transaction is 21000 gas.

  2. The creation is 32000 gas.

  3. For each non zero byte the gas units used are: N*16 (N is the number of non zero bytes)

  4. For each zero byte : n*4 (n = number of zero bytes)

  5. Cost of runtime bytecode which is: runtimeBytecode*200

  6. Opcode cost for initialization code which I found thanks to remix from the debug section.

The opcodes:Opcodes

With a total of 147. (It also gives the number of deployed bytecode, 0x01f8 is 504 in decimal)

So given these costs and the bytecode the cost would be:

  • 21.000 Gtransaction
  • 32.000 Gcreate
  • 7.328 Non zero bytes (458 * 16)
  • 312 Zero bytes (78 * 4)
  • 100.800 Deployed Bytecode Cost (504 * 200)
  • 147 Opcode cost for initialization code

161.587 Total

161.621 Total expected (from metamask)

(-34)

So the total i found is 161.587 but in metamask/remix it's 161.621 and I can't seem to find the mistake. Am i missing something?

unipsilo
  • 21
  • 2

0 Answers0