ERC1820Implementer.test.js 2.7 KB

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