Skip to content
Back to Curriculum
ADVANCED

DelegateCall & Proxies

`delegatecall` is a low-level opcode that calls another contract's function, but executes it **in the context of the caller**. This means `msg.sender`, `msg.value`, and most importantly **Storage** refer to the calling contract, not the target.

Syntax Structure

Example.sol
Loading...

Key Components

  • Boilerplate structure required by the Solc compiler.
  • Placement of state variables versus functions.

Practical Implementation

A basic Proxy pattern demonstrating storage modification.

Practical.sol
Loading...

Core Concepts

Context

Who is `this`? In normal call, `this` is the target. In delegatecall, `this` is the caller.

Storage Layout

Critical! Both contracts must have identical variable definitions in the same order.

Proxy

A shell contract that holds state but asks a Logic contract to manipulate it.

Mental Model

The Brain Transplant

Imagine code is a brain and storage is a body. A normal `call` is asking your friend to do math; they use their brain and their hands. `delegatecall` is borrowing your friend's brain to move *your* hands. The logic comes from them, but the effect happens to you.

Under the Hood

`DELEGATECALL` takes 6 arguments in assembly. It preserves `msg.sender` (unlike a normal call where `msg.sender` becomes the caller).

DELEGATECALLSLOADSSTORE

Gas Analysis

Slightly more expensive than call.

  • • Used heavily in proxies. The gas overhead of the proxy forwarding is ~1-2k gas.

Security Risks

The most dangerous opcode.

  • ! Storage Collisions: If Proxy has `address impl` at slot 0, and Logic has `uint count` at slot 0, updating count overwrites the implementation address -> bricked contract.
  • ! Self Destruct: If Logic has `selfdestruct`, anyone can call it via Proxy and destroy the Proxy.

Common Use Cases

Upgradeable Contracts (Proxies).
Libraries.
Meta-transactions.

Interactive Visualization

See exactly how the EVM handles this code in memory, storage, and the stack.

Interactive Simulation
0 / 0
Loading...
Type Solidity code (v0.8.26) and click Run. Compiler loads on first use (~2MB).

Click Run to execute the simulation

Then use the step controls to navigate