ERC165Mock.sol 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {IERC165} from "../utils/introspection/IERC165.sol";
  4. /**
  5. * https://eips.ethereum.org/EIPS/eip-214#specification
  6. * From the specification:
  7. * > Any attempts to make state-changing operations inside an execution instance with STATIC set to true will instead
  8. * throw an exception.
  9. * > These operations include [...], LOG0, LOG1, LOG2, [...]
  10. *
  11. * therefore, because this contract is staticcall'd we need to not emit events (which is how solidity-coverage works)
  12. * solidity-coverage ignores the /mocks folder, so we duplicate its implementation here to avoid instrumenting it
  13. */
  14. contract SupportsInterfaceWithLookupMock is IERC165 {
  15. /*
  16. * bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7
  17. */
  18. bytes4 public constant INTERFACE_ID_ERC165 = 0x01ffc9a7;
  19. /**
  20. * @dev A mapping of interface id to whether or not it's supported.
  21. */
  22. mapping(bytes4 interfaceId => bool) private _supportedInterfaces;
  23. /**
  24. * @dev A contract implementing SupportsInterfaceWithLookup
  25. * implement ERC-165 itself.
  26. */
  27. constructor() {
  28. _registerInterface(INTERFACE_ID_ERC165);
  29. }
  30. /**
  31. * @dev Implement supportsInterface(bytes4) using a lookup table.
  32. */
  33. function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
  34. return _supportedInterfaces[interfaceId];
  35. }
  36. /**
  37. * @dev Private method for registering an interface.
  38. */
  39. function _registerInterface(bytes4 interfaceId) internal {
  40. require(interfaceId != 0xffffffff, "ERC165InterfacesSupported: invalid interface id");
  41. _supportedInterfaces[interfaceId] = true;
  42. }
  43. }
  44. contract ERC165InterfacesSupported is SupportsInterfaceWithLookupMock {
  45. constructor(bytes4[] memory interfaceIds) {
  46. for (uint256 i = 0; i < interfaceIds.length; i++) {
  47. _registerInterface(interfaceIds[i]);
  48. }
  49. }
  50. }
  51. // Similar to ERC165InterfacesSupported, but revert (without reason) when an interface is not supported
  52. contract ERC165RevertInvalid is SupportsInterfaceWithLookupMock {
  53. constructor(bytes4[] memory interfaceIds) {
  54. for (uint256 i = 0; i < interfaceIds.length; i++) {
  55. _registerInterface(interfaceIds[i]);
  56. }
  57. }
  58. function supportsInterface(bytes4 interfaceId) public view override returns (bool) {
  59. require(super.supportsInterface(interfaceId));
  60. return true;
  61. }
  62. }
  63. contract ERC165MaliciousData {
  64. function supportsInterface(bytes4) public pure returns (bool) {
  65. assembly {
  66. mstore(0, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
  67. return(0, 32)
  68. }
  69. }
  70. }
  71. contract ERC165MissingData {
  72. function supportsInterface(bytes4 interfaceId) public view {} // missing return
  73. }
  74. contract ERC165NotSupported {}
  75. contract ERC165ReturnBombMock is IERC165 {
  76. function supportsInterface(bytes4 interfaceId) public pure override returns (bool) {
  77. if (interfaceId == type(IERC165).interfaceId) {
  78. assembly {
  79. mstore(0, 1)
  80. }
  81. }
  82. assembly {
  83. return(0, 101500)
  84. }
  85. }
  86. }