mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Add support for interfaceID.
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
@@ -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)
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -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
|
||||
@@ -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[type(Simpson).interfaceId] = 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
|
||||
Reference in New Issue
Block a user