SupportsInterface.behavior.js 5.4 KB

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