SupportsInterface.behavior.js 2.7 KB

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