From 224822c743be51ff6b2762a1958507b4ea81637c Mon Sep 17 00:00:00 2001 From: William Entriken Date: Wed, 14 Feb 2018 00:05:57 -0500 Subject: [PATCH] Add two competing implementations --- EIPS/eip-165.md | 61 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/EIPS/eip-165.md b/EIPS/eip-165.md index 18b7c918..aff72d70 100644 --- a/EIPS/eip-165.md +++ b/EIPS/eip-165.md @@ -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