UUPSUpgradeable.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const ERC1967Proxy = artifacts.require('ERC1967Proxy');
  3. const UUPSUpgradeableMock = artifacts.require('UUPSUpgradeableMock');
  4. const UUPSUpgradeableUnsafeMock = artifacts.require('UUPSUpgradeableUnsafeMock');
  5. const UUPSUpgradeableBrokenMock = artifacts.require('UUPSUpgradeableBrokenMock');
  6. const CountersImpl = artifacts.require('CountersImpl');
  7. contract('UUPSUpgradeable', function (accounts) {
  8. before(async function () {
  9. this.implInitial = await UUPSUpgradeableMock.new();
  10. this.implUpgradeOk = await UUPSUpgradeableMock.new();
  11. this.implUpgradeUnsafe = await UUPSUpgradeableUnsafeMock.new();
  12. this.implUpgradeBroken = await UUPSUpgradeableBrokenMock.new();
  13. this.implUpgradeNonUUPS = await CountersImpl.new();
  14. });
  15. beforeEach(async function () {
  16. const { address } = await ERC1967Proxy.new(this.implInitial.address, '0x');
  17. this.instance = await UUPSUpgradeableMock.at(address);
  18. });
  19. it('upgrade to upgradeable implementation', async function () {
  20. const { receipt } = await this.instance.upgradeTo(this.implUpgradeOk.address);
  21. expect(receipt.logs.filter(({ event }) => event === 'Upgraded').length).to.be.equal(1);
  22. expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeOk.address });
  23. });
  24. it('upgrade to upgradeable implementation with call', async function () {
  25. expect(await this.instance.current()).to.be.bignumber.equal('0');
  26. const { receipt } = await this.instance.upgradeToAndCall(
  27. this.implUpgradeOk.address,
  28. this.implUpgradeOk.contract.methods.increment().encodeABI(),
  29. );
  30. expect(receipt.logs.filter(({ event }) => event === 'Upgraded').length).to.be.equal(1);
  31. expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeOk.address });
  32. expect(await this.instance.current()).to.be.bignumber.equal('1');
  33. });
  34. it('upgrade to and unsafe upgradeable implementation', async function () {
  35. const { receipt } = await this.instance.upgradeTo(this.implUpgradeUnsafe.address);
  36. expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeUnsafe.address });
  37. });
  38. it('reject upgrade to broken upgradeable implementation', async function () {
  39. await expectRevert(
  40. this.instance.upgradeTo(this.implUpgradeBroken.address),
  41. 'ERC1967Upgrade: upgrade breaks further upgrades',
  42. );
  43. });
  44. // delegate to a non existing upgradeTo function causes a low level revert
  45. it('reject upgrade to non uups implementation', async function () {
  46. await expectRevert(
  47. this.instance.upgradeTo(this.implUpgradeNonUUPS.address),
  48. 'Address: low-level delegate call failed',
  49. );
  50. });
  51. it('reject proxy address as implementation', async function () {
  52. const { address } = await ERC1967Proxy.new(this.implInitial.address, '0x');
  53. const otherInstance = await UUPSUpgradeableMock.at(address);
  54. // infinite loop reverts when a nested call is out-of-gas
  55. await expectRevert(
  56. this.instance.upgradeTo(otherInstance.address),
  57. 'Address: low-level delegate call failed',
  58. );
  59. });
  60. });