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 () => {
const addr = '0x' + '1234'.repeat(10)
const gas = await env.ovmEth.estimateGas.transfer(addr, amount)
// 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 () => {
......@@ -69,7 +69,7 @@ describe('Native ETH Integration Tests', async () => {
'0xFFFF'
)
// 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', () => {
value: 0,
})
// 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 () => {
......
......@@ -142,35 +142,39 @@ export const DEFAULT_TRANSACTION = {
value: 0,
}
interface percentDeviationRange {
upperPercentDeviation: number
lowerPercentDeviation?: number
}
export const expectApprox = (
actual: BigNumber | number,
target: BigNumber | number,
upperDeviation: number,
lowerDeviation: number = 100
{ upperPercentDeviation, lowerPercentDeviation = 100 }: percentDeviationRange
) => {
actual = BigNumber.from(actual)
target = BigNumber.from(target)
const validDeviations =
upperDeviation >= 0 &&
upperDeviation <= 100 &&
lowerDeviation >= 0 &&
lowerDeviation <= 100
upperPercentDeviation >= 0 &&
upperPercentDeviation <= 100 &&
lowerPercentDeviation >= 0 &&
lowerPercentDeviation <= 100
if (!validDeviations) {
throw new Error(
'Upper and lower deviation percentage arguments should be between 0 and 100'
)
}
const upper = target.mul(100 + upperDeviation).div(100)
const lower = target.mul(100 - lowerDeviation).div(100)
const upper = target.mul(100 + upperPercentDeviation).div(100)
const lower = target.mul(100 - lowerPercentDeviation).div(100)
expect(
actual.lte(upper),
`Actual value is more than ${upperDeviation}% greater than target`
`Actual value is more than ${upperPercentDeviation}% greater than target`
).to.be.true
expect(
actual.gte(lower),
`Actual value is more than ${lowerDeviation}% less than target`
`Actual value is more than ${lowerPercentDeviation}% less than target`
).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