SupportsInterface.behavior.js 5.0 KB

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