In this track: Inside the EVM
advanced · 4 MIN READ

DELEGATECALL and execution context

Use another address’s code while retaining the caller’s state context.

LET’S PICTURE IT

A borrowed instruction manual operates on your cabinet rather than the manual owner’s cabinet.

SEE IT FOR YOURSELF
Pack small values into Solidity storage slotsStep 1 / 4
Pack small values into Solidity storage slotsFirstuint128 A16 bytesSeconduint128 B16 bytesThirduint256 C32 bytesSimple elementary types only; not a complete compiler layoutSlot 0 · 32 bytesUnused · 16 BA · 16 BHigh-order bytes ← → low-order bytesSlot 0 · fullB · 16 BA · 16 BDeclaration order and sizes determine this packingSlot 0BASlot 1C · 32 bytesTry different sequences in the storage-layout tool

In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

Separate code and storage address

DELEGATECALL runs target code using the caller’s storage context. In a proxy design, application writes generally affect the proxy’s storage.

Preserve caller-related fields

The delegated frame preserves msg.sender and msg.value from the delegating context. This differs from an ordinary CALL to the implementation address.

Coordinate storage meaning

Implementation code interprets whatever bytes already occupy the caller’s slots. A layout mismatch can corrupt state even when the delegated code is otherwise valid.

Treat target choice as authority

Code selected for delegation can act on the caller’s state. Protect the target selection and upgrade path rather than considering the target a harmless read-only library by default.

Trace the idea

User → Proxy
Proxy → DELEGATECALL Implementation
code source: Implementation
storage context: Proxy
msg.sender in delegated code: User

Stack examples list the bottom first and the top last. These small traces omit gas and surrounding dispatch unless stated.

QUICK CHECK · NO PRESSURE

Does delegated code normally write the implementation’s own storage?

Your progress stays in this browser.
Keep exploring the original referenceethereum.org ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.