ProxyAdmin.test.js 4.1 KB

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