You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
abi.encodeCall: Syntactic sugar vesion of abi.encodeWithSelector and abi.encodeWithSignature
The most commonly used method in projects is abi.encodeCall.
Its advantage is that, compared to abi.encodeWithSignature and abi.encodeWithSelector, abi.encodeCall automatically checks the function signature and parameters at compile time, preventing spelling mistakes and parameter type mismatches. abi.encodeWithSignature dynamically accepts a string to represent the function signature, so when you use abi.encodeWithSignature, the compiler does not check if the function name or parameters are correct, as it treats the string as regular input data.
// SPDX-License-Identifier: MITpragma solidity^0.8.26;
interfaceIERC20 {
function transfer(address, uint256) external;
}
contractToken {
function transfer(address, uint256) external {}
}
contractAbiEncode {
function test(address_contract, bytescalldatadata) external {
(boolok,) = _contract.call(data);
require(ok, "call failed");
}
function encodeWithSignature(addressto, uint256amount)
externalpurereturns (bytesmemory)
{
// Typo is not checked - "transfer(address, uint)"returnabi.encodeWithSignature("transfer(address,uint256)", to, amount);
}
function encodeWithSelector(addressto, uint256amount)
externalpurereturns (bytesmemory)
{
// Type is not checked - (IERC20.transfer.selector, true, amount)returnabi.encodeWithSelector(IERC20.transfer.selector, to, amount);
}
function encodeCall(addressto, uint256amount)
externalpurereturns (bytesmemory)
{
// Typo and type errors will not compilereturnabi.encodeCall(IERC20.transfer, (to, amount));
}
}
Motivation(动机)
In practical projects, I often encounter scenarios where abi.encodeCall is used. Therefore, it is recommended to add this function to this chapter.
Are you willing to submit a PR?(你愿意提交PR吗?)
Yes I am willing to submit a PR!(是的我愿意)
The text was updated successfully, but these errors were encountered:
Details(细节)
abi.encodeCall
: Syntactic sugar vesion ofabi.encodeWithSelector
andabi.encodeWithSignature
The most commonly used method in projects is
abi.encodeCall
.Its advantage is that, compared to
abi.encodeWithSignature
andabi.encodeWithSelector
,abi.encodeCall
automatically checks the function signature and parameters at compile time, preventing spelling mistakes and parameter type mismatches.abi.encodeWithSignature
dynamically accepts a string to represent the function signature, so when you useabi.encodeWithSignature
, the compiler does not check if the function name or parameters are correct, as it treats the string as regular input data.Motivation(动机)
In practical projects, I often encounter scenarios where
abi.encodeCall
is used. Therefore, it is recommended to add this function to this chapter.Are you willing to submit a PR?(你愿意提交PR吗?)
The text was updated successfully, but these errors were encountered: