-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for interfaceId. #8642
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
interface ERC165 { | ||
/// @notice Query if a contract implements an interface | ||
/// @param interfaceID The interface identifier, as specified in ERC-165 | ||
/// @dev Interface identification is specified in ERC-165. This function | ||
/// uses less than 30,000 gas. | ||
/// @return `true` if the contract implements `interfaceID` and | ||
/// `interfaceID` is not 0xffffffff, `false` otherwise | ||
function supportsInterface(bytes4 interfaceID) external view returns (bool); | ||
} | ||
|
||
interface Simpson { | ||
function is2D() external returns (bool); | ||
function skinColor() external returns (string memory); | ||
} | ||
|
||
contract Homer is ERC165, Simpson { | ||
function supportsInterface(bytes4 interfaceID) external view override returns (bool) { | ||
return | ||
interfaceID == this.supportsInterface.selector || // ERC165 | ||
interfaceID == this.is2D.selector ^ this.skinColor.selector; // Simpson | ||
} | ||
|
||
function is2D() external override returns (bool) { | ||
return true; | ||
} | ||
|
||
function skinColor() external override returns (string memory) { | ||
return "yellow"; | ||
} | ||
} | ||
|
||
// ---- | ||
// supportsInterface(bytes4): left(0x01ffc9a0) -> false | ||
// supportsInterface(bytes4): left(0x01ffc9a7) -> true | ||
// supportsInterface(bytes4): left(0x73b6b492) -> true | ||
// supportsInterface(bytes4): left(0x70b6b492) -> false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
interface ERC165 { | ||
/// @notice Query if a contract implements an interface | ||
/// @param interfaceID The interface identifier, as specified in ERC-165 | ||
/// @dev Interface identification is specified in ERC-165. This function | ||
/// uses less than 30,000 gas. | ||
/// @return `true` if the contract implements `interfaceID` and | ||
/// `interfaceID` is not 0xffffffff, `false` otherwise | ||
function supportsInterface(bytes4 interfaceID) external view returns (bool); | ||
} | ||
|
||
interface Simpson { | ||
function is2D() external returns (bool); | ||
function skinColor() external returns (string memory); | ||
} | ||
|
||
contract Homer is ERC165, Simpson { | ||
function supportsInterface(bytes4 interfaceID) external view override returns (bool) { | ||
return | ||
interfaceID == type(ERC165).interfaceId || | ||
interfaceID == type(Simpson).interfaceId; | ||
} | ||
|
||
function is2D() external override returns (bool) { | ||
return true; | ||
} | ||
|
||
function skinColor() external override returns (string memory) { | ||
return "yellow"; | ||
} | ||
} | ||
|
||
// ---- | ||
// supportsInterface(bytes4): left(0x01ffc9a0) -> false | ||
// supportsInterface(bytes4): left(0x01ffc9a7) -> true | ||
// supportsInterface(bytes4): left(0x73b6b492) -> true | ||
// supportsInterface(bytes4): left(0x70b6b492) -> false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
interface HelloWorld { | ||
function hello() external pure; | ||
function world(int) external pure; | ||
} | ||
|
||
interface HelloWorldWithEvent { | ||
event Event(); | ||
function hello() external pure; | ||
function world(int) external pure; | ||
} | ||
|
||
contract Test { | ||
bytes4 public hello_world = type(HelloWorld).interfaceId; | ||
bytes4 public hello_world_with_event = type(HelloWorldWithEvent).interfaceId; | ||
} | ||
|
||
// ==== | ||
// compileViaYul: also | ||
// ---- | ||
// hello_world() -> left(0xc6be8b58) | ||
// hello_world_with_event() -> left(0xc6be8b58) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
interface HelloWorld { | ||
function hello() external pure; | ||
function world(int) external pure; | ||
} | ||
|
||
interface HelloWorldDerived is HelloWorld { | ||
function other() external pure; | ||
} | ||
|
||
interface ERC165 { | ||
/// @notice Query if a contract implements an interface | ||
/// @param interfaceID The interface identifier, as specified in ERC-165 | ||
/// @dev Interface identification is specified in ERC-165. This function | ||
/// uses less than 30,000 gas. | ||
/// @return `true` if the contract implements `interfaceID` and | ||
/// `interfaceID` is not 0xffffffff, `false` otherwise | ||
function supportsInterface(bytes4 interfaceID) external view returns (bool); | ||
} | ||
|
||
contract Test { | ||
bytes4 public ghello_world_interfaceId = type(HelloWorld).interfaceId; | ||
bytes4 public ERC165_interfaceId = type(ERC165).interfaceId; | ||
|
||
function hello() public pure returns (bytes4 data){ | ||
HelloWorld i; | ||
return i.hello.selector; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, it seem to work. At least if Yul is not used. If Yul is used, we got the following error:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
function world() public pure returns (bytes4 data){ | ||
HelloWorld i; | ||
return i.world.selector; | ||
} | ||
|
||
function hello_world() public pure returns (bytes4 data){ | ||
// HelloWorld i; | ||
// return i.hello.selector ^ i.world.selector; // = 0xc6be8b58 | ||
return 0xc6be8b58; | ||
aarlt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
function hello_world_interfaceId() public pure returns (bytes4 data){ | ||
return type(HelloWorld).interfaceId; | ||
} | ||
|
||
function other() public pure returns (bytes4 data){ | ||
HelloWorldDerived i; | ||
return i.other.selector; | ||
} | ||
|
||
function hello_world_derived_interfaceId() public pure returns (bytes4 data){ | ||
return type(HelloWorldDerived).interfaceId; | ||
} | ||
} | ||
|
||
// ==== | ||
// compileViaYul: also | ||
// ---- | ||
// hello() -> left(0x19ff1d21) | ||
// world() -> left(0xdf419679) | ||
// | ||
// ERC165_interfaceId() -> left(0x01ffc9a7) | ||
// | ||
// hello_world() -> left(0xc6be8b58) | ||
// hello_world_interfaceId() -> left(0xc6be8b58) | ||
// ghello_world_interfaceId() -> left(0xc6be8b58) | ||
// | ||
// other() -> left(0x85295877) | ||
// hello_world_derived_interfaceId() -> left(0x85295877) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
interface ERC165 { | ||
/// @notice Query if a contract implements an interface | ||
/// @param interfaceID The interface identifier, as specified in ERC-165 | ||
/// @dev Interface identification is specified in ERC-165. This function | ||
/// uses less than 30,000 gas. | ||
/// @return `true` if the contract implements `interfaceID` and | ||
/// `interfaceID` is not 0xffffffff, `false` otherwise | ||
function supportsInterface(bytes4 interfaceID) external view returns (bool); | ||
} | ||
|
||
contract ERC165MappingImplementation is ERC165 { | ||
/// @dev You must not set element 0xffffffff to true | ||
mapping(bytes4 => bool) internal supportedInterfaces; | ||
|
||
constructor() internal { | ||
supportedInterfaces[this.supportsInterface.selector] = true; | ||
} | ||
|
||
function supportsInterface(bytes4 interfaceID) external view override returns (bool) { | ||
return supportedInterfaces[interfaceID]; | ||
} | ||
} | ||
|
||
interface Simpson { | ||
function is2D() external returns (bool); | ||
function skinColor() external returns (string memory); | ||
} | ||
|
||
contract Lisa is ERC165MappingImplementation, Simpson { | ||
constructor() public { | ||
supportedInterfaces[this.is2D.selector ^ this.skinColor.selector] = true; | ||
} | ||
|
||
function is2D() external override returns (bool) { | ||
return true; | ||
} | ||
|
||
function skinColor() external override returns (string memory) { | ||
return "yellow"; | ||
} | ||
} | ||
|
||
// ---- | ||
// supportsInterface(bytes4): left(0x01ffc9a0) -> false | ||
// supportsInterface(bytes4): left(0x01ffc9a7) -> true | ||
// supportsInterface(bytes4): left(0x73b6b492) -> true | ||
// supportsInterface(bytes4): left(0x70b6b492) -> false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should have used
EIP-165
😉