diff --git a/packages/contracts/test/contracts/L1/deployment/AddressDictator.spec.ts b/packages/contracts/test/contracts/L1/deployment/AddressDictator.spec.ts index 91bf86c1508f5..17d12d771e0e7 100644 --- a/packages/contracts/test/contracts/L1/deployment/AddressDictator.spec.ts +++ b/packages/contracts/test/contracts/L1/deployment/AddressDictator.spec.ts @@ -1,57 +1,50 @@ -/* External Imports */ import { ethers } from 'hardhat' -import { Contract, Signer, ContractFactory } from 'ethers' +import { Contract, Signer } from 'ethers' -/* Internal Imports */ import { expect } from '../../../setup' -import { NON_ZERO_ADDRESS } from '../../../helpers' +import { deploy, NON_ZERO_ADDRESS } from '../../../helpers' describe('AddressDictator', () => { - let signer: Signer - let otherSigner: Signer - let signerAddress: string - let Factory__AddressDictator: ContractFactory - let Factory__Lib_AddressManager: ContractFactory + let signer1: Signer + let signer2: Signer before(async () => { - ;[signer, otherSigner] = await ethers.getSigners() - - Factory__AddressDictator = await ethers.getContractFactory( - 'AddressDictator' - ) - - Factory__Lib_AddressManager = await ethers.getContractFactory( - 'Lib_AddressManager' - ) - - signerAddress = await signer.getAddress() + ;[signer1, signer2] = await ethers.getSigners() }) let AddressDictator: Contract let Lib_AddressManager: Contract beforeEach(async () => { - Lib_AddressManager = await Factory__Lib_AddressManager.connect( - signer - ).deploy() + Lib_AddressManager = await deploy('Lib_AddressManager', { + signer: signer1, + }) - AddressDictator = await Factory__AddressDictator.connect(signer).deploy( - Lib_AddressManager.address, - signerAddress, - ['addr1'], - [NON_ZERO_ADDRESS] - ) + AddressDictator = await deploy('AddressDictator', { + signer: signer1, + args: [ + Lib_AddressManager.address, + await signer1.getAddress(), + ['addr1'], + [NON_ZERO_ADDRESS], + ], + }) - Lib_AddressManager.transferOwnership(AddressDictator.address) + Lib_AddressManager.connect(signer1).transferOwnership( + AddressDictator.address + ) }) describe('initialize', () => { it('should revert when providing wrong arguments', async () => { await expect( - Factory__AddressDictator.connect(signer).deploy( - Lib_AddressManager.address, - signerAddress, - ['addr1', 'addr2'], - [NON_ZERO_ADDRESS] - ) + deploy('AddressDictator', { + signer: signer1, + args: [ + Lib_AddressManager.address, + await signer1.getAddress(), + ['addr1', 'addr2'], + [NON_ZERO_ADDRESS], + ], + }) ).to.be.revertedWith( 'AddressDictator: Must provide an equal number of names and addresses.' ) @@ -61,7 +54,7 @@ describe('AddressDictator', () => { describe('setAddresses', async () => { it('should change the addresses associated with a name', async () => { await AddressDictator.setAddresses() - expect(await Lib_AddressManager.getAddress('addr1')).to.be.equal( + expect(await Lib_AddressManager.getAddress('addr1')).to.equal( NON_ZERO_ADDRESS ) }) @@ -69,7 +62,7 @@ describe('AddressDictator', () => { describe('getNamedAddresses', () => { it('should return all the addresses and their names', async () => { - expect(await AddressDictator.getNamedAddresses()).to.be.deep.equal([ + expect(await AddressDictator.getNamedAddresses()).to.deep.equal([ ['addr1', NON_ZERO_ADDRESS], ]) }) @@ -77,13 +70,13 @@ describe('AddressDictator', () => { describe('returnOwnership', () => { it('should transfer contract ownership to finalOwner', async () => { - await expect(AddressDictator.connect(signer).returnOwnership()).to.not.be + await expect(AddressDictator.connect(signer1).returnOwnership()).to.not.be .reverted }) it('should revert when called by non-owner', async () => { await expect( - AddressDictator.connect(otherSigner).returnOwnership() + AddressDictator.connect(signer2).returnOwnership() ).to.be.revertedWith('AddressDictator: only callable by finalOwner') }) }) diff --git a/packages/contracts/test/contracts/L1/deployment/ChugSplashDictator.spec..ts b/packages/contracts/test/contracts/L1/deployment/ChugSplashDictator.spec..ts deleted file mode 100644 index e6767fd48a17d..0000000000000 --- a/packages/contracts/test/contracts/L1/deployment/ChugSplashDictator.spec..ts +++ /dev/null @@ -1,76 +0,0 @@ -/* External Imports */ -import { ethers } from 'hardhat' -import { Contract, Signer, ContractFactory } from 'ethers' - -/* Internal Imports */ -import { expect } from '../../../setup' - -describe('ChugSplashDictator', () => { - let signer: Signer - let otherSigner: Signer - let signerAddress: string - - let Factory__L1ChugSplashProxy: ContractFactory - let Factory__ChugSplashDictator: ContractFactory - before(async () => { - ;[signer, otherSigner] = await ethers.getSigners() - - Factory__L1ChugSplashProxy = await ethers.getContractFactory( - 'L1ChugSplashProxy' - ) - - Factory__ChugSplashDictator = await ethers.getContractFactory( - 'ChugSplashDictator' - ) - - signerAddress = await signer.getAddress() - }) - - let L1ChugSplashProxy: Contract - let ChugSplashDictator: Contract - beforeEach(async () => { - L1ChugSplashProxy = await Factory__L1ChugSplashProxy.connect(signer).deploy( - signerAddress - ) - - ChugSplashDictator = await Factory__ChugSplashDictator.connect( - signer - ).deploy( - L1ChugSplashProxy.address, - signerAddress, - ethers.utils.keccak256('0x1111'), - ethers.utils.keccak256('0x1234'), - ethers.utils.keccak256('0x5678'), - ethers.utils.keccak256('0x1234'), - ethers.utils.keccak256('0x1234') - ) - - await L1ChugSplashProxy.connect(signer).setOwner(ChugSplashDictator.address) - }) - - describe('doActions', () => { - it('should revert when sent wrong code', async () => { - await expect(ChugSplashDictator.doActions('0x2222')).to.be.revertedWith( - 'ChugSplashDictator: Incorrect code hash.' - ) - }) - - it('should set the proxy code, storage & owner', async () => { - await expect(ChugSplashDictator.connect(signer).doActions('0x1111')).to - .not.be.reverted - }) - }) - - describe('returnOwnership', () => { - it('should transfer contractc ownership to finalOwner', async () => { - await expect(ChugSplashDictator.connect(signer).returnOwnership()).to.not - .be.reverted - }) - - it('should revert when called by non-owner', async () => { - await expect( - ChugSplashDictator.connect(otherSigner).returnOwnership() - ).to.be.revertedWith('ChugSplashDictator: only callable by finalOwner') - }) - }) -}) diff --git a/packages/contracts/test/contracts/L1/deployment/ChugSplashDictator.spec.ts b/packages/contracts/test/contracts/L1/deployment/ChugSplashDictator.spec.ts new file mode 100644 index 0000000000000..260b7065bd10e --- /dev/null +++ b/packages/contracts/test/contracts/L1/deployment/ChugSplashDictator.spec.ts @@ -0,0 +1,65 @@ +import { ethers } from 'hardhat' +import { Contract, Signer } from 'ethers' + +import { expect } from '../../../setup' +import { deploy } from '../../../helpers' + +describe('ChugSplashDictator', () => { + let signer1: Signer + let signer2: Signer + before(async () => { + ;[signer1, signer2] = await ethers.getSigners() + }) + + let L1ChugSplashProxy: Contract + let ChugSplashDictator: Contract + beforeEach(async () => { + L1ChugSplashProxy = await deploy('L1ChugSplashProxy', { + signer: signer1, + args: [await signer1.getAddress()], + }) + + ChugSplashDictator = await deploy('ChugSplashDictator', { + signer: signer1, + args: [ + L1ChugSplashProxy.address, + await signer1.getAddress(), + ethers.utils.keccak256('0x1111'), + ethers.utils.keccak256('0x1234'), + ethers.utils.keccak256('0x5678'), + ethers.utils.keccak256('0x1234'), + ethers.utils.keccak256('0x1234'), + ], + }) + + await L1ChugSplashProxy.connect(signer1).setOwner( + ChugSplashDictator.address + ) + }) + + describe('doActions', () => { + it('should revert when sent wrong code', async () => { + await expect(ChugSplashDictator.doActions('0x2222')).to.be.revertedWith( + 'ChugSplashDictator: Incorrect code hash.' + ) + }) + + it('should set the proxy code, storage & owner', async () => { + await expect(ChugSplashDictator.connect(signer1).doActions('0x1111')).to + .not.be.reverted + }) + }) + + describe('returnOwnership', () => { + it('should transfer contractc ownership to finalOwner', async () => { + await expect(ChugSplashDictator.connect(signer1).returnOwnership()).to.not + .be.reverted + }) + + it('should revert when called by non-owner', async () => { + await expect( + ChugSplashDictator.connect(signer2).returnOwnership() + ).to.be.revertedWith('ChugSplashDictator: only callable by finalOwner') + }) + }) +}) diff --git a/packages/contracts/test/contracts/chugsplash/L1ChugSplashProxy.spec.ts b/packages/contracts/test/contracts/chugsplash/L1ChugSplashProxy.spec.ts index 9dece778785fe..ab469b57285e3 100644 --- a/packages/contracts/test/contracts/chugsplash/L1ChugSplashProxy.spec.ts +++ b/packages/contracts/test/contracts/chugsplash/L1ChugSplashProxy.spec.ts @@ -1,11 +1,10 @@ -/* Imports: External */ import hre from 'hardhat' import { Contract, Signer } from 'ethers' import { smock } from '@defi-wonderland/smock' -/* Imports: Internal */ import { expect } from '../../setup' import { getContractInterface } from '../../../src' +import { deploy } from '../../helpers' describe('L1ChugSplashProxy', () => { let signer1: Signer @@ -16,12 +15,9 @@ describe('L1ChugSplashProxy', () => { let L1ChugSplashProxy: Contract beforeEach(async () => { - const Factory__L1ChugSplashProxy = await hre.ethers.getContractFactory( - 'L1ChugSplashProxy' - ) - L1ChugSplashProxy = await Factory__L1ChugSplashProxy.deploy( - await signer1.getAddress() - ) + L1ChugSplashProxy = await deploy('L1ChugSplashProxy', { + args: [await signer1.getAddress()], + }) }) describe('getOwner', () => { @@ -173,14 +169,16 @@ describe('L1ChugSplashProxy', () => { const owner = await smock.fake( getContractInterface('iL1ChugSplashDeployer') ) - const factory = await hre.ethers.getContractFactory('L1ChugSplashProxy') - const proxy = await factory.deploy(owner.address) + + L1ChugSplashProxy = await deploy('L1ChugSplashProxy', { + args: [owner.address], + }) owner.isUpgrading.returns(true) await expect( owner.wallet.sendTransaction({ - to: proxy.address, + to: L1ChugSplashProxy.address, data: '0x', }) ).to.be.revertedWith( diff --git a/packages/contracts/test/helpers/utils/deploy.ts b/packages/contracts/test/helpers/utils/deploy.ts new file mode 100644 index 0000000000000..23525868b6fce --- /dev/null +++ b/packages/contracts/test/helpers/utils/deploy.ts @@ -0,0 +1,12 @@ +import hre from 'hardhat' + +export const deploy = async ( + name: string, + opts?: { + args?: any[] + signer?: any + } +) => { + const factory = await hre.ethers.getContractFactory(name, opts?.signer) + return factory.deploy(...(opts?.args || [])) +} diff --git a/packages/contracts/test/helpers/utils/index.ts b/packages/contracts/test/helpers/utils/index.ts index 27bae2fef287c..c7d4b27add422 100644 --- a/packages/contracts/test/helpers/utils/index.ts +++ b/packages/contracts/test/helpers/utils/index.ts @@ -1 +1,2 @@ export * from './eth-time' +export * from './deploy'