SupportsInterface.behavior.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. 'votingDelay()',
  63. 'votingPeriod()',
  64. 'quorum(uint256)',
  65. 'getVotes(address,uint256)',
  66. 'getVotesWithParams(address,uint256,bytes)',
  67. 'hasVoted(uint256,address)',
  68. 'propose(address[],uint256[],bytes[],string)',
  69. 'queue(address[],uint256[],bytes[],bytes32)',
  70. 'execute(address[],uint256[],bytes[],bytes32)',
  71. 'cancel(address[],uint256[],bytes[],bytes32)',
  72. 'castVote(uint256,uint8)',
  73. 'castVoteWithReason(uint256,uint8,string)',
  74. 'castVoteWithReasonAndParams(uint256,uint8,string,bytes)',
  75. 'castVoteBySig(uint256,uint8,address,bytes)',
  76. 'castVoteWithReasonAndParamsBySig(uint256,uint8,address,string,bytes,bytes)',
  77. ],
  78. ERC2981: ['royaltyInfo(uint256,uint256)'],
  79. };
  80. const INTERFACE_IDS = {};
  81. const FN_SIGNATURES = {};
  82. for (const k of Object.getOwnPropertyNames(INTERFACES)) {
  83. INTERFACE_IDS[k] = makeInterfaceId.ERC165(INTERFACES[k]);
  84. for (const fnName of INTERFACES[k]) {
  85. // the interface id of a single function is equivalent to its function signature
  86. FN_SIGNATURES[fnName] = makeInterfaceId.ERC165([fnName]);
  87. }
  88. }
  89. function shouldSupportInterfaces(interfaces = []) {
  90. describe('ERC165', function () {
  91. beforeEach(function () {
  92. this.contractUnderTest = this.mock || this.token || this.holder || this.accessControl;
  93. });
  94. describe('when the interfaceId is supported', function () {
  95. it('uses less than 30k gas', async function () {
  96. for (const k of interfaces) {
  97. const interfaceId = INTERFACE_IDS[k] ?? k;
  98. expect(await this.contractUnderTest.supportsInterface.estimateGas(interfaceId)).to.be.lte(30000);
  99. }
  100. });
  101. it('returns true', async function () {
  102. for (const k of interfaces) {
  103. const interfaceId = INTERFACE_IDS[k] ?? k;
  104. expect(await this.contractUnderTest.supportsInterface(interfaceId)).to.equal(true, `does not support ${k}`);
  105. }
  106. });
  107. });
  108. describe('when the interfaceId is not supported', function () {
  109. it('uses less thank 30k', async function () {
  110. expect(await this.contractUnderTest.supportsInterface.estimateGas(INVALID_ID)).to.be.lte(30000);
  111. });
  112. it('returns false', async function () {
  113. expect(await this.contractUnderTest.supportsInterface(INVALID_ID)).to.be.equal(false, `supports ${INVALID_ID}`);
  114. });
  115. });
  116. it('all interface functions are in ABI', async function () {
  117. for (const k of interfaces) {
  118. // skip interfaces for which we don't have a function list
  119. if (INTERFACES[k] === undefined) continue;
  120. for (const fnName of INTERFACES[k]) {
  121. const fnSig = FN_SIGNATURES[fnName];
  122. expect(this.contractUnderTest.abi.filter(fn => fn.signature === fnSig).length).to.equal(
  123. 1,
  124. `did not find ${fnName}`,
  125. );
  126. }
  127. }
  128. });
  129. });
  130. }
  131. module.exports = {
  132. shouldSupportInterfaces,
  133. };