Call | Delegatecall in solidity

Solidity Programming Language
2 min readFeb 7, 2022

Thanks to Deniz !

Code: https://github.com/veridelisi/basic-solidity/blob/main/000000000_calldelegatecall.sol

Other Source: https://kushgoyal.com/ethereum-solidity-how-use-call-delegatecall/

Aim: To understand call and delegatecall functions

CALL

We deploy “SomeContract

We deploy “OtherContract” and we take address.

We paste this address to the “simpleCall” function in “SomeContract” and fill the other 2 variable.

To use call you need to send encoded data as the param. The data will have the function signature and params encoded together.

contract SomeContract{

function simpleCall(address otherContract,uint256 setInt,string memory setString) public {

(bool success,)=otherContract.call(abi.encodeWithSignature(“setVal(uint256,string)”,setInt,setString));

Look at the otherContract.sol

function setVal(uint256 setInt,string calldata setString) public {

intVal = setInt;

stringVal = setString;

}

This is result in the OtherContract

It is possible to supply amount of gas and ether to the call method.

otherContract.call{value: 1 ether, gas: 1000000}(abi.encodeWithSignature(“setVal(uint256,string)”,setInt,setString));

If you are sending ether to the call method, setVal should be a payable function. Otherwise the call will fail.

DELEGATECALL

delegatecall is used to call a function of otherContract from someContract with someContract’s storage, balance and address supplied to the function. This is done for the purpose of using the function in otherContract as library code. Since the function will behave as it was a function of someContract itself.

We use simpleDelegateCall function in SomeContract . We put othercontract address and other 2 variables values. When we control the intVal and stringVal, we see that otherContract’ setVal function is used in SomeContract.

Dr. Engin YILMAZ

--

--

Solidity Programming Language

Solidity basics for beginners: Learn the fundamentals of smart contract development and build your first DApp! #Solidity #Foundry #Ethereum #Opcodes #DApps