UUPSUpgradeable.test.js 5.6 KB

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