2

Even though functions called with staticcall cannot modify the state, I ran into an issue when (accidentally) calling a non-view/pure function using staticcall. For example, using this contract:

pragma solidity 0.8.3;

contract TestContract {
  address public foo;
  
  function setFoo(address _foo) external {
    foo = _foo;
  }
  
  function doFoo(address _target, address _foo) external returns (bool success) {
    (success,) = _target.staticcall(abi.encodeWithSignature("setFoo(address)", _foo));
  }
}

Calling doFoo() in Remix, with the contract address of TestContract as _target, and another random address as _foo, the total gas consumption is 2,953,792. The gas limit is set to 3,000,000. Changing the gas limit to 30,000,000 will result in a gas consumption of about 29 million gas, so it looks like it´s using up the entire gas limit.

Why does staticcall have this behaviour?

1
  • 2
    When an opcode that modifies storage is executed within a staticcall it will generate an error and consume all available gas inside the staticcall. It was the standard behavior until revert opcode was introduced.
    – Ismael
    Commented Apr 1, 2021 at 20:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.