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:
msg.sender and msg.value) are maintained from the calling contract.delegatecallHere’s how delegatecall is typically used in Solidity:
(bool success, bytes memory returnData) = targetContract.delegatecall(data);
targetContract: The address of the contract you want to delegate the call to.data: The ABI-encoded data (including function signature and parameters) that will be executed on the target contract.success: A boolean indicating whether the call was successful or not.returnData: The data returned by the function call (if any).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. |