SupportsInterface.behavior.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. 'defaultAdmin()',
  41. 'defaultAdminTransferDelayedUntil()',
  42. 'pendingDefaultAdmin()',
  43. 'beginDefaultAdminTransfer(address)',
  44. 'acceptDefaultAdminTransfer()',
  45. 'cancelDefaultAdminTransfer()',
  46. ],
  47. Governor: [
  48. 'name()',
  49. 'version()',
  50. 'COUNTING_MODE()',
  51. 'hashProposal(address[],uint256[],bytes[],bytes32)',
  52. 'state(uint256)',
  53. 'proposalSnapshot(uint256)',
  54. 'proposalDeadline(uint256)',
  55. 'votingDelay()',
  56. 'votingPeriod()',
  57. 'quorum(uint256)',
  58. 'getVotes(address,uint256)',
  59. 'hasVoted(uint256,address)',
  60. 'propose(address[],uint256[],bytes[],string)',
  61. 'execute(address[],uint256[],bytes[],bytes32)',
  62. 'castVote(uint256,uint8)',
  63. 'castVoteWithReason(uint256,uint8,string)',
  64. 'castVoteBySig(uint256,uint8,uint8,bytes32,bytes32)',
  65. ],
  66. GovernorWithParams: [
  67. 'name()',
  68. 'version()',
  69. 'COUNTING_MODE()',
  70. 'hashProposal(address[],uint256[],bytes[],bytes32)',
  71. 'state(uint256)',
  72. 'proposalSnapshot(uint256)',
  73. 'proposalDeadline(uint256)',
  74. 'votingDelay()',
  75. 'votingPeriod()',
  76. 'quorum(uint256)',
  77. 'getVotes(address,uint256)',
  78. 'getVotesWithParams(address,uint256,bytes)',
  79. 'hasVoted(uint256,address)',
  80. 'propose(address[],uint256[],bytes[],string)',
  81. 'execute(address[],uint256[],bytes[],bytes32)',
  82. 'castVote(uint256,uint8)',
  83. 'castVoteWithReason(uint256,uint8,string)',
  84. 'castVoteWithReasonAndParams(uint256,uint8,string,bytes)',
  85. 'castVoteBySig(uint256,uint8,uint8,bytes32,bytes32)',
  86. 'castVoteWithReasonAndParamsBySig(uint256,uint8,string,bytes,uint8,bytes32,bytes32)',
  87. ],
  88. GovernorTimelock: ['timelock()', 'proposalEta(uint256)', 'queue(address[],uint256[],bytes[],bytes32)'],
  89. ERC2981: ['royaltyInfo(uint256,uint256)'],
  90. };
  91. const INTERFACE_IDS = {};
  92. const FN_SIGNATURES = {};
  93. for (const k of Object.getOwnPropertyNames(INTERFACES)) {
  94. INTERFACE_IDS[k] = makeInterfaceId.ERC165(INTERFACES[k]);
  95. for (const fnName of INTERFACES[k]) {
  96. // the interface id of a single function is equivalent to its function signature
  97. FN_SIGNATURES[fnName] = makeInterfaceId.ERC165([fnName]);
  98. }
  99. }
  100. function shouldSupportInterfaces(interfaces = []) {
  101. describe('ERC165', function () {
  102. beforeEach(function () {
  103. this.contractUnderTest = this.mock || this.token || this.holder || this.accessControl;
  104. });
  105. it('supportsInterface uses less than 30k gas', async function () {
  106. for (const k of interfaces) {
  107. const interfaceId = INTERFACE_IDS[k] ?? k;
  108. expect(await this.contractUnderTest.supportsInterface.estimateGas(interfaceId)).to.be.lte(30000);
  109. }
  110. });
  111. it('all interfaces are reported as supported', async function () {
  112. for (const k of interfaces) {
  113. const interfaceId = INTERFACE_IDS[k] ?? k;
  114. expect(await this.contractUnderTest.supportsInterface(interfaceId)).to.equal(true);
  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(1);
  124. }
  125. }
  126. });
  127. });
  128. }
  129. module.exports = {
  130. shouldSupportInterfaces,
  131. };