What if a dynamic call at the end of a function uses all the remaining gas of the caller

dynamic-callexternal-functiongas-limitout-of-gassolidity

Suppose we have a contact like this:

contract x {

   function y() external
   {
      // do some magic!
      if (some_condition) {
        another_contract_address.call(abi.encodeWithSignature("another_contract_func()"));
      }
   }

}

which does something in a function y and then dynamically calls another method of another contract as its last sentence under specific conditions. As far as I know, all the remaining gas of the caller contract will be used for executing the callee function. Regarding the fact that this dynamic call is the last expression in y, what will happen if this dynamic call exhausts all the remaining gas? Does the transaction finish successfully (due to the fact that the RETURN opcode uses 0 gas) or it will be failed with an out of gas error?

Best Answer

The states will get reverted back to initial state (the state when you called the function). But the gas fees and incentive which you'ld have provided to miner will not be refunded . Transaction block will be present there for eternity. any amount of ether if you have paid will be returned back. Dynamic call will function like if it was a function call of same contract. So on revertion process the state variable of dynamic contract calling and caller contract both will get reverted.

Related Topic