ProxyAdmin.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ImplV1 = artifacts.require('DummyImplementation');
  4. const ImplV2 = artifacts.require('DummyImplementationV2');
  5. const ProxyAdmin = artifacts.require('ProxyAdmin');
  6. const TransparentUpgradeableProxy = artifacts.require('TransparentUpgradeableProxy');
  7. const ITransparentUpgradeableProxy = artifacts.require('ITransparentUpgradeableProxy');
  8. const { getAddressInSlot, ImplementationSlot } = require('../../helpers/erc1967');
  9. const { expectRevertCustomError } = require('../../helpers/customError');
  10. const { computeCreateAddress } = require('../../helpers/create');
  11. contract('ProxyAdmin', function (accounts) {
  12. const [proxyAdminOwner, anotherAccount] = accounts;
  13. before('set implementations', async function () {
  14. this.implementationV1 = await ImplV1.new();
  15. this.implementationV2 = await ImplV2.new();
  16. });
  17. beforeEach(async function () {
  18. const initializeData = Buffer.from('');
  19. const proxy = await TransparentUpgradeableProxy.new(this.implementationV1.address, proxyAdminOwner, initializeData);
  20. const proxyNonce = await web3.eth.getTransactionCount(proxy.address);
  21. const proxyAdminAddress = computeCreateAddress(proxy.address, proxyNonce - 1); // Nonce already used
  22. this.proxyAdmin = await ProxyAdmin.at(proxyAdminAddress);
  23. this.proxy = await ITransparentUpgradeableProxy.at(proxy.address);
  24. });
  25. it('has an owner', async function () {
  26. expect(await this.proxyAdmin.owner()).to.equal(proxyAdminOwner);
  27. });
  28. describe('without data', function () {
  29. context('with unauthorized account', function () {
  30. it('fails to upgrade', async function () {
  31. await expectRevertCustomError(
  32. this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, '0x', {
  33. from: anotherAccount,
  34. }),
  35. 'OwnableUnauthorizedAccount',
  36. [anotherAccount],
  37. );
  38. });
  39. });
  40. context('with authorized account', function () {
  41. it('upgrades implementation', async function () {
  42. await this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, '0x', {
  43. from: proxyAdminOwner,
  44. });
  45. const implementationAddress = await getAddressInSlot(this.proxy, ImplementationSlot);
  46. expect(implementationAddress).to.be.equal(this.implementationV2.address);
  47. });
  48. });
  49. });
  50. describe('with data', function () {
  51. context('with unauthorized account', function () {
  52. it('fails to upgrade', async function () {
  53. const callData = new ImplV1('').contract.methods.initializeNonPayableWithValue(1337).encodeABI();
  54. await expectRevertCustomError(
  55. this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData, {
  56. from: anotherAccount,
  57. }),
  58. 'OwnableUnauthorizedAccount',
  59. [anotherAccount],
  60. );
  61. });
  62. });
  63. context('with authorized account', function () {
  64. context('with invalid callData', function () {
  65. it('fails to upgrade', async function () {
  66. const callData = '0x12345678';
  67. await expectRevert.unspecified(
  68. this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData, {
  69. from: proxyAdminOwner,
  70. }),
  71. );
  72. });
  73. });
  74. context('with valid callData', function () {
  75. it('upgrades implementation', async function () {
  76. const callData = new ImplV1('').contract.methods.initializeNonPayableWithValue(1337).encodeABI();
  77. await this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData, {
  78. from: proxyAdminOwner,
  79. });
  80. const implementationAddress = await getAddressInSlot(this.proxy, ImplementationSlot);
  81. expect(implementationAddress).to.be.equal(this.implementationV2.address);
  82. });
  83. });
  84. });
  85. });
  86. });