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

maint(ct): clean up GPO tests (#2494)

Cleans up OVM_GasPriceOracle tests using the same techniques as other
test cleanup PRs.
Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 935a98e6
/* External Imports */
import { ethers } from 'hardhat' import { ethers } from 'hardhat'
import { ContractFactory, Contract, Signer } from 'ethers' import { Contract } from 'ethers'
import { calculateL1GasUsed, calculateL1Fee } from '@eth-optimism/core-utils' import { calculateL1GasUsed, calculateL1Fee } from '@eth-optimism/core-utils'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
import { expect } from '../../../setup' import { expect } from '../../../setup'
import { deploy } from '../../../helpers'
describe('OVM_GasPriceOracle', () => { describe('OVM_GasPriceOracle', () => {
const initialGasPrice = 0 const initialGasPrice = 0
let signer1: Signer
let signer2: Signer
before(async () => {
;[signer1, signer2] = await ethers.getSigners()
})
let Factory__OVM_GasPriceOracle: ContractFactory let signer1: SignerWithAddress
let signer2: SignerWithAddress
before(async () => { before(async () => {
Factory__OVM_GasPriceOracle = await ethers.getContractFactory( ;[signer1, signer2] = await ethers.getSigners()
'OVM_GasPriceOracle'
)
}) })
let OVM_GasPriceOracle: Contract let OVM_GasPriceOracle: Contract
beforeEach(async () => { beforeEach(async () => {
OVM_GasPriceOracle = await Factory__OVM_GasPriceOracle.deploy( OVM_GasPriceOracle = await deploy('OVM_GasPriceOracle', {
await signer1.getAddress() signer: signer1,
) args: [signer1.address],
})
await OVM_GasPriceOracle.setOverhead(2750) await OVM_GasPriceOracle.setOverhead(2750)
await OVM_GasPriceOracle.setScalar(1500000) await OVM_GasPriceOracle.setScalar(1500000)
...@@ -33,9 +29,7 @@ describe('OVM_GasPriceOracle', () => { ...@@ -33,9 +29,7 @@ describe('OVM_GasPriceOracle', () => {
describe('owner', () => { describe('owner', () => {
it('should have an owner', async () => { it('should have an owner', async () => {
expect(await OVM_GasPriceOracle.owner()).to.equal( expect(await OVM_GasPriceOracle.owner()).to.equal(signer1.address)
await signer1.getAddress()
)
}) })
}) })
...@@ -46,12 +40,11 @@ describe('OVM_GasPriceOracle', () => { ...@@ -46,12 +40,11 @@ describe('OVM_GasPriceOracle', () => {
}) })
it('should succeed if called by the owner and is equal to `0`', async () => { it('should succeed if called by the owner and is equal to `0`', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setGasPrice(0)).to.not.be await expect(OVM_GasPriceOracle.setGasPrice(0)).to.not.be.reverted
.reverted
}) })
it('should emit event', async () => { it('should emit event', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setGasPrice(100)) await expect(OVM_GasPriceOracle.setGasPrice(100))
.to.emit(OVM_GasPriceOracle, 'GasPriceUpdated') .to.emit(OVM_GasPriceOracle, 'GasPriceUpdated')
.withArgs(100) .withArgs(100)
}) })
...@@ -65,25 +58,18 @@ describe('OVM_GasPriceOracle', () => { ...@@ -65,25 +58,18 @@ describe('OVM_GasPriceOracle', () => {
it('should change when setGasPrice is called', async () => { it('should change when setGasPrice is called', async () => {
const gasPrice = 1234 const gasPrice = 1234
await OVM_GasPriceOracle.connect(signer1).setGasPrice(gasPrice) await OVM_GasPriceOracle.setGasPrice(gasPrice)
expect(await OVM_GasPriceOracle.gasPrice()).to.equal(gasPrice) expect(await OVM_GasPriceOracle.gasPrice()).to.equal(gasPrice)
}) })
it('is the 1st storage slot', async () => { it('is the 1st storage slot', async () => {
const gasPrice = 333433 await OVM_GasPriceOracle.setGasPrice(333433)
const slot = 1
// set the price
await OVM_GasPriceOracle.connect(signer1).setGasPrice(gasPrice)
// get the storage slot value
const priceAtSlot = await signer1.provider.getStorageAt(
OVM_GasPriceOracle.address,
slot
)
expect(await OVM_GasPriceOracle.gasPrice()).to.equal( expect(await OVM_GasPriceOracle.gasPrice()).to.equal(
ethers.BigNumber.from(priceAtSlot) ethers.BigNumber.from(
await signer1.provider.getStorageAt(OVM_GasPriceOracle.address, 1)
)
) )
}) })
}) })
...@@ -95,12 +81,11 @@ describe('OVM_GasPriceOracle', () => { ...@@ -95,12 +81,11 @@ describe('OVM_GasPriceOracle', () => {
}) })
it('should succeed if called by the owner', async () => { it('should succeed if called by the owner', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setL1BaseFee(0)).to.not await expect(OVM_GasPriceOracle.setL1BaseFee(0)).to.not.be.reverted
.be.reverted
}) })
it('should emit event', async () => { it('should emit event', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setL1BaseFee(100)) await expect(OVM_GasPriceOracle.setL1BaseFee(100))
.to.emit(OVM_GasPriceOracle, 'L1BaseFeeUpdated') .to.emit(OVM_GasPriceOracle, 'L1BaseFeeUpdated')
.withArgs(100) .withArgs(100)
}) })
...@@ -113,24 +98,17 @@ describe('OVM_GasPriceOracle', () => { ...@@ -113,24 +98,17 @@ describe('OVM_GasPriceOracle', () => {
it('should change when setL1BaseFee is called', async () => { it('should change when setL1BaseFee is called', async () => {
const baseFee = 1234 const baseFee = 1234
await OVM_GasPriceOracle.connect(signer1).setL1BaseFee(baseFee) await OVM_GasPriceOracle.setL1BaseFee(baseFee)
expect(await OVM_GasPriceOracle.l1BaseFee()).to.equal(baseFee) expect(await OVM_GasPriceOracle.l1BaseFee()).to.equal(baseFee)
}) })
it('is the 2nd storage slot', async () => { it('is the 2nd storage slot', async () => {
const baseFee = 12345 await OVM_GasPriceOracle.setGasPrice(12345)
const slot = 2
// set the price
await OVM_GasPriceOracle.connect(signer1).setGasPrice(baseFee)
// get the storage slot value
const priceAtSlot = await signer1.provider.getStorageAt(
OVM_GasPriceOracle.address,
slot
)
expect(await OVM_GasPriceOracle.l1BaseFee()).to.equal( expect(await OVM_GasPriceOracle.l1BaseFee()).to.equal(
ethers.BigNumber.from(priceAtSlot) ethers.BigNumber.from(
await signer1.provider.getStorageAt(OVM_GasPriceOracle.address, 2)
)
) )
}) })
}) })
...@@ -150,9 +128,10 @@ describe('OVM_GasPriceOracle', () => { ...@@ -150,9 +128,10 @@ describe('OVM_GasPriceOracle', () => {
for (const input of inputs) { for (const input of inputs) {
it(`case: ${input}`, async () => { it(`case: ${input}`, async () => {
const overhead = await OVM_GasPriceOracle.overhead() const overhead = await OVM_GasPriceOracle.overhead()
const cost = await OVM_GasPriceOracle.getL1GasUsed(input)
const expected = calculateL1GasUsed(input, overhead) expect(await OVM_GasPriceOracle.getL1GasUsed(input)).to.deep.equal(
expect(cost).to.deep.equal(expected) calculateL1GasUsed(input, overhead)
)
}) })
} }
}) })
...@@ -162,19 +141,16 @@ describe('OVM_GasPriceOracle', () => { ...@@ -162,19 +141,16 @@ describe('OVM_GasPriceOracle', () => {
it(`case: ${input}`, async () => { it(`case: ${input}`, async () => {
await OVM_GasPriceOracle.setGasPrice(1) await OVM_GasPriceOracle.setGasPrice(1)
await OVM_GasPriceOracle.setL1BaseFee(1) await OVM_GasPriceOracle.setL1BaseFee(1)
const decimals = await OVM_GasPriceOracle.decimals()
const overhead = await OVM_GasPriceOracle.overhead() expect(await OVM_GasPriceOracle.getL1Fee(input)).to.deep.equal(
const scalar = await OVM_GasPriceOracle.scalar() calculateL1Fee(
const l1BaseFee = await OVM_GasPriceOracle.l1BaseFee() input,
const l1Fee = await OVM_GasPriceOracle.getL1Fee(input) await OVM_GasPriceOracle.overhead(),
const expected = calculateL1Fee( await OVM_GasPriceOracle.l1BaseFee(),
input, await OVM_GasPriceOracle.scalar(),
overhead, await OVM_GasPriceOracle.decimals()
l1BaseFee, )
scalar,
decimals
) )
expect(l1Fee).to.deep.equal(expected)
}) })
} }
}) })
...@@ -186,12 +162,11 @@ describe('OVM_GasPriceOracle', () => { ...@@ -186,12 +162,11 @@ describe('OVM_GasPriceOracle', () => {
}) })
it('should succeed if called by the owner', async () => { it('should succeed if called by the owner', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setOverhead(0)).to.not.be await expect(OVM_GasPriceOracle.setOverhead(0)).to.not.be.reverted
.reverted
}) })
it('should emit event', async () => { it('should emit event', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setOverhead(100)) await expect(OVM_GasPriceOracle.setOverhead(100))
.to.emit(OVM_GasPriceOracle, 'OverheadUpdated') .to.emit(OVM_GasPriceOracle, 'OverheadUpdated')
.withArgs(100) .withArgs(100)
}) })
...@@ -204,24 +179,17 @@ describe('OVM_GasPriceOracle', () => { ...@@ -204,24 +179,17 @@ describe('OVM_GasPriceOracle', () => {
it('should change when setOverhead is called', async () => { it('should change when setOverhead is called', async () => {
const overhead = 6657 const overhead = 6657
await OVM_GasPriceOracle.connect(signer1).setOverhead(overhead) await OVM_GasPriceOracle.setOverhead(overhead)
expect(await OVM_GasPriceOracle.overhead()).to.equal(overhead) expect(await OVM_GasPriceOracle.overhead()).to.equal(overhead)
}) })
it('is the 3rd storage slot', async () => { it('is the 3rd storage slot', async () => {
const overhead = 119090 await OVM_GasPriceOracle.setOverhead(119090)
const slot = 3
// set the price
await OVM_GasPriceOracle.connect(signer1).setOverhead(overhead)
// get the storage slot value
const priceAtSlot = await signer1.provider.getStorageAt(
OVM_GasPriceOracle.address,
slot
)
expect(await OVM_GasPriceOracle.overhead()).to.equal( expect(await OVM_GasPriceOracle.overhead()).to.equal(
ethers.BigNumber.from(priceAtSlot) ethers.BigNumber.from(
await signer1.provider.getStorageAt(OVM_GasPriceOracle.address, 3)
)
) )
}) })
}) })
...@@ -233,12 +201,11 @@ describe('OVM_GasPriceOracle', () => { ...@@ -233,12 +201,11 @@ describe('OVM_GasPriceOracle', () => {
}) })
it('should succeed if called by the owner', async () => { it('should succeed if called by the owner', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setScalar(0)).to.not.be await expect(OVM_GasPriceOracle.setScalar(0)).to.not.be.reverted
.reverted
}) })
it('should emit event', async () => { it('should emit event', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setScalar(100)) await expect(OVM_GasPriceOracle.setScalar(100))
.to.emit(OVM_GasPriceOracle, 'ScalarUpdated') .to.emit(OVM_GasPriceOracle, 'ScalarUpdated')
.withArgs(100) .withArgs(100)
}) })
...@@ -251,39 +218,27 @@ describe('OVM_GasPriceOracle', () => { ...@@ -251,39 +218,27 @@ describe('OVM_GasPriceOracle', () => {
it('should change when setScalar is called', async () => { it('should change when setScalar is called', async () => {
const scalar = 9999 const scalar = 9999
await OVM_GasPriceOracle.connect(signer1).setScalar(scalar) await OVM_GasPriceOracle.setScalar(scalar)
expect(await OVM_GasPriceOracle.scalar()).to.equal(scalar) expect(await OVM_GasPriceOracle.scalar()).to.equal(scalar)
}) })
it('is the 4rd storage slot', async () => { it('is the 4rd storage slot', async () => {
const overhead = 111111 await OVM_GasPriceOracle.setScalar(111111)
const slot = 4
// set the price
await OVM_GasPriceOracle.connect(signer1).setScalar(overhead)
// get the storage slot value
const priceAtSlot = await signer1.provider.getStorageAt(
OVM_GasPriceOracle.address,
slot
)
expect(await OVM_GasPriceOracle.scalar()).to.equal( expect(await OVM_GasPriceOracle.scalar()).to.equal(
ethers.BigNumber.from(priceAtSlot) ethers.BigNumber.from(
await signer1.provider.getStorageAt(OVM_GasPriceOracle.address, 4)
)
) )
}) })
}) })
describe('decimals', () => { describe('decimals', () => {
it('is the 5th storage slot', async () => { it('is the 5th storage slot', async () => {
const slot = 5
// get the storage slot value
const priceAtSlot = await signer1.provider.getStorageAt(
OVM_GasPriceOracle.address,
slot
)
expect(await OVM_GasPriceOracle.decimals()).to.equal( expect(await OVM_GasPriceOracle.decimals()).to.equal(
ethers.BigNumber.from(priceAtSlot) ethers.BigNumber.from(
await signer1.provider.getStorageAt(OVM_GasPriceOracle.address, 5)
)
) )
}) })
}) })
......
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