Commit 9142149a authored by Maurelian's avatar Maurelian Committed by Kelvin Fichter

refactor: Move expectApprox to core-utils

parent 9c1443a4
...@@ -4,15 +4,12 @@ import { expect } from 'chai' ...@@ -4,15 +4,12 @@ import { expect } from 'chai'
import { Wallet, utils, BigNumber } from 'ethers' import { Wallet, utils, BigNumber } from 'ethers'
import { serialize } from '@ethersproject/transactions' import { serialize } from '@ethersproject/transactions'
import { predeploys } from '@eth-optimism/contracts' import { predeploys } from '@eth-optimism/contracts'
import { expectApprox } from '@eth-optimism/core-utils'
/* Imports: Internal */ /* Imports: Internal */
import { Direction } from './shared/watcher-utils' import { Direction } from './shared/watcher-utils'
import { import { fundUser, PROXY_SEQUENCER_ENTRYPOINT_ADDRESS } from './shared/utils'
expectApprox,
fundUser,
PROXY_SEQUENCER_ENTRYPOINT_ADDRESS,
} from './shared/utils'
import { OptimismEnv, useDynamicTimeoutForWithdrawals } from './shared/env' import { OptimismEnv, useDynamicTimeoutForWithdrawals } from './shared/env'
const DEFAULT_TEST_GAS_L1 = 330_000 const DEFAULT_TEST_GAS_L1 = 330_000
......
import { injectL2Context } from '@eth-optimism/core-utils' import { expectApprox, injectL2Context } from '@eth-optimism/core-utils'
import { Wallet, BigNumber, Contract, ContractFactory } from 'ethers' import { Wallet, BigNumber, Contract, ContractFactory } from 'ethers'
import { serialize } from '@ethersproject/transactions' import { serialize } from '@ethersproject/transactions'
import { ethers } from 'hardhat' import { ethers } from 'hardhat'
...@@ -8,7 +8,6 @@ import { ...@@ -8,7 +8,6 @@ import {
l2Provider, l2Provider,
DEFAULT_TRANSACTION, DEFAULT_TRANSACTION,
fundUser, fundUser,
expectApprox,
L2_CHAINID, L2_CHAINID,
IS_LIVE_NETWORK, IS_LIVE_NETWORK,
} from './shared/utils' } from './shared/utils'
......
...@@ -154,42 +154,6 @@ export const DEFAULT_TRANSACTION = { ...@@ -154,42 +154,6 @@ export const DEFAULT_TRANSACTION = {
value: 0, value: 0,
} }
interface percentDeviationRange {
upperPercentDeviation: number
lowerPercentDeviation?: number
}
export const expectApprox = (
actual: BigNumber | number,
target: BigNumber | number,
{ upperPercentDeviation, lowerPercentDeviation = 100 }: percentDeviationRange
) => {
actual = BigNumber.from(actual)
target = BigNumber.from(target)
const validDeviations =
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 + upperPercentDeviation).div(100)
const lower = target.mul(100 - lowerPercentDeviation).div(100)
expect(
actual.lte(upper),
`Actual value (${actual}) is more than ${upperPercentDeviation}% greater than target (${target})`
).to.be.true
expect(
actual.gte(lower),
`Actual value (${actual}) is more than ${lowerPercentDeviation}% less than target (${target})`
).to.be.true
}
export const waitForL2Geth = async ( export const waitForL2Geth = async (
provider: providers.JsonRpcProvider provider: providers.JsonRpcProvider
): Promise<providers.JsonRpcProvider> => { ): Promise<providers.JsonRpcProvider> => {
......
...@@ -6,6 +6,7 @@ import { ...@@ -6,6 +6,7 @@ import {
AppendSequencerBatchParams, AppendSequencerBatchParams,
BatchContext, BatchContext,
encodeAppendSequencerBatch, encodeAppendSequencerBatch,
expectApprox,
} from '@eth-optimism/core-utils' } from '@eth-optimism/core-utils'
import { TransactionResponse } from '@ethersproject/abstract-provider' import { TransactionResponse } from '@ethersproject/abstract-provider'
import { keccak256 } from 'ethers/lib/utils' import { keccak256 } from 'ethers/lib/utils'
...@@ -21,7 +22,6 @@ import { ...@@ -21,7 +22,6 @@ import {
getEthTime, getEthTime,
getNextBlockNumber, getNextBlockNumber,
NON_ZERO_ADDRESS, NON_ZERO_ADDRESS,
expectApprox,
} from '../../../helpers' } from '../../../helpers'
// Still have some duplication from CanonicalTransactionChain.spec.ts, but it's so minimal that // Still have some duplication from CanonicalTransactionChain.spec.ts, but it's so minimal that
......
import { expect } from 'chai'
import { ethers } from 'hardhat' import { ethers } from 'hardhat'
import { BigNumber, Contract, Signer } from 'ethers' import { Contract, Signer } from 'ethers'
export class GasMeasurement { export class GasMeasurement {
GasMeasurementContract: Contract GasMeasurementContract: Contract
...@@ -25,39 +24,3 @@ export class GasMeasurement { ...@@ -25,39 +24,3 @@ export class GasMeasurement {
return gasCost return gasCost
} }
} }
interface percentDeviationRange {
upperPercentDeviation: number
lowerPercentDeviation?: number
}
export const expectApprox = (
actual: BigNumber | number,
target: BigNumber | number,
{ upperPercentDeviation, lowerPercentDeviation = 100 }: percentDeviationRange
): void => {
actual = BigNumber.from(actual)
target = BigNumber.from(target)
const validDeviations =
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 + upperPercentDeviation).div(100)
const lower = target.mul(100 - lowerPercentDeviation).div(100)
expect(
actual.lte(upper),
`Actual value (${actual}) is more than ${upperPercentDeviation}% greater than target (${target})`
).to.be.true
expect(
actual.gte(lower),
`Actual value (${actual}) is more than ${lowerPercentDeviation}% less than target (${target})`
).to.be.true
}
export * from './hex-strings' export * from './hex-strings'
export * from './misc' export * from './misc'
export * from './test-utils'
import { expect } from 'chai'
import { BigNumber } from 'ethers'
interface percentDeviationRange {
upperPercentDeviation: number
lowerPercentDeviation?: number
}
/**
* Assert that a number lies within a custom defined range of the target.
*/
export const expectApprox = (
actual: BigNumber | number,
target: BigNumber | number,
{ upperPercentDeviation, lowerPercentDeviation = 100 }: percentDeviationRange
): void => {
actual = BigNumber.from(actual)
target = BigNumber.from(target)
const validDeviations =
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 + upperPercentDeviation).div(100)
const lower = target.mul(100 - lowerPercentDeviation).div(100)
expect(
actual.lte(upper),
`Actual value (${actual}) is more than ${upperPercentDeviation}% greater than target (${target})`
).to.be.true
expect(
actual.gte(lower),
`Actual value (${actual}) is more than ${lowerPercentDeviation}% less than target (${target})`
).to.be.true
}
import { expect } from '../setup'
/* Imports: Internal */
import { expectApprox } from '../../src'
describe('expectApprox', async () => {
it('should throw an error if the actual value is higher than expected', async () => {
try {
expectApprox(121, 100, {
upperPercentDeviation: 20,
})
} catch (error) {
expect(error.message).to.equal(
'Actual value (121) is more than 20% greater than target (100): expected false to be true'
)
}
})
it('should throw an error if the actual value is lower than expected', async () => {
try {
expectApprox(79, 100, {
upperPercentDeviation: 0,
lowerPercentDeviation: 20,
})
} catch (error) {
expect(error.message).to.equal(
'Actual value (79) is more than 20% less than target (100): expected false 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