|
| 1 | +const { ethers } = require('hardhat'); |
| 2 | +const { expect } = require('chai'); |
| 3 | +const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); |
| 4 | + |
| 5 | +async function fixture() { |
| 6 | + const [admin] = await ethers.getSigners(); |
| 7 | + |
| 8 | + const implementation1 = await ethers.deployContract('UpgradeableImplementationMock', [1]); |
| 9 | + const implementation2 = await ethers.deployContract('UpgradeableImplementationMock', [2]); |
| 10 | + const implementation3 = await ethers.deployContract('UpgradeableImplementationMock', [3]); |
| 11 | + const beacon = await ethers.deployContract('UpgradeableBeacon', [implementation1, admin]); |
| 12 | + const proxy = await ethers |
| 13 | + .deployContract('HybridProxy', [beacon, '0x']) |
| 14 | + .then(({ target }) => implementation1.attach(target)); |
| 15 | + |
| 16 | + return { admin, beacon, proxy, implementation1, implementation2, implementation3 }; |
| 17 | +} |
| 18 | + |
| 19 | +describe('HybridProxy', function () { |
| 20 | + beforeEach(async function () { |
| 21 | + Object.assign(this, await loadFixture(fixture)); |
| 22 | + }); |
| 23 | + |
| 24 | + it('setup at construction', async function () { |
| 25 | + const data = ethers.randomBytes(128); |
| 26 | + await expect(ethers.deployContract('HybridProxy', [this.beacon, data])) |
| 27 | + .to.be.revertedWithCustomError(this.implementation1, 'UnexpectedCall') |
| 28 | + .withArgs(this.admin, 0, data); |
| 29 | + }); |
| 30 | + |
| 31 | + it('forwards calls', async function () { |
| 32 | + await expect(this.implementation1.attach(this.proxy).version()).to.eventually.equal(1); |
| 33 | + }); |
| 34 | + |
| 35 | + it('beacon upgrade', async function () { |
| 36 | + await this.beacon.upgradeTo(this.implementation2); |
| 37 | + await expect(this.implementation1.attach(this.proxy).version()).to.eventually.equal(2); |
| 38 | + }); |
| 39 | + |
| 40 | + it('decouple/recouple', async function () { |
| 41 | + await this.proxy.upgradeToAndCall(this.implementation3, '0x'); |
| 42 | + await expect(this.implementation1.attach(this.proxy).version()).to.eventually.equal(3); |
| 43 | + |
| 44 | + // beacon updated no longer affect the upgrade process |
| 45 | + await this.beacon.upgradeTo(this.implementation2); |
| 46 | + await expect(this.implementation1.attach(this.proxy).version()).to.eventually.equal(3); |
| 47 | + |
| 48 | + // recouple to beacon |
| 49 | + await this.proxy.upgradeToAndCall(this.beacon, '0x'); |
| 50 | + await expect(this.implementation1.attach(this.proxy).version()).to.eventually.equal(2); |
| 51 | + }); |
| 52 | +}); |
0 commit comments