SupportsInterface.behavior.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const { makeInterfaceId } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const INVALID_ID = '0xffffffff';
  4. const INTERFACES = {
  5. ERC165: ['supportsInterface(bytes4)'],
  6. ERC721: [
  7. 'balanceOf(address)',
  8. 'ownerOf(uint256)',
  9. 'approve(address,uint256)',
  10. 'getApproved(uint256)',
  11. 'setApprovalForAll(address,bool)',
  12. 'isApprovedForAll(address,address)',
  13. 'transferFrom(address,address,uint256)',
  14. 'safeTransferFrom(address,address,uint256)',
  15. 'safeTransferFrom(address,address,uint256,bytes)',
  16. ],
  17. ERC721Enumerable: ['totalSupply()', 'tokenOfOwnerByIndex(address,uint256)', 'tokenByIndex(uint256)'],
  18. ERC721Metadata: ['name()', 'symbol()', 'tokenURI(uint256)'],
  19. ERC1155: [
  20. 'balanceOf(address,uint256)',
  21. 'balanceOfBatch(address[],uint256[])',
  22. 'setApprovalForAll(address,bool)',
  23. 'isApprovedForAll(address,address)',
  24. 'safeTransferFrom(address,address,uint256,uint256,bytes)',
  25. 'safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)',
  26. ],
  27. ERC1155Receiver: [
  28. 'onERC1155Received(address,address,uint256,uint256,bytes)',
  29. 'onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)',
  30. ],
  31. AccessControl: [
  32. 'hasRole(bytes32,address)',
  33. 'getRoleAdmin(bytes32)',
  34. 'grantRole(bytes32,address)',
  35. 'revokeRole(bytes32,address)',
  36. 'renounceRole(bytes32,address)',
  37. ],
  38. AccessControlEnumerable: ['getRoleMember(bytes32,uint256)', 'getRoleMemberCount(bytes32)'],
  39. AccessControlDefaultAdminRules: [
  40. 'defaultAdminDelay()',
  41. 'pendingDefaultAdminDelay()',
  42. 'defaultAdmin()',
  43. 'pendingDefaultAdmin()',
  44. 'defaultAdminDelayIncreaseWait()',
  45. 'changeDefaultAdminDelay(uint48)',
  46. 'rollbackDefaultAdminDelay()',
  47. 'beginDefaultAdminTransfer(address)',
  48. 'acceptDefaultAdminTransfer()',
  49. 'cancelDefaultAdminTransfer()',
  50. ],
  51. Governor: [
  52. 'name()',
  53. 'version()',
  54. 'COUNTING_MODE()',
  55. 'hashProposal(address[],uint256[],bytes[],bytes32)',
  56. 'state(uint256)',
  57. 'proposalThreshold()',
  58. 'proposalSnapshot(uint256)',
  59. 'proposalDeadline(uint256)',
  60. 'proposalProposer(uint256)',
  61. 'proposalEta(uint256)',
  62. 'proposalNeedsQueuing(uint256)',
  63. 'votingDelay()',
  64. 'votingPeriod()',
  65. 'quorum(uint256)',
  66. 'getVotes(address,uint256)',
  67. 'getVotesWithParams(address,uint256,bytes)',
  68. 'hasVoted(uint256,address)',
  69. 'propose(address[],uint256[],bytes[],string)',
  70. 'queue(address[],uint256[],bytes[],bytes32)',
  71. 'execute(address[],uint256[],bytes[],bytes32)',
  72. 'cancel(address[],uint256[],bytes[],bytes32)',
  73. 'castVote(uint256,uint8)',
  74. 'castVoteWithReason(uint256,uint8,string)',
  75. 'castVoteWithReasonAndParams(uint256,uint8,string,bytes)',
  76. 'castVoteBySig(uint256,uint8,address,bytes)',
  77. 'castVoteWithReasonAndParamsBySig(uint256,uint8,address,string,bytes,bytes)',
  78. ],
  79. ERC2981: ['royaltyInfo(uint256,uint256)'],
  80. };
  81. const INTERFACE_IDS = {};
  82. const FN_SIGNATURES = {};
  83. for (const k of Object.getOwnPropertyNames(INTERFACES)) {
  84. INTERFACE_IDS[k] = makeInterfaceId.ERC165(INTERFACES[k]);
  85. for (const fnName of INTERFACES[k]) {
  86. // the interface id of a single function is equivalent to its function signature
  87. FN_SIGNATURES[fnName] = makeInterfaceId.ERC165([fnName]);
  88. }
  89. }
  90. function shouldSupportInterfaces(interfaces = []) {
  91. describe('ERC165', function () {
  92. beforeEach(function () {
  93. this.contractUnderTest = this.mock || this.token || this.holder || this.accessControl;
  94. });
  95. describe('when the interfaceId is supported', function () {
  96. it('uses less than 30k gas', async function () {
  97. for (const k of interfaces) {
  98. const interfaceId = INTERFACE_IDS[k] ?? k;
  99. expect(await this.contractUnderTest.supportsInterface.estimateGas(interfaceId)).to.be.lte(30000);
  100. }
  101. });
  102. it('returns true', async function () {
  103. for (const k of interfaces) {
  104. const interfaceId = INTERFACE_IDS[k] ?? k;
  105. expect(await this.contractUnderTest.supportsInterface(interfaceId)).to.equal(true, `does not support ${k}`);
  106. }
  107. });
  108. });
  109. describe('when the interfaceId is not supported', function () {
  110. it('uses less thank 30k', async function () {
  111. expect(await this.contractUnderTest.supportsInterface.estimateGas(INVALID_ID)).to.be.lte(30000);
  112. });
  113. it('returns false', async function () {
  114. expect(await this.contractUnderTest.supportsInterface(INVALID_ID)).to.be.equal(false, `supports ${INVALID_ID}`);
  115. });
  116. });
  117. it('all interface functions are in ABI', async function () {
  118. for (const k of interfaces) {
  119. // skip interfaces for which we don't have a function list
  120. if (INTERFACES[k] === undefined) continue;
  121. for (const fnName of INTERFACES[k]) {
  122. const fnSig = FN_SIGNATURES[fnName];
  123. expect(this.contractUnderTest.abi.filter(fn => fn.signature === fnSig).length).to.equal(
  124. 1,
  125. `did not find ${fnName}`,
  126. );
  127. }
  128. }
  129. });
  130. });
  131. }
  132. module.exports = {
  133. shouldSupportInterfaces,
  134. };