Migrate proxy folder to ethersjs (#4746)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: ernestognw <ernestognw@gmail.com>
pull/4775/head
Renan Souza 1 year ago committed by GitHub
parent c35057978f
commit ae69142379
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 39
      test/helpers/erc1967.js
  2. 77
      test/proxy/Clones.behaviour.js
  3. 99
      test/proxy/Clones.test.js
  4. 25
      test/proxy/ERC1967/ERC1967Proxy.test.js
  5. 138
      test/proxy/ERC1967/ERC1967Utils.test.js
  6. 112
      test/proxy/Proxy.behaviour.js
  7. 190
      test/proxy/beacon/BeaconProxy.test.js
  8. 63
      test/proxy/beacon/UpgradeableBeacon.test.js
  9. 88
      test/proxy/transparent/ProxyAdmin.test.js
  10. 421
      test/proxy/transparent/TransparentUpgradeableProxy.behaviour.js
  11. 40
      test/proxy/transparent/TransparentUpgradeableProxy.test.js
  12. 196
      test/proxy/utils/Initializable.test.js
  13. 157
      test/proxy/utils/UUPSUpgradeable.test.js

@ -5,37 +5,32 @@ const ImplementationLabel = 'eip1967.proxy.implementation';
const AdminLabel = 'eip1967.proxy.admin'; const AdminLabel = 'eip1967.proxy.admin';
const BeaconLabel = 'eip1967.proxy.beacon'; const BeaconLabel = 'eip1967.proxy.beacon';
function labelToSlot(label) { const erc1967slot = label => ethers.toBeHex(ethers.toBigInt(ethers.id(label)) - 1n);
return ethers.toBeHex(BigInt(ethers.keccak256(ethers.toUtf8Bytes(label))) - 1n); const erc7201slot = label => ethers.toBeHex(ethers.toBigInt(ethers.keccak256(erc1967slot(label))) & ~0xffn);
}
function getSlot(address, slot) { const getSlot = (address, slot) =>
return getStorageAt( (ethers.isAddressable(address) ? address.getAddress() : Promise.resolve(address)).then(address =>
ethers.isAddress(address) ? address : address.address, getStorageAt(address, ethers.isBytesLike(slot) ? slot : erc1967slot(slot)),
ethers.isBytesLike(slot) ? slot : labelToSlot(slot),
); );
}
function setSlot(address, slot, value) { const setSlot = (address, slot, value) =>
return setStorageAt( Promise.all([
ethers.isAddress(address) ? address : address.address, ethers.isAddressable(address) ? address.getAddress() : Promise.resolve(address),
ethers.isBytesLike(slot) ? slot : labelToSlot(slot), ethers.isAddressable(value) ? value.getAddress() : Promise.resolve(value),
value, ]).then(([address, value]) => setStorageAt(address, ethers.isBytesLike(slot) ? slot : erc1967slot(slot), value));
);
}
async function getAddressInSlot(address, slot) { const getAddressInSlot = (address, slot) =>
const slotValue = await getSlot(address, slot); getSlot(address, slot).then(slotValue => ethers.AbiCoder.defaultAbiCoder().decode(['address'], slotValue)[0]);
return ethers.getAddress(slotValue.substring(slotValue.length - 40));
}
module.exports = { module.exports = {
ImplementationLabel, ImplementationLabel,
AdminLabel, AdminLabel,
BeaconLabel, BeaconLabel,
ImplementationSlot: labelToSlot(ImplementationLabel), ImplementationSlot: erc1967slot(ImplementationLabel),
AdminSlot: labelToSlot(AdminLabel), AdminSlot: erc1967slot(AdminLabel),
BeaconSlot: labelToSlot(BeaconLabel), BeaconSlot: erc1967slot(BeaconLabel),
erc1967slot,
erc7201slot,
setSlot, setSlot,
getSlot, getSlot,
getAddressInSlot, getAddressInSlot,

@ -1,33 +1,29 @@
const { expectRevert } = require('@openzeppelin/test-helpers'); const { ethers } = require('hardhat');
const { expect } = require('chai'); const { expect } = require('chai');
const DummyImplementation = artifacts.require('DummyImplementation'); module.exports = function shouldBehaveLikeClone() {
module.exports = function shouldBehaveLikeClone(createClone) {
before('deploy implementation', async function () {
this.implementation = web3.utils.toChecksumAddress((await DummyImplementation.new()).address);
});
const assertProxyInitialization = function ({ value, balance }) { const assertProxyInitialization = function ({ value, balance }) {
it('initializes the proxy', async function () { it('initializes the proxy', async function () {
const dummy = new DummyImplementation(this.proxy); const dummy = await ethers.getContractAt('DummyImplementation', this.proxy);
expect(await dummy.value()).to.be.bignumber.equal(value.toString()); expect(await dummy.value()).to.equal(value);
}); });
it('has expected balance', async function () { it('has expected balance', async function () {
expect(await web3.eth.getBalance(this.proxy)).to.be.bignumber.equal(balance.toString()); expect(await ethers.provider.getBalance(this.proxy)).to.equal(balance);
}); });
}; };
describe('initialization without parameters', function () { describe('initialization without parameters', function () {
describe('non payable', function () { describe('non payable', function () {
const expectedInitializedValue = 10; const expectedInitializedValue = 10n;
const initializeData = new DummyImplementation('').contract.methods['initializeNonPayable()']().encodeABI();
beforeEach(async function () {
this.initializeData = await this.implementation.interface.encodeFunctionData('initializeNonPayable');
});
describe('when not sending balance', function () { describe('when not sending balance', function () {
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createClone(this.implementation, initializeData)).address; this.proxy = await this.createClone(this.initializeData);
}); });
assertProxyInitialization({ assertProxyInitialization({
@ -37,21 +33,24 @@ module.exports = function shouldBehaveLikeClone(createClone) {
}); });
describe('when sending some balance', function () { describe('when sending some balance', function () {
const value = 10e5; const value = 10n ** 6n;
it('reverts', async function () { it('reverts', async function () {
await expectRevert.unspecified(createClone(this.implementation, initializeData, { value })); await expect(this.createClone(this.initializeData, { value })).to.be.reverted;
}); });
}); });
}); });
describe('payable', function () { describe('payable', function () {
const expectedInitializedValue = 100; const expectedInitializedValue = 100n;
const initializeData = new DummyImplementation('').contract.methods['initializePayable()']().encodeABI();
beforeEach(async function () {
this.initializeData = await this.implementation.interface.encodeFunctionData('initializePayable');
});
describe('when not sending balance', function () { describe('when not sending balance', function () {
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createClone(this.implementation, initializeData)).address; this.proxy = await this.createClone(this.initializeData);
}); });
assertProxyInitialization({ assertProxyInitialization({
@ -61,10 +60,10 @@ module.exports = function shouldBehaveLikeClone(createClone) {
}); });
describe('when sending some balance', function () { describe('when sending some balance', function () {
const value = 10e5; const value = 10n ** 6n;
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createClone(this.implementation, initializeData, { value })).address; this.proxy = await this.createClone(this.initializeData, { value });
}); });
assertProxyInitialization({ assertProxyInitialization({
@ -77,14 +76,17 @@ module.exports = function shouldBehaveLikeClone(createClone) {
describe('initialization with parameters', function () { describe('initialization with parameters', function () {
describe('non payable', function () { describe('non payable', function () {
const expectedInitializedValue = 10; const expectedInitializedValue = 10n;
const initializeData = new DummyImplementation('').contract.methods
.initializeNonPayableWithValue(expectedInitializedValue) beforeEach(async function () {
.encodeABI(); this.initializeData = await this.implementation.interface.encodeFunctionData('initializeNonPayableWithValue', [
expectedInitializedValue,
]);
});
describe('when not sending balance', function () { describe('when not sending balance', function () {
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createClone(this.implementation, initializeData)).address; this.proxy = await this.createClone(this.initializeData);
}); });
assertProxyInitialization({ assertProxyInitialization({
@ -94,23 +96,26 @@ module.exports = function shouldBehaveLikeClone(createClone) {
}); });
describe('when sending some balance', function () { describe('when sending some balance', function () {
const value = 10e5; const value = 10n ** 6n;
it('reverts', async function () { it('reverts', async function () {
await expectRevert.unspecified(createClone(this.implementation, initializeData, { value })); await expect(this.createClone(this.initializeData, { value })).to.be.reverted;
}); });
}); });
}); });
describe('payable', function () { describe('payable', function () {
const expectedInitializedValue = 42; const expectedInitializedValue = 42n;
const initializeData = new DummyImplementation('').contract.methods
.initializePayableWithValue(expectedInitializedValue) beforeEach(function () {
.encodeABI(); this.initializeData = this.implementation.interface.encodeFunctionData('initializePayableWithValue', [
expectedInitializedValue,
]);
});
describe('when not sending balance', function () { describe('when not sending balance', function () {
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createClone(this.implementation, initializeData)).address; this.proxy = await this.createClone(this.initializeData);
}); });
assertProxyInitialization({ assertProxyInitialization({
@ -120,10 +125,10 @@ module.exports = function shouldBehaveLikeClone(createClone) {
}); });
describe('when sending some balance', function () { describe('when sending some balance', function () {
const value = 10e5; const value = 10n ** 6n;
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createClone(this.implementation, initializeData, { value })).address; this.proxy = await this.createClone(this.initializeData, { value });
}); });
assertProxyInitialization({ assertProxyInitialization({

@ -1,62 +1,87 @@
const { ethers } = require('ethers'); const { ethers } = require('hardhat');
const { expectEvent } = require('@openzeppelin/test-helpers');
const { expect } = require('chai'); const { expect } = require('chai');
const { expectRevertCustomError } = require('../helpers/customError'); const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const shouldBehaveLikeClone = require('./Clones.behaviour'); const shouldBehaveLikeClone = require('./Clones.behaviour');
const Clones = artifacts.require('$Clones'); async function fixture() {
const [deployer] = await ethers.getSigners();
contract('Clones', function (accounts) { const factory = await ethers.deployContract('$Clones');
const [deployer] = accounts; const implementation = await ethers.deployContract('DummyImplementation');
const newClone = async (initData, opts = {}) => {
const clone = await factory.$clone.staticCall(implementation).then(address => implementation.attach(address));
await factory.$clone(implementation);
await deployer.sendTransaction({ to: clone, value: opts.value ?? 0n, data: initData ?? '0x' });
return clone;
};
const newCloneDeterministic = async (initData, opts = {}) => {
const salt = opts.salt ?? ethers.randomBytes(32);
const clone = await factory.$cloneDeterministic
.staticCall(implementation, salt)
.then(address => implementation.attach(address));
await factory.$cloneDeterministic(implementation, salt);
await deployer.sendTransaction({ to: clone, value: opts.value ?? 0n, data: initData ?? '0x' });
return clone;
};
return { deployer, factory, implementation, newClone, newCloneDeterministic };
}
describe('Clones', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('clone', function () { describe('clone', function () {
shouldBehaveLikeClone(async (implementation, initData, opts = {}) => { beforeEach(async function () {
const factory = await Clones.new(); this.createClone = this.newClone;
const receipt = await factory.$clone(implementation);
const address = receipt.logs.find(({ event }) => event === 'return$clone').args.instance;
await web3.eth.sendTransaction({ from: deployer, to: address, value: opts.value, data: initData });
return { address };
}); });
shouldBehaveLikeClone();
}); });
describe('cloneDeterministic', function () { describe('cloneDeterministic', function () {
shouldBehaveLikeClone(async (implementation, initData, opts = {}) => { beforeEach(async function () {
const salt = web3.utils.randomHex(32); this.createClone = this.newCloneDeterministic;
const factory = await Clones.new();
const receipt = await factory.$cloneDeterministic(implementation, salt);
const address = receipt.logs.find(({ event }) => event === 'return$cloneDeterministic').args.instance;
await web3.eth.sendTransaction({ from: deployer, to: address, value: opts.value, data: initData });
return { address };
}); });
it('address already used', async function () { shouldBehaveLikeClone();
const implementation = web3.utils.randomHex(20);
const salt = web3.utils.randomHex(32); it('revert if address already used', async function () {
const factory = await Clones.new(); const salt = ethers.randomBytes(32);
// deploy once // deploy once
expectEvent(await factory.$cloneDeterministic(implementation, salt), 'return$cloneDeterministic'); await expect(this.factory.$cloneDeterministic(this.implementation, salt)).to.emit(
this.factory,
'return$cloneDeterministic',
);
// deploy twice // deploy twice
await expectRevertCustomError(factory.$cloneDeterministic(implementation, salt), 'ERC1167FailedCreateClone', []); await expect(this.factory.$cloneDeterministic(this.implementation, salt)).to.be.revertedWithCustomError(
this.factory,
'ERC1167FailedCreateClone',
);
}); });
it('address prediction', async function () { it('address prediction', async function () {
const implementation = web3.utils.randomHex(20); const salt = ethers.randomBytes(32);
const salt = web3.utils.randomHex(32);
const factory = await Clones.new();
const predicted = await factory.$predictDeterministicAddress(implementation, salt);
const creationCode = [ const creationCode = ethers.concat([
'0x3d602d80600a3d3981f3363d3d373d3d3d363d73', '0x3d602d80600a3d3981f3363d3d373d3d3d363d73',
implementation.replace(/0x/, '').toLowerCase(), this.implementation.target,
'5af43d82803e903d91602b57fd5bf3', '0x5af43d82803e903d91602b57fd5bf3',
].join(''); ]);
expect(ethers.getCreate2Address(factory.address, salt, ethers.keccak256(creationCode))).to.be.equal(predicted); const predicted = await this.factory.$predictDeterministicAddress(this.implementation, salt);
const expected = ethers.getCreate2Address(this.factory.target, salt, ethers.keccak256(creationCode));
expect(predicted).to.equal(expected);
expectEvent(await factory.$cloneDeterministic(implementation, salt), 'return$cloneDeterministic', { await expect(this.factory.$cloneDeterministic(this.implementation, salt))
instance: predicted, .to.emit(this.factory, 'return$cloneDeterministic')
}); .withArgs(predicted);
}); });
}); });
}); });

@ -1,12 +1,23 @@
const { ethers } = require('hardhat');
const shouldBehaveLikeProxy = require('../Proxy.behaviour'); const shouldBehaveLikeProxy = require('../Proxy.behaviour');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const fixture = async () => {
const [nonContractAddress] = await ethers.getSigners();
const implementation = await ethers.deployContract('DummyImplementation');
const createProxy = (implementation, initData, opts) =>
ethers.deployContract('ERC1967Proxy', [implementation, initData], opts);
const ERC1967Proxy = artifacts.require('ERC1967Proxy'); return { nonContractAddress, implementation, createProxy };
};
contract('ERC1967Proxy', function (accounts) { describe('ERC1967Proxy', function () {
// `undefined`, `null` and other false-ish opts will not be forwarded. beforeEach(async function () {
const createProxy = async function (implementation, initData, opts) { Object.assign(this, await loadFixture(fixture));
return ERC1967Proxy.new(implementation, initData, ...[opts].filter(Boolean)); });
};
shouldBehaveLikeProxy(createProxy, accounts); shouldBehaveLikeProxy();
}); });

@ -1,70 +1,65 @@
const { expectEvent, constants } = require('@openzeppelin/test-helpers'); const { ethers } = require('hardhat');
const { expectRevertCustomError } = require('../../helpers/customError'); const { expect } = require('chai');
const { getAddressInSlot, setSlot, ImplementationSlot, AdminSlot, BeaconSlot } = require('../../helpers/erc1967'); const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { ZERO_ADDRESS } = constants; const { getAddressInSlot, setSlot, ImplementationSlot, AdminSlot, BeaconSlot } = require('../../helpers/erc1967');
const ERC1967Utils = artifacts.require('$ERC1967Utils'); async function fixture() {
const [, admin, anotherAccount] = await ethers.getSigners();
const V1 = artifacts.require('DummyImplementation'); const utils = await ethers.deployContract('$ERC1967Utils');
const V2 = artifacts.require('CallReceiverMock'); const v1 = await ethers.deployContract('DummyImplementation');
const UpgradeableBeaconMock = artifacts.require('UpgradeableBeaconMock'); const v2 = await ethers.deployContract('CallReceiverMock');
const UpgradeableBeaconReentrantMock = artifacts.require('UpgradeableBeaconReentrantMock');
contract('ERC1967Utils', function (accounts) { return { admin, anotherAccount, utils, v1, v2 };
const [, admin, anotherAccount] = accounts; }
const EMPTY_DATA = '0x';
describe('ERC1967Utils', function () {
beforeEach('setup', async function () { beforeEach('setup', async function () {
this.utils = await ERC1967Utils.new(); Object.assign(this, await loadFixture(fixture));
this.v1 = await V1.new();
this.v2 = await V2.new();
}); });
describe('IMPLEMENTATION_SLOT', function () { describe('IMPLEMENTATION_SLOT', function () {
beforeEach('set v1 implementation', async function () { beforeEach('set v1 implementation', async function () {
await setSlot(this.utils, ImplementationSlot, this.v1.address); await setSlot(this.utils, ImplementationSlot, this.v1);
}); });
describe('getImplementation', function () { describe('getImplementation', function () {
it('returns current implementation and matches implementation slot value', async function () { it('returns current implementation and matches implementation slot value', async function () {
expect(await this.utils.$getImplementation()).to.equal(this.v1.address); expect(await this.utils.$getImplementation()).to.equal(this.v1.target);
expect(await getAddressInSlot(this.utils.address, ImplementationSlot)).to.equal(this.v1.address); expect(await getAddressInSlot(this.utils, ImplementationSlot)).to.equal(this.v1.target);
}); });
}); });
describe('upgradeToAndCall', function () { describe('upgradeToAndCall', function () {
it('sets implementation in storage and emits event', async function () { it('sets implementation in storage and emits event', async function () {
const newImplementation = this.v2.address; const newImplementation = this.v2;
const receipt = await this.utils.$upgradeToAndCall(newImplementation, EMPTY_DATA); const tx = await this.utils.$upgradeToAndCall(newImplementation, '0x');
expect(await getAddressInSlot(this.utils.address, ImplementationSlot)).to.equal(newImplementation); expect(await getAddressInSlot(this.utils, ImplementationSlot)).to.equal(newImplementation.target);
expectEvent(receipt, 'Upgraded', { implementation: newImplementation }); await expect(tx).to.emit(this.utils, 'Upgraded').withArgs(newImplementation.target);
}); });
it('reverts when implementation does not contain code', async function () { it('reverts when implementation does not contain code', async function () {
await expectRevertCustomError( await expect(this.utils.$upgradeToAndCall(this.anotherAccount, '0x'))
this.utils.$upgradeToAndCall(anotherAccount, EMPTY_DATA), .to.be.revertedWithCustomError(this.utils, 'ERC1967InvalidImplementation')
'ERC1967InvalidImplementation', .withArgs(this.anotherAccount.address);
[anotherAccount],
);
}); });
describe('when data is empty', function () { describe('when data is empty', function () {
it('reverts when value is sent', async function () { it('reverts when value is sent', async function () {
await expectRevertCustomError( await expect(this.utils.$upgradeToAndCall(this.v2, '0x', { value: 1 })).to.be.revertedWithCustomError(
this.utils.$upgradeToAndCall(this.v2.address, EMPTY_DATA, { value: 1 }), this.utils,
'ERC1967NonPayable', 'ERC1967NonPayable',
[],
); );
}); });
}); });
describe('when data is not empty', function () { describe('when data is not empty', function () {
it('delegates a call to the new implementation', async function () { it('delegates a call to the new implementation', async function () {
const initializeData = this.v2.contract.methods.mockFunction().encodeABI(); const initializeData = this.v2.interface.encodeFunctionData('mockFunction');
const receipt = await this.utils.$upgradeToAndCall(this.v2.address, initializeData); const tx = await this.utils.$upgradeToAndCall(this.v2, initializeData);
await expectEvent.inTransaction(receipt.tx, await V2.at(this.utils.address), 'MockFunctionCalled'); await expect(tx).to.emit(await ethers.getContractAt('CallReceiverMock', this.utils), 'MockFunctionCalled');
}); });
}); });
}); });
@ -72,99 +67,94 @@ contract('ERC1967Utils', function (accounts) {
describe('ADMIN_SLOT', function () { describe('ADMIN_SLOT', function () {
beforeEach('set admin', async function () { beforeEach('set admin', async function () {
await setSlot(this.utils, AdminSlot, admin); await setSlot(this.utils, AdminSlot, this.admin);
}); });
describe('getAdmin', function () { describe('getAdmin', function () {
it('returns current admin and matches admin slot value', async function () { it('returns current admin and matches admin slot value', async function () {
expect(await this.utils.$getAdmin()).to.equal(admin); expect(await this.utils.$getAdmin()).to.equal(this.admin.address);
expect(await getAddressInSlot(this.utils.address, AdminSlot)).to.equal(admin); expect(await getAddressInSlot(this.utils, AdminSlot)).to.equal(this.admin.address);
}); });
}); });
describe('changeAdmin', function () { describe('changeAdmin', function () {
it('sets admin in storage and emits event', async function () { it('sets admin in storage and emits event', async function () {
const newAdmin = anotherAccount; const newAdmin = this.anotherAccount;
const receipt = await this.utils.$changeAdmin(newAdmin); const tx = await this.utils.$changeAdmin(newAdmin);
expect(await getAddressInSlot(this.utils.address, AdminSlot)).to.equal(newAdmin); expect(await getAddressInSlot(this.utils, AdminSlot)).to.equal(newAdmin.address);
expectEvent(receipt, 'AdminChanged', { previousAdmin: admin, newAdmin: newAdmin }); await expect(tx).to.emit(this.utils, 'AdminChanged').withArgs(this.admin.address, newAdmin.address);
}); });
it('reverts when setting the address zero as admin', async function () { it('reverts when setting the address zero as admin', async function () {
await expectRevertCustomError(this.utils.$changeAdmin(ZERO_ADDRESS), 'ERC1967InvalidAdmin', [ZERO_ADDRESS]); await expect(this.utils.$changeAdmin(ethers.ZeroAddress))
.to.be.revertedWithCustomError(this.utils, 'ERC1967InvalidAdmin')
.withArgs(ethers.ZeroAddress);
}); });
}); });
}); });
describe('BEACON_SLOT', function () { describe('BEACON_SLOT', function () {
beforeEach('set beacon', async function () { beforeEach('set beacon', async function () {
this.beacon = await UpgradeableBeaconMock.new(this.v1.address); this.beacon = await ethers.deployContract('UpgradeableBeaconMock', [this.v1]);
await setSlot(this.utils, BeaconSlot, this.beacon.address); await setSlot(this.utils, BeaconSlot, this.beacon);
}); });
describe('getBeacon', function () { describe('getBeacon', function () {
it('returns current beacon and matches beacon slot value', async function () { it('returns current beacon and matches beacon slot value', async function () {
expect(await this.utils.$getBeacon()).to.equal(this.beacon.address); expect(await this.utils.$getBeacon()).to.equal(this.beacon.target);
expect(await getAddressInSlot(this.utils.address, BeaconSlot)).to.equal(this.beacon.address); expect(await getAddressInSlot(this.utils, BeaconSlot)).to.equal(this.beacon.target);
}); });
}); });
describe('upgradeBeaconToAndCall', function () { describe('upgradeBeaconToAndCall', function () {
it('sets beacon in storage and emits event', async function () { it('sets beacon in storage and emits event', async function () {
const newBeacon = await UpgradeableBeaconMock.new(this.v2.address); const newBeacon = await ethers.deployContract('UpgradeableBeaconMock', [this.v2]);
const receipt = await this.utils.$upgradeBeaconToAndCall(newBeacon.address, EMPTY_DATA); const tx = await this.utils.$upgradeBeaconToAndCall(newBeacon, '0x');
expect(await getAddressInSlot(this.utils.address, BeaconSlot)).to.equal(newBeacon.address); expect(await getAddressInSlot(this.utils, BeaconSlot)).to.equal(newBeacon.target);
expectEvent(receipt, 'BeaconUpgraded', { beacon: newBeacon.address }); await expect(tx).to.emit(this.utils, 'BeaconUpgraded').withArgs(newBeacon.target);
}); });
it('reverts when beacon does not contain code', async function () { it('reverts when beacon does not contain code', async function () {
await expectRevertCustomError( await expect(this.utils.$upgradeBeaconToAndCall(this.anotherAccount, '0x'))
this.utils.$upgradeBeaconToAndCall(anotherAccount, EMPTY_DATA), .to.be.revertedWithCustomError(this.utils, 'ERC1967InvalidBeacon')
'ERC1967InvalidBeacon', .withArgs(this.anotherAccount.address);
[anotherAccount],
);
}); });
it("reverts when beacon's implementation does not contain code", async function () { it("reverts when beacon's implementation does not contain code", async function () {
const newBeacon = await UpgradeableBeaconMock.new(anotherAccount); const newBeacon = await ethers.deployContract('UpgradeableBeaconMock', [this.anotherAccount]);
await expectRevertCustomError( await expect(this.utils.$upgradeBeaconToAndCall(newBeacon, '0x'))
this.utils.$upgradeBeaconToAndCall(newBeacon.address, EMPTY_DATA), .to.be.revertedWithCustomError(this.utils, 'ERC1967InvalidImplementation')
'ERC1967InvalidImplementation', .withArgs(this.anotherAccount.address);
[anotherAccount],
);
}); });
describe('when data is empty', function () { describe('when data is empty', function () {
it('reverts when value is sent', async function () { it('reverts when value is sent', async function () {
const newBeacon = await UpgradeableBeaconMock.new(this.v2.address); const newBeacon = await ethers.deployContract('UpgradeableBeaconMock', [this.v2]);
await expectRevertCustomError( await expect(this.utils.$upgradeBeaconToAndCall(newBeacon, '0x', { value: 1 })).to.be.revertedWithCustomError(
this.utils.$upgradeBeaconToAndCall(newBeacon.address, EMPTY_DATA, { value: 1 }), this.utils,
'ERC1967NonPayable', 'ERC1967NonPayable',
[],
); );
}); });
}); });
describe('when data is not empty', function () { describe('when data is not empty', function () {
it('delegates a call to the new implementation', async function () { it('delegates a call to the new implementation', async function () {
const initializeData = this.v2.contract.methods.mockFunction().encodeABI(); const initializeData = this.v2.interface.encodeFunctionData('mockFunction');
const newBeacon = await UpgradeableBeaconMock.new(this.v2.address); const newBeacon = await ethers.deployContract('UpgradeableBeaconMock', [this.v2]);
const receipt = await this.utils.$upgradeBeaconToAndCall(newBeacon.address, initializeData); const tx = await this.utils.$upgradeBeaconToAndCall(newBeacon, initializeData);
await expectEvent.inTransaction(receipt.tx, await V2.at(this.utils.address), 'MockFunctionCalled'); await expect(tx).to.emit(await ethers.getContractAt('CallReceiverMock', this.utils), 'MockFunctionCalled');
}); });
}); });
describe('reentrant beacon implementation() call', function () { describe('reentrant beacon implementation() call', function () {
it('sees the new beacon implementation', async function () { it('sees the new beacon implementation', async function () {
const newBeacon = await UpgradeableBeaconReentrantMock.new(); const newBeacon = await ethers.deployContract('UpgradeableBeaconReentrantMock');
await expectRevertCustomError( await expect(this.utils.$upgradeBeaconToAndCall(newBeacon, '0x'))
this.utils.$upgradeBeaconToAndCall(newBeacon.address, '0x'), .to.be.revertedWithCustomError(newBeacon, 'BeaconProxyBeaconSlotAddress')
'BeaconProxyBeaconSlotAddress', .withArgs(newBeacon.target);
[newBeacon.address],
);
}); });
}); });
}); });

@ -1,100 +1,94 @@
const { expectRevert } = require('@openzeppelin/test-helpers'); const { ethers } = require('hardhat');
const { getSlot, ImplementationSlot } = require('../helpers/erc1967');
const { expect } = require('chai'); const { expect } = require('chai');
const { expectRevertCustomError } = require('../helpers/customError');
const DummyImplementation = artifacts.require('DummyImplementation'); const { getAddressInSlot, ImplementationSlot } = require('../helpers/erc1967');
module.exports = function shouldBehaveLikeProxy(createProxy, accounts) { module.exports = function shouldBehaveLikeProxy() {
it('cannot be initialized with a non-contract address', async function () { it('cannot be initialized with a non-contract address', async function () {
const nonContractAddress = accounts[0]; const initializeData = '0x';
const initializeData = Buffer.from(''); await expect(this.createProxy(this.nonContractAddress, initializeData))
await expectRevert.unspecified(createProxy(nonContractAddress, initializeData)); .to.be.revertedWithCustomError(await ethers.getContractFactory('ERC1967Proxy'), 'ERC1967InvalidImplementation')
}); .withArgs(this.nonContractAddress.address);
before('deploy implementation', async function () {
this.implementation = web3.utils.toChecksumAddress((await DummyImplementation.new()).address);
}); });
const assertProxyInitialization = function ({ value, balance }) { const assertProxyInitialization = function ({ value, balance }) {
it('sets the implementation address', async function () { it('sets the implementation address', async function () {
const implementationSlot = await getSlot(this.proxy, ImplementationSlot); expect(await getAddressInSlot(this.proxy, ImplementationSlot)).to.equal(this.implementation.target);
const implementationAddress = web3.utils.toChecksumAddress(implementationSlot.substr(-40));
expect(implementationAddress).to.be.equal(this.implementation);
}); });
it('initializes the proxy', async function () { it('initializes the proxy', async function () {
const dummy = new DummyImplementation(this.proxy); const dummy = this.implementation.attach(this.proxy);
expect(await dummy.value()).to.be.bignumber.equal(value.toString()); expect(await dummy.value()).to.equal(value);
}); });
it('has expected balance', async function () { it('has expected balance', async function () {
expect(await web3.eth.getBalance(this.proxy)).to.be.bignumber.equal(balance.toString()); expect(await ethers.provider.getBalance(this.proxy)).to.equal(balance);
}); });
}; };
describe('without initialization', function () { describe('without initialization', function () {
const initializeData = Buffer.from(''); const initializeData = '0x';
describe('when not sending balance', function () { describe('when not sending balance', function () {
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createProxy(this.implementation, initializeData)).address; this.proxy = await this.createProxy(this.implementation, initializeData);
}); });
assertProxyInitialization({ value: 0, balance: 0 }); assertProxyInitialization({ value: 0n, balance: 0n });
}); });
describe('when sending some balance', function () { describe('when sending some balance', function () {
const value = 10e5; const value = 10n ** 5n;
it('reverts', async function () { it('reverts', async function () {
await expectRevertCustomError( await expect(this.createProxy(this.implementation, initializeData, { value })).to.be.reverted;
createProxy(this.implementation, initializeData, { value }),
'ERC1967NonPayable',
[],
);
}); });
}); });
}); });
describe('initialization without parameters', function () { describe('initialization without parameters', function () {
describe('non payable', function () { describe('non payable', function () {
const expectedInitializedValue = 10; const expectedInitializedValue = 10n;
const initializeData = new DummyImplementation('').contract.methods['initializeNonPayable()']().encodeABI();
beforeEach(function () {
this.initializeData = this.implementation.interface.encodeFunctionData('initializeNonPayable');
});
describe('when not sending balance', function () { describe('when not sending balance', function () {
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createProxy(this.implementation, initializeData)).address; this.proxy = await this.createProxy(this.implementation, this.initializeData);
}); });
assertProxyInitialization({ assertProxyInitialization({
value: expectedInitializedValue, value: expectedInitializedValue,
balance: 0, balance: 0n,
}); });
}); });
describe('when sending some balance', function () { describe('when sending some balance', function () {
const value = 10e5; const value = 10n ** 5n;
it('reverts', async function () { it('reverts', async function () {
await expectRevert.unspecified(createProxy(this.implementation, initializeData, { value })); await expect(this.createProxy(this.implementation, this.initializeData, { value })).to.be.reverted;
}); });
}); });
}); });
describe('payable', function () { describe('payable', function () {
const expectedInitializedValue = 100; const expectedInitializedValue = 100n;
const initializeData = new DummyImplementation('').contract.methods['initializePayable()']().encodeABI();
beforeEach(function () {
this.initializeData = this.implementation.interface.encodeFunctionData('initializePayable');
});
describe('when not sending balance', function () { describe('when not sending balance', function () {
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createProxy(this.implementation, initializeData)).address; this.proxy = await this.createProxy(this.implementation, this.initializeData);
}); });
assertProxyInitialization({ assertProxyInitialization({
value: expectedInitializedValue, value: expectedInitializedValue,
balance: 0, balance: 0n,
}); });
}); });
@ -102,7 +96,7 @@ module.exports = function shouldBehaveLikeProxy(createProxy, accounts) {
const value = 10e5; const value = 10e5;
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createProxy(this.implementation, initializeData, { value })).address; this.proxy = await this.createProxy(this.implementation, this.initializeData, { value });
}); });
assertProxyInitialization({ assertProxyInitialization({
@ -115,14 +109,17 @@ module.exports = function shouldBehaveLikeProxy(createProxy, accounts) {
describe('initialization with parameters', function () { describe('initialization with parameters', function () {
describe('non payable', function () { describe('non payable', function () {
const expectedInitializedValue = 10; const expectedInitializedValue = 10n;
const initializeData = new DummyImplementation('').contract.methods
.initializeNonPayableWithValue(expectedInitializedValue) beforeEach(function () {
.encodeABI(); this.initializeData = this.implementation.interface.encodeFunctionData('initializeNonPayableWithValue', [
expectedInitializedValue,
]);
});
describe('when not sending balance', function () { describe('when not sending balance', function () {
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createProxy(this.implementation, initializeData)).address; this.proxy = await this.createProxy(this.implementation, this.initializeData);
}); });
assertProxyInitialization({ assertProxyInitialization({
@ -135,33 +132,36 @@ module.exports = function shouldBehaveLikeProxy(createProxy, accounts) {
const value = 10e5; const value = 10e5;
it('reverts', async function () { it('reverts', async function () {
await expectRevert.unspecified(createProxy(this.implementation, initializeData, { value })); await expect(this.createProxy(this.implementation, this.initializeData, { value })).to.be.reverted;
}); });
}); });
}); });
describe('payable', function () { describe('payable', function () {
const expectedInitializedValue = 42; const expectedInitializedValue = 42n;
const initializeData = new DummyImplementation('').contract.methods
.initializePayableWithValue(expectedInitializedValue) beforeEach(function () {
.encodeABI(); this.initializeData = this.implementation.interface.encodeFunctionData('initializePayableWithValue', [
expectedInitializedValue,
]);
});
describe('when not sending balance', function () { describe('when not sending balance', function () {
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createProxy(this.implementation, initializeData)).address; this.proxy = await this.createProxy(this.implementation, this.initializeData);
}); });
assertProxyInitialization({ assertProxyInitialization({
value: expectedInitializedValue, value: expectedInitializedValue,
balance: 0, balance: 0n,
}); });
}); });
describe('when sending some balance', function () { describe('when sending some balance', function () {
const value = 10e5; const value = 10n ** 5n;
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
this.proxy = (await createProxy(this.implementation, initializeData, { value })).address; this.proxy = await this.createProxy(this.implementation, this.initializeData, { value });
}); });
assertProxyInitialization({ assertProxyInitialization({
@ -172,10 +172,12 @@ module.exports = function shouldBehaveLikeProxy(createProxy, accounts) {
}); });
describe('reverting initialization', function () { describe('reverting initialization', function () {
const initializeData = new DummyImplementation('').contract.methods.reverts().encodeABI(); beforeEach(function () {
this.initializeData = this.implementation.interface.encodeFunctionData('reverts');
});
it('reverts', async function () { it('reverts', async function () {
await expectRevert(createProxy(this.implementation, initializeData), 'DummyImplementation reverted'); await expect(this.createProxy(this.implementation, this.initializeData)).to.be.reverted;
}); });
}); });
}); });

@ -1,152 +1,138 @@
const { expectRevert } = require('@openzeppelin/test-helpers'); const { ethers } = require('hardhat');
const { getSlot, BeaconSlot } = require('../../helpers/erc1967'); const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { getAddressInSlot, BeaconSlot } = require('../../helpers/erc1967');
const { expectRevertCustomError } = require('../../helpers/customError'); async function fixture() {
const [admin, other] = await ethers.getSigners();
const { expect } = require('chai'); const v1 = await ethers.deployContract('DummyImplementation');
const v2 = await ethers.deployContract('DummyImplementationV2');
const factory = await ethers.getContractFactory('BeaconProxy');
const beacon = await ethers.deployContract('UpgradeableBeacon', [v1, admin]);
const newBeaconProxy = (beacon, data, opts = {}) => factory.deploy(beacon, data, opts);
const UpgradeableBeacon = artifacts.require('UpgradeableBeacon'); return { admin, other, factory, beacon, v1, v2, newBeaconProxy };
const BeaconProxy = artifacts.require('BeaconProxy'); }
const DummyImplementation = artifacts.require('DummyImplementation');
const DummyImplementationV2 = artifacts.require('DummyImplementationV2');
const BadBeaconNoImpl = artifacts.require('BadBeaconNoImpl');
const BadBeaconNotContract = artifacts.require('BadBeaconNotContract');
contract('BeaconProxy', function (accounts) { describe('BeaconProxy', function () {
const [upgradeableBeaconAdmin, anotherAccount] = accounts; beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('bad beacon is not accepted', async function () { describe('bad beacon is not accepted', async function () {
it('non-contract beacon', async function () { it('non-contract beacon', async function () {
await expectRevertCustomError(BeaconProxy.new(anotherAccount, '0x'), 'ERC1967InvalidBeacon', [anotherAccount]); const notBeacon = this.other;
await expect(this.newBeaconProxy(notBeacon, '0x'))
.to.be.revertedWithCustomError(this.factory, 'ERC1967InvalidBeacon')
.withArgs(notBeacon.address);
}); });
it('non-compliant beacon', async function () { it('non-compliant beacon', async function () {
const beacon = await BadBeaconNoImpl.new(); const badBeacon = await ethers.deployContract('BadBeaconNoImpl');
await expectRevert.unspecified(BeaconProxy.new(beacon.address, '0x'));
await expect(this.newBeaconProxy(badBeacon, '0x')).to.be.revertedWithoutReason;
}); });
it('non-contract implementation', async function () { it('non-contract implementation', async function () {
const beacon = await BadBeaconNotContract.new(); const badBeacon = await ethers.deployContract('BadBeaconNotContract');
const implementation = await beacon.implementation();
await expectRevertCustomError(BeaconProxy.new(beacon.address, '0x'), 'ERC1967InvalidImplementation', [
implementation,
]);
});
});
before('deploy implementation', async function () { await expect(this.newBeaconProxy(badBeacon, '0x'))
this.implementationV0 = await DummyImplementation.new(); .to.be.revertedWithCustomError(this.factory, 'ERC1967InvalidImplementation')
this.implementationV1 = await DummyImplementationV2.new(); .withArgs(await badBeacon.implementation());
});
}); });
describe('initialization', function () { describe('initialization', function () {
before(function () { async function assertInitialized({ value, balance }) {
this.assertInitialized = async ({ value, balance }) => { const beaconAddress = await getAddressInSlot(this.proxy, BeaconSlot);
const beaconSlot = await getSlot(this.proxy, BeaconSlot); expect(beaconAddress).to.equal(this.beacon.target);
const beaconAddress = web3.utils.toChecksumAddress(beaconSlot.substr(-40));
expect(beaconAddress).to.equal(this.beacon.address);
const dummy = new DummyImplementation(this.proxy.address); const dummy = this.v1.attach(this.proxy);
expect(await dummy.value()).to.bignumber.eq(value); expect(await dummy.value()).to.equal(value);
expect(await web3.eth.getBalance(this.proxy.address)).to.bignumber.eq(balance);
};
});
beforeEach('deploy beacon', async function () { expect(await ethers.provider.getBalance(this.proxy)).to.equal(balance);
this.beacon = await UpgradeableBeacon.new(this.implementationV0.address, upgradeableBeaconAdmin); }
});
it('no initialization', async function () { it('no initialization', async function () {
const data = Buffer.from(''); this.proxy = await this.newBeaconProxy(this.beacon, '0x');
this.proxy = await BeaconProxy.new(this.beacon.address, data); await assertInitialized.bind(this)({ value: 0n, balance: 0n });
await this.assertInitialized({ value: '0', balance: '0' });
}); });
it('non-payable initialization', async function () { it('non-payable initialization', async function () {
const value = '55'; const value = 55n;
const data = this.implementationV0.contract.methods.initializeNonPayableWithValue(value).encodeABI(); const data = this.v1.interface.encodeFunctionData('initializeNonPayableWithValue', [value]);
this.proxy = await BeaconProxy.new(this.beacon.address, data);
await this.assertInitialized({ value, balance: '0' }); this.proxy = await this.newBeaconProxy(this.beacon, data);
await assertInitialized.bind(this)({ value, balance: 0n });
}); });
it('payable initialization', async function () { it('payable initialization', async function () {
const value = '55'; const value = 55n;
const data = this.implementationV0.contract.methods.initializePayableWithValue(value).encodeABI(); const data = this.v1.interface.encodeFunctionData('initializePayableWithValue', [value]);
const balance = '100'; const balance = 100n;
this.proxy = await BeaconProxy.new(this.beacon.address, data, { value: balance });
await this.assertInitialized({ value, balance }); this.proxy = await this.newBeaconProxy(this.beacon, data, { value: balance });
await assertInitialized.bind(this)({ value, balance });
}); });
it('reverting initialization due to value', async function () { it('reverting initialization due to value', async function () {
const data = Buffer.from(''); await expect(this.newBeaconProxy(this.beacon, '0x', { value: 1n })).to.be.revertedWithCustomError(
await expectRevertCustomError( this.factory,
BeaconProxy.new(this.beacon.address, data, { value: '1' }),
'ERC1967NonPayable', 'ERC1967NonPayable',
[],
); );
}); });
it('reverting initialization function', async function () { it('reverting initialization function', async function () {
const data = this.implementationV0.contract.methods.reverts().encodeABI(); const data = this.v1.interface.encodeFunctionData('reverts');
await expectRevert(BeaconProxy.new(this.beacon.address, data), 'DummyImplementation reverted'); await expect(this.newBeaconProxy(this.beacon, data)).to.be.revertedWith('DummyImplementation reverted');
}); });
}); });
it('upgrade a proxy by upgrading its beacon', async function () { describe('upgrade', async function () {
const beacon = await UpgradeableBeacon.new(this.implementationV0.address, upgradeableBeaconAdmin); it('upgrade a proxy by upgrading its beacon', async function () {
const value = 10n;
const data = this.v1.interface.encodeFunctionData('initializeNonPayableWithValue', [value]);
const proxy = await this.newBeaconProxy(this.beacon, data).then(instance => this.v1.attach(instance));
const value = '10'; // test initial values
const data = this.implementationV0.contract.methods.initializeNonPayableWithValue(value).encodeABI(); expect(await proxy.value()).to.equal(value);
const proxy = await BeaconProxy.new(beacon.address, data);
const dummy = new DummyImplementation(proxy.address); // test initial version
expect(await proxy.version()).to.equal('V1');
// test initial values // upgrade beacon
expect(await dummy.value()).to.bignumber.eq(value); await this.beacon.connect(this.admin).upgradeTo(this.v2);
// test initial version // test upgraded version
expect(await dummy.version()).to.eq('V1'); expect(await proxy.version()).to.equal('V2');
});
// upgrade beacon
await beacon.upgradeTo(this.implementationV1.address, { from: upgradeableBeaconAdmin });
// test upgraded version
expect(await dummy.version()).to.eq('V2');
});
it('upgrade 2 proxies by upgrading shared beacon', async function () {
const value1 = '10';
const value2 = '42';
const beacon = await UpgradeableBeacon.new(this.implementationV0.address, upgradeableBeaconAdmin);
const proxy1InitializeData = this.implementationV0.contract.methods
.initializeNonPayableWithValue(value1)
.encodeABI();
const proxy1 = await BeaconProxy.new(beacon.address, proxy1InitializeData);
const proxy2InitializeData = this.implementationV0.contract.methods it('upgrade 2 proxies by upgrading shared beacon', async function () {
.initializeNonPayableWithValue(value2) const value1 = 10n;
.encodeABI(); const data1 = this.v1.interface.encodeFunctionData('initializeNonPayableWithValue', [value1]);
const proxy2 = await BeaconProxy.new(beacon.address, proxy2InitializeData); const proxy1 = await this.newBeaconProxy(this.beacon, data1).then(instance => this.v1.attach(instance));
const dummy1 = new DummyImplementation(proxy1.address); const value2 = 42n;
const dummy2 = new DummyImplementation(proxy2.address); const data2 = this.v1.interface.encodeFunctionData('initializeNonPayableWithValue', [value2]);
const proxy2 = await this.newBeaconProxy(this.beacon, data2).then(instance => this.v1.attach(instance));
// test initial values // test initial values
expect(await dummy1.value()).to.bignumber.eq(value1); expect(await proxy1.value()).to.equal(value1);
expect(await dummy2.value()).to.bignumber.eq(value2); expect(await proxy2.value()).to.equal(value2);
// test initial version // test initial version
expect(await dummy1.version()).to.eq('V1'); expect(await proxy1.version()).to.equal('V1');
expect(await dummy2.version()).to.eq('V1'); expect(await proxy2.version()).to.equal('V1');
// upgrade beacon // upgrade beacon
await beacon.upgradeTo(this.implementationV1.address, { from: upgradeableBeaconAdmin }); await this.beacon.connect(this.admin).upgradeTo(this.v2);
// test upgraded version // test upgraded version
expect(await dummy1.version()).to.eq('V2'); expect(await proxy1.version()).to.equal('V2');
expect(await dummy2.version()).to.eq('V2'); expect(await proxy2.version()).to.equal('V2');
});
}); });
}); });

@ -1,54 +1,55 @@
const { expectEvent } = require('@openzeppelin/test-helpers'); const { ethers } = require('hardhat');
const { expect } = require('chai'); const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { expectRevertCustomError } = require('../../helpers/customError'); async function fixture() {
const [admin, other] = await ethers.getSigners();
const UpgradeableBeacon = artifacts.require('UpgradeableBeacon'); const v1 = await ethers.deployContract('Implementation1');
const Implementation1 = artifacts.require('Implementation1'); const v2 = await ethers.deployContract('Implementation2');
const Implementation2 = artifacts.require('Implementation2'); const beacon = await ethers.deployContract('UpgradeableBeacon', [v1, admin]);
contract('UpgradeableBeacon', function (accounts) { return { admin, other, beacon, v1, v2 };
const [owner, other] = accounts; }
it('cannot be created with non-contract implementation', async function () { describe('UpgradeableBeacon', function () {
await expectRevertCustomError(UpgradeableBeacon.new(other, owner), 'BeaconInvalidImplementation', [other]); beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
}); });
context('once deployed', async function () { it('cannot be created with non-contract implementation', async function () {
beforeEach('deploying beacon', async function () { await expect(ethers.deployContract('UpgradeableBeacon', [this.other, this.admin]))
this.v1 = await Implementation1.new(); .to.be.revertedWithCustomError(this.beacon, 'BeaconInvalidImplementation')
this.beacon = await UpgradeableBeacon.new(this.v1.address, owner); .withArgs(this.other.address);
}); });
describe('once deployed', async function () {
it('emits Upgraded event to the first implementation', async function () { it('emits Upgraded event to the first implementation', async function () {
const beacon = await UpgradeableBeacon.new(this.v1.address, owner); await expect(this.beacon.deploymentTransaction()).to.emit(this.beacon, 'Upgraded').withArgs(this.v1.target);
await expectEvent.inTransaction(beacon.contract.transactionHash, beacon, 'Upgraded', {
implementation: this.v1.address,
});
}); });
it('returns implementation', async function () { it('returns implementation', async function () {
expect(await this.beacon.implementation()).to.equal(this.v1.address); expect(await this.beacon.implementation()).to.equal(this.v1.target);
}); });
it('can be upgraded by the owner', async function () { it('can be upgraded by the admin', async function () {
const v2 = await Implementation2.new(); await expect(this.beacon.connect(this.admin).upgradeTo(this.v2))
const receipt = await this.beacon.upgradeTo(v2.address, { from: owner }); .to.emit(this.beacon, 'Upgraded')
expectEvent(receipt, 'Upgraded', { implementation: v2.address }); .withArgs(this.v2.target);
expect(await this.beacon.implementation()).to.equal(v2.address);
expect(await this.beacon.implementation()).to.equal(this.v2.target);
}); });
it('cannot be upgraded to a non-contract', async function () { it('cannot be upgraded to a non-contract', async function () {
await expectRevertCustomError(this.beacon.upgradeTo(other, { from: owner }), 'BeaconInvalidImplementation', [ await expect(this.beacon.connect(this.admin).upgradeTo(this.other))
other, .to.be.revertedWithCustomError(this.beacon, 'BeaconInvalidImplementation')
]); .withArgs(this.other.address);
}); });
it('cannot be upgraded by other account', async function () { it('cannot be upgraded by other account', async function () {
const v2 = await Implementation2.new(); await expect(this.beacon.connect(this.other).upgradeTo(this.v2))
await expectRevertCustomError(this.beacon.upgradeTo(v2.address, { from: other }), 'OwnableUnauthorizedAccount', [ .to.be.revertedWithCustomError(this.beacon, 'OwnableUnauthorizedAccount')
other, .withArgs(this.other.address);
]);
}); });
}); });
}); });

@ -1,36 +1,33 @@
const { ethers } = require('hardhat'); const { ethers } = require('hardhat');
const { expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai'); const { expect } = require('chai');
const ImplV1 = artifacts.require('DummyImplementation'); const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const ImplV2 = artifacts.require('DummyImplementationV2');
const ProxyAdmin = artifacts.require('ProxyAdmin');
const TransparentUpgradeableProxy = artifacts.require('TransparentUpgradeableProxy');
const ITransparentUpgradeableProxy = artifacts.require('ITransparentUpgradeableProxy');
const { getAddressInSlot, ImplementationSlot } = require('../../helpers/erc1967'); const { getAddressInSlot, ImplementationSlot } = require('../../helpers/erc1967');
const { expectRevertCustomError } = require('../../helpers/customError');
contract('ProxyAdmin', function (accounts) { async function fixture() {
const [proxyAdminOwner, anotherAccount] = accounts; const [admin, other] = await ethers.getSigners();
before('set implementations', async function () { const v1 = await ethers.deployContract('DummyImplementation');
this.implementationV1 = await ImplV1.new(); const v2 = await ethers.deployContract('DummyImplementationV2');
this.implementationV2 = await ImplV2.new();
});
beforeEach(async function () { const proxy = await ethers
const initializeData = Buffer.from(''); .deployContract('TransparentUpgradeableProxy', [v1, admin, '0x'])
const proxy = await TransparentUpgradeableProxy.new(this.implementationV1.address, proxyAdminOwner, initializeData); .then(instance => ethers.getContractAt('ITransparentUpgradeableProxy', instance));
const proxyAdmin = await ethers.getContractAt(
'ProxyAdmin',
ethers.getCreateAddress({ from: proxy.target, nonce: 1n }),
);
const proxyNonce = await web3.eth.getTransactionCount(proxy.address); return { admin, other, v1, v2, proxy, proxyAdmin };
const proxyAdminAddress = ethers.getCreateAddress({ from: proxy.address, nonce: proxyNonce - 1 }); // Nonce already used }
this.proxyAdmin = await ProxyAdmin.at(proxyAdminAddress);
this.proxy = await ITransparentUpgradeableProxy.at(proxy.address); describe('ProxyAdmin', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
}); });
it('has an owner', async function () { it('has an owner', async function () {
expect(await this.proxyAdmin.owner()).to.equal(proxyAdminOwner); expect(await this.proxyAdmin.owner()).to.equal(this.admin.address);
}); });
it('has an interface version', async function () { it('has an interface version', async function () {
@ -40,24 +37,16 @@ contract('ProxyAdmin', function (accounts) {
describe('without data', function () { describe('without data', function () {
context('with unauthorized account', function () { context('with unauthorized account', function () {
it('fails to upgrade', async function () { it('fails to upgrade', async function () {
await expectRevertCustomError( await expect(this.proxyAdmin.connect(this.other).upgradeAndCall(this.proxy, this.v2, '0x'))
this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, '0x', { .to.be.revertedWithCustomError(this.proxyAdmin, 'OwnableUnauthorizedAccount')
from: anotherAccount, .withArgs(this.other.address);
}),
'OwnableUnauthorizedAccount',
[anotherAccount],
);
}); });
}); });
context('with authorized account', function () { context('with authorized account', function () {
it('upgrades implementation', async function () { it('upgrades implementation', async function () {
await this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, '0x', { await this.proxyAdmin.connect(this.admin).upgradeAndCall(this.proxy, this.v2, '0x');
from: proxyAdminOwner, expect(await getAddressInSlot(this.proxy, ImplementationSlot)).to.be.equal(this.v2.target);
});
const implementationAddress = await getAddressInSlot(this.proxy, ImplementationSlot);
expect(implementationAddress).to.be.equal(this.implementationV2.address);
}); });
}); });
}); });
@ -65,37 +54,26 @@ contract('ProxyAdmin', function (accounts) {
describe('with data', function () { describe('with data', function () {
context('with unauthorized account', function () { context('with unauthorized account', function () {
it('fails to upgrade', async function () { it('fails to upgrade', async function () {
const callData = new ImplV1('').contract.methods.initializeNonPayableWithValue(1337).encodeABI(); const data = this.v1.interface.encodeFunctionData('initializeNonPayableWithValue', [1337n]);
await expectRevertCustomError( await expect(this.proxyAdmin.connect(this.other).upgradeAndCall(this.proxy, this.v2, data))
this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData, { .to.be.revertedWithCustomError(this.proxyAdmin, 'OwnableUnauthorizedAccount')
from: anotherAccount, .withArgs(this.other.address);
}),
'OwnableUnauthorizedAccount',
[anotherAccount],
);
}); });
}); });
context('with authorized account', function () { context('with authorized account', function () {
context('with invalid callData', function () { context('with invalid callData', function () {
it('fails to upgrade', async function () { it('fails to upgrade', async function () {
const callData = '0x12345678'; const data = '0x12345678';
await expectRevert.unspecified( await expect(this.proxyAdmin.connect(this.admin).upgradeAndCall(this.proxy, this.v2, data)).to.be.reverted;
this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData, {
from: proxyAdminOwner,
}),
);
}); });
}); });
context('with valid callData', function () { context('with valid callData', function () {
it('upgrades implementation', async function () { it('upgrades implementation', async function () {
const callData = new ImplV1('').contract.methods.initializeNonPayableWithValue(1337).encodeABI(); const data = this.v2.interface.encodeFunctionData('initializeNonPayableWithValue', [1337n]);
await this.proxyAdmin.upgradeAndCall(this.proxy.address, this.implementationV2.address, callData, { await this.proxyAdmin.connect(this.admin).upgradeAndCall(this.proxy, this.v2, data);
from: proxyAdminOwner, expect(await getAddressInSlot(this.proxy, ImplementationSlot)).to.be.equal(this.v2.target);
});
const implementationAddress = await getAddressInSlot(this.proxy, ImplementationSlot);
expect(implementationAddress).to.be.equal(this.implementationV2.address);
}); });
}); });
}); });

@ -1,261 +1,223 @@
const { BN, expectRevert, expectEvent, constants } = require('@openzeppelin/test-helpers'); const { ethers } = require('hardhat');
const { ZERO_ADDRESS } = constants;
const { getAddressInSlot, ImplementationSlot, AdminSlot } = require('../../helpers/erc1967');
const { expectRevertCustomError } = require('../../helpers/customError');
const { expect } = require('chai'); const { expect } = require('chai');
const { ethers, web3 } = require('hardhat');
const { getAddressInSlot, ImplementationSlot, AdminSlot } = require('../../helpers/erc1967');
const { impersonate } = require('../../helpers/account'); const { impersonate } = require('../../helpers/account');
const Implementation1 = artifacts.require('Implementation1'); // createProxy, initialOwner, accounts
const Implementation2 = artifacts.require('Implementation2'); module.exports = function shouldBehaveLikeTransparentUpgradeableProxy() {
const Implementation3 = artifacts.require('Implementation3'); before(async function () {
const Implementation4 = artifacts.require('Implementation4'); const implementationV0 = await ethers.deployContract('DummyImplementation');
const MigratableMockV1 = artifacts.require('MigratableMockV1'); const implementationV1 = await ethers.deployContract('DummyImplementation');
const MigratableMockV2 = artifacts.require('MigratableMockV2');
const MigratableMockV3 = artifacts.require('MigratableMockV3'); const createProxyWithImpersonatedProxyAdmin = async (logic, initData, opts = undefined) => {
const InitializableMock = artifacts.require('InitializableMock'); const [proxy, tx] = await this.createProxy(logic, initData, opts).then(instance =>
const DummyImplementation = artifacts.require('DummyImplementation'); Promise.all([ethers.getContractAt('ITransparentUpgradeableProxy', instance), instance.deploymentTransaction()]),
const ClashingImplementation = artifacts.require('ClashingImplementation'); );
const Ownable = artifacts.require('Ownable');
const proxyAdmin = await ethers.getContractAt(
module.exports = function shouldBehaveLikeTransparentUpgradeableProxy(createProxy, initialOwner, accounts) { 'ProxyAdmin',
const [anotherAccount] = accounts; ethers.getCreateAddress({ from: proxy.target, nonce: 1n }),
);
async function createProxyWithImpersonatedProxyAdmin(logic, initData, opts = undefined) { const proxyAdminAsSigner = await proxyAdmin.getAddress().then(impersonate);
const proxy = await createProxy(logic, initData, opts);
return {
// Expect proxy admin to be the first and only contract created by the proxy instance: logic.attach(proxy.target), // attaching proxy directly works well for everything except for event resolution
const proxyAdminAddress = ethers.getCreateAddress({ from: proxy.address, nonce: 1 }); proxy,
await impersonate(proxyAdminAddress); proxyAdmin,
proxyAdminAsSigner,
return { tx,
proxy, };
proxyAdminAddress,
}; };
}
before(async function () { Object.assign(this, {
this.implementationV0 = (await DummyImplementation.new()).address; implementationV0,
this.implementationV1 = (await DummyImplementation.new()).address; implementationV1,
createProxyWithImpersonatedProxyAdmin,
});
}); });
beforeEach(async function () { beforeEach(async function () {
const initializeData = Buffer.from(''); Object.assign(this, await this.createProxyWithImpersonatedProxyAdmin(this.implementationV0, '0x'));
const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin(
this.implementationV0,
initializeData,
);
this.proxy = proxy;
this.proxyAdminAddress = proxyAdminAddress;
}); });
describe('implementation', function () { describe('implementation', function () {
it('returns the current implementation address', async function () { it('returns the current implementation address', async function () {
const implementationAddress = await getAddressInSlot(this.proxy, ImplementationSlot); expect(await getAddressInSlot(this.proxy, ImplementationSlot)).to.equal(this.implementationV0.target);
expect(implementationAddress).to.be.equal(this.implementationV0);
}); });
it('delegates to the implementation', async function () { it('delegates to the implementation', async function () {
const dummy = new DummyImplementation(this.proxy.address); expect(await this.instance.get()).to.be.true;
const value = await dummy.get();
expect(value).to.equal(true);
}); });
}); });
describe('proxy admin', function () { describe('proxy admin', function () {
it('emits AdminChanged event during construction', async function () { it('emits AdminChanged event during construction', async function () {
await expectEvent.inConstruction(this.proxy, 'AdminChanged', { await expect(this.tx).to.emit(this.proxy, 'AdminChanged').withArgs(ethers.ZeroAddress, this.proxyAdmin.target);
previousAdmin: ZERO_ADDRESS,
newAdmin: this.proxyAdminAddress,
});
}); });
it('sets the proxy admin in storage with the correct initial owner', async function () { it('sets the proxy admin in storage with the correct initial owner', async function () {
expect(await getAddressInSlot(this.proxy, AdminSlot)).to.be.equal(this.proxyAdminAddress); expect(await getAddressInSlot(this.proxy, AdminSlot)).to.equal(this.proxyAdmin.target);
const proxyAdmin = await Ownable.at(this.proxyAdminAddress);
expect(await proxyAdmin.owner()).to.be.equal(initialOwner); expect(await this.proxyAdmin.owner()).to.equal(this.owner.address);
}); });
it('can overwrite the admin by the implementation', async function () { it('can overwrite the admin by the implementation', async function () {
const dummy = new DummyImplementation(this.proxy.address); await this.instance.unsafeOverrideAdmin(this.other);
await dummy.unsafeOverrideAdmin(anotherAccount);
const ERC1967AdminSlotValue = await getAddressInSlot(this.proxy, AdminSlot); const ERC1967AdminSlotValue = await getAddressInSlot(this.proxy, AdminSlot);
expect(ERC1967AdminSlotValue).to.be.equal(anotherAccount); expect(ERC1967AdminSlotValue).to.equal(this.other.address);
expect(ERC1967AdminSlotValue).to.not.equal(this.proxyAdmin.address);
// Still allows previous admin to execute admin operations // Still allows previous admin to execute admin operations
expect(ERC1967AdminSlotValue).to.not.equal(this.proxyAdminAddress); await expect(this.proxy.connect(this.proxyAdminAsSigner).upgradeToAndCall(this.implementationV1, '0x'))
expectEvent( .to.emit(this.proxy, 'Upgraded')
await this.proxy.upgradeToAndCall(this.implementationV1, '0x', { from: this.proxyAdminAddress }), .withArgs(this.implementationV1.target);
'Upgraded',
{
implementation: this.implementationV1,
},
);
}); });
}); });
describe('upgradeToAndCall', function () { describe('upgradeToAndCall', function () {
describe('without migrations', function () { describe('without migrations', function () {
beforeEach(async function () { beforeEach(async function () {
this.behavior = await InitializableMock.new(); this.behavior = await ethers.deployContract('InitializableMock');
}); });
describe('when the call does not fail', function () { describe('when the call does not fail', function () {
const initializeData = new InitializableMock('').contract.methods['initializeWithX(uint256)'](42).encodeABI(); beforeEach(function () {
this.initializeData = this.behavior.interface.encodeFunctionData('initializeWithX', [42n]);
});
describe('when the sender is the admin', function () { describe('when the sender is the admin', function () {
const value = 1e5; const value = 10n ** 5n;
beforeEach(async function () { beforeEach(async function () {
this.receipt = await this.proxy.upgradeToAndCall(this.behavior.address, initializeData, { this.tx = await this.proxy
from: this.proxyAdminAddress, .connect(this.proxyAdminAsSigner)
value, .upgradeToAndCall(this.behavior, this.initializeData, {
}); value,
});
}); });
it('upgrades to the requested implementation', async function () { it('upgrades to the requested implementation', async function () {
const implementationAddress = await getAddressInSlot(this.proxy, ImplementationSlot); expect(await getAddressInSlot(this.proxy, ImplementationSlot)).to.equal(this.behavior.target);
expect(implementationAddress).to.be.equal(this.behavior.address);
}); });
it('emits an event', function () { it('emits an event', async function () {
expectEvent(this.receipt, 'Upgraded', { implementation: this.behavior.address }); await expect(this.tx).to.emit(this.proxy, 'Upgraded').withArgs(this.behavior.target);
}); });
it('calls the initializer function', async function () { it('calls the initializer function', async function () {
const migratable = new InitializableMock(this.proxy.address); expect(await this.behavior.attach(this.proxy).x()).to.equal(42n);
const x = await migratable.x();
expect(x).to.be.bignumber.equal('42');
}); });
it('sends given value to the proxy', async function () { it('sends given value to the proxy', async function () {
const balance = await web3.eth.getBalance(this.proxy.address); expect(await ethers.provider.getBalance(this.proxy)).to.equal(value);
expect(balance.toString()).to.be.bignumber.equal(value.toString());
}); });
it('uses the storage of the proxy', async function () { it('uses the storage of the proxy', async function () {
// storage layout should look as follows: // storage layout should look as follows:
// - 0: Initializable storage ++ initializerRan ++ onlyInitializingRan // - 0: Initializable storage ++ initializerRan ++ onlyInitializingRan
// - 1: x // - 1: x
const storedValue = await web3.eth.getStorageAt(this.proxy.address, 1); expect(await ethers.provider.getStorage(this.proxy, 1n)).to.equal(42n);
expect(parseInt(storedValue)).to.eq(42);
}); });
}); });
describe('when the sender is not the admin', function () { describe('when the sender is not the admin', function () {
it('reverts', async function () { it('reverts', async function () {
await expectRevert.unspecified( await expect(this.proxy.connect(this.other).upgradeToAndCall(this.behavior, this.initializeData)).to.be
this.proxy.upgradeToAndCall(this.behavior.address, initializeData, { from: anotherAccount }), .reverted;
);
}); });
}); });
}); });
describe('when the call does fail', function () { describe('when the call does fail', function () {
const initializeData = new InitializableMock('').contract.methods.fail().encodeABI(); beforeEach(function () {
this.initializeData = this.behavior.interface.encodeFunctionData('fail');
});
it('reverts', async function () { it('reverts', async function () {
await expectRevert.unspecified( await expect(this.proxy.connect(this.proxyAdminAsSigner).upgradeToAndCall(this.behavior, this.initializeData))
this.proxy.upgradeToAndCall(this.behavior.address, initializeData, { from: this.proxyAdminAddress }), .to.be.reverted;
);
}); });
}); });
}); });
describe('with migrations', function () { describe('with migrations', function () {
describe('when the sender is the admin', function () { describe('when the sender is the admin', function () {
const value = 1e5; const value = 10n ** 5n;
describe('when upgrading to V1', function () { describe('when upgrading to V1', function () {
const v1MigrationData = new MigratableMockV1('').contract.methods.initialize(42).encodeABI();
beforeEach(async function () { beforeEach(async function () {
this.behaviorV1 = await MigratableMockV1.new(); this.behaviorV1 = await ethers.deployContract('MigratableMockV1');
this.balancePreviousV1 = new BN(await web3.eth.getBalance(this.proxy.address)); const v1MigrationData = this.behaviorV1.interface.encodeFunctionData('initialize', [42n]);
this.receipt = await this.proxy.upgradeToAndCall(this.behaviorV1.address, v1MigrationData, {
from: this.proxyAdminAddress, this.balancePreviousV1 = await ethers.provider.getBalance(this.proxy);
value, this.tx = await this.proxy
}); .connect(this.proxyAdminAsSigner)
.upgradeToAndCall(this.behaviorV1, v1MigrationData, {
value,
});
}); });
it('upgrades to the requested version and emits an event', async function () { it('upgrades to the requested version and emits an event', async function () {
const implementation = await getAddressInSlot(this.proxy, ImplementationSlot); expect(await getAddressInSlot(this.proxy, ImplementationSlot)).to.equal(this.behaviorV1.target);
expect(implementation).to.be.equal(this.behaviorV1.address);
expectEvent(this.receipt, 'Upgraded', { implementation: this.behaviorV1.address }); await expect(this.tx).to.emit(this.proxy, 'Upgraded').withArgs(this.behaviorV1.target);
}); });
it("calls the 'initialize' function and sends given value to the proxy", async function () { it("calls the 'initialize' function and sends given value to the proxy", async function () {
const migratable = new MigratableMockV1(this.proxy.address); expect(await this.behaviorV1.attach(this.proxy).x()).to.equal(42n);
expect(await ethers.provider.getBalance(this.proxy)).to.equal(this.balancePreviousV1 + value);
const x = await migratable.x();
expect(x).to.be.bignumber.equal('42');
const balance = await web3.eth.getBalance(this.proxy.address);
expect(new BN(balance)).to.be.bignumber.equal(this.balancePreviousV1.addn(value));
}); });
describe('when upgrading to V2', function () { describe('when upgrading to V2', function () {
const v2MigrationData = new MigratableMockV2('').contract.methods.migrate(10, 42).encodeABI();
beforeEach(async function () { beforeEach(async function () {
this.behaviorV2 = await MigratableMockV2.new(); this.behaviorV2 = await ethers.deployContract('MigratableMockV2');
this.balancePreviousV2 = new BN(await web3.eth.getBalance(this.proxy.address)); const v2MigrationData = this.behaviorV2.interface.encodeFunctionData('migrate', [10n, 42n]);
this.receipt = await this.proxy.upgradeToAndCall(this.behaviorV2.address, v2MigrationData, {
from: this.proxyAdminAddress, this.balancePreviousV2 = await ethers.provider.getBalance(this.proxy);
value, this.tx = await this.proxy
}); .connect(this.proxyAdminAsSigner)
.upgradeToAndCall(this.behaviorV2, v2MigrationData, {
value,
});
}); });
it('upgrades to the requested version and emits an event', async function () { it('upgrades to the requested version and emits an event', async function () {
const implementation = await getAddressInSlot(this.proxy, ImplementationSlot); expect(await getAddressInSlot(this.proxy, ImplementationSlot)).to.equal(this.behaviorV2.target);
expect(implementation).to.be.equal(this.behaviorV2.address);
expectEvent(this.receipt, 'Upgraded', { implementation: this.behaviorV2.address }); await expect(this.tx).to.emit(this.proxy, 'Upgraded').withArgs(this.behaviorV2.target);
}); });
it("calls the 'migrate' function and sends given value to the proxy", async function () { it("calls the 'migrate' function and sends given value to the proxy", async function () {
const migratable = new MigratableMockV2(this.proxy.address); expect(await this.behaviorV2.attach(this.proxy).x()).to.equal(10n);
expect(await this.behaviorV2.attach(this.proxy).y()).to.equal(42n);
const x = await migratable.x(); expect(await ethers.provider.getBalance(this.proxy)).to.equal(this.balancePreviousV2 + value);
expect(x).to.be.bignumber.equal('10');
const y = await migratable.y();
expect(y).to.be.bignumber.equal('42');
const balance = new BN(await web3.eth.getBalance(this.proxy.address));
expect(balance).to.be.bignumber.equal(this.balancePreviousV2.addn(value));
}); });
describe('when upgrading to V3', function () { describe('when upgrading to V3', function () {
const v3MigrationData = new MigratableMockV3('').contract.methods['migrate()']().encodeABI();
beforeEach(async function () { beforeEach(async function () {
this.behaviorV3 = await MigratableMockV3.new(); this.behaviorV3 = await ethers.deployContract('MigratableMockV3');
this.balancePreviousV3 = new BN(await web3.eth.getBalance(this.proxy.address)); const v3MigrationData = this.behaviorV3.interface.encodeFunctionData('migrate()');
this.receipt = await this.proxy.upgradeToAndCall(this.behaviorV3.address, v3MigrationData, {
from: this.proxyAdminAddress, this.balancePreviousV3 = await ethers.provider.getBalance(this.proxy);
value, this.tx = await this.proxy
}); .connect(this.proxyAdminAsSigner)
.upgradeToAndCall(this.behaviorV3, v3MigrationData, {
value,
});
}); });
it('upgrades to the requested version and emits an event', async function () { it('upgrades to the requested version and emits an event', async function () {
const implementation = await getAddressInSlot(this.proxy, ImplementationSlot); expect(await getAddressInSlot(this.proxy, ImplementationSlot)).to.equal(this.behaviorV3.target);
expect(implementation).to.be.equal(this.behaviorV3.address);
expectEvent(this.receipt, 'Upgraded', { implementation: this.behaviorV3.address }); await expect(this.tx).to.emit(this.proxy, 'Upgraded').withArgs(this.behaviorV3.target);
}); });
it("calls the 'migrate' function and sends given value to the proxy", async function () { it("calls the 'migrate' function and sends given value to the proxy", async function () {
const migratable = new MigratableMockV3(this.proxy.address); expect(await this.behaviorV3.attach(this.proxy).x()).to.equal(42n);
expect(await this.behaviorV3.attach(this.proxy).y()).to.equal(10n);
const x = await migratable.x(); expect(await ethers.provider.getBalance(this.proxy)).to.equal(this.balancePreviousV3 + value);
expect(x).to.be.bignumber.equal('42');
const y = await migratable.y();
expect(y).to.be.bignumber.equal('10');
const balance = new BN(await web3.eth.getBalance(this.proxy.address));
expect(balance).to.be.bignumber.equal(this.balancePreviousV3.addn(value));
}); });
}); });
}); });
@ -263,12 +225,10 @@ module.exports = function shouldBehaveLikeTransparentUpgradeableProxy(createProx
}); });
describe('when the sender is not the admin', function () { describe('when the sender is not the admin', function () {
const from = anotherAccount;
it('reverts', async function () { it('reverts', async function () {
const behaviorV1 = await MigratableMockV1.new(); const behaviorV1 = await ethers.deployContract('MigratableMockV1');
const v1MigrationData = new MigratableMockV1('').contract.methods.initialize(42).encodeABI(); const v1MigrationData = behaviorV1.interface.encodeFunctionData('initialize', [42n]);
await expectRevert.unspecified(this.proxy.upgradeToAndCall(behaviorV1.address, v1MigrationData, { from })); await expect(this.proxy.connect(this.other).upgradeToAndCall(behaviorV1, v1MigrationData)).to.be.reverted;
}); });
}); });
}); });
@ -276,137 +236,122 @@ module.exports = function shouldBehaveLikeTransparentUpgradeableProxy(createProx
describe('transparent proxy', function () { describe('transparent proxy', function () {
beforeEach('creating proxy', async function () { beforeEach('creating proxy', async function () {
const initializeData = Buffer.from(''); this.clashingImplV0 = await ethers.deployContract('ClashingImplementation');
this.clashingImplV0 = (await ClashingImplementation.new()).address; this.clashingImplV1 = await ethers.deployContract('ClashingImplementation');
this.clashingImplV1 = (await ClashingImplementation.new()).address;
const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin( Object.assign(this, await this.createProxyWithImpersonatedProxyAdmin(this.clashingImplV0, '0x'));
this.clashingImplV0,
initializeData,
);
this.proxy = proxy;
this.proxyAdminAddress = proxyAdminAddress;
this.clashing = new ClashingImplementation(this.proxy.address);
}); });
it('proxy admin cannot call delegated functions', async function () { it('proxy admin cannot call delegated functions', async function () {
await expectRevertCustomError( const interface = await ethers.getContractFactory('TransparentUpgradeableProxy');
this.clashing.delegatedFunction({ from: this.proxyAdminAddress }),
await expect(this.instance.connect(this.proxyAdminAsSigner).delegatedFunction()).to.be.revertedWithCustomError(
interface,
'ProxyDeniedAdminAccess', 'ProxyDeniedAdminAccess',
[],
); );
}); });
describe('when function names clash', function () { describe('when function names clash', function () {
it('executes the proxy function if the sender is the admin', async function () { it('executes the proxy function if the sender is the admin', async function () {
const receipt = await this.proxy.upgradeToAndCall(this.clashingImplV1, '0x', { await expect(this.proxy.connect(this.proxyAdminAsSigner).upgradeToAndCall(this.clashingImplV1, '0x'))
from: this.proxyAdminAddress, .to.emit(this.proxy, 'Upgraded')
}); .withArgs(this.clashingImplV1.target);
expectEvent(receipt, 'Upgraded', { implementation: this.clashingImplV1 });
}); });
it('delegates the call to implementation when sender is not the admin', async function () { it('delegates the call to implementation when sender is not the admin', async function () {
const receipt = await this.proxy.upgradeToAndCall(this.clashingImplV1, '0x', { await expect(this.proxy.connect(this.other).upgradeToAndCall(this.clashingImplV1, '0x'))
from: anotherAccount, .to.emit(this.instance, 'ClashingImplementationCall')
}); .to.not.emit(this.proxy, 'Upgraded');
expectEvent.notEmitted(receipt, 'Upgraded');
expectEvent.inTransaction(receipt.tx, this.clashing, 'ClashingImplementationCall');
}); });
}); });
}); });
describe('regression', () => { describe('regression', function () {
const initializeData = Buffer.from(''); const initializeData = '0x';
it('should add new function', async () => { it('should add new function', async function () {
const instance1 = await Implementation1.new(); const impl1 = await ethers.deployContract('Implementation1');
const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin( const impl2 = await ethers.deployContract('Implementation2');
instance1.address, const { instance, proxy, proxyAdminAsSigner } = await this.createProxyWithImpersonatedProxyAdmin(
impl1,
initializeData, initializeData,
); );
const proxyInstance1 = new Implementation1(proxy.address); await instance.setValue(42n);
await proxyInstance1.setValue(42);
// `getValue` is not available in impl1
await expect(impl2.attach(instance).getValue()).to.be.reverted;
const instance2 = await Implementation2.new(); // do upgrade
await proxy.upgradeToAndCall(instance2.address, '0x', { from: proxyAdminAddress }); await proxy.connect(proxyAdminAsSigner).upgradeToAndCall(impl2, '0x');
const proxyInstance2 = new Implementation2(proxy.address); // `getValue` is available in impl2
const res = await proxyInstance2.getValue(); expect(await impl2.attach(instance).getValue()).to.equal(42n);
expect(res.toString()).to.eq('42');
}); });
it('should remove function', async () => { it('should remove function', async function () {
const instance2 = await Implementation2.new(); const impl1 = await ethers.deployContract('Implementation1');
const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin( const impl2 = await ethers.deployContract('Implementation2');
instance2.address, const { instance, proxy, proxyAdminAsSigner } = await this.createProxyWithImpersonatedProxyAdmin(
impl2,
initializeData, initializeData,
); );
const proxyInstance2 = new Implementation2(proxy.address); await instance.setValue(42n);
await proxyInstance2.setValue(42);
const res = await proxyInstance2.getValue(); // `getValue` is available in impl2
expect(res.toString()).to.eq('42'); expect(await impl2.attach(instance).getValue()).to.equal(42n);
const instance1 = await Implementation1.new(); // do downgrade
await proxy.upgradeToAndCall(instance1.address, '0x', { from: proxyAdminAddress }); await proxy.connect(proxyAdminAsSigner).upgradeToAndCall(impl1, '0x');
const proxyInstance1 = new Implementation2(proxy.address); // `getValue` is not available in impl1
await expectRevert.unspecified(proxyInstance1.getValue()); await expect(impl2.attach(instance).getValue()).to.be.reverted;
}); });
it('should change function signature', async () => { it('should change function signature', async function () {
const instance1 = await Implementation1.new(); const impl1 = await ethers.deployContract('Implementation1');
const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin( const impl3 = await ethers.deployContract('Implementation3');
instance1.address, const { instance, proxy, proxyAdminAsSigner } = await this.createProxyWithImpersonatedProxyAdmin(
impl1,
initializeData, initializeData,
); );
const proxyInstance1 = new Implementation1(proxy.address); await instance.setValue(42n);
await proxyInstance1.setValue(42);
const instance3 = await Implementation3.new(); await proxy.connect(proxyAdminAsSigner).upgradeToAndCall(impl3, '0x');
await proxy.upgradeToAndCall(instance3.address, '0x', { from: proxyAdminAddress });
const proxyInstance3 = new Implementation3(proxy.address);
const res = await proxyInstance3.getValue(8); expect(await impl3.attach(instance).getValue(8n)).to.equal(50n);
expect(res.toString()).to.eq('50');
}); });
it('should add fallback function', async () => { it('should add fallback function', async function () {
const initializeData = Buffer.from(''); const impl1 = await ethers.deployContract('Implementation1');
const instance1 = await Implementation1.new(); const impl4 = await ethers.deployContract('Implementation4');
const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin( const { instance, proxy, proxyAdminAsSigner } = await this.createProxyWithImpersonatedProxyAdmin(
instance1.address, impl1,
initializeData, initializeData,
); );
const instance4 = await Implementation4.new(); await proxy.connect(proxyAdminAsSigner).upgradeToAndCall(impl4, '0x');
await proxy.upgradeToAndCall(instance4.address, '0x', { from: proxyAdminAddress });
const proxyInstance4 = new Implementation4(proxy.address);
const data = '0x'; await this.other.sendTransaction({ to: proxy });
await web3.eth.sendTransaction({ to: proxy.address, from: anotherAccount, data });
const res = await proxyInstance4.getValue(); expect(await impl4.attach(instance).getValue()).to.equal(1n);
expect(res.toString()).to.eq('1');
}); });
it('should remove fallback function', async () => { it('should remove fallback function', async function () {
const instance4 = await Implementation4.new(); const impl2 = await ethers.deployContract('Implementation2');
const { proxy, proxyAdminAddress } = await createProxyWithImpersonatedProxyAdmin( const impl4 = await ethers.deployContract('Implementation4');
instance4.address, const { instance, proxy, proxyAdminAsSigner } = await this.createProxyWithImpersonatedProxyAdmin(
impl4,
initializeData, initializeData,
); );
const instance2 = await Implementation2.new(); await proxy.connect(proxyAdminAsSigner).upgradeToAndCall(impl2, '0x');
await proxy.upgradeToAndCall(instance2.address, '0x', { from: proxyAdminAddress });
const data = '0x'; await expect(this.other.sendTransaction({ to: proxy })).to.be.reverted;
await expectRevert.unspecified(web3.eth.sendTransaction({ to: proxy.address, from: anotherAccount, data }));
const proxyInstance2 = new Implementation2(proxy.address); expect(await impl2.attach(instance).getValue()).to.equal(0n);
const res = await proxyInstance2.getValue();
expect(res.toString()).to.eq('0');
}); });
}); });
}; };

@ -1,24 +1,28 @@
const { ethers } = require('hardhat');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const shouldBehaveLikeProxy = require('../Proxy.behaviour'); const shouldBehaveLikeProxy = require('../Proxy.behaviour');
const shouldBehaveLikeTransparentUpgradeableProxy = require('./TransparentUpgradeableProxy.behaviour'); const shouldBehaveLikeTransparentUpgradeableProxy = require('./TransparentUpgradeableProxy.behaviour');
const TransparentUpgradeableProxy = artifacts.require('TransparentUpgradeableProxy'); async function fixture() {
const ITransparentUpgradeableProxy = artifacts.require('ITransparentUpgradeableProxy'); const [owner, other, ...accounts] = await ethers.getSigners();
contract('TransparentUpgradeableProxy', function (accounts) { const implementation = await ethers.deployContract('DummyImplementation');
const [owner, ...otherAccounts] = accounts;
const createProxy = function (logic, initData, opts = undefined) {
// `undefined`, `null` and other false-ish opts will not be forwarded. return ethers.deployContract('TransparentUpgradeableProxy', [logic, owner, initData], opts);
const createProxy = async function (logic, initData, opts = undefined) {
const { address, transactionHash } = await TransparentUpgradeableProxy.new(
logic,
owner,
initData,
...[opts].filter(Boolean),
);
const instance = await ITransparentUpgradeableProxy.at(address);
return { ...instance, transactionHash };
}; };
shouldBehaveLikeProxy(createProxy, otherAccounts); return { nonContractAddress: owner, owner, other, accounts, implementation, createProxy };
shouldBehaveLikeTransparentUpgradeableProxy(createProxy, owner, otherAccounts); }
describe('TransparentUpgradeableProxy', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
shouldBehaveLikeProxy();
// createProxy, owner, otherAccounts
shouldBehaveLikeTransparentUpgradeableProxy();
}); });

@ -1,220 +1,218 @@
const { expectEvent } = require('@openzeppelin/test-helpers'); const { ethers } = require('hardhat');
const { expect } = require('chai'); const { expect } = require('chai');
const { expectRevertCustomError } = require('../../helpers/customError'); const {
const { MAX_UINT64 } = require('../../helpers/constants'); bigint: { MAX_UINT64 },
} = require('../../helpers/constants');
const InitializableMock = artifacts.require('InitializableMock');
const ConstructorInitializableMock = artifacts.require('ConstructorInitializableMock'); describe('Initializable', function () {
const ChildConstructorInitializableMock = artifacts.require('ChildConstructorInitializableMock');
const ReinitializerMock = artifacts.require('ReinitializerMock');
const SampleChild = artifacts.require('SampleChild');
const DisableBad1 = artifacts.require('DisableBad1');
const DisableBad2 = artifacts.require('DisableBad2');
const DisableOk = artifacts.require('DisableOk');
contract('Initializable', function () {
describe('basic testing without inheritance', function () { describe('basic testing without inheritance', function () {
beforeEach('deploying', async function () { beforeEach('deploying', async function () {
this.contract = await InitializableMock.new(); this.mock = await ethers.deployContract('InitializableMock');
}); });
describe('before initialize', function () { describe('before initialize', function () {
it('initializer has not run', async function () { it('initializer has not run', async function () {
expect(await this.contract.initializerRan()).to.equal(false); expect(await this.mock.initializerRan()).to.be.false;
}); });
it('_initializing returns false before initialization', async function () { it('_initializing returns false before initialization', async function () {
expect(await this.contract.isInitializing()).to.equal(false); expect(await this.mock.isInitializing()).to.be.false;
}); });
}); });
describe('after initialize', function () { describe('after initialize', function () {
beforeEach('initializing', async function () { beforeEach('initializing', async function () {
await this.contract.initialize(); await this.mock.initialize();
}); });
it('initializer has run', async function () { it('initializer has run', async function () {
expect(await this.contract.initializerRan()).to.equal(true); expect(await this.mock.initializerRan()).to.be.true;
}); });
it('_initializing returns false after initialization', async function () { it('_initializing returns false after initialization', async function () {
expect(await this.contract.isInitializing()).to.equal(false); expect(await this.mock.isInitializing()).to.be.false;
}); });
it('initializer does not run again', async function () { it('initializer does not run again', async function () {
await expectRevertCustomError(this.contract.initialize(), 'InvalidInitialization', []); await expect(this.mock.initialize()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
}); });
}); });
describe('nested under an initializer', function () { describe('nested under an initializer', function () {
it('initializer modifier reverts', async function () { it('initializer modifier reverts', async function () {
await expectRevertCustomError(this.contract.initializerNested(), 'InvalidInitialization', []); await expect(this.mock.initializerNested()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
}); });
it('onlyInitializing modifier succeeds', async function () { it('onlyInitializing modifier succeeds', async function () {
await this.contract.onlyInitializingNested(); await this.mock.onlyInitializingNested();
expect(await this.contract.onlyInitializingRan()).to.equal(true); expect(await this.mock.onlyInitializingRan()).to.be.true;
}); });
}); });
it('cannot call onlyInitializable function outside the scope of an initializable function', async function () { it('cannot call onlyInitializable function outside the scope of an initializable function', async function () {
await expectRevertCustomError(this.contract.initializeOnlyInitializing(), 'NotInitializing', []); await expect(this.mock.initializeOnlyInitializing()).to.be.revertedWithCustomError(this.mock, 'NotInitializing');
}); });
}); });
it('nested initializer can run during construction', async function () { it('nested initializer can run during construction', async function () {
const contract2 = await ConstructorInitializableMock.new(); const mock = await ethers.deployContract('ConstructorInitializableMock');
expect(await contract2.initializerRan()).to.equal(true); expect(await mock.initializerRan()).to.be.true;
expect(await contract2.onlyInitializingRan()).to.equal(true); expect(await mock.onlyInitializingRan()).to.be.true;
}); });
it('multiple constructor levels can be initializers', async function () { it('multiple constructor levels can be initializers', async function () {
const contract2 = await ChildConstructorInitializableMock.new(); const mock = await ethers.deployContract('ChildConstructorInitializableMock');
expect(await contract2.initializerRan()).to.equal(true); expect(await mock.initializerRan()).to.be.true;
expect(await contract2.childInitializerRan()).to.equal(true); expect(await mock.childInitializerRan()).to.be.true;
expect(await contract2.onlyInitializingRan()).to.equal(true); expect(await mock.onlyInitializingRan()).to.be.true;
}); });
describe('reinitialization', function () { describe('reinitialization', function () {
beforeEach('deploying', async function () { beforeEach('deploying', async function () {
this.contract = await ReinitializerMock.new(); this.mock = await ethers.deployContract('ReinitializerMock');
}); });
it('can reinitialize', async function () { it('can reinitialize', async function () {
expect(await this.contract.counter()).to.be.bignumber.equal('0'); expect(await this.mock.counter()).to.equal(0n);
await this.contract.initialize(); await this.mock.initialize();
expect(await this.contract.counter()).to.be.bignumber.equal('1'); expect(await this.mock.counter()).to.equal(1n);
await this.contract.reinitialize(2); await this.mock.reinitialize(2);
expect(await this.contract.counter()).to.be.bignumber.equal('2'); expect(await this.mock.counter()).to.equal(2n);
await this.contract.reinitialize(3); await this.mock.reinitialize(3);
expect(await this.contract.counter()).to.be.bignumber.equal('3'); expect(await this.mock.counter()).to.equal(3n);
}); });
it('can jump multiple steps', async function () { it('can jump multiple steps', async function () {
expect(await this.contract.counter()).to.be.bignumber.equal('0'); expect(await this.mock.counter()).to.equal(0n);
await this.contract.initialize(); await this.mock.initialize();
expect(await this.contract.counter()).to.be.bignumber.equal('1'); expect(await this.mock.counter()).to.equal(1n);
await this.contract.reinitialize(128); await this.mock.reinitialize(128);
expect(await this.contract.counter()).to.be.bignumber.equal('2'); expect(await this.mock.counter()).to.equal(2n);
}); });
it('cannot nest reinitializers', async function () { it('cannot nest reinitializers', async function () {
expect(await this.contract.counter()).to.be.bignumber.equal('0'); expect(await this.mock.counter()).to.equal(0n);
await expectRevertCustomError(this.contract.nestedReinitialize(2, 2), 'InvalidInitialization', []); await expect(this.mock.nestedReinitialize(2, 2)).to.be.revertedWithCustomError(
await expectRevertCustomError(this.contract.nestedReinitialize(2, 3), 'InvalidInitialization', []); this.mock,
await expectRevertCustomError(this.contract.nestedReinitialize(3, 2), 'InvalidInitialization', []); 'InvalidInitialization',
);
await expect(this.mock.nestedReinitialize(2, 3)).to.be.revertedWithCustomError(
this.mock,
'InvalidInitialization',
);
await expect(this.mock.nestedReinitialize(3, 2)).to.be.revertedWithCustomError(
this.mock,
'InvalidInitialization',
);
}); });
it('can chain reinitializers', async function () { it('can chain reinitializers', async function () {
expect(await this.contract.counter()).to.be.bignumber.equal('0'); expect(await this.mock.counter()).to.equal(0n);
await this.contract.chainReinitialize(2, 3); await this.mock.chainReinitialize(2, 3);
expect(await this.contract.counter()).to.be.bignumber.equal('2'); expect(await this.mock.counter()).to.equal(2n);
}); });
it('_getInitializedVersion returns right version', async function () { it('_getInitializedVersion returns right version', async function () {
await this.contract.initialize(); await this.mock.initialize();
expect(await this.contract.getInitializedVersion()).to.be.bignumber.equal('1'); expect(await this.mock.getInitializedVersion()).to.equal(1n);
await this.contract.reinitialize(12); await this.mock.reinitialize(12);
expect(await this.contract.getInitializedVersion()).to.be.bignumber.equal('12'); expect(await this.mock.getInitializedVersion()).to.equal(12n);
}); });
describe('contract locking', function () { describe('contract locking', function () {
it('prevents initialization', async function () { it('prevents initialization', async function () {
await this.contract.disableInitializers(); await this.mock.disableInitializers();
await expectRevertCustomError(this.contract.initialize(), 'InvalidInitialization', []); await expect(this.mock.initialize()).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
}); });
it('prevents re-initialization', async function () { it('prevents re-initialization', async function () {
await this.contract.disableInitializers(); await this.mock.disableInitializers();
await expectRevertCustomError(this.contract.reinitialize(255), 'InvalidInitialization', []); await expect(this.mock.reinitialize(255n)).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
}); });
it('can lock contract after initialization', async function () { it('can lock contract after initialization', async function () {
await this.contract.initialize(); await this.mock.initialize();
await this.contract.disableInitializers(); await this.mock.disableInitializers();
await expectRevertCustomError(this.contract.reinitialize(255), 'InvalidInitialization', []); await expect(this.mock.reinitialize(255n)).to.be.revertedWithCustomError(this.mock, 'InvalidInitialization');
}); });
}); });
}); });
describe('events', function () { describe('events', function () {
it('constructor initialization emits event', async function () { it('constructor initialization emits event', async function () {
const contract = await ConstructorInitializableMock.new(); const mock = await ethers.deployContract('ConstructorInitializableMock');
await expect(mock.deploymentTransaction()).to.emit(mock, 'Initialized').withArgs(1n);
await expectEvent.inTransaction(contract.transactionHash, contract, 'Initialized', { version: '1' });
}); });
it('initialization emits event', async function () { it('initialization emits event', async function () {
const contract = await ReinitializerMock.new(); const mock = await ethers.deployContract('ReinitializerMock');
await expect(mock.initialize()).to.emit(mock, 'Initialized').withArgs(1n);
const { receipt } = await contract.initialize();
expect(receipt.logs.filter(({ event }) => event === 'Initialized').length).to.be.equal(1);
expectEvent(receipt, 'Initialized', { version: '1' });
}); });
it('reinitialization emits event', async function () { it('reinitialization emits event', async function () {
const contract = await ReinitializerMock.new(); const mock = await ethers.deployContract('ReinitializerMock');
await expect(mock.reinitialize(128)).to.emit(mock, 'Initialized').withArgs(128n);
const { receipt } = await contract.reinitialize(128);
expect(receipt.logs.filter(({ event }) => event === 'Initialized').length).to.be.equal(1);
expectEvent(receipt, 'Initialized', { version: '128' });
}); });
it('chained reinitialization emits multiple events', async function () { it('chained reinitialization emits multiple events', async function () {
const contract = await ReinitializerMock.new(); const mock = await ethers.deployContract('ReinitializerMock');
const { receipt } = await contract.chainReinitialize(2, 3); await expect(mock.chainReinitialize(2, 3))
expect(receipt.logs.filter(({ event }) => event === 'Initialized').length).to.be.equal(2); .to.emit(mock, 'Initialized')
expectEvent(receipt, 'Initialized', { version: '2' }); .withArgs(2n)
expectEvent(receipt, 'Initialized', { version: '3' }); .to.emit(mock, 'Initialized')
.withArgs(3n);
}); });
}); });
describe('complex testing with inheritance', function () { describe('complex testing with inheritance', function () {
const mother = '12'; const mother = 12n;
const gramps = '56'; const gramps = '56';
const father = '34'; const father = 34n;
const child = '78'; const child = 78n;
beforeEach('deploying', async function () { beforeEach('deploying', async function () {
this.contract = await SampleChild.new(); this.mock = await ethers.deployContract('SampleChild');
}); await this.mock.initialize(mother, gramps, father, child);
beforeEach('initializing', async function () {
await this.contract.initialize(mother, gramps, father, child);
}); });
it('initializes human', async function () { it('initializes human', async function () {
expect(await this.contract.isHuman()).to.be.equal(true); expect(await this.mock.isHuman()).to.be.true;
}); });
it('initializes mother', async function () { it('initializes mother', async function () {
expect(await this.contract.mother()).to.be.bignumber.equal(mother); expect(await this.mock.mother()).to.equal(mother);
}); });
it('initializes gramps', async function () { it('initializes gramps', async function () {
expect(await this.contract.gramps()).to.be.bignumber.equal(gramps); expect(await this.mock.gramps()).to.equal(gramps);
}); });
it('initializes father', async function () { it('initializes father', async function () {
expect(await this.contract.father()).to.be.bignumber.equal(father); expect(await this.mock.father()).to.equal(father);
}); });
it('initializes child', async function () { it('initializes child', async function () {
expect(await this.contract.child()).to.be.bignumber.equal(child); expect(await this.mock.child()).to.equal(child);
}); });
}); });
describe('disabling initialization', function () { describe('disabling initialization', function () {
it('old and new patterns in bad sequence', async function () { it('old and new patterns in bad sequence', async function () {
await expectRevertCustomError(DisableBad1.new(), 'InvalidInitialization', []); const DisableBad1 = await ethers.getContractFactory('DisableBad1');
await expectRevertCustomError(DisableBad2.new(), 'InvalidInitialization', []); await expect(DisableBad1.deploy()).to.be.revertedWithCustomError(DisableBad1, 'InvalidInitialization');
const DisableBad2 = await ethers.getContractFactory('DisableBad2');
await expect(DisableBad2.deploy()).to.be.revertedWithCustomError(DisableBad2, 'InvalidInitialization');
}); });
it('old and new patterns in good sequence', async function () { it('old and new patterns in good sequence', async function () {
const ok = await DisableOk.new(); const ok = await ethers.deployContract('DisableOk');
await expectEvent.inConstruction(ok, 'Initialized', { version: '1' }); await expect(ok.deploymentTransaction())
await expectEvent.inConstruction(ok, 'Initialized', { version: MAX_UINT64 }); .to.emit(ok, 'Initialized')
.withArgs(1n)
.to.emit(ok, 'Initialized')
.withArgs(MAX_UINT64);
}); });
}); });
}); });

@ -1,28 +1,36 @@
const { expectEvent } = require('@openzeppelin/test-helpers'); const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { getAddressInSlot, ImplementationSlot } = require('../../helpers/erc1967'); const { getAddressInSlot, ImplementationSlot } = require('../../helpers/erc1967');
const { expectRevertCustomError } = require('../../helpers/customError');
const ERC1967Proxy = artifacts.require('ERC1967Proxy');
const UUPSUpgradeableMock = artifacts.require('UUPSUpgradeableMock');
const UUPSUpgradeableUnsafeMock = artifacts.require('UUPSUpgradeableUnsafeMock');
const NonUpgradeableMock = artifacts.require('NonUpgradeableMock');
const UUPSUnsupportedProxiableUUID = artifacts.require('UUPSUnsupportedProxiableUUID');
const Clones = artifacts.require('$Clones');
contract('UUPSUpgradeable', function () {
before(async function () {
this.implInitial = await UUPSUpgradeableMock.new();
this.implUpgradeOk = await UUPSUpgradeableMock.new();
this.implUpgradeUnsafe = await UUPSUpgradeableUnsafeMock.new();
this.implUpgradeNonUUPS = await NonUpgradeableMock.new();
this.implUnsupportedUUID = await UUPSUnsupportedProxiableUUID.new();
// Used for testing non ERC1967 compliant proxies (clones are proxies that don't use the ERC1967 implementation slot)
this.cloneFactory = await Clones.new();
});
async function fixture() {
const implInitial = await ethers.deployContract('UUPSUpgradeableMock');
const implUpgradeOk = await ethers.deployContract('UUPSUpgradeableMock');
const implUpgradeUnsafe = await ethers.deployContract('UUPSUpgradeableUnsafeMock');
const implUpgradeNonUUPS = await ethers.deployContract('NonUpgradeableMock');
const implUnsupportedUUID = await ethers.deployContract('UUPSUnsupportedProxiableUUID');
// Used for testing non ERC1967 compliant proxies (clones are proxies that don't use the ERC1967 implementation slot)
const cloneFactory = await ethers.deployContract('$Clones');
const instance = await ethers
.deployContract('ERC1967Proxy', [implInitial, '0x'])
.then(proxy => implInitial.attach(proxy.target));
return {
implInitial,
implUpgradeOk,
implUpgradeUnsafe,
implUpgradeNonUUPS,
implUnsupportedUUID,
cloneFactory,
instance,
};
}
describe('UUPSUpgradeable', function () {
beforeEach(async function () { beforeEach(async function () {
const { address } = await ERC1967Proxy.new(this.implInitial.address, '0x'); Object.assign(this, await loadFixture(fixture));
this.instance = await UUPSUpgradeableMock.at(address);
}); });
it('has an interface version', async function () { it('has an interface version', async function () {
@ -30,102 +38,83 @@ contract('UUPSUpgradeable', function () {
}); });
it('upgrade to upgradeable implementation', async function () { it('upgrade to upgradeable implementation', async function () {
const { receipt } = await this.instance.upgradeToAndCall(this.implUpgradeOk.address, '0x'); await expect(this.instance.upgradeToAndCall(this.implUpgradeOk, '0x'))
expect(receipt.logs.filter(({ event }) => event === 'Upgraded').length).to.be.equal(1); .to.emit(this.instance, 'Upgraded')
expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeOk.address }); .withArgs(this.implUpgradeOk.target);
expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.be.equal(this.implUpgradeOk.address);
expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.equal(this.implUpgradeOk.target);
}); });
it('upgrade to upgradeable implementation with call', async function () { it('upgrade to upgradeable implementation with call', async function () {
expect(await this.instance.current()).to.be.bignumber.equal('0'); expect(await this.instance.current()).to.equal(0n);
const { receipt } = await this.instance.upgradeToAndCall( await expect(
this.implUpgradeOk.address, this.instance.upgradeToAndCall(this.implUpgradeOk, this.implUpgradeOk.interface.encodeFunctionData('increment')),
this.implUpgradeOk.contract.methods.increment().encodeABI(), )
); .to.emit(this.instance, 'Upgraded')
expect(receipt.logs.filter(({ event }) => event === 'Upgraded').length).to.be.equal(1); .withArgs(this.implUpgradeOk.target);
expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeOk.address });
expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.be.equal(this.implUpgradeOk.address); expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.equal(this.implUpgradeOk.target);
expect(await this.instance.current()).to.be.bignumber.equal('1'); expect(await this.instance.current()).to.equal(1n);
}); });
it('calling upgradeTo on the implementation reverts', async function () { it('calling upgradeTo on the implementation reverts', async function () {
await expectRevertCustomError( await expect(this.implInitial.upgradeToAndCall(this.implUpgradeOk, '0x')).to.be.revertedWithCustomError(
this.implInitial.upgradeToAndCall(this.implUpgradeOk.address, '0x'), this.implInitial,
'UUPSUnauthorizedCallContext', 'UUPSUnauthorizedCallContext',
[],
); );
}); });
it('calling upgradeToAndCall on the implementation reverts', async function () { it('calling upgradeToAndCall on the implementation reverts', async function () {
await expectRevertCustomError( await expect(
this.implInitial.upgradeToAndCall( this.implInitial.upgradeToAndCall(
this.implUpgradeOk.address, this.implUpgradeOk,
this.implUpgradeOk.contract.methods.increment().encodeABI(), this.implUpgradeOk.interface.encodeFunctionData('increment'),
), ),
'UUPSUnauthorizedCallContext', ).to.be.revertedWithCustomError(this.implUpgradeOk, 'UUPSUnauthorizedCallContext');
[],
);
});
it('calling upgradeTo from a contract that is not an ERC1967 proxy (with the right implementation) reverts', async function () {
const receipt = await this.cloneFactory.$clone(this.implUpgradeOk.address);
const instance = await UUPSUpgradeableMock.at(
receipt.logs.find(({ event }) => event === 'return$clone').args.instance,
);
await expectRevertCustomError(
instance.upgradeToAndCall(this.implUpgradeUnsafe.address, '0x'),
'UUPSUnauthorizedCallContext',
[],
);
}); });
it('calling upgradeToAndCall from a contract that is not an ERC1967 proxy (with the right implementation) reverts', async function () { it('calling upgradeToAndCall from a contract that is not an ERC1967 proxy (with the right implementation) reverts', async function () {
const receipt = await this.cloneFactory.$clone(this.implUpgradeOk.address); const instance = await this.cloneFactory.$clone
const instance = await UUPSUpgradeableMock.at( .staticCall(this.implUpgradeOk)
receipt.logs.find(({ event }) => event === 'return$clone').args.instance, .then(address => this.implInitial.attach(address));
); await this.cloneFactory.$clone(this.implUpgradeOk);
await expectRevertCustomError( await expect(instance.upgradeToAndCall(this.implUpgradeUnsafe, '0x')).to.be.revertedWithCustomError(
instance.upgradeToAndCall(this.implUpgradeUnsafe.address, '0x'), instance,
'UUPSUnauthorizedCallContext', 'UUPSUnauthorizedCallContext',
[],
); );
}); });
it('rejects upgrading to an unsupported UUID', async function () { it('rejects upgrading to an unsupported UUID', async function () {
await expectRevertCustomError( await expect(this.instance.upgradeToAndCall(this.implUnsupportedUUID, '0x'))
this.instance.upgradeToAndCall(this.implUnsupportedUUID.address, '0x'), .to.be.revertedWithCustomError(this.instance, 'UUPSUnsupportedProxiableUUID')
'UUPSUnsupportedProxiableUUID', .withArgs(ethers.id('invalid UUID'));
[web3.utils.keccak256('invalid UUID')],
);
}); });
it('upgrade to and unsafe upgradeable implementation', async function () { it('upgrade to and unsafe upgradeable implementation', async function () {
const { receipt } = await this.instance.upgradeToAndCall(this.implUpgradeUnsafe.address, '0x'); await expect(this.instance.upgradeToAndCall(this.implUpgradeUnsafe, '0x'))
expectEvent(receipt, 'Upgraded', { implementation: this.implUpgradeUnsafe.address }); .to.emit(this.instance, 'Upgraded')
expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.be.equal(this.implUpgradeUnsafe.address); .withArgs(this.implUpgradeUnsafe.target);
expect(await getAddressInSlot(this.instance, ImplementationSlot)).to.equal(this.implUpgradeUnsafe.target);
}); });
// delegate to a non existing upgradeTo function causes a low level revert // delegate to a non existing upgradeTo function causes a low level revert
it('reject upgrade to non uups implementation', async function () { it('reject upgrade to non uups implementation', async function () {
await expectRevertCustomError( await expect(this.instance.upgradeToAndCall(this.implUpgradeNonUUPS, '0x'))
this.instance.upgradeToAndCall(this.implUpgradeNonUUPS.address, '0x'), .to.be.revertedWithCustomError(this.instance, 'ERC1967InvalidImplementation')
'ERC1967InvalidImplementation', .withArgs(this.implUpgradeNonUUPS.target);
[this.implUpgradeNonUUPS.address],
);
}); });
it('reject proxy address as implementation', async function () { it('reject proxy address as implementation', async function () {
const { address } = await ERC1967Proxy.new(this.implInitial.address, '0x'); const otherInstance = await ethers
const otherInstance = await UUPSUpgradeableMock.at(address); .deployContract('ERC1967Proxy', [this.implInitial, '0x'])
.then(proxy => this.implInitial.attach(proxy.target));
await expectRevertCustomError( await expect(this.instance.upgradeToAndCall(otherInstance, '0x'))
this.instance.upgradeToAndCall(otherInstance.address, '0x'), .to.be.revertedWithCustomError(this.instance, 'ERC1967InvalidImplementation')
'ERC1967InvalidImplementation', .withArgs(otherInstance.target);
[otherInstance.address],
);
}); });
}); });

Loading…
Cancel
Save