Delegatecall: The Detailed and Animated Guide

RareSkills
RareSkills
Published in
18 min readJun 29, 2024

--

This article explains how delegatecall works in detail. The Ethereum Virtual Machine (EVM) offers four opcodes for making calls between contracts:

  • CALL (F1)
  • CALLCODE (F2)
  • STATICCALL (FA)
  • and DELEGATECALL (F4).

Notably, the CALLCODE opcode has been deprecated since Solidity v5, being replaced by DELEGATECALL. These opcodes have a direct implementation in Solidity and can be executed as methods of variables of type address.

To gain a better understanding of how delegatecall works, let’s first review the functionality of the CALL opcode.

CALL

To demonstrate call consider the following contract:

contract Called {
uint public number;

function increment() public {
number++;
}
}

The most direct way to execute the increment() function from another contract is by utilizing the Called contract interface. In this recipe, we can execute the function with a statement as simple as called.increment() where called is the address of Called. But calling increment() can also be achieved using a low-level call as show in the contract below:

contract Caller {

address constant public calledAddress = 0xd9145CCE52D386f254917e481eB44e9943F39138; // Called's address

function callIncrement() public {
calledAddress.call(abi.encodeWithSignature("increment()"));
}
}

--

--

RareSkills
RareSkills

RareSkills.io is the leading blockchain developer education platform.