Skip to content

Replace abi.encodeWithSelector by abi.encodeCall #3693

@wiasliaw

Description

@wiasliaw

🧐 Motivation
Since 0.8.11, abi.encodeCall provide type-safe encode utility comparing with abi.encodeWithSelector.

📝 Details
Example below and detail are from DevTools Summit | Topics in Solidity - Harikrishnan Mulackal.

abi.encodeWithSelector can use with interface.<function>.selector to prevent typo error, but it doesn't provide type checking.

interface miniERC20 {
  function transfer(address to, uint256 value) external;
}

// works successfully
function transferData(uint256 to, uint256 value)
    public
    pure
    returns (bytes memory)
{
    return abi.encodeWithSelector(miniERC20.transfer.selector, to, value);
}

abi.encodeCall provide type checking during compile time.

function transferData(uint256 to, uint256 value)
    public
    pure
    returns (bytes memory)
{
    return abi.encodeCall(miniERC20.transfer, (to, value));
}

result

error[5407]: TypeError: Cannot implicitly convert component at position 0 from "uint256" to "address".

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changeChanges that break backwards compatibility of the public API.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions