SupportsInterface.behavior.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. const { expect } = require('chai');
  2. const { interfaceId } = require('../../helpers/methods');
  3. const { mapValues } = require('../../helpers/iterate');
  4. const INVALID_ID = '0xffffffff';
  5. const GOVERNOR_INTERFACE = [
  6. 'name()',
  7. 'version()',
  8. 'COUNTING_MODE()',
  9. 'hashProposal(address[],uint256[],bytes[],bytes32)',
  10. 'state(uint256)',
  11. 'proposalThreshold()',
  12. 'proposalSnapshot(uint256)',
  13. 'proposalDeadline(uint256)',
  14. 'proposalProposer(uint256)',
  15. 'proposalEta(uint256)',
  16. 'proposalNeedsQueuing(uint256)',
  17. 'votingDelay()',
  18. 'votingPeriod()',
  19. 'quorum(uint256)',
  20. 'getVotes(address,uint256)',
  21. 'getVotesWithParams(address,uint256,bytes)',
  22. 'hasVoted(uint256,address)',
  23. 'propose(address[],uint256[],bytes[],string)',
  24. 'queue(address[],uint256[],bytes[],bytes32)',
  25. 'execute(address[],uint256[],bytes[],bytes32)',
  26. 'cancel(address[],uint256[],bytes[],bytes32)',
  27. 'castVote(uint256,uint8)',
  28. 'castVoteWithReason(uint256,uint8,string)',
  29. 'castVoteWithReasonAndParams(uint256,uint8,string,bytes)',
  30. 'castVoteBySig(uint256,uint8,address,bytes)',
  31. 'castVoteWithReasonAndParamsBySig(uint256,uint8,address,string,bytes,bytes)',
  32. ];
  33. const SIGNATURES = {
  34. ERC165: ['supportsInterface(bytes4)'],
  35. ERC721: [
  36. 'balanceOf(address)',
  37. 'ownerOf(uint256)',
  38. 'approve(address,uint256)',
  39. 'getApproved(uint256)',
  40. 'setApprovalForAll(address,bool)',
  41. 'isApprovedForAll(address,address)',
  42. 'transferFrom(address,address,uint256)',
  43. 'safeTransferFrom(address,address,uint256)',
  44. 'safeTransferFrom(address,address,uint256,bytes)',
  45. ],
  46. ERC721Enumerable: ['totalSupply()', 'tokenOfOwnerByIndex(address,uint256)', 'tokenByIndex(uint256)'],
  47. ERC721Metadata: ['name()', 'symbol()', 'tokenURI(uint256)'],
  48. ERC1155: [
  49. 'balanceOf(address,uint256)',
  50. 'balanceOfBatch(address[],uint256[])',
  51. 'setApprovalForAll(address,bool)',
  52. 'isApprovedForAll(address,address)',
  53. 'safeTransferFrom(address,address,uint256,uint256,bytes)',
  54. 'safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)',
  55. ],
  56. ERC1155MetadataURI: ['uri(uint256)'],
  57. ERC1155Receiver: [
  58. 'onERC1155Received(address,address,uint256,uint256,bytes)',
  59. 'onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)',
  60. ],
  61. ERC1363: [
  62. 'transferAndCall(address,uint256)',
  63. 'transferAndCall(address,uint256,bytes)',
  64. 'transferFromAndCall(address,address,uint256)',
  65. 'transferFromAndCall(address,address,uint256,bytes)',
  66. 'approveAndCall(address,uint256)',
  67. 'approveAndCall(address,uint256,bytes)',
  68. ],
  69. AccessControl: [
  70. 'hasRole(bytes32,address)',
  71. 'getRoleAdmin(bytes32)',
  72. 'grantRole(bytes32,address)',
  73. 'revokeRole(bytes32,address)',
  74. 'renounceRole(bytes32,address)',
  75. ],
  76. AccessControlEnumerable: ['getRoleMember(bytes32,uint256)', 'getRoleMemberCount(bytes32)'],
  77. AccessControlDefaultAdminRules: [
  78. 'defaultAdminDelay()',
  79. 'pendingDefaultAdminDelay()',
  80. 'defaultAdmin()',
  81. 'pendingDefaultAdmin()',
  82. 'defaultAdminDelayIncreaseWait()',
  83. 'changeDefaultAdminDelay(uint48)',
  84. 'rollbackDefaultAdminDelay()',
  85. 'beginDefaultAdminTransfer(address)',
  86. 'acceptDefaultAdminTransfer()',
  87. 'cancelDefaultAdminTransfer()',
  88. ],
  89. Governor: GOVERNOR_INTERFACE,
  90. Governor_5_3: GOVERNOR_INTERFACE.concat('getProposalId(address[],uint256[],bytes[],bytes32)'),
  91. ERC2981: ['royaltyInfo(uint256,uint256)'],
  92. };
  93. const INTERFACE_IDS = mapValues(SIGNATURES, interfaceId);
  94. function shouldSupportInterfaces(interfaces = [], signatures = SIGNATURES) {
  95. // case where only signatures are provided
  96. if (!Array.isArray(interfaces)) {
  97. signatures = interfaces;
  98. interfaces = Object.keys(interfaces);
  99. }
  100. interfaces.unshift('ERC165');
  101. signatures.ERC165 = SIGNATURES.ERC165;
  102. const interfaceIds = mapValues(signatures, interfaceId, ([name]) => interfaces.includes(name));
  103. describe('ERC165', function () {
  104. beforeEach(function () {
  105. this.contractUnderTest = this.mock || this.token;
  106. });
  107. describe('when the interfaceId is supported', function () {
  108. it('uses less than 30k gas', async function () {
  109. for (const k of interfaces) {
  110. const interfaceId = interfaceIds[k] ?? k;
  111. expect(await this.contractUnderTest.supportsInterface.estimateGas(interfaceId)).to.lte(30_000n);
  112. }
  113. });
  114. it('returns true', async function () {
  115. for (const k of interfaces) {
  116. const interfaceId = interfaceIds[k] ?? k;
  117. expect(await this.contractUnderTest.supportsInterface(interfaceId), `does not support ${k}`).to.be.true;
  118. }
  119. });
  120. });
  121. describe('when the interfaceId is not supported', function () {
  122. it('uses less than 30k', async function () {
  123. expect(await this.contractUnderTest.supportsInterface.estimateGas(INVALID_ID)).to.lte(30_000n);
  124. });
  125. it('returns false', async function () {
  126. expect(await this.contractUnderTest.supportsInterface(INVALID_ID), `supports ${INVALID_ID}`).to.be.false;
  127. });
  128. });
  129. it('all interface functions are in ABI', async function () {
  130. for (const k of interfaces) {
  131. // skip interfaces for which we don't have a function list
  132. if (signatures[k] === undefined) continue;
  133. // Check the presence of each function in the contract's interface
  134. for (const fnSig of signatures[k]) {
  135. expect(this.contractUnderTest.interface.hasFunction(fnSig), `did not find ${fnSig}`).to.be.true;
  136. }
  137. }
  138. });
  139. });
  140. }
  141. module.exports = {
  142. SIGNATURES,
  143. INTERFACE_IDS,
  144. shouldSupportInterfaces,
  145. };