SupportsInterface.behavior.js 4.5 KB

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