SupportsInterface.behavior.js 4.4 KB

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