SupportsInterface.behavior.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. };
  52. const INTERFACE_IDS = {};
  53. const FN_SIGNATURES = {};
  54. for (const k of Object.getOwnPropertyNames(INTERFACES)) {
  55. INTERFACE_IDS[k] = makeInterfaceId.ERC165(INTERFACES[k]);
  56. for (const fnName of INTERFACES[k]) {
  57. // the interface id of a single function is equivalent to its function signature
  58. FN_SIGNATURES[fnName] = makeInterfaceId.ERC165([fnName]);
  59. }
  60. }
  61. function shouldSupportInterfaces (interfaces = []) {
  62. describe('Contract interface', function () {
  63. beforeEach(function () {
  64. this.contractUnderTest = this.mock || this.token || this.holder || this.accessControl;
  65. });
  66. for (const k of interfaces) {
  67. const interfaceId = INTERFACE_IDS[k];
  68. describe(k, function () {
  69. describe('ERC165\'s supportsInterface(bytes4)', function () {
  70. it('uses less than 30k gas [skip-on-coverage]', async function () {
  71. expect(await this.contractUnderTest.supportsInterface.estimateGas(interfaceId)).to.be.lte(30000);
  72. });
  73. it('claims support', async function () {
  74. expect(await this.contractUnderTest.supportsInterface(interfaceId)).to.equal(true);
  75. });
  76. });
  77. for (const fnName of INTERFACES[k]) {
  78. const fnSig = FN_SIGNATURES[fnName];
  79. describe(fnName, function () {
  80. it('has to be implemented', function () {
  81. expect(this.contractUnderTest.abi.filter(fn => fn.signature === fnSig).length).to.equal(1);
  82. });
  83. });
  84. }
  85. });
  86. }
  87. });
  88. }
  89. module.exports = {
  90. shouldSupportInterfaces,
  91. };