ProxyAdmin.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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(proxyAdminOwner);
  18. const proxy = await TransparentUpgradeableProxy.new(
  19. this.implementationV1.address,
  20. this.proxyAdmin.address,
  21. initializeData,
  22. );
  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('#changeProxyAdmin', function () {
  29. it('fails to change proxy admin if its not the proxy owner', async function () {
  30. await expectRevert(
  31. this.proxyAdmin.changeProxyAdmin(this.proxy.address, newAdmin, { from: anotherAccount }),
  32. 'caller is not the owner',
  33. );
  34. });
  35. it('changes proxy admin', async function () {
  36. await this.proxyAdmin.changeProxyAdmin(this.proxy.address, newAdmin, { from: proxyAdminOwner });
  37. const newProxyAdmin = await getAddressInSlot(this.proxy, AdminSlot);
  38. expect(newProxyAdmin).to.be.equal(newAdmin);
  39. });
  40. });
  41. describe('#upgrade', function () {
  42. context('with unauthorized account', function () {
  43. it('fails to upgrade', async function () {
  44. await expectRevert(
  45. this.proxyAdmin.upgrade(this.proxy.address, this.implementationV2.address, { from: anotherAccount }),
  46. 'caller is not the owner',
  47. );
  48. });
  49. });
  50. context('with authorized account', function () {
  51. it('upgrades implementation', async function () {
  52. await this.proxyAdmin.upgrade(this.proxy.address, this.implementationV2.address, { from: proxyAdminOwner });
  53. const implementationAddress = await getAddressInSlot(this.proxy, ImplementationSlot);
  54. expect(implementationAddress).to.be.equal(this.implementationV2.address);
  55. });
  56. });
  57. });
  58. describe('#upgradeAndCall', function () {
  59. context('with unauthorized account', function () {
  60. it('fails to upgrade', async function () {
  61. const callData = new ImplV1('').contract.methods.initializeNonPayableWithValue(1337).encodeABI();
  62. await expectRevert(
  63. this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData, {
  64. from: anotherAccount,
  65. }),
  66. 'caller is not the owner',
  67. );
  68. });
  69. });
  70. context('with authorized account', function () {
  71. context('with invalid callData', function () {
  72. it('fails to upgrade', async function () {
  73. const callData = '0x12345678';
  74. await expectRevert.unspecified(
  75. this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData, {
  76. from: proxyAdminOwner,
  77. }),
  78. );
  79. });
  80. });
  81. context('with valid callData', function () {
  82. it('upgrades implementation', async function () {
  83. const callData = new ImplV1('').contract.methods.initializeNonPayableWithValue(1337).encodeABI();
  84. await this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData, {
  85. from: proxyAdminOwner,
  86. });
  87. const implementationAddress = await getAddressInSlot(this.proxy, ImplementationSlot);
  88. expect(implementationAddress).to.be.equal(this.implementationV2.address);
  89. });
  90. });
  91. });
  92. });
  93. });