What is delegatecall in smart contracts?
Delegatecall is a call that runs code from another contract inside the calling contract: the code comes from the target, but the storage, balance and address are those of the caller, and msg.sender and msg.value keep their values (Solidity docs). Proxies and libraries are built on it, and it means the called code can write any storage slot of the caller.
How does delegatecall differ from call?
| Question | call | delegatecall |
|---|---|---|
| Whose code runs | B | B |
| Whose storage it reads and writes | B | A |
address(this) inside that code |
B | A |
msg.sender inside that code |
A | Whoever called A |
The code from B writes storage slots by number, not by name, so if A and B don't lay out their variables the same way, each write lands on whatever A keeps in that slot.
A wallet whose owner slot a delegatecalled contract overwrites (illustrative, not audited code) DelegateWallet.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
contract Logic {
address public owner; // slot 0
function claim() external {
owner = msg.sender;
}
}
contract Wallet {
address public owner; // also slot 0, so claim() in Logic writes it
address public immutable logic;
constructor(address logic_) {
owner = msg.sender;
logic = logic_;
}
// Bug: forwards any call. Calling claim() through the wallet makes the caller its owner.
fallback() external {
(bool ok, ) = logic.delegatecall(msg.data);
require(ok, "delegatecall failed");
}
}
In a review, the questions are which address the code delegates to, who can change that address and whether both contracts share one storage layout. A finding names the line that delegates and the slot it can overwrite, as owner is in the wallet above.
How has borrowed code gone wrong before?
| Protocol | Parity multi-sig wallet library |
|---|---|
| Date | 6 November 2017 |
| Chain | Ethereum |
| Loss | 513,774.16 ETH frozen in 587 wallets |
| What happened | Parity multi-sig wallets deployed after 20 July 2017 were small stubs that kept most of their logic in one shared library contract, and nobody had initialized the library. A user initialized it, became its owner and destroyed it, and every wallet that relied on it could no longer move its funds. |
| Sources | Parity Technologies |
Parity had already been hit once, in July 2017 (Petrov). A Halborn write-up on delegatecall bugs describes that attack: the attacker re-initialized a wallet using functions stored in the shared library, claimed ownership and drained funds from it (Halborn). The wallet in the code above fails the same way, because a forwarded call rewrites its owner slot.
- The target is fixed or set only by an authorized role, never taken from user input.
- Caller and target agree on storage layout, slot by slot.
- Nobody else can initialize, take over or replace the code you delegate to.
Sources
- Introduction to smart contracts: delegatecall and libraries, Solidity documentation checked 3 December 2026
- A postmortem on the Parity multi-sig library self-destruct, Parity Technologies checked 3 December 2026
- Delegatecall vulnerabilities in Solidity, Halborn checked 3 December 2026
- Another Parity Wallet hack explained, Sergey Petrov (Medium) checked 3 December 2026