EIP-214 · Final · Core · 2017-02-13
EIP-214: New opcode STATICCALL
Adds the STATICCALL opcode (0xfa), which calls another contract while forbidding any state change in that call and its subcalls, giving safe read-only calls; Solidity uses it for view functions.
What problem does it solve?
Before 2017, every call to another contract was a full CALL. The callee could write storage, emit logs, create contracts, send ether or call back into you. Even when you only wanted to read a price or a balance, you had to trust the target not to do anything else, or audit it yourself. This uncertainty is how re-entrancy bugs happen.
EIP-214 gives the EVM a call that guarantees nothing changes. All account state is the same after a static call as it was before.
How it works
- A new opcode,
STATICCALL(0xfa), takes the same arguments asCALLminusvalue:gas, addr, argsOffset, argsSize, retOffset, retSize. The value is always zero. - The EVM gains a
STATICflag.STATICCALLsets it totruefor the child call, and every nested call inherits it. When the static call returns, the flag goes back to what it was. - While
STATICis set, these operations throw an exception instead of running:SSTORELOG0toLOG4CREATE,CREATE2SELFDESTRUCTCALLwith non-zero value
CALLCODEis not counted as state-changing, even with value. OrdinaryCALLwith zero value,DELEGATECALLand all reads are allowed.
Existing opcodes behave exactly as before unless they run inside a static context, so old contracts are unaffected. STATICCALL shipped in the Byzantium hard fork (EIP-609).
Sample code
Solidity emits STATICCALL automatically when you call an external view or pure function:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IOracle {
function price() external view returns (uint256);
}
contract Reader {
function read(IOracle o) external view returns (uint256) {
return o.price(); // compiled as STATICCALL
}
}
The same call in assembly, with the success flag checked:
function rawRead(address target, bytes memory data) internal view returns (bytes memory out) {
bool ok;
assembly {
ok := staticcall(gas(), target, add(data, 0x20), mload(data), 0, 0)
}
require(ok, "static call failed (or tried to modify state)");
out = new bytes(returndatasize());
assembly { returndatacopy(add(out, 0x20), 0, returndatasize()) }
}
Off-chain, eth_call runs code without saving any changes, but it does not set the STATIC flag. A view function is enforced on-chain only when it is called through STATICCALL.
Things to know
- A state-changing operation inside a static context is an exceptional halt. The static call returns failure and consumes the gas given to it.
- Because the flag is inherited, a target cannot escape the restriction by calling a third contract.
STATICCALLprevents state changes. It does not make results trustworthy: a read-only oracle can still return manipulated values.- Later opcodes respect the flag too. For example,
TSTOREfrom EIP-1153 is forbidden in a static context. - Reading the full return data is done with
RETURNDATASIZEandRETURNDATACOPYfrom EIP-211, which also shipped in Byzantium.