2

As shown here, is is possible to read the code of a contract using plain Solidity by using the .code member of the address type:

contract GetCode {
    function at(address _addr) public view returns (bytes memory o_code) {
        return _addr.code;
    }
}

Does this return any code for precompiles?

Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143

1 Answers1

2

The answer is no - The .code member does not return the code for precompiled addresses.

Let's take the following examples:

pragma solidity >=0.8.18;

contract Foo { function getCode1() external view returns (bytes memory) { address addr1 = address(1); return addr1.code; }

function getCode2() external view returns (bytes memory) {
    address addr2 = address(2);
    return addr2.code;
}

}

Calling either getCode1 or getCode2 returns a zero bytes array.

For more information about precompiles, see this other Q&A here on StackExchange: List of pre-compiled contracts.

Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143