ERC-7511 PUSH0 minimal proxy contract (Minimal Proxy Contract with PUSH0)
Draft · Other · created 2023-09-04
A standard that uses the PUSH0 opcode added in the Shanghai upgrade to optimize the existing minimal proxy contract (ERC-1167). It keeps the same functionality while saving 200 gas on deployment and 5 gas on execution.
Why it is needed
When creating multiple contracts with the same logic, instead of copying and deploying the entire code, one approach is to use a minimal proxy (ERC-1167), a very small delegate contract that forwards calls directly to the original (implementation) contract. However, the existing minimal proxy code was designed before the PUSH0 opcode introduced by the Shanghai upgrade existed. As a result, it uses workaround instructions like DUP (3 gas each), which duplicates a value to push 0 onto the stack, or SWAP, which swaps positions, making the code 1 byte longer and costing more execution gas. This standard redesigns the proxy code from scratch using PUSH0 (2 gas each), reducing code length and gas while also making it easier to read.
What to implement
function clone0(address impl) public payable returns (address addr): Takes an implementation contract address, deploys a PUSH0 minimal proxy (clone0), and returns its address. If deployment fails, it reverts with a FailedCreateClone error.
In depth
Runtime code specification
The standard runtime code is 365f5f375f5f365f73bebe...be5af43d5f5f3e5f3d91602a57fd5bf3, exactly 44 bytes long. Within it, the 20-byte slot from index 9 to 28 (inclusive) holds the master implementation contract's address. 0xbebe... is a placeholder marking that slot. This is 1 byte shorter than the existing ERC-1167 runtime code, and the readability of the code has also been improved by replacing the spot where RETURNDATASIZE and DUP were used to produce 0 with PUSH0.
Minimal creation code
The minimal creation code used for deployment is 602c8060095f395ff3 followed by the 44-byte runtime code, for a total of 53 bytes. The first 9 bytes, 602c8060095f395ff3, are the initcode, which copies the following runtime code into memory and returns it as the contract code. The remaining 44 bytes become the actual proxy runtime code.
Four stages of operation
The proxy's core operation consists of four stages. Stage 1 copies the call data with CALLDATACOPY, building the arguments [0, 0, cds] (cds being the call data size) from two PUSH0s and CALLDATASIZE. Stage 2 is DELEGATECALL, which stacks the arguments [gas, address, 0, cds, 0, 0] and executes it, receiving the success flag suc. Stage 3 copies the return data with RETURNDATACOPY. Stage 4 branches on suc; since opcodes have no if/else, JUMPI and JUMPDEST are used. The JUMPI target is 0x2a; on success it passes through JUMPDEST to RETURN, and on failure it executes REVERT. The arguments for REVERT and RETURN, [0, rds], are prepared only once before the JUMPI to avoid duplication.
Why one SWAP2 remains
In stage 4, one SWAP2 operation cannot be eliminated. The return data size rds can only be obtained after DELEGATECALL finishes, so the order of the earlier-stacked suc and the later-stacked [0, rds] must be rearranged into [suc, 0, rds] via SWAP2 for the JUMPI and RETURN/REVERT arguments to line up. However, an unnecessary SWAP present in the existing code was removed, shortening the code by 1 byte.
Savings and Solidity deployment
By replacing two DUPs (3 gas each) with two PUSH0s (2 gas each) and shortening the code by 1 byte, 200 gas is saved on deployment and 5 gas on execution. The original text presents an example Clone0Factory contract that deploys using the create instruction. The clone0 function assembles the full creation code by inserting the implementation address between the first 18 bytes and the last 15 bytes of the creation code, then deploys it with create, and raises a FailedCreateClone error if the resulting address is 0. Because PUSH0 is required, Solidity version above 0.8.20 and an EVM version at or after Shanghai are needed.
Backward compatibility
Because this proxy uses the PUSH0 opcode (EIP-3855), it can only be deployed after the Shanghai upgrade. Once deployed, its behavior is completely identical to the existing minimal proxy (ERC-1167).
Proposal status
This document is still in Draft status. It should be kept in mind that this is a proposal whose contents may change in the future.
Things to watch out for
- Cannot be upgraded: A minimal proxy delegates logic to an implementation contract, and this link is fixed at deployment time. After creation, which implementation it points to cannot be changed.
- Caution with initialization: Because the proxy contract has no constructor, an initialization function must be called after deployment. Skipping this step can leave the contract in an unsafe state.
- Safety of the logic contract: If the logic contract has a vulnerability, every proxy contract pointing to it is affected as well.
- Transparency issues: Because the structure is complex, users may mistake the proxy for an empty contract, and it can be hard to trace the actual logic contract.