Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
This article covers ERC1167, a standard that proposes a straightforward, gas-efficient way to clone contracts.
This article translates, summarizes, and adds explanations to the specification linked below.
I also cover a range of other ERCs:
This standard includes descriptions of EVM opcodes. If you are unfamiliar with opcodes, see the following resource.
Overview
This standard proposes an efficient, cost-effective way to clone contract functionality. Its aim is to standardize how one contract can use the functionality of another designated contract.
Specifically, a contract can borrow functionality located at another particular address (contract). This form of redirection or delegation makes it easy to identify the location of the external implementation a contract uses. With a standardized approach, tools such as Etherscan can more readily determine how a contract works.
The implementation is highly efficient: when the contract is called, it delegates the call and available gas, subject to EVM gas forwarding rules, to the contract at the specified address.
It then passes the result or return data back to the caller.
If the delegated functionality fails for any reason (revert), the failure data is also passed back accurately.
This approach simplifies the use of functionality from another contract while maintaining consistency and predictability. Even complex systems and applications can combine different pieces of functionality smoothly.
Motivation
This standard makes it possible to reproduce all the functionality of a given contract. It focuses on avoiding unintended modification of other data (storage slot collisions) and minimizing the gas cost of creating new copies.
Storage slot collisions
A storage slot collision occurs when different pieces of data are assigned the same storage location (slot). In smart contracts and other programming contexts, shared address spaces can introduce bugs and security risks if layouts conflict.
For example, an Ethereum smart contract stores its state (persistent contract data) in storage slots, distinct from transient EVM memory. A contract's storage layout determines which slots hold its state.
Example
Suppose you are developing a smart contract for an online banking system. It contains a variable for tracking user balances and another for storing an important system setting.
contract Bank {
mapping(address => uint256) public balances;
bool public systemMaintenance = false;
// Other variables and functions...
}Here, balances tracks the balance of each user, while systemMaintenance represents the maintenance status of the system.
Normally, these variables occupy distinct storage locations.
However, if a conflicting layout assigns them the same slot, code updating balances could unintentionally modify systemMaintenance as well.
Such a situation could lead to security issues.
For example, maintenance mode might be enabled unintentionally, halting all transactions.
To avoid such problems, developers need to pay attention to storage locations when designing contracts and test them thoroughly. When cloning an existing contract, it is important to understand its storage layout accurately and avoid collisions.
This standard lets developers create new contracts that use the same functionality as an existing contract. Because this can be done efficiently and without unnecessary cost or effort, it helps developers build blockchain applications and services more easily and economically.
Specification
This standard defines a procedure for replicating contract functionality in a new contract.
Specifically, it uses the runtime bytecode 363d3d373d3d3d363d73bebebebebebebebebebebebebebebebebebebebe5af43d82803e903d91602b57fd5bf3.
A new clone can be created by replacing bytes at offsets 10 through 29 with the address of the original implementation contract.
The resulting contract uses the same implementation functionality as the original.
This method accurately reproduces contract functionality while keeping deployment gas costs low. Detailed instructions and implementation examples are available in the optionality/clone-factory repository.
Think of the standard as a recipe for efficiently and accurately cloning contracts on a blockchain.
A clone contract creates a new contract that uses the functionality of an existing one. The following sections describe its bytecode and how a particular address is substituted into it.
Bytecode
363d3d373d3d3d363d73bebebebebebebebebebebebebebebebebebebebe5af43d82803e903d91602b57fd5bf3 is the runtime bytecode for a clone contract.
Bytes at offsets 10 through 29 (inclusive), represented by the bebebebebebebebebebebebebebebebebebe portion, are replaced with the address of the master implementation contract to be cloned.
This allows the clone to reproduce the functionality of a specific master contract.
Example
Suppose you want to create a new contract with the functionality of a particular token contract.
If that token contract has the address 0x1234567890123456789012345678901234567890, replace the bebebebebebebebebebebebebebebebebebe portion of the bytecode with that address.
The modified runtime bytecode is deployed using suitable creation code on the Ethereum network, producing a clone that uses the master contract's functionality.
Standard clone bytecode is therefore a useful tool for efficiently reproducing contracts with a particular set of functionality and creating new instances. It can improve development efficiency, lower costs, and expand the ways Ethereum smart contracts can be used.
Improving gas efficiency
- Code reuse
- A clone contract reuses the code of an existing contract (the master contract).
- Instead of uploading the same code to the blockchain again when creating a new contract, the clone only needs to reference the existing code. This substantially reduces deployment gas.
- Smaller bytecode
- A clone contract consists of very short bytecode containing a reference to the master contract.
- Shorter bytecode requires less data to be stored on-chain, reducing gas consumption accordingly.
- Vanity address optimization
- Using a master contract address with a particular pattern (for example, several leading zero bytes) can further shorten its address reference in the clone.
- This reduces the contract size further and lowers deployment gas costs.
- Shared functionality
- Clones reuse the master contract's functionality directly, so identical logic does not need to be deployed for every new contract.
- This reuse reduces redundant on-chain data and deployment gas consumption.
These factors make clone contracts especially gas-efficient to deploy and much more economical than deploying separate contracts with the same functionality from scratch. The optimizations also conserve blockchain resources and improve the efficiency of the network overall.
Additional considerations
The proposal aims to replicate contract functionality efficiently and safely on a blockchain. Its main goals are as follows.
Low-cost deployment
Minimize the cost of deploying new clones to a blockchain. This makes clones more accessible to a wider range of users.
Support for clone initialization
A clone can be initialized when it is created so that it is ready for use immediately. This can be achieved with a factory contract model.
Factory contract model
The factory contract model uses a contract called a factory to create and manage other contracts. This pattern is widely used on blockchains such as Ethereum.
Example
Suppose you operate an online marketplace and want each seller to have their own shop. Each shop should have its own inventory, pricing, and sales records. A factory contract model can help.
- Create a factory contract
- Start by creating a contract called the shop factory.
- It provides functionality for creating new shop contracts.
- Create a shop contract template
- Each shop is based on a shop contract with functionality for managing inventory, prices, and sales records.
- This shop contract acts as the template cloned by the factory contract.
- Create a new shop
- When sellers want to join the marketplace, they instruct the factory to create their own shop contract.
- The factory deploys the new shop contract on-chain and assigns control to the seller.
- Operate and manage the shop
- Sellers can add products, set prices, and manage sales through their shop contracts.
- The factory can also track all shop contracts and provide additional functionality or updates where the application design permits them.
The factory contract model makes it easy to create and manage many independent contracts and operate large applications or services efficiently.
Simple clone bytecode
The clone bytecode (the basic set of instructions for the contract) is kept simple so that it is easier to inspect and understand.
Fixed functionality and reliability
The minimal proxy fixes the implementation address at creation and has no built-in mechanism for changing it. This supports predictable, reliable behavior.
Low execution overhead
The additional cost of using a clone is kept to a minimum. Each operation incurs only a small additional overhead.
Handling errors and revert messages
When something goes wrong, the revert data can be forwarded to the caller appropriately.
The proposal aims to lower the barriers to cloning contracts on a blockchain and make the technique accessible to more people, while keeping operation simple, predictable, and safe.
Compatibility
This standard can be introduced without breaking older systems. However, systems that use earlier proxy bytecode variants do not automatically conform to the new standard.
Tests
Test cases verify that a contract behaves correctly under a variety of conditions. The following situations should be tested.
- Calls without arguments
- Call a function without passing any arguments and verify that it works correctly.
- This is the most basic test.
- Calls with arguments
- Pass particular arguments to a function and verify that it behaves as expected.
- This checks that the function handles valid inputs correctly.
- Calls with fixed-length return data
- Test a function that always returns data of the same length.
- This helps verify that the returned data has the expected size.
- Calls with variable-length return data
- Test a function that returns data of a potentially varying length.
- This is important for confirming that data of different lengths is handled correctly.
- Calls that revert
- Deliberately invoke a failing function and check that the correct payload, such as an error message, is returned.
- This verifies that the contract behaves appropriately when an error occurs.
These tests help verify that contracts behave correctly even in unexpected situations, improving reliability and safety. The implementation includes these test cases, which developers can use to help ensure contract quality.
Implementation
Standard Proxy
| 0x00000000 36 calldatasize cds
| 0x00000001 3d returndatasize 0 cds
| 0x00000002 3d returndatasize 0 0 cds
| 0x00000003 37 calldatacopy
| 0x00000004 3d returndatasize 0
| 0x00000005 3d returndatasize 0 0
| 0x00000006 3d returndatasize 0 0 0
| 0x00000007 36 calldatasize cds 0 0 0
| 0x00000008 3d returndatasize 0 cds 0 0 0
| 0x00000009 73bebebebebe. push20 0xbebebebe 0xbebe 0 cds 0 0 0
| 0x0000001e 5a gas gas 0xbebe 0 cds 0 0 0
| 0x0000001f f4 delegatecall suc 0
| 0x00000020 3d returndatasize rds suc 0
| 0x00000021 82 dup3 0 rds suc 0
| 0x00000022 80 dup1 0 0 rds suc 0
| 0x00000023 3e returndatacopy suc 0
| 0x00000024 90 swap1 0 suc
| 0x00000025 3d returndatasize rds 0 suc
| 0x00000026 91 swap2 suc 0 rds
| 0x00000027 602b push1 0x2b 0x2b suc 0 rds
| ,=< 0x00000029 57 jumpi 0 rds
| | 0x0000002a fd revert
| `-> 0x0000002b 5b jumpdest 0 rds
\ 0x0000002c f3 returnA proxy is an intermediary contract that uses the functionality of another contract.
How the Standard Proxy works
The standard shows what the proxy contract bytecode looks like. The code receives external input and forwards the call to a contract at a specified address. It does this using a delegate call, executing the other contract's code in the proxy's execution context.
If execution succeeds, the result is returned to the caller. If it fails, the error is propagated to the caller and execution reverts.
Reducing gas costs
This bytecode uses a particular technique to reduce gas costs. Building on EIP211, it reduces unnecessary operations and saves gas.
See the following article for more on EIP211.
[EIP211] Managing Variable-Length Return Data with RETURNDATASIZE and RETURNDATACOPY
Execution flow
The code above is part of the bytecode used by Ethereum smart contracts. It shows how a proxy delegates operations to another contract. Each line represents an EVM (Ethereum Virtual Machine) instruction and its operation. The individual steps are described below.
0x00000000 36 calldatasize cds- Obtain the size of the incoming data (
calldata).
- Obtain the size of the incoming data (
0x00000001 3d returndatasize 0 cds- Push the size of the return data available in the current call frame (
0at this point) onto the stack.
- Push the size of the return data available in the current call frame (
0x00000002 3d returndatasize 0 0 cds- Push the return data size (
0here) onto the stack again.
- Push the return data size (
0x00000003 37 calldatacopy- Copy the external input data into EVM memory.
0x00000004 3d returndatasize 0- Push the return data size (
0) onto the stack.
- Push the return data size (
0x00000005 3d returndatasize 0 0- Push the return data size (
0) onto the stack again.
- Push the return data size (
0x00000006 3d returndatasize 0 0 0- Push another copy of the return data size (
0) onto the stack.
- Push another copy of the return data size (
0x00000007 36 calldatasize cds 0 0 0- Push the size of
calldataonto the stack again.
- Push the size of
0x00000008 3d returndatasize 0 cds 0 0 0- Push the return data size (
0) onto the stack.
- Push the return data size (
0x00000009 73bebebebebe push20 0xbebebebe 0xbebe 0 cds 0 0 0- Push the destination address for
delegatecall(0xbebebebebebe...in this example) onto the stack.
- Push the destination address for
0x0000001e 5a gas gas 0xbebe 0 cds 0 0 0- Push the amount of available gas onto the stack.
0x0000001f f4 delegatecall suc 0- Use the stack arguments to execute
delegatecall, delegating execution to another contract.
- Use the stack arguments to execute
0x00000020 3d returndatasize rds suc 0- Push the size of the return data from
delegatecallonto the stack.
- Push the size of the return data from
0x00000021 82 dup3 0 rds suc 0- Duplicate the third stack item and push it onto the top of the stack.
0x00000022 80 dup1 0 0 rds suc 0- Duplicate the top stack item and push it onto the top of the stack.
0x00000023 3e returndatacopy suc 0- Copy the return data from
delegatecallinto memory.
- Copy the return data from
0x00000024 90 swap1 0 suc- Swap the top two stack items.
0x00000025 3d returndatasize rds 0 suc- Push the return data size onto the stack.
0x00000026 91 swap2 suc 0 rds- Swap the second and third items from the top of the stack.
0x00000027 602b push1 0x2b 0x2b suc 0 rds- Push the jump destination (
0x2b) onto the stack.
- Push the jump destination (
0x00000029 57 jumpi 0 rds- Perform a conditional jump.
- Jump to
0x2bif the second stack item (the delegate call success flag) is nonzero. The Japanese source describes this condition as zero, but that is inconsistent withJUMPIand the bytecode's success branch.
0x0000002a fd revert- If the jump is not taken, revert the operation and undo its state changes.
0x0000002b 5b jumpdest 0 rds- This is the jump destination.
- Execution continues with the following instruction.
0x0000002c f3 return- Return the final result to the caller.
This instruction sequence shows how a proxy uses a delegate call to execute another contract's code and return the result to its caller.
Vanity Address Optimization
| 0x00000000 36 calldatasize cds
| 0x00000001 3d returndatasize 0 cds
| 0x00000002 3d returndatasize 0 0 cds
| 0x00000003 37 calldatacopy
| 0x00000004 3d returndatasize 0
| 0x00000005 3d returndatasize 0 0
| 0x00000006 3d returndatasize 0 0 0
| 0x00000007 36 calldatasize cds 0 0 0
| 0x00000008 3d returndatasize 0 cds 0 0 0
| 0x00000009 6fbebebebebe. push16 0xbebebebe 0xbebe 0 cds 0 0 0
| 0x0000001a 5a gas gas 0xbebe 0 cds 0 0 0
| 0x0000001b f4 delegatecall suc 0
| 0x0000001c 3d returndatasize rds suc 0
| 0x0000001d 82 dup3 0 rds suc 0
| 0x0000001e 80 dup1 0 0 rds suc 0
| 0x0000001f 3e returndatacopy suc 0
| 0x00000020 90 swap1 0 suc
| 0x00000021 3d returndatasize rds 0 suc
| 0x00000022 91 swap2 suc 0 rds
| 0x00000023 6027 push1 0x27 0x27 suc 0 rds
| ,=< 0x00000025 57 jumpi 0 rds
| | 0x00000026 fd revert
| `-> 0x00000027 5b jumpdest 0 rds
\ 0x00000028 f3 returnVanity address optimization improves the efficiency of deploying proxy contracts to a blockchain.
Here, a vanity address is an address with a particular pattern (in this case, leading 0 bytes).
This method reduces proxy contract size and deployment cost.
Specifically, the master contract (the implementation to which calls are delegated) is deployed at an address with leading zero bytes.
The portion of the proxy bytecode that references the master address can then be shortened.
For example, an address with four leading zero bytes allows the usual 20-byte address operand to be reduced to 16 bytes.
This makes the overall proxy contract smaller and reduces deployment gas costs.
The bytecode above shows how the proxy works internally when this optimization is applied. The contract receives external input, delegates execution to the master contract using the shortened address operand, and returns the result to the caller. If the operation fails, the revert data is propagated appropriately.
This optimization enables smaller, more cost-efficient proxy deployments. It is especially beneficial in terms of cost and efficiency for large applications and services that need to deploy many contracts.
Execution flow
The code above is part of Ethereum smart contract bytecode and represents EVM (Ethereum Virtual Machine) instructions. This particular example is a proxy contract with the ability to delegate calls to another contract. The individual steps are described below.
0x00000000 36 calldatasize cds- Push the size of the incoming data (
calldata) onto the stack.
- Push the size of the incoming data (
0x00000001 3d returndatasize 0 cds- Push the size of the return data available in the current context (
0at this point) onto the stack.
- Push the size of the return data available in the current context (
0x00000002 3d returndatasize 0 0 cds- Push the return data size (
0) onto the stack again.
- Push the return data size (
0x00000003 37 calldatacopy- Copy the external input data (
calldata) into EVM memory.
- Copy the external input data (
0x00000004 3d returndatasize 0- Push the return data size (
0) onto the stack.
- Push the return data size (
0x00000005 3d returndatasize 0 0- Push another copy of the return data size (
0) onto the stack.
- Push another copy of the return data size (
0x00000006 3d returndatasize 0 0 0- Push the return data size (
0) onto the stack again.
- Push the return data size (
0x00000007 36 calldatasize cds 0 0 0- Push the size of
calldataonto the stack again.
- Push the size of
0x00000008 3d returndatasize 0 cds 0 0 0- Push the return data size (
0) onto the stack.
- Push the return data size (
0x00000009 6fbebebebebe push16 0xbebebebe 0xbebe 0 cds 0 0 0:- Push the shortened address operand for the target contract (
0xbebebebe...) onto the stack.
- Push the shortened address operand for the target contract (
0x0000001a 5a gas gas 0xbebe 0 cds 0 0 0- Push the amount of available gas onto the stack.
0x0000001b f4 delegatecall suc 0- Delegate execution to the target contract and push the success flag onto the stack (
1on success,0on failure).
- Delegate execution to the target contract and push the success flag onto the stack (
0x0000001c 3d returndatasize rds suc 0- Push the size of the return data from
delegatecallonto the stack.
- Push the size of the return data from
0x0000001d 82 dup3 0 rds suc 0- Duplicate the third stack item (
0in this case) and push it onto the top of the stack.
- Duplicate the third stack item (
0x0000001e 80 dup1 0 0 rds suc 0- Duplicate the top stack item (
0in this case) and push it onto the top of the stack.
- Duplicate the top stack item (
0x0000001f 3e returndatacopy suc 0- Copy the return data from
delegatecallinto memory.
- Copy the return data from
0x00000020 90 swap1 0 suc- Swap the top two stack items.
0x00000021 3d returndatasize rds 0 suc- Push the return data size onto the stack.
0x00000022 91 swap2 suc 0 rds- Swap the second and third stack items.
0x00000023 6027 push1 0x27 0x27 suc 0 rds- Push the jump destination (
0x27) onto the stack.
- Push the jump destination (
0x00000025 57 jumpi 0 rds- Perform a conditional jump.
- If the second stack item (the success flag) is nonzero, jump to the specified destination (
0x27).
0x00000026 fd revert- If the jump is not taken (that is,
delegatecallfailed), revert the operation and undo its state changes.
- If the jump is not taken (that is,
0x00000027 5b jumpdest 0 rds- This is the jump destination.
- Execution continues with the following instruction.
0x00000028 f3 return- Return the final result to the caller.
This code implements a proxy's delegation of calls to another contract, with steps for memory operations, conditional jumps, and error handling.
Comparison with the Standard Proxy
Both code examples show Ethereum proxy contracts performing delegate calls. The basic execution flow is the same, but the following details differ.
Standard Proxy
0x00000009 73bebebebebe. push20 0xbebebebe- This instruction pushes a
20-byte address (0xbebebebebebe...) onto the stack. - This is the destination address of the delegate call.
- This instruction pushes a
0x00000027 602b push1 0x2b- This instruction pushes a one-byte value (
0x2b) onto the stack. - This is subsequently used as the destination of the conditional jump (
jumpi).
- This instruction pushes a one-byte value (
0x0000002b 5b jumpdest 0 rds- This
jumpdestis located at offset0x2b.
- This
Vanity Address Optimization
0x00000009 6fbebebebebe. push16 0xbebebebe- This instruction pushes a
16-byte address operand (0xbebebebe) onto the stack. - The operand represents the trailing
16bytes of the address, so it has a different length from the first example. - Vanity address optimization makes it possible to omit the leading zero bytes.
- This instruction pushes a
0x00000023 6027 push1 0x27- This instruction pushes a one-byte value (
0x27) onto the stack. - This value is smaller than
0x2bin the first example, reflecting the adjusted jump destination required by the shorter address operand.
- This instruction pushes a one-byte value (
0x00000027 5b jumpdest 0 rds- This
jumpdestis located at offset0x27.
- This
Differences
- Address operand length
- The first example uses a
20-byte address operand, while the second uses a16-byte operand as a result of vanity address optimization.
- The first example uses a
- jumpdest
- Vanity address optimization places the jump destination at a smaller offset in the second example.
These differences reflect an optimization that reduces proxy contract size and saves gas during deployment. Vanity address optimization is achieved by shortening the address operand and adjusting the bytecode accordingly.
Citation
Peter Murray (@yarrumretep), Nate Welch (@flygoing), Joe Messerman (@JAMesserman), "ERC-1167: Minimal Proxy Contract," Ethereum Improvement Proposals, no. 1167, June 2018. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-1167.
Conclusion
This article covered ERC1167, a standard that proposes a straightforward, gas-efficient way to clone contracts. I hope this explanation was helpful.
If you have questions, feel free to contact me via Twitter DM:
You can also find my other articles on the following platforms:



