UUPSUpgradeable.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 Clones = artifacts.require('$Clones');
  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. // Used for testing non ERC1967 compliant proxies (clones are proxies that don't use the ERC1967 implementation slot)
  18. this.cloneFactory = await Clones.new();
  19. });
  20. beforeEach(async function () {
  21. const { address } = await ERC1967Proxy.new(this.implInitial.address, '0x');
  22. this.instance = await UUPSUpgradeableMock.at(address);
  23. });
  24. it('upgrade to upgradeable implementation', async function () {
  25. const { receipt } = await this.instance.upgradeToAndCall(this.implUpgradeOk.address, '0x');
  26. expect(receipt.logs.filter(({ event }) => event === 'Upgraded').length).to.be.equal(1);
  27. expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeOk.address });
  28. expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.be.equal(this.implUpgradeOk.address);
  29. });
  30. it('upgrade to upgradeable implementation with call', async function () {
  31. expect(await this.instance.current()).to.be.bignumber.equal('0');
  32. const { receipt } = await this.instance.upgradeToAndCall(
  33. this.implUpgradeOk.address,
  34. this.implUpgradeOk.contract.methods.increment().encodeABI(),
  35. );
  36. expect(receipt.logs.filter(({ event }) => event === 'Upgraded').length).to.be.equal(1);
  37. expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeOk.address });
  38. expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.be.equal(this.implUpgradeOk.address);
  39. expect(await this.instance.current()).to.be.bignumber.equal('1');
  40. });
  41. it('calling upgradeTo on the implementation reverts', async function () {
  42. await expectRevertCustomError(
  43. this.implInitial.upgradeToAndCall(this.implUpgradeOk.address, '0x'),
  44. 'UUPSUnauthorizedCallContext',
  45. [],
  46. );
  47. });
  48. it('calling upgradeToAndCall on the implementation reverts', async function () {
  49. await expectRevertCustomError(
  50. this.implInitial.upgradeToAndCall(
  51. this.implUpgradeOk.address,
  52. this.implUpgradeOk.contract.methods.increment().encodeABI(),
  53. ),
  54. 'UUPSUnauthorizedCallContext',
  55. [],
  56. );
  57. });
  58. it('calling upgradeTo from a contract that is not an ERC1967 proxy (with the right implementation) reverts', async function () {
  59. const receipt = await this.cloneFactory.$clone(this.implUpgradeOk.address);
  60. const instance = await UUPSUpgradeableMock.at(
  61. receipt.logs.find(({ event }) => event === 'return$clone').args.instance,
  62. );
  63. await expectRevertCustomError(
  64. instance.upgradeToAndCall(this.implUpgradeUnsafe.address, '0x'),
  65. 'UUPSUnauthorizedCallContext',
  66. [],
  67. );
  68. });
  69. it('calling upgradeToAndCall from a contract that is not an ERC1967 proxy (with the right implementation) reverts', async function () {
  70. const receipt = await this.cloneFactory.$clone(this.implUpgradeOk.address);
  71. const instance = await UUPSUpgradeableMock.at(
  72. receipt.logs.find(({ event }) => event === 'return$clone').args.instance,
  73. );
  74. await expectRevertCustomError(
  75. instance.upgradeToAndCall(this.implUpgradeUnsafe.address, '0x'),
  76. 'UUPSUnauthorizedCallContext',
  77. [],
  78. );
  79. });
  80. it('rejects upgrading to an unsupported UUID', async function () {
  81. await expectRevertCustomError(
  82. this.instance.upgradeToAndCall(this.implUnsupportedUUID.address, '0x'),
  83. 'UUPSUnsupportedProxiableUUID',
  84. [web3.utils.keccak256('invalid UUID')],
  85. );
  86. });
  87. it('upgrade to and unsafe upgradeable implementation', async function () {
  88. const { receipt } = await this.instance.upgradeToAndCall(this.implUpgradeUnsafe.address, '0x');
  89. expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeUnsafe.address });
  90. expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.be.equal(this.implUpgradeUnsafe.address);
  91. });
  92. // delegate to a non existing upgradeTo function causes a low level revert
  93. it('reject upgrade to non uups implementation', async function () {
  94. await expectRevertCustomError(
  95. this.instance.upgradeToAndCall(this.implUpgradeNonUUPS.address, '0x'),
  96. 'ERC1967InvalidImplementation',
  97. [this.implUpgradeNonUUPS.address],
  98. );
  99. });
  100. it('reject proxy address as implementation', async function () {
  101. const { address } = await ERC1967Proxy.new(this.implInitial.address, '0x');
  102. const otherInstance = await UUPSUpgradeableMock.at(address);
  103. await expectRevertCustomError(
  104. this.instance.upgradeToAndCall(otherInstance.address, '0x'),
  105. 'ERC1967InvalidImplementation',
  106. [otherInstance.address],
  107. );
  108. });
  109. });