In Solidity, delegatecall is a low-level function used to execute code from another contract while maintaining the context (i.e., storage, msg.sender, msg.value) of the calling contract.

delegatecall is a special type of message call used to execute functions of another contract, but it runs in the context of the calling contract. This means:

Syntax of delegatecall

Here’s how delegatecall is typically used in Solidity:

(bool success, bytes memory returnData) = targetContract.delegatecall(data);

Usecase of delegateCall

  1. Proxy contracts
  2. Storing big contracts across multiple small contracts (because of the size limit of smart contracts)

Difference b/w CCIs and delegateCall

Aspect call delegatecall
Code Execution Executes code in the target contract. Executes code in the target contract, but in the context of the calling contract.
Storage Modifies the target contract’s storage. Modifies the calling contract's storage.
msg.sender Contract address of the contract calling the target contract Stays the same as the calling contract's msg.sender.
Use Case Interacting with another contract. Upgradable proxies, modular contracts.