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 { 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')
})
})
......
/* External Imports */
import { ethers } from 'hardhat'
import { Contract, Signer, ContractFactory } from 'ethers'
import { Contract, Signer } from 'ethers'
/* Internal Imports */
import { expect } from '../../../setup'
import { deploy } from '../../../helpers'
describe('ChugSplashDictator', () => {
let signer: Signer
let otherSigner: Signer
let signerAddress: string
let Factory__L1ChugSplashProxy: ContractFactory
let Factory__ChugSplashDictator: ContractFactory
let signer1: Signer
let signer2: Signer
before(async () => {
;[signer, otherSigner] = await ethers.getSigners()
Factory__L1ChugSplashProxy = await ethers.getContractFactory(
'L1ChugSplashProxy'
)
Factory__ChugSplashDictator = await ethers.getContractFactory(
'ChugSplashDictator'
)
signerAddress = await signer.getAddress()
;[signer1, signer2] = await ethers.getSigners()
})
let L1ChugSplashProxy: Contract
let ChugSplashDictator: Contract
beforeEach(async () => {
L1ChugSplashProxy = await Factory__L1ChugSplashProxy.connect(signer).deploy(
signerAddress
)
L1ChugSplashProxy = await deploy('L1ChugSplashProxy', {
signer: signer1,
args: [await signer1.getAddress()],
})
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')
)
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(signer).setOwner(ChugSplashDictator.address)
await L1ChugSplashProxy.connect(signer1).setOwner(
ChugSplashDictator.address
)
})
describe('doActions', () => {
......@@ -56,20 +45,20 @@ describe('ChugSplashDictator', () => {
})
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
})
})
describe('returnOwnership', () => {
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
})
it('should revert when called by non-owner', async () => {
await expect(
ChugSplashDictator.connect(otherSigner).returnOwnership()
ChugSplashDictator.connect(signer2).returnOwnership()
).to.be.revertedWith('ChugSplashDictator: only callable by finalOwner')
})
})
......
/* 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<Contract>(
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(
......
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 './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