0

Please stop me if I am wrong.

When we call any "state-changing" function of smart contract, this function will be run by all the miners of the mainnet. As a caller, I need to pay this using the ether and whenever I use the metamask, it estimates how much it will cost.

Here is what I am not sure

How to even estimate the number of miners? we don't know how many miners are there or how many of them will run my function? If I paid X amount and there are more miners than I paid, it won't be runnable by other miners.

How is this handled? Am I missing something?

1
  • 1
    The number of miners on the network has nothing to do with all of this. As far as you're concerned, you may assume that there's a single miner that you are paying to. Dec 2 '20 at 7:24
2

The gas fee for executing a state-changing function in a smart contract is the sum of:

  1. The total cost of the executed opcodes, where each opcode has a specific cost
  2. The total cost of changed storage slots (states), where:
    • Changing a slot from zero to non-zero costs 20000 gas
    • Changing a slot from non-zero to non-zero costs 10000 gas
    • Changing a slot from non-zero to zero refunds 5000 gas (under certain restrictions)

The whole thing depends on the actual state of the blockchain at the time of execution.

For example, the cost of executing:

if (someContract.someNonPureFunction()) {
    // do something
}
else {
    // do something else
}

Depends on the state of someContract.

That's why the whole thing is referred to as gas-estimate.

As you understand, the number of miners on the network has nothing to do with all of this.

As far as you're concerned, you may assume that there's a single miner that you are paying to.

3
  • thank you for your response; however, I am even more confused now. What do you mean the number of miners has nothing to do with it? Say function A is non-pure and I invoke this which creates a transaction. This transaction needs to be executed by all miners right? Aren't each run requires a transaction fee? Say x is the fee for one miner. If there are 5 miners, fee is 5x. Am I wrong? Dec 3 '20 at 3:01
  • 1
    @EmrahSarıboz: Yes, you're wrong. If the fee is x, then you pay x. Dec 3 '20 at 4:29
  • I got it, thanks Dec 3 '20 at 5:01

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.