Advanced Functions
Delegate Call and Call
Beginner Level
Solidity provides low-level functions to interact with other contracts.
1) Call
- Executes a function in another contract.
msg.senderandmsg.valueare set to the calling contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
(bool success, ) = address(otherContract).call(
abi.encodeWithSignature("someFunction(uint256)", 123)
);
require(success, "Call failed");
2) Delegatecall
- Executes code from another contract in the context of the calling contract.
- Keeps the storage,
msg.sender, andmsg.valueof the calling contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
(bool success, ) = address(libraryContract).delegatecall(
abi.encodeWithSignature("updateValue(uint256)", 100)
);
require(success, "Delegatecall failed");
- Useful for upgradable contracts, libraries, and proxy patterns.
Continue Learning
Explore more topics in Solidity or browse other tutorials.