If fees are derived based on the equation (startGas - remainingGas) * gasPrice, why is gas even needed to be specified for a transaction?

The intrinsic gas can be derived from the contract itself, and as such so can the fee. So what is the reason for allowing originators to edit this?

  • Hi there. Do you mean why is it possible to set your own gasPrice? Apr 8 '18 at 14:45
  • Yes, why is it possible at all to set your own gas price when the operations in the smart contract detail their own gas cost anyway? Can't the amount of gas just be derived based on the types of operations and the number of them? And then from this, the fee can be derived which is then paid to the miner. I can't get my head around why gas is needed to be specified at all. Apr 8 '18 at 14:49

Gas cost is not the same as gas price.

The code in your smart contract equates to an equivalent set of EVM instructions. Each instruction has an associated gas cost, depending on how intensive an operation it is. Gas costs have been discussed in previous threads:

The gas cost of a given contract is intrinsic. You can't set it yourself, though it's recommended you try to code your contract in such a way to minimise the number of instructions, and therefore keep the associated gas cost to a minimum.

Gas costs are also referred to as gasUsed.

Gas price (gasPrice), on the other hand, is the amount that you are willing to pay per unit of instruction (i.e. per unit of gas cost). It's given in wei (1e-18 ether - see https://ethgasstation.info/).

Gas prices form a market. The number of transactions the network can support is limited by the size of the blocks, and other throughput mechanics. If the network is saturated, then someone is going to miss out. Setting a higher gas price allows you to price your transaction into the market, ahead of other lower-priced transactions.

So, overall:

Total cost = gasUsed * gasPrice

  • Ok, so the only point of originators being allowed to set a gas amount is simply to get their transaction finalised faster than others? I understand the concept of gas cost and that each operation costs gas, but I would have thought that after the operations are complete, a total gas amount cost could be derived which is then multiplied by the gasPrice and taken out of the amount of eth in the transaction. Actually, I think I'm getting this now. If there was no gas offered to begin with miners might waste a lot of time on transactions and receive nothing as there is no escrow. Apr 8 '18 at 15:13
  • So setting the amount of gas has two mechanisms: get your transaction mined quicker and also put up an amount that miners can receive regardless of whether or not your contract fails. Apr 8 '18 at 15:14
  • 1
    Yep, correct. If your contract could fail without incurring any cost, then it'd be easy to write a dense contract that would ultimately deliberately fail, with the intention of DDoSing the mining network. Charging for gas up-front regardless of success/failure is a way to deter this. Apr 8 '18 at 15:22
  • No problem - happy to help :-) Apr 8 '18 at 15:28

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.