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.

Background

ERC-3448 defines a minimal proxy that stores immutable metadata at the end of its bytecode. This article explains how the proxy shares one implementation while passing its own configuration to every delegated call.

This article explains the proposal linked below.

I also cover other ERCs in the proposal index.

For background on the EVM opcodes used here, see the following resource.

Overview

ERC-3448 specifies the bytecode of a minimal proxy. A fixed layout lets users and tools such as Etherscan identify the target implementation and the metadata attached to a clone.

The proxy forwards calls to that implementation using DELEGATECALL. It appends the metadata and its length to the caller's original calldata. If the delegated call succeeds, the proxy returns its result. If it reverts, the proxy passes the revert data back to the caller.

With ERC-20 or ERC-721 clones, for example, developers and users can recognize how calls are forwarded and where the per-clone configuration resides. The proxy layout identifies the implementation but does not guarantee its correctness.

For background on ERC-20, see the following article.

https://chaldene.net/erc20

For background on ERC-721, see the following article.

https://chaldene.net/erc721

Motivation

Cloning is useful when many contracts need the same logic with different settings. Think of making several cakes from one recipe and varying the flavor each time. In the same way, a developer can deploy several ERC-20 or ERC-721 contracts that share an implementation while each clone supplies its own configuration.

Specification

A MetaProxy runs a fixed sequence of bytecode. The destination of DELEGATECALL is embedded into that sequence, so each proxy can use the target contract's code in the proxy's own execution context.

text
20 bytes target contract address
----------------------------------------
363d3d373d3d3d3d60368038038091363936013d7300000000000000000000000000000000000000005af43d3d93803e603457fd5bf3

Replace the 20 zero bytes immediately after the PUSH20 opcode with the target address. In the bytecode shown above, they occupy zero-based offsets 21 through 40. The Japanese article describes 21 through 41 as inclusive, but that would cover 21 bytes.

The proxy can also contain developer-defined metadata. Its final word records the length so the implementation can identify where that data starts.

text
<54 bytes metaproxy> <arbitrary data> <length in bytes of arbitrary data (uint256)>

The last 32 bytes encode the length of the metadata as a uint256. The full layout is the 54-byte MetaProxy runtime followed by the metadata and its length word.

Rationale

The format has three cost and usability goals.

Low-cost metadata storage

Writing configuration into each clone's storage slots can be costly. Putting immutable values in the bytecode gives each clone a cheaper way to keep deployment-specific metadata.

Low-cost clone deployment

A small proxy costs less to deploy than another full copy of the implementation. Many contracts can therefore share the same base logic.

Propagating revert data

The proxy passes revert data back to the original caller. This lets developers see the failure reported by the implementation.

These goals apply to ERC-20 and ERC-721 contracts as well as other kinds of contracts that can reuse the same implementation.

Storage slots

Storage slots are where a smart contract keeps persistent state. One way to picture them is as drawers in a cabinet. A contract reads a value from a slot when it needs it and writes the slot when that value changes.

Storage is expensive because blockchain nodes replicate the state. Developers can reduce costs by using storage slots sparingly and choosing other representations for data that never changes.

Compatibility

The proposal identifies no compatibility issues.

Tests

Proxy tests check that the deployed bytecode forwards inputs and preserves the implementation's result.

Calls without arguments

Call a function that takes no arguments and check that it runs correctly without input values.

Calls with arguments

Pass input values and check that the implementation receives and uses them as expected.

Calls that return values

Check that the proxy passes the implementation's return value back unchanged.

Reverting calls

Check that the proxy forwards the revert data when a delegated call fails.

The EIP's assets directory contains the Solidity contracts for these tests. These cases help establish that proxying does not change the expected behavior of the underlying functions.

Implementation

The reference factory is available here.

https://eips.ethereum.org/assets/eip-3448/MetaProxyFactory.sol

Deployment bytecode

text
// PUSH1 11;
// CODESIZE;
// SUB;
// DUP1;
// PUSH1 11;
// RETURNDATASIZE;
// CODECOPY;
// RETURNDATASIZE;
// RETURN;

The deployment bytecode runs during contract creation. It copies the runtime code and the metadata out of the creation code and returns them as the deployed bytecode.

  1. PUSH1 11; pushes the length of the deployment prefix.
  2. CODESIZE; pushes the full length of the creation code.
  3. SUB; subtracts the prefix length from the total.
  4. DUP1; duplicates the remaining length. One copy is used for copying and the other for returning the result.
  5. PUSH1 11; pushes the source offset after the deployment prefix.
  6. RETURNDATASIZE; pushes zero to use as the destination offset in memory.
  7. CODECOPY; copies the specified number of bytes from the creation code into memory.
  8. RETURNDATASIZE; pushes zero again to use as the starting offset for the return.
  9. RETURN; returns the copied bytes. Those bytes become the deployed code.

MetaProxy runtime

text
// copy args
// CALLDATASIZE;   calldatasize
// RETURNDATASIZE; 0, calldatasize
// RETURNDATASIZE; 0, 0, calldatasize
// CALLDATACOPY;

// RETURNDATASIZE; 0
// RETURNDATASIZE; 0, 0
// RETURNDATASIZE; 0, 0, 0
// RETURNDATASIZE; 0, 0, 0, 0

// PUSH1 54;       54, 0, 0, 0, 0
// DUP1;           54, 54, 0, 0, 0, 0
// CODESIZE;       codesize, 54, 54, 0, 0, 0, 0
// SUB;            codesize-54, 54, 0, 0, 0, 0
// DUP1;           codesize-54, codesize-54, 54, 0, 0, 0, 0
// SWAP2;          54, codesize-54, codesize-54, 0, 0, 0, 0
// CALLDATASIZE;   calldatasize, 54, codesize-54, codesize-54, 0, 0, 0, 0
// CODECOPY;       codesize-54, 0, 0, 0, 0

// CALLDATASIZE;   calldatasize, codesize-54, 0, 0, 0, 0
// ADD;            calldatasize+codesize-54, 0, 0, 0, 0
// RETURNDATASIZE; 0, calldatasize+codesize-54, 0, 0, 0, 0
// PUSH20 0;       addr, 0, calldatasize+codesize-54, 0, 0, 0, 0 - zero is replaced with shl(96, address())
// GAS;            gas, addr, 0, calldatasize+codesize-54, 0, 0, 0, 0
// DELEGATECALL;   (gas, addr, 0, calldatasize() + metadata, 0, 0) delegatecall to the target contract;
//
// RETURNDATASIZE; returndatasize, retcode, 0, 0
// RETURNDATASIZE; returndatasize, returndatasize, retcode, 0, 0
// SWAP4;          0, returndatasize, retcode, 0, returndatasize
// DUP1;           0, 0, returndatasize, retcode, 0, returndatasize
// RETURNDATACOPY; (0, 0, returndatasize) - Copy everything into memory that the call returned

// stack = retcode, 0, returndatasize # this is for either revert(0, returndatasize()) or return (0, returndatasize())

// PUSH1 _SUCCESS_; push jumpdest of _SUCCESS_
// JUMPI;          jump if delegatecall returned `1`
// REVERT;         (0, returndatasize()) if delegatecall returned `0`
// JUMPDEST _SUCCESS_;
// RETURN;         (0, returndatasize()) if delegatecall returned non-zero (1)

The runtime first copies the caller's inputs, appends the metadata and length read from its own code, and makes a delegated call. It then copies the delegated result and decides whether to return it or revert.

  1. CALLDATASIZE pushes the length of the caller's input.
  2. RETURNDATASIZE pushes the current return data length, which is zero before any call.
  3. CALLDATACOPY copies the input bytes into memory before forwarding them.
  4. PUSH1 54 pushes the length of the fixed runtime instructions.
  5. DUP1 duplicates the item at the top of the stack.
  6. CODESIZE pushes the full size of the deployed code.
  7. SUB subtracts the 54-byte instructions from that size.
  8. SWAP2 exchanges the top item with the third item on the stack.
  9. CODECOPY copies the metadata and its length word after the caller's input in memory.
  10. ADD adds the copied suffix length to the original input length.
  11. PUSH20 0 marks the 20-byte immediate value that the factory replaces with the delegated call's target address.
  12. GAS pushes the available gas.
  13. DELEGATECALL invokes the target's code in the MetaProxy's execution context.
  14. RETURNDATACOPY copies the returned bytes into memory.
  15. PUSH1 SUCCESS pushes the destination of the success branch.
  16. JUMPI jumps there when the delegated call reports success.
  17. REVERT forwards the failure data when the delegated call fails.
  18. JUMPDEST SUCCESS marks the entry point for the success branch.
  19. RETURN returns the copied data to the caller.

Examples

Constructing a proxy with abi.encode

javascript
/// @notice MetaProxy construction via abi encoded bytes.
function createFromBytes (
  address a,
  uint256 b,
  uint256[] calldata c
) external payable returns (address proxy) {
  // creates a new proxy where the metadata is the result of abi.encode()
  proxy = MetaProxyFactory._metaProxyFromBytes(address(this), abi.encode(a, b, c));
  require(proxy != address(0));
  // optional one-time setup, a constructor() substitute
  MyContract(proxy).init{ value: msg.value }();
}

createFromBytes accepts an address a, a number b, and an array c. It stores their ABI encoding in the proxy's metadata, optionally calls init as a one-time setup step in place of a constructor, and returns the deployed proxy address.

Constructing a proxy from calldata

javascript
/// @notice MetaProxy construction via calldata.
function createFromCalldata (
  address a,
  uint256 b,
  uint256[] calldata c
) external payable returns (address proxy) {
  // creates a new proxy where the metadata is everything after the 4th byte from calldata.
  proxy = MetaProxyFactory._metaProxyFromCalldata(address(this));
  require(proxy != address(0));
  // optional one-time setup, a constructor() substitute
  MyContract(proxy).init{ value: msg.value }();
}

createFromCalldata builds a proxy whose metadata comes directly from the caller's calldata after the four-byte selector. It can also run the optional initialization and returns the proxy address.

Reading metadata with calldata and abi.decode

javascript
/// @notice Returns the metadata of this (MetaProxy) contract.
/// Only relevant with contracts created via the MetaProxy standard.
/// @dev This function is aimed to be invoked with- & without a call.
function getMetadataWithoutCall () public pure returns (
  address a,
  uint256 b,
  uint256[] memory c
) {
  bytes memory data;
  assembly {
    let posOfMetadataSize := sub(calldatasize(), 32)
    let size := calldataload(posOfMetadataSize)
    let dataPtr := sub(posOfMetadataSize, size)
    data := mload(64)
    // increment free memory pointer by metadata size + 32 bytes (length)
    mstore(64, add(data, add(size, 32)))
    mstore(data, size)
    let memPtr := add(data, 32)
    calldatacopy(memPtr, dataPtr, size)
  }
  return abi.decode(data, (address, uint256, uint256[]));
}

getMetadataWithoutCall reads the metadata length from the final word of calldata, derives the start of the metadata, and decodes it as address a, number b, and array c.

Reading metadata through a call to the proxy

javascript
/// @notice Returns the metadata of this (MetaProxy) contract.
/// Only relevant with contracts created via the MetaProxy standard.
/// @dev This function is aimed to to be invoked via a call.
function getMetadataViaCall () public pure returns (
  address a,
  uint256 b,
  uint256[] memory c
) {
  assembly {
    let posOfMetadataSize := sub(calldatasize(), 32)
    let size := calldataload(posOfMetadataSize)
    let dataPtr := sub(posOfMetadataSize, size)
    calldatacopy(0, dataPtr, size)
    return(0, size)
  }
}

getMetadataViaCall also reads the appended metadata. It copies the bytes directly to memory and returns them in a form the caller can decode. This pattern is intended to be invoked via a call, such as a call from the proxy to itself through its forwarding path.

Solidity structs and custom encodings can also represent metadata. The choice depends on the configuration each clone needs, whether it implements ERC-20, ERC-721, or another interface.

Security considerations

The standard specifies the proxy bytecode. It leaves the behavior of the target implementation and its use of metadata to the application. Review the chosen implementation and the assumptions it makes about delegated calls and appended metadata before deploying clones. Matching the MetaProxy format alone is not a substitute for that review.

Citation

pinkiebell (@pinkiebell), “ERC-3448: MetaProxy Standard,” Ethereum Improvement Proposals, no. 3448, March 2021. Available at https://eips.ethereum.org/EIPS/eip-3448.

Conclusion

ERC-3448 defines a minimal proxy that clones existing contract logic and appends immutable metadata to its bytecode. Several contracts can share the same implementation and still pass their own settings to it.

If you have questions, feel free to reach out by direct message.

Twitter @cardene777

I also publish elsewhere.

https://chaldene.net/