Commit b107a032 authored by Reggie Gomez's avatar Reggie Gomez Committed by smartcontracts

refactor(integration): Add clarity to the expectApprox function signature

parent 70c89129
---
'@eth-optimism/integration-tests': patch
---
Make expectApprox more readable by passing optional args as an object with well named keys
...@@ -57,7 +57,7 @@ describe('Native ETH Integration Tests', async () => { ...@@ -57,7 +57,7 @@ describe('Native ETH Integration Tests', async () => {
const addr = '0x' + '1234'.repeat(10) const addr = '0x' + '1234'.repeat(10)
const gas = await env.ovmEth.estimateGas.transfer(addr, amount) const gas = await env.ovmEth.estimateGas.transfer(addr, amount)
// Expect gas to be less than or equal to the target plus 1% // Expect gas to be less than or equal to the target plus 1%
expectApprox(gas, 6430020, 1) expectApprox(gas, 6430020, { upperPercentDeviation: 1 })
}) })
it('Should estimate gas for ETH withdraw', async () => { it('Should estimate gas for ETH withdraw', async () => {
...@@ -69,7 +69,7 @@ describe('Native ETH Integration Tests', async () => { ...@@ -69,7 +69,7 @@ describe('Native ETH Integration Tests', async () => {
'0xFFFF' '0xFFFF'
) )
// Expect gas to be less than or equal to the target plus 1% // Expect gas to be less than or equal to the target plus 1%
expectApprox(gas, 6700060, 1) expectApprox(gas, 6700060, { upperPercentDeviation: 1 })
}) })
}) })
......
...@@ -376,7 +376,7 @@ describe('Basic RPC tests', () => { ...@@ -376,7 +376,7 @@ describe('Basic RPC tests', () => {
value: 0, value: 0,
}) })
// Expect gas to be less than or equal to the target plus 1% // Expect gas to be less than or equal to the target plus 1%
expectApprox(estimate, 5920012, 1) expectApprox(estimate, 5920012, { upperPercentDeviation: 1 })
}) })
it('should return a gas estimate that grows with the size of data', async () => { it('should return a gas estimate that grows with the size of data', async () => {
......
...@@ -142,35 +142,39 @@ export const DEFAULT_TRANSACTION = { ...@@ -142,35 +142,39 @@ export const DEFAULT_TRANSACTION = {
value: 0, value: 0,
} }
interface percentDeviationRange {
upperPercentDeviation: number
lowerPercentDeviation?: number
}
export const expectApprox = ( export const expectApprox = (
actual: BigNumber | number, actual: BigNumber | number,
target: BigNumber | number, target: BigNumber | number,
upperDeviation: number, { upperPercentDeviation, lowerPercentDeviation = 100 }: percentDeviationRange
lowerDeviation: number = 100
) => { ) => {
actual = BigNumber.from(actual) actual = BigNumber.from(actual)
target = BigNumber.from(target) target = BigNumber.from(target)
const validDeviations = const validDeviations =
upperDeviation >= 0 && upperPercentDeviation >= 0 &&
upperDeviation <= 100 && upperPercentDeviation <= 100 &&
lowerDeviation >= 0 && lowerPercentDeviation >= 0 &&
lowerDeviation <= 100 lowerPercentDeviation <= 100
if (!validDeviations) { if (!validDeviations) {
throw new Error( throw new Error(
'Upper and lower deviation percentage arguments should be between 0 and 100' 'Upper and lower deviation percentage arguments should be between 0 and 100'
) )
} }
const upper = target.mul(100 + upperDeviation).div(100) const upper = target.mul(100 + upperPercentDeviation).div(100)
const lower = target.mul(100 - lowerDeviation).div(100) const lower = target.mul(100 - lowerPercentDeviation).div(100)
expect( expect(
actual.lte(upper), actual.lte(upper),
`Actual value is more than ${upperDeviation}% greater than target` `Actual value is more than ${upperPercentDeviation}% greater than target`
).to.be.true ).to.be.true
expect( expect(
actual.gte(lower), actual.gte(lower),
`Actual value is more than ${lowerDeviation}% less than target` `Actual value is more than ${lowerPercentDeviation}% less than target`
).to.be.true ).to.be.true
} }
......
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