SupportsInterface.behavior.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const { ethers } = require('ethers');
  2. const { expect } = require('chai');
  3. const { selector } = require('../../helpers/methods');
  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 = Object.fromEntries(
  83. Object.entries(SIGNATURES).map(([name, signatures]) => [
  84. name,
  85. ethers.toBeHex(
  86. signatures.reduce((id, fnSig) => id ^ BigInt(selector(fnSig)), 0n),
  87. 4,
  88. ),
  89. ]),
  90. );
  91. function shouldSupportInterfaces(interfaces = []) {
  92. describe('ERC165', function () {
  93. beforeEach(function () {
  94. this.contractUnderTest = this.mock || this.token || this.holder;
  95. });
  96. describe('when the interfaceId is supported', function () {
  97. it('uses less than 30k gas', async function () {
  98. for (const k of interfaces) {
  99. const interface = INTERFACE_IDS[k] ?? k;
  100. expect(await this.contractUnderTest.supportsInterface.estimateGas(interface)).to.be.lte(30000);
  101. }
  102. });
  103. it('returns true', async function () {
  104. for (const k of interfaces) {
  105. const interfaceId = INTERFACE_IDS[k] ?? k;
  106. expect(await this.contractUnderTest.supportsInterface(interfaceId)).to.equal(true, `does not support ${k}`);
  107. }
  108. });
  109. });
  110. describe('when the interfaceId is not supported', function () {
  111. it('uses less than 30k', async function () {
  112. expect(await this.contractUnderTest.supportsInterface.estimateGas(INVALID_ID)).to.be.lte(30000);
  113. });
  114. it('returns false', async function () {
  115. expect(await this.contractUnderTest.supportsInterface(INVALID_ID)).to.be.equal(false, `supports ${INVALID_ID}`);
  116. });
  117. });
  118. it('all interface functions are in ABI', async function () {
  119. for (const k of interfaces) {
  120. // skip interfaces for which we don't have a function list
  121. if (SIGNATURES[k] === undefined) continue;
  122. for (const fnSig of SIGNATURES[k]) {
  123. // TODO: Remove Truffle case when ethersjs migration is done
  124. if (this.contractUnderTest.abi) {
  125. const fnSelector = selector(fnSig);
  126. return expect(this.contractUnderTest.abi.filter(fn => fn.signature === fnSelector).length).to.equal(
  127. 1,
  128. `did not find ${fnSig}`,
  129. );
  130. }
  131. expect(!!this.contractUnderTest.interface.getFunction(fnSig), `did not find ${fnSig}`).to.be.true;
  132. }
  133. }
  134. });
  135. });
  136. }
  137. module.exports = {
  138. shouldSupportInterfaces,
  139. };