SupportsInterface.behavior.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const { makeInterfaceId } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const INTERFACES = {
  4. ERC165: [
  5. 'supportsInterface(bytes4)',
  6. ],
  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: [
  19. 'totalSupply()',
  20. 'tokenOfOwnerByIndex(address,uint256)',
  21. 'tokenByIndex(uint256)',
  22. ],
  23. ERC721Metadata: [
  24. 'name()',
  25. 'symbol()',
  26. 'tokenURI(uint256)',
  27. ],
  28. ERC1155: [
  29. 'balanceOf(address,uint256)',
  30. 'balanceOfBatch(address[],uint256[])',
  31. 'setApprovalForAll(address,bool)',
  32. 'isApprovedForAll(address,address)',
  33. 'safeTransferFrom(address,address,uint256,uint256,bytes)',
  34. 'safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)',
  35. ],
  36. ERC1155Receiver: [
  37. 'onERC1155Received(address,address,uint256,uint256,bytes)',
  38. 'onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)',
  39. ],
  40. AccessControl: [
  41. 'hasRole(bytes32,address)',
  42. 'getRoleAdmin(bytes32)',
  43. 'grantRole(bytes32,address)',
  44. 'revokeRole(bytes32,address)',
  45. 'renounceRole(bytes32,address)',
  46. ],
  47. AccessControlEnumerable: [
  48. 'getRoleMember(bytes32,uint256)',
  49. 'getRoleMemberCount(bytes32)',
  50. ],
  51. Governor: [
  52. 'name()',
  53. 'version()',
  54. 'COUNTING_MODE()',
  55. 'hashProposal(address[],uint256[],bytes[],bytes32)',
  56. 'state(uint256)',
  57. 'proposalSnapshot(uint256)',
  58. 'proposalDeadline(uint256)',
  59. 'votingDelay()',
  60. 'votingPeriod()',
  61. 'quorum(uint256)',
  62. 'getVotes(address,uint256)',
  63. 'hasVoted(uint256,address)',
  64. 'propose(address[],uint256[],bytes[],string)',
  65. 'execute(address[],uint256[],bytes[],bytes32)',
  66. 'castVote(uint256,uint8)',
  67. 'castVoteWithReason(uint256,uint8,string)',
  68. 'castVoteBySig(uint256,uint8,uint8,bytes32,bytes32)',
  69. ],
  70. GovernorTimelock: [
  71. 'timelock()',
  72. 'proposalEta(uint256)',
  73. 'queue(address[],uint256[],bytes[],bytes32)',
  74. ],
  75. };
  76. const INTERFACE_IDS = {};
  77. const FN_SIGNATURES = {};
  78. for (const k of Object.getOwnPropertyNames(INTERFACES)) {
  79. INTERFACE_IDS[k] = makeInterfaceId.ERC165(INTERFACES[k]);
  80. for (const fnName of INTERFACES[k]) {
  81. // the interface id of a single function is equivalent to its function signature
  82. FN_SIGNATURES[fnName] = makeInterfaceId.ERC165([fnName]);
  83. }
  84. }
  85. function shouldSupportInterfaces (interfaces = []) {
  86. describe('Contract interface', function () {
  87. beforeEach(function () {
  88. this.contractUnderTest = this.mock || this.token || this.holder || this.accessControl;
  89. });
  90. for (const k of interfaces) {
  91. const interfaceId = INTERFACE_IDS[k];
  92. describe(k, function () {
  93. describe('ERC165\'s supportsInterface(bytes4)', function () {
  94. it('uses less than 30k gas [skip-on-coverage]', async function () {
  95. expect(await this.contractUnderTest.supportsInterface.estimateGas(interfaceId)).to.be.lte(30000);
  96. });
  97. it('claims support [skip-on-coverage]', async function () {
  98. expect(await this.contractUnderTest.supportsInterface(interfaceId)).to.equal(true);
  99. });
  100. });
  101. for (const fnName of INTERFACES[k]) {
  102. const fnSig = FN_SIGNATURES[fnName];
  103. describe(fnName, function () {
  104. it('has to be implemented', function () {
  105. expect(this.contractUnderTest.abi.filter(fn => fn.signature === fnSig).length).to.equal(1);
  106. });
  107. });
  108. }
  109. });
  110. }
  111. });
  112. }
  113. module.exports = {
  114. shouldSupportInterfaces,
  115. };