UUPSUpgradeable.test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { getAddressInSlot, ImplementationSlot } = require('../../helpers/storage');
  5. async function fixture() {
  6. const implInitial = await ethers.deployContract('UUPSUpgradeableMock');
  7. const implUpgradeOk = await ethers.deployContract('UUPSUpgradeableMock');
  8. const implUpgradeUnsafe = await ethers.deployContract('UUPSUpgradeableUnsafeMock');
  9. const implUpgradeNonUUPS = await ethers.deployContract('NonUpgradeableMock');
  10. const implUnsupportedUUID = await ethers.deployContract('UUPSUnsupportedProxiableUUID');
  11. // Used for testing non ERC1967 compliant proxies (clones are proxies that don't use the ERC1967 implementation slot)
  12. const cloneFactory = await ethers.deployContract('$Clones');
  13. const instance = await ethers
  14. .deployContract('ERC1967Proxy', [implInitial, '0x'])
  15. .then(proxy => implInitial.attach(proxy.target));
  16. return {
  17. implInitial,
  18. implUpgradeOk,
  19. implUpgradeUnsafe,
  20. implUpgradeNonUUPS,
  21. implUnsupportedUUID,
  22. cloneFactory,
  23. instance,
  24. };
  25. }
  26. describe('UUPSUpgradeable', function () {
  27. beforeEach(async function () {
  28. Object.assign(this, await loadFixture(fixture));
  29. });
  30. it('has an interface version', async function () {
  31. expect(await this.instance.UPGRADE_INTERFACE_VERSION()).to.equal('5.0.0');
  32. });
  33. it('upgrade to upgradeable implementation', async function () {
  34. await expect(this.instance.upgradeToAndCall(this.implUpgradeOk, '0x'))
  35. .to.emit(this.instance, 'Upgraded')
  36. .withArgs(this.implUpgradeOk);
  37. expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.equal(this.implUpgradeOk);
  38. });
  39. it('upgrade to upgradeable implementation with call', async function () {
  40. expect(await this.instance.current()).to.equal(0n);
  41. await expect(
  42. this.instance.upgradeToAndCall(this.implUpgradeOk, this.implUpgradeOk.interface.encodeFunctionData('increment')),
  43. )
  44. .to.emit(this.instance, 'Upgraded')
  45. .withArgs(this.implUpgradeOk);
  46. expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.equal(this.implUpgradeOk);
  47. expect(await this.instance.current()).to.equal(1n);
  48. });
  49. it('calling upgradeTo on the implementation reverts', async function () {
  50. await expect(this.implInitial.upgradeToAndCall(this.implUpgradeOk, '0x')).to.be.revertedWithCustomError(
  51. this.implInitial,
  52. 'UUPSUnauthorizedCallContext',
  53. );
  54. });
  55. it('calling upgradeToAndCall on the implementation reverts', async function () {
  56. await expect(
  57. this.implInitial.upgradeToAndCall(
  58. this.implUpgradeOk,
  59. this.implUpgradeOk.interface.encodeFunctionData('increment'),
  60. ),
  61. ).to.be.revertedWithCustomError(this.implUpgradeOk, 'UUPSUnauthorizedCallContext');
  62. });
  63. it('calling upgradeToAndCall from a contract that is not an ERC1967 proxy (with the right implementation) reverts', async function () {
  64. const instance = await this.cloneFactory.$clone
  65. .staticCall(this.implUpgradeOk)
  66. .then(address => this.implInitial.attach(address));
  67. await this.cloneFactory.$clone(this.implUpgradeOk);
  68. await expect(instance.upgradeToAndCall(this.implUpgradeUnsafe, '0x')).to.be.revertedWithCustomError(
  69. instance,
  70. 'UUPSUnauthorizedCallContext',
  71. );
  72. });
  73. it('rejects upgrading to an unsupported UUID', async function () {
  74. await expect(this.instance.upgradeToAndCall(this.implUnsupportedUUID, '0x'))
  75. .to.be.revertedWithCustomError(this.instance, 'UUPSUnsupportedProxiableUUID')
  76. .withArgs(ethers.id('invalid UUID'));
  77. });
  78. it('upgrade to and unsafe upgradeable implementation', async function () {
  79. await expect(this.instance.upgradeToAndCall(this.implUpgradeUnsafe, '0x'))
  80. .to.emit(this.instance, 'Upgraded')
  81. .withArgs(this.implUpgradeUnsafe);
  82. expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.equal(this.implUpgradeUnsafe);
  83. });
  84. // delegate to a non existing upgradeTo function causes a low level revert
  85. it('reject upgrade to non uups implementation', async function () {
  86. await expect(this.instance.upgradeToAndCall(this.implUpgradeNonUUPS, '0x'))
  87. .to.be.revertedWithCustomError(this.instance, 'ERC1967InvalidImplementation')
  88. .withArgs(this.implUpgradeNonUUPS);
  89. });
  90. it('reject proxy address as implementation', async function () {
  91. const otherInstance = await ethers
  92. .deployContract('ERC1967Proxy', [this.implInitial, '0x'])
  93. .then(proxy => this.implInitial.attach(proxy.target));
  94. await expect(this.instance.upgradeToAndCall(otherInstance, '0x'))
  95. .to.be.revertedWithCustomError(this.instance, 'ERC1967InvalidImplementation')
  96. .withArgs(otherInstance);
  97. });
  98. });