EIPs Explained

EIP-7 · Final · Core · 2015-11-15

EIP-7: DELEGATECALL

Added the DELEGATECALL opcode, which runs another contract's code inside the caller's own storage and context, making proxies, upgradeable contracts and shared libraries possible.

What problem does it solve?

Early Ethereum had CALLCODE, which let a contract borrow another contract's code while keeping its own storage. But inside the borrowed code, msg.sender became the calling contract and msg.value was reset. Code that checked "who called me?" or "how much ETH was sent?" broke as soon as it ran through a forwarding contract.

EIP-7 adds DELEGATECALL, which behaves like CALLCODE but passes the original sender and value through. The borrowed code runs as if it were part of the calling contract. This shipped in the Homestead hard fork (EIP-606) at block 1,150,000.

How it works

DELEGATECALL is opcode 0xf4. It takes six stack arguments: gas, to, in_offset, in_size, out_offset, out_size. It has no value argument, because the value of the current call is passed through unchanged.

When contract A delegate-calls contract B:

Inside B's code Refers to
address(this) A
storage reads / writes A's storage
msg.sender (CALLER) whoever called A
msg.value (CALLVALUE) the value sent to A
the code being executed B's

Other details from the spec:

  • No gas stipend is added. The gas argument is all the callee gets, and unused gas is refunded as normal.
  • No account is ever created, so the upfront cost is just the base call cost plus gas.
  • The 1024 call-depth limit still applies.

The spec's own use cases are splitting large code across several contracts and keeping a mutable "code address" so a contract's logic can be swapped later. That second idea is the basis of every upgradeable proxy in use today.

Sample code

A minimal forwarding proxy. All calls run the implementation's code against the proxy's storage:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract MiniProxy {
    address public implementation; // slot 0 - see warning below

    constructor(address impl) {
        implementation = impl;
    }

    fallback() external payable {
        address impl = implementation;
        assembly {
            calldatacopy(0, 0, calldatasize())
            let ok := delegatecall(gas(), impl, 0, calldatasize(), 0, 0)
            returndatacopy(0, 0, returndatasize())
            switch ok
            case 0 { revert(0, returndatasize()) }
            default { return(0, returndatasize()) }
        }
    }
}

In high-level Solidity, address.delegatecall(data) returns (bool success, bytes memory result).

Things to know

  • Storage layout must match. The callee's code writes to the caller's storage slots by position. If the two contracts disagree about what lives in slot 0, data is silently corrupted. EIP-1967 defines fixed, out-of-the-way slots for proxy bookkeeping to avoid exactly this.
  • Never delegate-call untrusted code. The callee has full control of your storage and balance, including the ability to selfdestruct in older EVM versions. The 2017 Parity multisig incidents involved wallets that delegated to a shared library contract.
  • DELEGATECALL is what EIP-1167 minimal proxies, UUPS and transparent proxies, and EIP-2535 diamonds are built on.
  • CALLCODE still exists but is effectively obsolete. Use DELEGATECALL instead.
  • This EIP is Final and has been active on mainnet since 2016.

#evm #opcode #proxy #core #homestead

Details

Authors
Vitalik Buterin (@vbuterin)
Status
Final
Type
Standards Track / Core
Created
2015-11-15
Required by

Machine-readable: Markdown · JSON