EIP-6913 SETCODE opcode (immediate code replacement) (SETCODE instruction)
No call decision · EVM & execution · EIP status Withdrawn · created 2023-04-20
Introduces the SETCODE (0xfc) opcode, which immediately replaces the code of the executing account with the contents of memory. It is a new code-replacement method in response to the deprecation of SELFDESTRUCT.
Why it is needed
Contracts are typically upgraded by calling an external implementation via CALL, going through a proxy via DELEGATECALL, or re-planting code via SELFDESTRUCT followed by CREATE2. The CALL approach requires designing internal state so that every future implementation can read it, and DELEGATECALL proxies tend to grow structurally complex. The SELFDESTRUCT-plus-CREATE2 approach can replace code in place without external calls, but the moment SELFDESTRUCT executes, all internal storage is wiped, and the code deletion itself is deferred until the end of the transaction, reducing availability in the interim. Moreover, since SELFDESTRUCT itself is slated for deprecation, this method will no longer be usable at all. SETCODE provides a way to replace only the code in place, without erasing state, in this situation.
What changes
Today
- Handling upgrades via a DELEGATECALL proxy or CALL makes the structure complex or forces internal state to be shared.
- Replacing code via SELFDESTRUCT followed by CREATE2 wipes all storage.
- Code deletion is deferred until the end of the transaction, reducing availability in the interim.
- Because SELFDESTRUCT is slated for deprecation, this method itself becomes unusable.
After EIP-6913
- SETCODE immediately replaces the executing account's own code with the contents of memory.
- Balance, nonce, and storage remain unchanged.
- The code replacement completes within the same transaction, without any external contract call.
- SETCODE is prohibited inside DELEGATECALL, allowing code immutability to be statically verified.
Who is affected
- Contract and upgrade-pattern developers: They can use a new upgrade method that does not erase state, instead of SELFDESTRUCT plus CREATE2.
- Wallets and block explorers: Upgradeable code should be flagged, and a warning should be shown when approving token approve calls.
- Static analysis tools: Because SETCODE is prohibited inside DELEGATECALL, code immutability can be verified on-chain.
In depth
Opcode behavior
SETCODE (0xfc) pops two words, offset and length, from the stack and replaces the executing account's code with that range of memory as the new code. In a read-only execution context such as STATICCALL, it reverts with an exception. It also reverts with an exception when the executing code differs from the account's code, as happens inside DELEGATECALL or CREATE. The same validation applied to CREATE and CREATE2 is performed immediately, and execution reverts on failure.
Timing and scope of replacement
EXTCODESIZE, EXTCODECOPY, and message calls such as DELEGATECALL, CALLCODE, CALL, and STATICCALL immediately look up and execute the replaced new code. However, the frame that is already executing, including the frame that invoked SETCODE itself, continues running the previous code until it finishes, and CODESIZE/CODECOPY within it also continue to reference the previous code. If the current frame or a parent frame reverts, as with SSTORE, this code replacement is rolled back along with it.
Difference from SELFDESTRUCT
SELFDESTRUCT wipes balance, nonce, and storage entirely, and the code deletion itself is also deferred until the end of the transaction. SETCODE changes only the code, leaving balance, nonce, and storage untouched. After SELFDESTRUCT, execution ends, but after SETCODE, execution continues normally, so it can produce a return value or run post-replacement validation, and if that validation fails, it can be rolled back with REVERT or a recursive SETCODE. REVERT uses less gas.
Gas cost
The gas cost of SETCODE is Gselfdestruct plus (Gcodedeposit multiplied by the number of bytes of new code). This is similar to CREATE, but since it does not create a new execution context or account, it does not include Gcreate.
Static analysis and immutability flags
Because SETCODE is prohibited inside DELEGATECALL in almost all cases, if code executed via DELEGATECALL does not contain SETCODE, that code can be safely treated as immutable. Once code has been observed as immutable in an execution that was never reverted, it is guaranteed to remain immutable thereafter, making it possible to statically analyze code immutability on-chain.
Security considerations
Because upgrade mechanisms carry the risk of permanent failure, upgrade authority should be restricted, should not be exercised when rushed or tired, should be tested under conditions as close to production as possible, and should be independently reviewed by multiple engineers. Block explorers, wallets, and similar tools should flag upgradeable code, and clients should warn against approving ERC-20 or ERC-721 approve calls for upgradeable accounts.
Withdrawn status
This EIP has status Withdrawn. The metadata states that the author withdrew it in favor of EIP-7702, which addresses the same goal of code-replacement upgrades.