Blog

The Simplest Explanation of a `delegatecall` You Will Ever Find

For smart contracts, there's one feature that, while incredibly powerful, can also be a source of confusion for newcomers: delegatecall. But fear not! We're here to break it down into the simplest terms possible.

a) What is delegatecall?

A delegatecall is a call that allows one contract to invoke a function from another contract, with a twist. Instead of operating in the context of the called contract, it executes in the context of the calling contract. This means that while the code is from the external contract, any changes to storage variables are made in the storage of the calling contract. With an explanation we will illustrate what exactly that means.

b) Why is delegatecall useful?

The power of delegatecall lies in its flexibility. It's instrumental in scenarios like proxy contracts or architecture where a main contract acts as a gateway to various function implementations. Essentially, it allows for a contract to dynamically adopt new logic without the need for redeployment. This modular approach can significantly enhance a contract's adaptability over time.

c) A Simple Example for Clarity

To truly grasp delegatecall, let's walk through an illustrative example involving a simple staking contract.

Original Staking Contract: Imagine we start with a straightforward staking contract that allows users to deposit their tokens. However, in its initial form, it lacks the functionality for users to withdraw their staked tokens:


Enhanced with delegatecall:

To introduce withdrawal functionality without implementing a withdraw function, we can modify the staking contract to allow delegatecall to another address:


The withdrawalImplementation can expose the withdraw function. In our example, it is important to ensure that the implementation does not expose any other logic, otherwise this could be triggered as well.

By utilizing delegatecall, we can now interact with a separate contract that implements the withdrawal logic. This means users could theoretically withdraw their stakes if the staking contract delegates a call to a contract containing the withdrawal function. This would decrease their balance and transfer out tokens, all done on behalf of the main contract but using the implementation contract’s logic.

While the flexibility offered by delegatecall is undeniable, it comes with significant risks. If not properly secured, it can be exploited to execute unauthorized code, potentially leading to loss of funds or other critical issues. For instance an unguarded delegatecall could enable a malicious actor to drain a contract's funds by directing the call to a malicious contract (or function).

Therefore, whenever you encounter a delegatecall in a Solidity codebase, approach with heightened scrutiny. Understanding the context in which it's used and ensuring there are adequate safeguards against unauthorized manipulation are crucial steps in mitigating potential vulnerabilities.