Skip to content

feat(cheatcodes): add vm.getSelectors(name) cheatcode to get a list of all selectors #11484

@dadadave80

Description

@dadadave80

Component

Other (please describe)

Describe the feature you would like

Current Workflow:

Currently, to retrieve function selectors from a contract name, I use the following helper function:

function getSelectors(string memory _contractName) internal returns (bytes4[] memory selectors_) {
    string[] memory cmd = new string[](5);
    cmd[0] = "forge";
    cmd[1] = "inspect";
    cmd[2] = _contractName;
    cmd[3] = "methodIdentifiers";
    cmd[4] = "--json";

    bytes memory res = vm.ffi(cmd);
    string memory output = string(res);

    string[] memory keys = vm.parseJsonKeys(output, "");
    uint256 keysLength = keys.length;

    // Initialize the selectors array with the selectorCount
    selectors_ = new bytes4[](keysLength);

    for (uint256 i; i < keysLength; ++i) {
        selectors_[i] = bytes4(bytes32(keccak256(bytes(keys[i]))));
    }
}

The issue:

  • This approach requires enabling ffi, which many developers may not be comfortable with for security reasons.
  • It introduces external command execution, which could slow down tests and create unnecessary complexity.
  • ffi usage reduces portability and reproducibility in some environments.

Proposed Solution

  • Add a native utility function to Vm.sol (or another appropriate cheatcode interface) for retrieving selectors directly from compiled contract data:
function getSelectors(string memory contractName) external returns (bytes4[] memory);

This function would:

  • Accept the contract name (or optionally, its bytecode).
  • Return an array of all function selectors derived from the contract’s ABI.
  • Eliminate the need for ffi and forge inspect calls during tests.

Additional Notes

  • I also noticed that no current Vm.sol function accepts string in memory, which may require adjustments or documentation.
  • This feature would be extremely useful for developers working with Diamond Standard (EIP-2535) or similar patterns where function selector management is critical.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions