Blockchain & Cryptocurrency Glossary

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

  • search-icon Clear Definitions
  • search-icon Practical
  • search-icon Technical
  • search-icon Related Terms

DelegateCall

1 min read
Pronunciation
[del-uh-git kawl]
Analogy
Imagine Contract A (a manager) needs to perform a specialized calculation defined in Contract B (a specialist's rulebook). Instead of sending data to Contract B and getting a result back, `DelegateCall` is like the manager temporarily *becoming* the specialist, using the specialist's rulebook (Contract B's code) but applying it directly to their own paperwork (Contract A's storage) and acting with their own authority (`msg.sender`).
Definition
A low-level operation code (opcode) in the Ethereum Virtual Machine (EVM) that allows a smart contract (the caller) to execute code from another contract (the library or logic contract) within the context of the caller contract. This means the called code operates on the caller's storage, and `msg.sender` and `msg.value` remain unchanged.
Key Points Intro
`DelegateCall` is a powerful EVM feature enabling code reuse and proxy patterns, but requires careful handling.
Key Points

Executes code from another contract ('library' or 'logic' contract) within the calling contract's context.

The called code modifies the *calling* contract's storage.

The values of `msg.sender` and `msg.value` in the called code are those of the original call to the calling contract.

Essential for implementing libraries and upgradeable proxy patterns.

Requires careful management of storage layout to avoid collisions between the caller and the called contract.

Example
Upgradeable proxy contracts use `delegatecall` to forward user interactions to the current logic/implementation contract. Shared library contracts are often called using `delegatecall` so they can operate directly on the calling contract's state.
Technical Deep Dive
The `DELEGATECALL` opcode (0xF4) is similar to `CALL` (0xF1), but it preserves the execution context (`msg.sender`, `msg.value`, storage). This means the code at the target address runs as if it were part of the calling contract. Because storage is shared, developers must ensure that the storage variable layouts in the proxy/caller and the logic/library contract do not conflict, otherwise one might overwrite the other's data unintentionally. Libraries in Solidity often use internal functions which effectively get compiled into the calling contract, avoiding some `delegatecall` complexities, but external library functions or proxy patterns rely heavily on it.
Security Warning
Improper use of `delegatecall`, especially regarding storage layout management, is a major source of vulnerabilities. If a contract performs a `delegatecall` to an untrusted or vulnerable contract, the called code could arbitrarily modify the caller's storage, potentially leading to complete compromise (e.g., changing ownership, draining funds). Use with extreme caution.

DelegateCall - Related Articles

No related articles for this term.