Commit a31609c5 authored by smartcontracts's avatar smartcontracts Committed by GitHub

maint(ct): clean up contract tests 1 (#2334)

First commit in a series of commits to clean up the contract tests. Made
a deploy helper function to remove some ugly nested awaits. Also
generally starting to remove unnecessary Factory objects.
Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent d076a577
/* External Imports */
import { ethers } from 'hardhat' import { ethers } from 'hardhat'
import { Contract, Signer, ContractFactory } from 'ethers' import { Contract, Signer } from 'ethers'
/* Internal Imports */
import { expect } from '../../../setup' import { expect } from '../../../setup'
import { NON_ZERO_ADDRESS } from '../../../helpers' import { deploy, NON_ZERO_ADDRESS } from '../../../helpers'
describe('AddressDictator', () => { describe('AddressDictator', () => {
let signer: Signer let signer1: Signer
let otherSigner: Signer let signer2: Signer
let signerAddress: string
let Factory__AddressDictator: ContractFactory
let Factory__Lib_AddressManager: ContractFactory
before(async () => { before(async () => {
;[signer, otherSigner] = await ethers.getSigners() ;[signer1, signer2] = await ethers.getSigners()
Factory__AddressDictator = await ethers.getContractFactory(
'AddressDictator'
)
Factory__Lib_AddressManager = await ethers.getContractFactory(
'Lib_AddressManager'
)
signerAddress = await signer.getAddress()
}) })
let AddressDictator: Contract let AddressDictator: Contract
let Lib_AddressManager: Contract let Lib_AddressManager: Contract
beforeEach(async () => { beforeEach(async () => {
Lib_AddressManager = await Factory__Lib_AddressManager.connect( Lib_AddressManager = await deploy('Lib_AddressManager', {
signer signer: signer1,
).deploy() })
AddressDictator = await Factory__AddressDictator.connect(signer).deploy( AddressDictator = await deploy('AddressDictator', {
Lib_AddressManager.address, signer: signer1,
signerAddress, args: [
['addr1'], Lib_AddressManager.address,
[NON_ZERO_ADDRESS] await signer1.getAddress(),
) ['addr1'],
[NON_ZERO_ADDRESS],
],
})
Lib_AddressManager.transferOwnership(AddressDictator.address) Lib_AddressManager.connect(signer1).transferOwnership(
AddressDictator.address
)
}) })
describe('initialize', () => { describe('initialize', () => {
it('should revert when providing wrong arguments', async () => { it('should revert when providing wrong arguments', async () => {
await expect( await expect(
Factory__AddressDictator.connect(signer).deploy( deploy('AddressDictator', {
Lib_AddressManager.address, signer: signer1,
signerAddress, args: [
['addr1', 'addr2'], Lib_AddressManager.address,
[NON_ZERO_ADDRESS] await signer1.getAddress(),
) ['addr1', 'addr2'],
[NON_ZERO_ADDRESS],
],
})
).to.be.revertedWith( ).to.be.revertedWith(
'AddressDictator: Must provide an equal number of names and addresses.' 'AddressDictator: Must provide an equal number of names and addresses.'
) )
...@@ -61,7 +54,7 @@ describe('AddressDictator', () => { ...@@ -61,7 +54,7 @@ describe('AddressDictator', () => {
describe('setAddresses', async () => { describe('setAddresses', async () => {
it('should change the addresses associated with a name', async () => { it('should change the addresses associated with a name', async () => {
await AddressDictator.setAddresses() await AddressDictator.setAddresses()
expect(await Lib_AddressManager.getAddress('addr1')).to.be.equal( expect(await Lib_AddressManager.getAddress('addr1')).to.equal(
NON_ZERO_ADDRESS NON_ZERO_ADDRESS
) )
}) })
...@@ -69,7 +62,7 @@ describe('AddressDictator', () => { ...@@ -69,7 +62,7 @@ describe('AddressDictator', () => {
describe('getNamedAddresses', () => { describe('getNamedAddresses', () => {
it('should return all the addresses and their names', async () => { 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], ['addr1', NON_ZERO_ADDRESS],
]) ])
}) })
...@@ -77,13 +70,13 @@ describe('AddressDictator', () => { ...@@ -77,13 +70,13 @@ describe('AddressDictator', () => {
describe('returnOwnership', () => { describe('returnOwnership', () => {
it('should transfer contract ownership to finalOwner', async () => { 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 .reverted
}) })
it('should revert when called by non-owner', async () => { it('should revert when called by non-owner', async () => {
await expect( await expect(
AddressDictator.connect(otherSigner).returnOwnership() AddressDictator.connect(signer2).returnOwnership()
).to.be.revertedWith('AddressDictator: only callable by finalOwner') ).to.be.revertedWith('AddressDictator: only callable by finalOwner')
}) })
}) })
......
/* External Imports */
import { ethers } from 'hardhat' import { ethers } from 'hardhat'
import { Contract, Signer, ContractFactory } from 'ethers' import { Contract, Signer } from 'ethers'
/* Internal Imports */
import { expect } from '../../../setup' import { expect } from '../../../setup'
import { deploy } from '../../../helpers'
describe('ChugSplashDictator', () => { describe('ChugSplashDictator', () => {
let signer: Signer let signer1: Signer
let otherSigner: Signer let signer2: Signer
let signerAddress: string
let Factory__L1ChugSplashProxy: ContractFactory
let Factory__ChugSplashDictator: ContractFactory
before(async () => { before(async () => {
;[signer, otherSigner] = await ethers.getSigners() ;[signer1, signer2] = await ethers.getSigners()
Factory__L1ChugSplashProxy = await ethers.getContractFactory(
'L1ChugSplashProxy'
)
Factory__ChugSplashDictator = await ethers.getContractFactory(
'ChugSplashDictator'
)
signerAddress = await signer.getAddress()
}) })
let L1ChugSplashProxy: Contract let L1ChugSplashProxy: Contract
let ChugSplashDictator: Contract let ChugSplashDictator: Contract
beforeEach(async () => { beforeEach(async () => {
L1ChugSplashProxy = await Factory__L1ChugSplashProxy.connect(signer).deploy( L1ChugSplashProxy = await deploy('L1ChugSplashProxy', {
signerAddress signer: signer1,
) args: [await signer1.getAddress()],
})
ChugSplashDictator = await Factory__ChugSplashDictator.connect( ChugSplashDictator = await deploy('ChugSplashDictator', {
signer signer: signer1,
).deploy( args: [
L1ChugSplashProxy.address, L1ChugSplashProxy.address,
signerAddress, await signer1.getAddress(),
ethers.utils.keccak256('0x1111'), ethers.utils.keccak256('0x1111'),
ethers.utils.keccak256('0x1234'), ethers.utils.keccak256('0x1234'),
ethers.utils.keccak256('0x5678'), ethers.utils.keccak256('0x5678'),
ethers.utils.keccak256('0x1234'), ethers.utils.keccak256('0x1234'),
ethers.utils.keccak256('0x1234') ethers.utils.keccak256('0x1234'),
) ],
})
await L1ChugSplashProxy.connect(signer).setOwner(ChugSplashDictator.address) await L1ChugSplashProxy.connect(signer1).setOwner(
ChugSplashDictator.address
)
}) })
describe('doActions', () => { describe('doActions', () => {
...@@ -56,20 +45,20 @@ describe('ChugSplashDictator', () => { ...@@ -56,20 +45,20 @@ describe('ChugSplashDictator', () => {
}) })
it('should set the proxy code, storage & owner', async () => { it('should set the proxy code, storage & owner', async () => {
await expect(ChugSplashDictator.connect(signer).doActions('0x1111')).to await expect(ChugSplashDictator.connect(signer1).doActions('0x1111')).to
.not.be.reverted .not.be.reverted
}) })
}) })
describe('returnOwnership', () => { describe('returnOwnership', () => {
it('should transfer contractc ownership to finalOwner', async () => { it('should transfer contractc ownership to finalOwner', async () => {
await expect(ChugSplashDictator.connect(signer).returnOwnership()).to.not await expect(ChugSplashDictator.connect(signer1).returnOwnership()).to.not
.be.reverted .be.reverted
}) })
it('should revert when called by non-owner', async () => { it('should revert when called by non-owner', async () => {
await expect( await expect(
ChugSplashDictator.connect(otherSigner).returnOwnership() ChugSplashDictator.connect(signer2).returnOwnership()
).to.be.revertedWith('ChugSplashDictator: only callable by finalOwner') ).to.be.revertedWith('ChugSplashDictator: only callable by finalOwner')
}) })
}) })
......
/* Imports: External */
import hre from 'hardhat' import hre from 'hardhat'
import { Contract, Signer } from 'ethers' import { Contract, Signer } from 'ethers'
import { smock } from '@defi-wonderland/smock' import { smock } from '@defi-wonderland/smock'
/* Imports: Internal */
import { expect } from '../../setup' import { expect } from '../../setup'
import { getContractInterface } from '../../../src' import { getContractInterface } from '../../../src'
import { deploy } from '../../helpers'
describe('L1ChugSplashProxy', () => { describe('L1ChugSplashProxy', () => {
let signer1: Signer let signer1: Signer
...@@ -16,12 +15,9 @@ describe('L1ChugSplashProxy', () => { ...@@ -16,12 +15,9 @@ describe('L1ChugSplashProxy', () => {
let L1ChugSplashProxy: Contract let L1ChugSplashProxy: Contract
beforeEach(async () => { beforeEach(async () => {
const Factory__L1ChugSplashProxy = await hre.ethers.getContractFactory( L1ChugSplashProxy = await deploy('L1ChugSplashProxy', {
'L1ChugSplashProxy' args: [await signer1.getAddress()],
) })
L1ChugSplashProxy = await Factory__L1ChugSplashProxy.deploy(
await signer1.getAddress()
)
}) })
describe('getOwner', () => { describe('getOwner', () => {
...@@ -173,14 +169,16 @@ describe('L1ChugSplashProxy', () => { ...@@ -173,14 +169,16 @@ describe('L1ChugSplashProxy', () => {
const owner = await smock.fake<Contract>( const owner = await smock.fake<Contract>(
getContractInterface('iL1ChugSplashDeployer') 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) owner.isUpgrading.returns(true)
await expect( await expect(
owner.wallet.sendTransaction({ owner.wallet.sendTransaction({
to: proxy.address, to: L1ChugSplashProxy.address,
data: '0x', data: '0x',
}) })
).to.be.revertedWith( ).to.be.revertedWith(
......
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 || []))
}
export * from './eth-time' export * from './eth-time'
export * from './deploy'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment