6

Consider the following situation: my function can be divided into 2 parts

  1. Always consumes constant gas;
  2. Always results in a refund of 0 < R < 1500000 gas.

After the second part had happened, but before the end of a function call, how do I know the value of R?

The problem is that the msg.gas(which is remaining gas) doesn't account for the refund(cause the refund cannot be used inside the transaction itself). Due to this the following approach will fail:

uint startGas = msg.gas;                   // e.g. 100000 gas
delete myArray;                            // consumes 40000 gas, but will refund 20000 in the end
uint gasUsed = startGas - msg.gas + 21500; // gasUsed = 100000 - 60000 + 21500 == 61500 gas (21500 is for initial tx and function call)

We estimated that the gas spent for execution will be 61500, but in fact, after the refund, transaction receipt gasUsed value will be 41500.

I wonder if there is something like msg.refundGas is planned, or should I create a feature request?

4

There's no way to know the value of the EVM's refund counter from inside the EVM; if you want to account for refunds, you'll have to do them yourself, manually.

3

Given that delete myArray can be dangerous (How to clear large arrays without blowing the gas limit?), you probably want to know exactly how much storage you are clearing. Then you could multiply the number of storage slots cleared by 15,000 and check if the refund needs to be limited. Precise gas calculations can be tricky and could require testing.

Feel free and anyone can submit feature requests on Github.

  • 1
    delete myArray is just an example of an operation that results in a gas refund. In my real cases, there is lots of different logic that may, or not, result in a refund. – Oleksii Matiiasevych May 12 '16 at 14:43
  • 1
    Nick said it well, currently "There's no way to know the value of the EVM's refund counter from inside the EVM". Seems like a reasonable feature request to propose. – eth May 12 '16 at 18:08
  • Can we simulate the contract outside the blockchain with current input values and retrieve the simulated amount of gas used into the smart contract? (Note this is a newbee question as I don't even know if you can get data from outside the blockchain in a contract...) – Nicolas Massart May 12 '16 at 21:02
  • 1
    @NMassart Simulation can be done, but you're correct that contracts can't retrieve data. – eth May 13 '16 at 1:18

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.