After reading this article about Solidity call()
and delegateCall()
: https://medium.com/coinmonks/delegatecall-calling-another-contract-function-in-solidity-b579f804178c
I have the following questions:
It mentions in the end (Wrap Up section):
- If we know the ABI of the target function, we can directly use the target function signature
- If we don’t know the ABI of the target function, we can use call(), or delegatecall(). But in the case of delegatecall(), we need to care about the order of the field variable.
But if we look at the call()
and delegateCall()
for the Calculator contract example, we still needed the Calculator address and the function signature as well, as per the line of code below. So what is the difference then? call()
and delegateCall()
also need the ABI of the target function.
function addValuesWithDelegateCall(address calculator, uint256 a, uint256 b) public returns (uint256) {
(bool success, bytes memory result) = calculator.delegatecall(abi.encodeWithSignature("add(uint256,uint256)", a, b));
emit AddedValuesByDelegateCall(a, b, success);
return abi.decode(result, (uint256));
}
function addValuesWithCall(address calculator, uint256 a, uint256 b) public returns (uint256) {
(bool success, bytes memory result) = calculator.call(abi.encodeWithSignature("add(uint256,uint256)", a, b));
emit AddedValuesByCall(a, b, success);
return abi.decode(result, (uint256));
}
It also mentions:
In Ethereum function call can be expressed by bytecode as long as 4 + 32 * N bytes. And this bytecode consists of two parts
What does N
represent?