ERC1820Implementer.test.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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, anyone]) {
  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(
  19. this.registry.setInterfaceImplementer(
  20. implementee, this.interfaceA, this.implementer.address, { from: implementee }
  21. )
  22. );
  23. });
  24. });
  25. context('with registered interfaces', function () {
  26. beforeEach(async function () {
  27. await this.implementer.registerInterfaceForAddress(this.interfaceA, implementee);
  28. });
  29. it('returns true when interface implementation is queried', async function () {
  30. (await this.implementer.canImplementInterfaceForAddress(this.interfaceA, implementee))
  31. .should.equal(ERC1820_ACCEPT_MAGIC);
  32. });
  33. it('returns false when interface implementation for non-supported interfaces is queried', async function () {
  34. (await this.implementer.canImplementInterfaceForAddress(this.interfaceB, implementee))
  35. .should.not.equal(ERC1820_ACCEPT_MAGIC);
  36. });
  37. it('returns false when interface implementation for non-supported addresses is queried', async function () {
  38. (await this.implementer.canImplementInterfaceForAddress(this.interfaceA, anyone))
  39. .should.not.equal(ERC1820_ACCEPT_MAGIC);
  40. });
  41. it('can be set as an implementer for supported interfaces in the registry', async function () {
  42. await this.registry.setInterfaceImplementer(
  43. implementee, this.interfaceA, this.implementer.address, { from: implementee }
  44. );
  45. (await this.registry.getInterfaceImplementer(implementee, this.interfaceA))
  46. .should.equal(this.implementer.address);
  47. });
  48. });
  49. });