SupportsInterface.behavior.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. ERC6909: [
  93. 'balanceOf(address,uint256)',
  94. 'allowance(address,address,uint256)',
  95. 'isOperator(address,address)',
  96. 'transfer(address,uint256,uint256)',
  97. 'transferFrom(address,address,uint256,uint256)',
  98. 'approve(address,uint256,uint256)',
  99. 'setOperator(address,bool)',
  100. ],
  101. };
  102. const INTERFACE_IDS = mapValues(SIGNATURES, interfaceId);
  103. function shouldSupportInterfaces(interfaces = [], signatures = SIGNATURES) {
  104. // case where only signatures are provided
  105. if (!Array.isArray(interfaces)) {
  106. signatures = interfaces;
  107. interfaces = Object.keys(interfaces);
  108. }
  109. interfaces.unshift('ERC165');
  110. signatures.ERC165 = SIGNATURES.ERC165;
  111. const interfaceIds = mapValues(signatures, interfaceId, ([name]) => interfaces.includes(name));
  112. describe('ERC165', function () {
  113. beforeEach(function () {
  114. this.contractUnderTest = this.mock || this.token;
  115. });
  116. describe('when the interfaceId is supported', function () {
  117. it('uses less than 30k gas', async function () {
  118. for (const k of interfaces) {
  119. const interfaceId = interfaceIds[k] ?? k;
  120. expect(await this.contractUnderTest.supportsInterface.estimateGas(interfaceId)).to.lte(30_000n);
  121. }
  122. });
  123. it('returns true', async function () {
  124. for (const k of interfaces) {
  125. const interfaceId = interfaceIds[k] ?? k;
  126. expect(await this.contractUnderTest.supportsInterface(interfaceId), `does not support ${k}`).to.be.true;
  127. }
  128. });
  129. });
  130. describe('when the interfaceId is not supported', function () {
  131. it('uses less than 30k', async function () {
  132. expect(await this.contractUnderTest.supportsInterface.estimateGas(INVALID_ID)).to.lte(30_000n);
  133. });
  134. it('returns false', async function () {
  135. expect(await this.contractUnderTest.supportsInterface(INVALID_ID), `supports ${INVALID_ID}`).to.be.false;
  136. });
  137. });
  138. it('all interface functions are in ABI', async function () {
  139. for (const k of interfaces) {
  140. // skip interfaces for which we don't have a function list
  141. if (signatures[k] === undefined) continue;
  142. // Check the presence of each function in the contract's interface
  143. for (const fnSig of signatures[k]) {
  144. expect(this.contractUnderTest.interface.hasFunction(fnSig), `did not find ${fnSig}`).to.be.true;
  145. }
  146. }
  147. });
  148. });
  149. }
  150. module.exports = {
  151. SIGNATURES,
  152. INTERFACE_IDS,
  153. shouldSupportInterfaces,
  154. };