SupportsInterface.behavior.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. AccessControl: [
  34. 'hasRole(bytes32,address)',
  35. 'getRoleAdmin(bytes32)',
  36. 'grantRole(bytes32,address)',
  37. 'revokeRole(bytes32,address)',
  38. 'renounceRole(bytes32,address)',
  39. ],
  40. AccessControlEnumerable: ['getRoleMember(bytes32,uint256)', 'getRoleMemberCount(bytes32)'],
  41. AccessControlDefaultAdminRules: [
  42. 'defaultAdminDelay()',
  43. 'pendingDefaultAdminDelay()',
  44. 'defaultAdmin()',
  45. 'pendingDefaultAdmin()',
  46. 'defaultAdminDelayIncreaseWait()',
  47. 'changeDefaultAdminDelay(uint48)',
  48. 'rollbackDefaultAdminDelay()',
  49. 'beginDefaultAdminTransfer(address)',
  50. 'acceptDefaultAdminTransfer()',
  51. 'cancelDefaultAdminTransfer()',
  52. ],
  53. Governor: [
  54. 'name()',
  55. 'version()',
  56. 'COUNTING_MODE()',
  57. 'hashProposal(address[],uint256[],bytes[],bytes32)',
  58. 'state(uint256)',
  59. 'proposalThreshold()',
  60. 'proposalSnapshot(uint256)',
  61. 'proposalDeadline(uint256)',
  62. 'proposalProposer(uint256)',
  63. 'proposalEta(uint256)',
  64. 'proposalNeedsQueuing(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. 'queue(address[],uint256[],bytes[],bytes32)',
  73. 'execute(address[],uint256[],bytes[],bytes32)',
  74. 'cancel(address[],uint256[],bytes[],bytes32)',
  75. 'castVote(uint256,uint8)',
  76. 'castVoteWithReason(uint256,uint8,string)',
  77. 'castVoteWithReasonAndParams(uint256,uint8,string,bytes)',
  78. 'castVoteBySig(uint256,uint8,address,bytes)',
  79. 'castVoteWithReasonAndParamsBySig(uint256,uint8,address,string,bytes,bytes)',
  80. ],
  81. ERC2981: ['royaltyInfo(uint256,uint256)'],
  82. };
  83. const INTERFACE_IDS = mapValues(SIGNATURES, interfaceId);
  84. function shouldSupportInterfaces(interfaces = []) {
  85. describe('ERC165', function () {
  86. beforeEach(function () {
  87. this.contractUnderTest = this.mock || this.token || this.holder;
  88. });
  89. describe('when the interfaceId is supported', function () {
  90. it('uses less than 30k gas', async function () {
  91. for (const k of interfaces) {
  92. const interface = INTERFACE_IDS[k] ?? k;
  93. expect(await this.contractUnderTest.supportsInterface.estimateGas(interface)).to.lte(30_000n);
  94. }
  95. });
  96. it('returns true', async function () {
  97. for (const k of interfaces) {
  98. const interfaceId = INTERFACE_IDS[k] ?? k;
  99. expect(await this.contractUnderTest.supportsInterface(interfaceId), `does not support ${k}`).to.be.true;
  100. }
  101. });
  102. });
  103. describe('when the interfaceId is not supported', function () {
  104. it('uses less than 30k', async function () {
  105. expect(await this.contractUnderTest.supportsInterface.estimateGas(INVALID_ID)).to.lte(30_000n);
  106. });
  107. it('returns false', async function () {
  108. expect(await this.contractUnderTest.supportsInterface(INVALID_ID), `supports ${INVALID_ID}`).to.be.false;
  109. });
  110. });
  111. it('all interface functions are in ABI', async function () {
  112. for (const k of interfaces) {
  113. // skip interfaces for which we don't have a function list
  114. if (SIGNATURES[k] === undefined) continue;
  115. // Check the presence of each function in the contract's interface
  116. for (const fnSig of SIGNATURES[k]) {
  117. expect(this.contractUnderTest.interface.hasFunction(fnSig), `did not find ${fnSig}`).to.be.true;
  118. }
  119. }
  120. });
  121. });
  122. }
  123. module.exports = {
  124. shouldSupportInterfaces,
  125. };