Search code examples
nearprotocol

Gas counting. What is the difference between burnt gas and used gas?


During the transaction execution we are counting how much we "gas burned" and how much "gas used". Why do we have to track these counters separately?


Solution

    • Gas used includes gas burnt, so gas_used >= gas_burnt, always;
    • When any kind of fee is getting applied it counts towards both gas_burnt and gas_used;
    • When contract performs a cross-contract call and attaches X amount of gas to this call, then X counts towards gas_used but not gas_burnt. This is the only source of discrepancy between gas_used and gas_burnt. If the smart contract fails before it finishes the execution then none of the cross-contract calls (or transactions created by the contract) get executed and the attached gas is refunded;
    • Based on the previous bullet point when contract fails gas_used - gas_burnt is getting refunded back to the account, while gas_burnt is getting lost for good (because gas_burnt corresponds to the fees that actually costed some computation efforts to the validators).