The referenced ABI encoding function is inefficient as it will perform a keccak256 hash of the provided function name on-chain inefficiently.
Example:
/**
* @dev Attempts to call the `decimals()` function on an ERC-20 token contract.
* @param tokenAddress The address of the ERC-20 token contract.
* @return success Indicates if the call was successful.
* @return decimals The number of decimals the token uses, or 0 if the call failed.
*/
function tryGetDecimals(address tokenAddress) public view returns (bool success, uint8 decimals) {
bytes memory payload = abi.encodeWithSignature("decimals()");
// Low-level call to the token contract
bytes memory returnData;
(success, returnData) = tokenAddress.staticcall(payload);
// If call was successful and returned data is the expected length for uint8
if (success && returnData.length == 32) {
// Decode the return data
decimals = abi.decode(returnData, (uint8));
} else {
// Default to 0 decimals if call failed or returned unexpected data
return (false, 0);
}
}
Recommendation:
We advise the system to employ the abi.encodeWithSelector function and the selector to be acquired via the selector syntax (i.e. ERC20.decimals.selector).
DET-02C: Inefficient Specification of Signature
Description:
The referenced ABI encoding function is inefficient as it will perform a
keccak256
hash of the provided function name on-chain inefficiently.Example:
Recommendation:
We advise the system to employ the
abi.encodeWithSelector
function and the selector to be acquired via the selector syntax (i.e.ERC20.decimals.selector
).