UUPSUpgradeable.test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const { expectEvent } = require('@openzeppelin/test-helpers');
  2. const { getAddressInSlot, ImplementationSlot } = require('../../helpers/erc1967');
  3. const { expectRevertCustomError } = require('../../helpers/customError');
  4. const ERC1967Proxy = artifacts.require('ERC1967Proxy');
  5. const UUPSUpgradeableMock = artifacts.require('UUPSUpgradeableMock');
  6. const UUPSUpgradeableUnsafeMock = artifacts.require('UUPSUpgradeableUnsafeMock');
  7. const NonUpgradeableMock = artifacts.require('NonUpgradeableMock');
  8. const UUPSUnsupportedProxiableUUID = artifacts.require('UUPSUnsupportedProxiableUUID');
  9. const Address = artifacts.require('$Address');
  10. contract('UUPSUpgradeable', function () {
  11. before(async function () {
  12. this.implInitial = await UUPSUpgradeableMock.new();
  13. this.implUpgradeOk = await UUPSUpgradeableMock.new();
  14. this.implUpgradeUnsafe = await UUPSUpgradeableUnsafeMock.new();
  15. this.implUpgradeNonUUPS = await NonUpgradeableMock.new();
  16. this.implUnsupportedUUID = await UUPSUnsupportedProxiableUUID.new();
  17. this.helper = await Address.new();
  18. });
  19. beforeEach(async function () {
  20. const { address } = await ERC1967Proxy.new(this.implInitial.address, '0x');
  21. this.instance = await UUPSUpgradeableMock.at(address);
  22. });
  23. it('upgrade to upgradeable implementation', async function () {
  24. const { receipt } = await this.instance.upgradeTo(this.implUpgradeOk.address);
  25. expect(receipt.logs.filter(({ event }) => event === 'Upgraded').length).to.be.equal(1);
  26. expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeOk.address });
  27. expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.be.equal(this.implUpgradeOk.address);
  28. });
  29. it('upgrade to upgradeable implementation with call', async function () {
  30. expect(await this.instance.current()).to.be.bignumber.equal('0');
  31. const { receipt } = await this.instance.upgradeToAndCall(
  32. this.implUpgradeOk.address,
  33. this.implUpgradeOk.contract.methods.increment().encodeABI(),
  34. );
  35. expect(receipt.logs.filter(({ event }) => event === 'Upgraded').length).to.be.equal(1);
  36. expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeOk.address });
  37. expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.be.equal(this.implUpgradeOk.address);
  38. expect(await this.instance.current()).to.be.bignumber.equal('1');
  39. });
  40. it('calling upgradeTo on the implementation reverts', async function () {
  41. await expectRevertCustomError(
  42. this.implInitial.upgradeTo(this.implUpgradeOk.address),
  43. 'UUPSUnauthorizedCallContext',
  44. [],
  45. );
  46. });
  47. it('calling upgradeToAndCall on the implementation reverts', async function () {
  48. await expectRevertCustomError(
  49. this.implInitial.upgradeToAndCall(
  50. this.implUpgradeOk.address,
  51. this.implUpgradeOk.contract.methods.increment().encodeABI(),
  52. ),
  53. 'UUPSUnauthorizedCallContext',
  54. [],
  55. );
  56. });
  57. it('calling upgradeTo from a contract that is not an ERC1967 proxy (with the right implementation) reverts', async function () {
  58. await expectRevertCustomError(
  59. this.helper.$functionDelegateCall(
  60. this.implUpgradeOk.address,
  61. this.implUpgradeOk.contract.methods.upgradeTo(this.implUpgradeUnsafe.address).encodeABI(),
  62. ),
  63. 'UUPSUnauthorizedCallContext',
  64. [],
  65. );
  66. });
  67. it('calling upgradeToAndCall from a contract that is not an ERC1967 proxy (with the right implementation) reverts', async function () {
  68. await expectRevertCustomError(
  69. this.helper.$functionDelegateCall(
  70. this.implUpgradeOk.address,
  71. this.implUpgradeOk.contract.methods.upgradeToAndCall(this.implUpgradeUnsafe.address, '0x').encodeABI(),
  72. ),
  73. 'UUPSUnauthorizedCallContext',
  74. [],
  75. );
  76. });
  77. it('rejects upgrading to an unsupported UUID', async function () {
  78. await expectRevertCustomError(
  79. this.instance.upgradeTo(this.implUnsupportedUUID.address),
  80. 'UUPSUnsupportedProxiableUUID',
  81. [web3.utils.keccak256('invalid UUID')],
  82. );
  83. });
  84. it('upgrade to and unsafe upgradeable implementation', async function () {
  85. const { receipt } = await this.instance.upgradeTo(this.implUpgradeUnsafe.address);
  86. expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeUnsafe.address });
  87. expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.be.equal(this.implUpgradeUnsafe.address);
  88. });
  89. // delegate to a non existing upgradeTo function causes a low level revert
  90. it('reject upgrade to non uups implementation', async function () {
  91. await expectRevertCustomError(
  92. this.instance.upgradeTo(this.implUpgradeNonUUPS.address),
  93. 'ERC1967InvalidImplementation',
  94. [this.implUpgradeNonUUPS.address],
  95. );
  96. });
  97. it('reject proxy address as implementation', async function () {
  98. const { address } = await ERC1967Proxy.new(this.implInitial.address, '0x');
  99. const otherInstance = await UUPSUpgradeableMock.at(address);
  100. await expectRevertCustomError(this.instance.upgradeTo(otherInstance.address), 'ERC1967InvalidImplementation', [
  101. otherInstance.address,
  102. ]);
  103. });
  104. });