ERC1820Implementer.test.js 2.6 KB

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