ERC1820Implementer.test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const { expectRevert, singletons } = require('@openzeppelin/test-helpers');
  2. const { bufferToHex, keccakFromString } = require('ethereumjs-util');
  3. const { expect } = require('chai');
  4. const ERC1820Implementer = artifacts.require('$ERC1820Implementer');
  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 ERC1820Implementer.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)).to.not.equal(
  17. ERC1820_ACCEPT_MAGIC,
  18. );
  19. });
  20. it('reverts when attempting to set as implementer in the registry', async function () {
  21. await expectRevert(
  22. this.registry.setInterfaceImplementer(implementee, this.interfaceA, this.implementer.address, {
  23. from: implementee,
  24. }),
  25. 'Does not implement the interface',
  26. );
  27. });
  28. });
  29. context('with registered interfaces', function () {
  30. beforeEach(async function () {
  31. await this.implementer.$_registerInterfaceForAddress(this.interfaceA, implementee);
  32. });
  33. it('returns true when interface implementation is queried', async function () {
  34. expect(await this.implementer.canImplementInterfaceForAddress(this.interfaceA, implementee)).to.equal(
  35. ERC1820_ACCEPT_MAGIC,
  36. );
  37. });
  38. it('returns false when interface implementation for non-supported interfaces is queried', async function () {
  39. expect(await this.implementer.canImplementInterfaceForAddress(this.interfaceB, implementee)).to.not.equal(
  40. ERC1820_ACCEPT_MAGIC,
  41. );
  42. });
  43. it('returns false when interface implementation for non-supported addresses is queried', async function () {
  44. expect(await this.implementer.canImplementInterfaceForAddress(this.interfaceA, other)).to.not.equal(
  45. ERC1820_ACCEPT_MAGIC,
  46. );
  47. });
  48. it('can be set as an implementer for supported interfaces in the registry', async function () {
  49. await this.registry.setInterfaceImplementer(implementee, this.interfaceA, this.implementer.address, {
  50. from: implementee,
  51. });
  52. expect(await this.registry.getInterfaceImplementer(implementee, this.interfaceA)).to.equal(
  53. this.implementer.address,
  54. );
  55. });
  56. });
  57. });