Add two competing implementations

This commit is contained in:
William Entriken 2018-02-14 00:05:57 -05:00 committed by GitHub
parent 7558488487
commit 224822c743
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 57 additions and 4 deletions

View File

@ -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;
@ -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