utils.ts 5.89 KB
Newer Older
1
/* Imports: External */
2 3
import { Wallet, providers, BigNumber, utils } from 'ethers'
import { predeploys } from '@eth-optimism/contracts'
4 5 6 7 8 9
import { remove0x } from '@eth-optimism/core-utils'
import {
  CrossChainMessenger,
  NumberLike,
  asL2Provider,
} from '@eth-optimism/sdk'
10
import { cleanEnv, str, num, bool, makeValidator } from 'envalid'
11
import dotenv from 'dotenv'
12
dotenv.config()
13 14

/* Imports: Internal */
15
import { OptimismEnv } from './env'
16

17 18 19 20
export const isLiveNetwork = () => {
  return process.env.IS_LIVE_NETWORK === 'true'
}

21 22 23 24 25 26 27 28 29
export const HARDHAT_CHAIN_ID = 31337
export const DEFAULT_TEST_GAS_L1 = 330_000
export const DEFAULT_TEST_GAS_L2 = 1_300_000
export const ON_CHAIN_GAS_PRICE = 'onchain'

const gasPriceValidator = makeValidator((gasPrice) => {
  if (gasPrice === 'onchain') {
    return gasPrice
  }
30

31 32 33 34 35 36 37
  return num()._parse(gasPrice).toString()
})

const procEnv = cleanEnv(process.env, {
  L1_GAS_PRICE: gasPriceValidator({
    default: '0',
  }),
38
  L1_URL: str({ default: 'http://localhost:9545' }),
39
  L1_POLLING_INTERVAL: num({ default: 10 }),
40

41
  L2_CHAINID: num({ default: 987 }),
42 43 44 45
  L2_GAS_PRICE: gasPriceValidator({
    default: 'onchain',
  }),
  L2_URL: str({ default: 'http://localhost:8545' }),
46
  L2_POLLING_INTERVAL: num({ default: 10 }),
47 48 49 50 51 52 53 54
  L2_WALLET_MIN_BALANCE_ETH: num({
    default: 2,
  }),
  L2_WALLET_TOP_UP_AMOUNT_ETH: num({
    default: 3,
  }),

  REPLICA_URL: str({ default: 'http://localhost:8549' }),
55
  REPLICA_POLLING_INTERVAL: num({ default: 10 }),
56

57 58
  VERIFIER_URL: str({ default: 'http://localhost:8547' }),

59 60
  HEALTHCHECK_URL: str({ default: 'http://localhost:7300/metrics' }),

61 62 63 64
  PRIVATE_KEY: str({
    default:
      '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
  }),
65 66 67 68
  GAS_PRICE_ORACLE_PRIVATE_KEY: str({
    default:
      '0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba',
  }),
69 70 71 72 73 74 75 76 77 78 79 80 81 82

  OVMCONTEXT_SPEC_NUM_TXS: num({
    default: 5,
  }),
  DTL_ENQUEUE_CONFIRMATIONS: num({
    default: 0,
  }),

  RUN_WITHDRAWAL_TESTS: bool({
    default: true,
  }),
  RUN_REPLICA_TESTS: bool({
    default: true,
  }),
83 84 85
  RUN_HEALTHCHECK_TESTS: bool({
    default: true,
  }),
86 87 88 89 90 91
  RUN_DEBUG_TRACE_TESTS: bool({
    default: true,
  }),
  RUN_STRESS_TESTS: bool({
    default: true,
  }),
92 93 94
  RUN_VERIFIER_TESTS: bool({
    default: true,
  }),
95 96 97
  RUN_SYSTEM_ADDRESS_TESTS: bool({
    default: false,
  }),
98 99 100 101 102 103 104

  MOCHA_TIMEOUT: num({
    default: 120_000,
  }),
  MOCHA_BAIL: bool({
    default: false,
  }),
105 106 107
  BATCH_SUBMITTER_SEQUENCER_BATCH_TYPE: str({
    default: 'zlib',
  }),
108 109
})

110 111
export const envConfig = procEnv

112
// The hardhat instance
113 114
export const l1Provider = new providers.JsonRpcProvider(procEnv.L1_URL)
l1Provider.pollingInterval = procEnv.L1_POLLING_INTERVAL
115

116
export const l2Provider = asL2Provider(
117
  new providers.JsonRpcProvider(procEnv.L2_URL)
118
)
119
l2Provider.pollingInterval = procEnv.L2_POLLING_INTERVAL
120

121
export const replicaProvider = asL2Provider(
122
  new providers.JsonRpcProvider(procEnv.REPLICA_URL)
123
)
124
replicaProvider.pollingInterval = procEnv.REPLICA_POLLING_INTERVAL
125

126
export const verifierProvider = asL2Provider(
127 128 129 130
  new providers.JsonRpcProvider(procEnv.VERIFIER_URL)
)
verifierProvider.pollingInterval = procEnv.L2_POLLING_INTERVAL

131
// The sequencer private key which is funded on L1
132
export const l1Wallet = new Wallet(procEnv.PRIVATE_KEY, l1Provider)
133 134 135 136 137

// A random private key which should always be funded with deposits from L1 -> L2
// if it's using non-0 gas price
export const l2Wallet = l1Wallet.connect(l2Provider)

138 139
// The owner of the GasPriceOracle on L2
export const gasPriceOracleWallet = new Wallet(
140
  procEnv.GAS_PRICE_ORACLE_PRIVATE_KEY,
141 142 143
  l2Provider
)

144
// Predeploys
145
export const OVM_ETH_ADDRESS = predeploys.OVM_ETH
146

147
export const L2_CHAINID = procEnv.L2_CHAINID
148

149
export const fundUser = async (
150 151
  messenger: CrossChainMessenger,
  amount: NumberLike,
152 153
  recipient?: string
) => {
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  await messenger.waitForMessageReceipt(
    await messenger.depositETH(amount, {
      l2GasLimit: DEFAULT_TEST_GAS_L2,
      overrides: {
        gasPrice: DEFAULT_TEST_GAS_L1,
      },
    })
  )

  if (recipient !== undefined) {
    const tx = await messenger.l2Signer.sendTransaction({
      to: recipient,
      value: amount,
    })
    await tx.wait()
  }
170 171
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
export const conditionalTest = (
  condition: (env?: OptimismEnv) => Promise<boolean>,
  name,
  fn,
  message?: string,
  timeout?: number
) => {
  it(name, async function () {
    const shouldRun = await condition()
    if (!shouldRun) {
      console.log(message)
      this.skip()
      return
    }

    await fn()
188
  }).timeout(timeout || envConfig.MOCHA_TIMEOUT * 2)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
}

export const withdrawalTest = (name, fn, timeout?: number) =>
  conditionalTest(
    () => Promise.resolve(procEnv.RUN_WITHDRAWAL_TESTS),
    name,
    fn,
    `Skipping withdrawal test.`,
    timeout
  )

export const hardhatTest = (name, fn) =>
  conditionalTest(
    isHardhat,
    name,
    fn,
    'Skipping test on non-Hardhat environment.'
  )

208 209 210 211
const abiCoder = new utils.AbiCoder()
export const encodeSolidityRevertMessage = (_reason: string): string => {
  return '0x08c379a0' + remove0x(abiCoder.encode(['string'], [_reason]))
}
212

213 214 215 216 217 218 219 220
export const defaultTransactionFactory = () => {
  return {
    to: '0x' + '1234'.repeat(10),
    gasLimit: 8_000_000,
    gasPrice: BigNumber.from(0),
    data: '0x',
    value: 0,
  }
221
}
222

223 224 225
export const gasPriceForL2 = async () => {
  if (procEnv.L2_GAS_PRICE === ON_CHAIN_GAS_PRICE) {
    return l2Wallet.getGasPrice()
Matthew Slipper's avatar
Matthew Slipper committed
226 227
  }

228
  return utils.parseUnits(procEnv.L2_GAS_PRICE, 'wei')
229 230
}

231 232 233
export const gasPriceForL1 = async () => {
  if (procEnv.L1_GAS_PRICE === ON_CHAIN_GAS_PRICE) {
    return l1Wallet.getGasPrice()
234
  }
235 236

  return utils.parseUnits(procEnv.L1_GAS_PRICE, 'wei')
237
}
Matthew Slipper's avatar
Matthew Slipper committed
238

239 240 241
export const isHardhat = async () => {
  const chainId = await l1Wallet.getChainId()
  return chainId === HARDHAT_CHAIN_ID
Matthew Slipper's avatar
Matthew Slipper committed
242
}
243 244 245 246 247 248 249 250 251

export const die = (...args) => {
  console.log(...args)
  process.exit(1)
}

export const logStderr = (msg: string) => {
  process.stderr.write(`${msg}\n`)
}