This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode: 0xf4
Introduced: Homestead (EIP-7)
DELEGATECALL executes code from another account while preserving the complete execution context (msg.sender, msg.value, storage). This enables library patterns and upgradeable proxy contracts by allowing code reuse without changing the caller’s state context.
Specification
Stack Input:
Stack Output:
Gas Cost: 700 + cold_access + memory_expansion
Operation:
Behavior
DELEGATECALL executes foreign code as if it were part of the caller:
- Pop 6 stack arguments (no value parameter)
- Calculate gas cost:
- Base: 700 gas (Tangerine Whistle+)
- Cold access: +2,600 gas for first access (Berlin+)
- Memory expansion for input and output regions
- Read calldata from memory
- Forward gas: Up to 63/64 of remaining gas (EIP-150)
- Execute target’s code preserving context:
- msg.sender = preserved from caller (NOT changed!)
- msg.value = preserved from caller (NOT changed!)
- Storage = caller’s storage (modifications affect caller!)
- Code = target’s code
- address(this) = caller’s address
- Copy returndata to memory
- Set return_data buffer to full returndata
- Push success flag
- Refund unused gas from child execution
Key characteristics:
- Complete context preservation (msg.sender, msg.value unchanged)
- Storage operations affect caller
- No value transfer (preserves existing msg.value)
- Foundation for library and proxy patterns
Examples
Library Pattern
Proxy Pattern
Library Contract
Upgradeable Storage Pattern
Gas Cost
Total cost: 700 + cold_access + memory_expansion + forwarded_gas
Base Cost: 700 gas (Tangerine Whistle+)
Pre-Tangerine Whistle: 40 gas
No Value Transfer Cost
DELEGATECALL never transfers value - preserves msg.value:
Cold Access: +2,600 gas (Berlin+)
EIP-2929 (Berlin+): First access to target address:
Memory Expansion
Same as CALL - charges for both input and output regions:
Gas Forwarding (EIP-150)
Tangerine Whistle+: 63/64 rule:
Example Calculation
Common Usage
Minimal Proxy (EIP-1167)
Diamond Pattern (EIP-2535)
Library Call Helper
Security
Storage Collision
Storage layout MUST match between caller and callee:
Mitigation: Use consistent storage layout:
Uninitialized Proxy
Proxy storage must be initialized before use:
Selfdestruct in Implementation
SELFDESTRUCT in library can destroy proxy:
Function Selector Collision
Different functions with same selector can cause unexpected behavior:
Delegate to Untrusted Contract
Only DELEGATECALL to trusted, audited code:
Implementation
References