SupportsInterface.behavior.js 5.1 KB

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