How are delegatecall and staticcall in Solidity different?

The functions staticcall and delegatecall are two low-level functions in Solidity that are used to interact with other contracts deployed on the Ethereum blockchain. Low-level functions in Solidity allow for direct interaction with the Ethereum Virtual Machine (EVM) and are normally used for advanced operations and optimizations. Although both functions seem very similar, they have their distinct features.

Apart from the very commonly used call function, both staticcall and delegatecall are also used to call a function in another contract and execute it. However, the difference between staticcall and delegatecall lies in the way the function is executed.

How are delegatecall and staticcall different?

The staticcall function allows a contract to read data from another contract but does not allow the contract to modify its state. It is called in a read-only context, which means that the called function cannot modify any storage variables and can only read from them. When a contract needs to retrieve information from another contract without altering any state variables, this function tends to be particularly useful.

On the other hand, delegatecall allows a contract to delegate some functionality to another contract while preserving its state. It is used to call a function in another contract and execute it in the context of the current contract. This means that any storage variables accessed or modified during the execution of the called function will be from the current contract. The purpose of delegatecall is typically to delegate some functionality to another contract while preserving the state of the current contract.

Let's explore the difference between the two through the use of examples.

The staticcall function code example

In the code example below, we can see how staticcall is utilized by contract class_B to access the public variable N in contract class_A. The staticcall function only reads the variable value and stores it into data:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Explanation

  • Line 1: We use pragma to specify the version of Solidity. We want to compile the code in pragma.

  • Lines 4–9: We define Class_A, in which we declare a public number N to be equal to 25.

  • Line 15: We define a read_N function in Class_B that reads the value of N from Class_A, and we define a pointer address to Class_A named Class_A_address.

  • Lines 18–22: We use the staticcall() function to read the data N from the contract Class_A using the Class_A_address variable as input and encode the function signature of N using the encodeWithSignature() function.

  • Line 25: We use the require() function to ensure the success of the staticcall() function.

  • Line 28: We return the decoded data using the decode() function.

The delegatecall function code example

In the code example below, we can see how delegatecall is utilized by contract class_B to calculate the sum of N1 and N2 using the find_Sum function in contract class_A. The answer is decoded and stored in final_answer:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Explanation

  • Line 1: We use pragma to specify the version of Solidity. We want to compile the code in pragma.

  • Lines 10–12: We define the find_Sum() function in the contract Class_A, in which we pass the numbers N1 and N2 to be added.

  • Line 19: We define a pointer address to Class_A named class_A_address.

  • Lines 25–28: We use the delegatecall() function to call the function find_Sum() from the contract Class_A using the class_A_address variable as input and encode the function signature of find_Sum() using the encodeWithSignature() function along the inputs N1 and N2.

  • Line 35: We use the require() function to ensure the execution of the delegatecall() function.

  • Line 38: We store the decoded result of addition using the decode() function in the final_answer variable.

Summary

Both staticcall and delegatecall are important low-level functions in Solidity that allow contracts to interact with other contracts in different ways. The staticcall function is used to read data from another contract without modifying its state, while delegatecall is used to delegate some functionality to another contract while preserving the state of the current contract. Understanding the differences between these functions is crucial for developing efficient and secure smart contracts on the Ethereum blockchain.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved