ERC1820Implementer.test.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const { expectRevert, singletons } = require('@openzeppelin/test-helpers');
  2. const { bufferToHex, keccak256 } = require('ethereumjs-util');
  3. const { expect } = require('chai');
  4. const ERC1820ImplementerMock = artifacts.require('ERC1820ImplementerMock');
  5. contract('ERC1820Implementer', function ([_, registryFunder, implementee, other]) {
  6. const ERC1820_ACCEPT_MAGIC = bufferToHex(keccak256('ERC1820_ACCEPT_MAGIC'));
  7. beforeEach(async function () {
  8. this.implementer = await ERC1820ImplementerMock.new();
  9. this.registry = await singletons.ERC1820Registry(registryFunder);
  10. this.interfaceA = bufferToHex(keccak256('interfaceA'));
  11. this.interfaceB = bufferToHex(keccak256('interfaceB'));
  12. });
  13. context('with no registered interfaces', function () {
  14. it('returns false when interface implementation is queried', async function () {
  15. expect(await this.implementer.canImplementInterfaceForAddress(this.interfaceA, implementee))
  16. .to.not.equal(ERC1820_ACCEPT_MAGIC);
  17. });
  18. it('reverts when attempting to set as implementer in the registry', async function () {
  19. await expectRevert(
  20. this.registry.setInterfaceImplementer(
  21. implementee, this.interfaceA, this.implementer.address, { from: implementee }
  22. ),
  23. 'Does not implement the interface'
  24. );
  25. });
  26. });
  27. context('with registered interfaces', function () {
  28. beforeEach(async function () {
  29. await this.implementer.registerInterfaceForAddress(this.interfaceA, implementee);
  30. });
  31. it('returns true when interface implementation is queried', async function () {
  32. expect(await this.implementer.canImplementInterfaceForAddress(this.interfaceA, implementee))
  33. .to.equal(ERC1820_ACCEPT_MAGIC);
  34. });
  35. it('returns false when interface implementation for non-supported interfaces is queried', async function () {
  36. expect(await this.implementer.canImplementInterfaceForAddress(this.interfaceB, implementee))
  37. .to.not.equal(ERC1820_ACCEPT_MAGIC);
  38. });
  39. it('returns false when interface implementation for non-supported addresses is queried', async function () {
  40. expect(await this.implementer.canImplementInterfaceForAddress(this.interfaceA, other))
  41. .to.not.equal(ERC1820_ACCEPT_MAGIC);
  42. });
  43. it('can be set as an implementer for supported interfaces in the registry', async function () {
  44. await this.registry.setInterfaceImplementer(
  45. implementee, this.interfaceA, this.implementer.address, { from: implementee }
  46. );
  47. expect(await this.registry.getInterfaceImplementer(implementee, this.interfaceA))
  48. .to.equal(this.implementer.address);
  49. });
  50. });
  51. });