SupportsInterface.behavior.js 2.5 KB

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