I set the gas limit to 1 000 000 for every transaction. But each transaction takes less amount of gas than 1 000 000. Could you please explain why it happens or what I need to read to get this right. The same problem occurs when I use a call message and pass the gas limit to another contract. For example, I pass 100 000 gas but much less passed in reality.
1 Answer
The gas limit you set in remix is just the maximum gas you wish to spend on a given transaction. It does not mean that you will spend this much gas. The actual gas you spend depends on what your contract is doing. If there are complex calculations involved, you will spend more gas to compensate the miners for their processing power. If the total gas required for the transaction exceeds the gas limit you set, the transaction will fail.
The block gas limit is set by the network and is the maximum amount of gas allowed in a single block.
A really good breakdown of how the gas prices work is the Ethereum docs: https://ethereum.org/en/developers/docs/gas/