Skip to content

Commit

Permalink
Add two competing implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
fulldecent authored Feb 14, 2018
1 parent 7558488 commit 224822c
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions EIPS/eip-165.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ contract Selector {
}
```

Note: interfaces do not permit optional functions, therefore, the interface identity will not include them.

Note: an ERC standard may define multiple interfaces to separate core functionality from optional features. For example, [one draft standard defines](https://github.com/ethereum/EIPs/pull/841) ERC721, ERC721Metadata and ERC721Enumerable interfaces.
Note: interfaces do not permit optional functions, therefore, the interface identity will not them.

### How a Contract will Publish the Interfaces it Implements

A contract that is compliant with ERC-165 shall implement the following function:
A contract that is compliant with ERC-165 shall implement the following interface (referred as `ERC165.sol`):

```solidity
pragma solidity ^0.4.19;
Expand Down Expand Up @@ -116,6 +114,61 @@ XXXXXXXX HELP NEEDED XXXXXXXXX

## Implementation

This approach uses a `view` function implementation of `supportsInterface`. The execution cost is 478 gas for any input. But contract initialization requires storing each interface (`SSTORE` is 20,000 gas).

```solidity
pragma solidity ^0.4.19;
import "./ERC165.sol";
interface Simpson {
function is2D() external returns (bool);
function skinColor() external returns (string);
}
contract Lisa is ERC165, Simpson {
mapping(bytes4 => bool) supportedInterfaces;
function Lisa() public {
supportedInterfaces[this.supportsInterface.selector] = true;
supportedInterfaces[this.is2D.selector ^ this.skinColor.selector] = true;
}
function supportsInterface(bytes4 interfaceID) external view returns (bool) {
return supportedInterfaces[interfaceID];
}
// ... is usually 2D
// skin color is yellow
}
```

Following is a `pure` function implementation of `supportsInterface`. The worst-case execution cost is 236 gas, but increases linearly with a higher number of supported interfaces.

```solidity
pragma solidity ^0.4.19;
import "./ERC165.sol";
interface Simpson {
function is2D() external returns (bool);
function skinColor() external returns (string);
}
contract Homer is ERC165, Simpson {
function supportsInterface(bytes4 interfaceID) external view returns (bool) {
return
interfaceID == this.supportsInterface.selector || // ERC165
interfaceID == this.is2D.selector
^ this.skinColor.selector; // Simpson
}
}
```

With three or more supported interfaces (including ERC165 itself as a required supported interface), the mapping table approach (for any case) costs less gas than the worst case for pure approach.



XXXXXX IN PROGRES XXXXXX [https://github.com/jbaylina/EIP165Cache](https://github.com/jbaylina/EIP165Cache)

## Copyright
Expand Down

0 comments on commit 224822c

Please sign in to comment.