Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
/* External Imports */
import { ethers } from 'hardhat'
import { ContractFactory, Contract, Signer } from 'ethers'
import { Contract } from 'ethers'
import { calculateL1GasUsed, calculateL1Fee } from '@eth-optimism/core-utils'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'

import { expect } from '../../../setup'
import { deploy } from '../../../helpers'

describe('OVM_GasPriceOracle', () => {
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 () => {
Factory__OVM_GasPriceOracle = await ethers.getContractFactory(
'OVM_GasPriceOracle'
)
;[signer1, signer2] = await ethers.getSigners()
})

let OVM_GasPriceOracle: Contract
beforeEach(async () => {
OVM_GasPriceOracle = await Factory__OVM_GasPriceOracle.deploy(
await signer1.getAddress()
)
OVM_GasPriceOracle = await deploy('OVM_GasPriceOracle', {
signer: signer1,
args: [signer1.address],
})

await OVM_GasPriceOracle.setOverhead(2750)
await OVM_GasPriceOracle.setScalar(1500000)
Expand All @@ -33,9 +29,7 @@ describe('OVM_GasPriceOracle', () => {

describe('owner', () => {
it('should have an owner', async () => {
expect(await OVM_GasPriceOracle.owner()).to.equal(
await signer1.getAddress()
)
expect(await OVM_GasPriceOracle.owner()).to.equal(signer1.address)
})
})

Expand All @@ -46,12 +40,11 @@ describe('OVM_GasPriceOracle', () => {
})

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
.reverted
await expect(OVM_GasPriceOracle.setGasPrice(0)).to.not.be.reverted
})

it('should emit event', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setGasPrice(100))
await expect(OVM_GasPriceOracle.setGasPrice(100))
.to.emit(OVM_GasPriceOracle, 'GasPriceUpdated')
.withArgs(100)
})
Expand All @@ -65,25 +58,18 @@ describe('OVM_GasPriceOracle', () => {
it('should change when setGasPrice is called', async () => {
const gasPrice = 1234

await OVM_GasPriceOracle.connect(signer1).setGasPrice(gasPrice)
await OVM_GasPriceOracle.setGasPrice(gasPrice)

expect(await OVM_GasPriceOracle.gasPrice()).to.equal(gasPrice)
})

it('is the 1st storage slot', async () => {
const gasPrice = 333433
const slot = 1
await OVM_GasPriceOracle.setGasPrice(333433)

// 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(
ethers.BigNumber.from(priceAtSlot)
ethers.BigNumber.from(
await signer1.provider.getStorageAt(OVM_GasPriceOracle.address, 1)
)
)
})
})
Expand All @@ -95,12 +81,11 @@ describe('OVM_GasPriceOracle', () => {
})

it('should succeed if called by the owner', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setL1BaseFee(0)).to.not
.be.reverted
await expect(OVM_GasPriceOracle.setL1BaseFee(0)).to.not.be.reverted
})

it('should emit event', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setL1BaseFee(100))
await expect(OVM_GasPriceOracle.setL1BaseFee(100))
.to.emit(OVM_GasPriceOracle, 'L1BaseFeeUpdated')
.withArgs(100)
})
Expand All @@ -113,24 +98,17 @@ describe('OVM_GasPriceOracle', () => {

it('should change when setL1BaseFee is called', async () => {
const baseFee = 1234
await OVM_GasPriceOracle.connect(signer1).setL1BaseFee(baseFee)
await OVM_GasPriceOracle.setL1BaseFee(baseFee)
expect(await OVM_GasPriceOracle.l1BaseFee()).to.equal(baseFee)
})

it('is the 2nd storage slot', async () => {
const baseFee = 12345
const slot = 2

// set the price
await OVM_GasPriceOracle.connect(signer1).setGasPrice(baseFee)
await OVM_GasPriceOracle.setGasPrice(12345)

// get the storage slot value
const priceAtSlot = await signer1.provider.getStorageAt(
OVM_GasPriceOracle.address,
slot
)
expect(await OVM_GasPriceOracle.l1BaseFee()).to.equal(
ethers.BigNumber.from(priceAtSlot)
ethers.BigNumber.from(
await signer1.provider.getStorageAt(OVM_GasPriceOracle.address, 2)
)
)
})
})
Expand All @@ -150,9 +128,10 @@ describe('OVM_GasPriceOracle', () => {
for (const input of inputs) {
it(`case: ${input}`, async () => {
const overhead = await OVM_GasPriceOracle.overhead()
const cost = await OVM_GasPriceOracle.getL1GasUsed(input)
const expected = calculateL1GasUsed(input, overhead)
expect(cost).to.deep.equal(expected)

expect(await OVM_GasPriceOracle.getL1GasUsed(input)).to.deep.equal(
calculateL1GasUsed(input, overhead)
)
})
}
})
Expand All @@ -162,19 +141,16 @@ describe('OVM_GasPriceOracle', () => {
it(`case: ${input}`, async () => {
await OVM_GasPriceOracle.setGasPrice(1)
await OVM_GasPriceOracle.setL1BaseFee(1)
const decimals = await OVM_GasPriceOracle.decimals()
const overhead = await OVM_GasPriceOracle.overhead()
const scalar = await OVM_GasPriceOracle.scalar()
const l1BaseFee = await OVM_GasPriceOracle.l1BaseFee()
const l1Fee = await OVM_GasPriceOracle.getL1Fee(input)
const expected = calculateL1Fee(
input,
overhead,
l1BaseFee,
scalar,
decimals

expect(await OVM_GasPriceOracle.getL1Fee(input)).to.deep.equal(
calculateL1Fee(
input,
await OVM_GasPriceOracle.overhead(),
await OVM_GasPriceOracle.l1BaseFee(),
await OVM_GasPriceOracle.scalar(),
await OVM_GasPriceOracle.decimals()
)
)
expect(l1Fee).to.deep.equal(expected)
})
}
})
Expand All @@ -186,12 +162,11 @@ describe('OVM_GasPriceOracle', () => {
})

it('should succeed if called by the owner', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setOverhead(0)).to.not.be
.reverted
await expect(OVM_GasPriceOracle.setOverhead(0)).to.not.be.reverted
})

it('should emit event', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setOverhead(100))
await expect(OVM_GasPriceOracle.setOverhead(100))
.to.emit(OVM_GasPriceOracle, 'OverheadUpdated')
.withArgs(100)
})
Expand All @@ -204,24 +179,17 @@ describe('OVM_GasPriceOracle', () => {

it('should change when setOverhead is called', async () => {
const overhead = 6657
await OVM_GasPriceOracle.connect(signer1).setOverhead(overhead)
await OVM_GasPriceOracle.setOverhead(overhead)
expect(await OVM_GasPriceOracle.overhead()).to.equal(overhead)
})

it('is the 3rd storage slot', async () => {
const overhead = 119090
const slot = 3
await OVM_GasPriceOracle.setOverhead(119090)

// 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(
ethers.BigNumber.from(priceAtSlot)
ethers.BigNumber.from(
await signer1.provider.getStorageAt(OVM_GasPriceOracle.address, 3)
)
)
})
})
Expand All @@ -233,12 +201,11 @@ describe('OVM_GasPriceOracle', () => {
})

it('should succeed if called by the owner', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setScalar(0)).to.not.be
.reverted
await expect(OVM_GasPriceOracle.setScalar(0)).to.not.be.reverted
})

it('should emit event', async () => {
await expect(OVM_GasPriceOracle.connect(signer1).setScalar(100))
await expect(OVM_GasPriceOracle.setScalar(100))
.to.emit(OVM_GasPriceOracle, 'ScalarUpdated')
.withArgs(100)
})
Expand All @@ -251,39 +218,27 @@ describe('OVM_GasPriceOracle', () => {

it('should change when setScalar is called', async () => {
const scalar = 9999
await OVM_GasPriceOracle.connect(signer1).setScalar(scalar)
await OVM_GasPriceOracle.setScalar(scalar)
expect(await OVM_GasPriceOracle.scalar()).to.equal(scalar)
})

it('is the 4rd storage slot', async () => {
const overhead = 111111
const slot = 4

// set the price
await OVM_GasPriceOracle.connect(signer1).setScalar(overhead)
await OVM_GasPriceOracle.setScalar(111111)

// get the storage slot value
const priceAtSlot = await signer1.provider.getStorageAt(
OVM_GasPriceOracle.address,
slot
)
expect(await OVM_GasPriceOracle.scalar()).to.equal(
ethers.BigNumber.from(priceAtSlot)
ethers.BigNumber.from(
await signer1.provider.getStorageAt(OVM_GasPriceOracle.address, 4)
)
)
})
})

describe('decimals', () => {
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(
ethers.BigNumber.from(priceAtSlot)
ethers.BigNumber.from(
await signer1.provider.getStorageAt(OVM_GasPriceOracle.address, 5)
)
)
})
})
Expand Down