SupportsInterface.behavior.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 = []) {
  93. interfaces.unshift('ERC165');
  94. describe('ERC165', function () {
  95. beforeEach(function () {
  96. this.contractUnderTest = this.mock || this.token;
  97. });
  98. describe('when the interfaceId is supported', function () {
  99. it('uses less than 30k gas', async function () {
  100. for (const k of interfaces) {
  101. const interfaceId = INTERFACE_IDS[k] ?? k;
  102. expect(await this.contractUnderTest.supportsInterface.estimateGas(interfaceId)).to.lte(30_000n);
  103. }
  104. });
  105. it('returns true', async function () {
  106. for (const k of interfaces) {
  107. const interfaceId = INTERFACE_IDS[k] ?? k;
  108. expect(await this.contractUnderTest.supportsInterface(interfaceId), `does not support ${k}`).to.be.true;
  109. }
  110. });
  111. });
  112. describe('when the interfaceId is not supported', function () {
  113. it('uses less than 30k', async function () {
  114. expect(await this.contractUnderTest.supportsInterface.estimateGas(INVALID_ID)).to.lte(30_000n);
  115. });
  116. it('returns false', async function () {
  117. expect(await this.contractUnderTest.supportsInterface(INVALID_ID), `supports ${INVALID_ID}`).to.be.false;
  118. });
  119. });
  120. it('all interface functions are in ABI', async function () {
  121. for (const k of interfaces) {
  122. // skip interfaces for which we don't have a function list
  123. if (SIGNATURES[k] === undefined) continue;
  124. // Check the presence of each function in the contract's interface
  125. for (const fnSig of SIGNATURES[k]) {
  126. expect(this.contractUnderTest.interface.hasFunction(fnSig), `did not find ${fnSig}`).to.be.true;
  127. }
  128. }
  129. });
  130. });
  131. }
  132. module.exports = {
  133. shouldSupportInterfaces,
  134. };