UpgradeableBeacon.test.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. async function fixture() {
  5. const [admin, other] = await ethers.getSigners();
  6. const v1 = await ethers.deployContract('Implementation1');
  7. const v2 = await ethers.deployContract('Implementation2');
  8. const beacon = await ethers.deployContract('UpgradeableBeacon', [v1, admin]);
  9. return { admin, other, beacon, v1, v2 };
  10. }
  11. describe('UpgradeableBeacon', function () {
  12. beforeEach(async function () {
  13. Object.assign(this, await loadFixture(fixture));
  14. });
  15. it('cannot be created with non-contract implementation', async function () {
  16. await expect(ethers.deployContract('UpgradeableBeacon', [this.other, this.admin]))
  17. .to.be.revertedWithCustomError(this.beacon, 'BeaconInvalidImplementation')
  18. .withArgs(this.other);
  19. });
  20. describe('once deployed', function () {
  21. it('emits Upgraded event to the first implementation', async function () {
  22. await expect(this.beacon.deploymentTransaction()).to.emit(this.beacon, 'Upgraded').withArgs(this.v1);
  23. });
  24. it('returns implementation', async function () {
  25. expect(await this.beacon.implementation()).to.equal(this.v1);
  26. });
  27. it('can be upgraded by the admin', async function () {
  28. await expect(this.beacon.connect(this.admin).upgradeTo(this.v2))
  29. .to.emit(this.beacon, 'Upgraded')
  30. .withArgs(this.v2);
  31. expect(await this.beacon.implementation()).to.equal(this.v2);
  32. });
  33. it('cannot be upgraded to a non-contract', async function () {
  34. await expect(this.beacon.connect(this.admin).upgradeTo(this.other))
  35. .to.be.revertedWithCustomError(this.beacon, 'BeaconInvalidImplementation')
  36. .withArgs(this.other);
  37. });
  38. it('cannot be upgraded by other account', async function () {
  39. await expect(this.beacon.connect(this.other).upgradeTo(this.v2))
  40. .to.be.revertedWithCustomError(this.beacon, 'OwnableUnauthorizedAccount')
  41. .withArgs(this.other);
  42. });
  43. });
  44. });