utils.ts 6.8 KB
Newer Older
1
/* Imports: External */
2 3 4 5 6 7
import {
  Contract,
  Wallet,
  constants,
  providers,
  BigNumber,
8
  utils,
9
} from 'ethers'
10 11 12 13 14
import {
  getContractFactory,
  getContractInterface,
  predeploys,
} from '@eth-optimism/contracts'
15 16 17 18 19 20
import { remove0x } from '@eth-optimism/core-utils'
import {
  CrossChainMessenger,
  NumberLike,
  asL2Provider,
} from '@eth-optimism/sdk'
21
import { cleanEnv, str, num, bool, makeValidator } from 'envalid'
22
import dotenv from 'dotenv'
23
dotenv.config()
24 25

/* Imports: Internal */
26
import { OptimismEnv } from './env'
27

28 29 30 31
export const isLiveNetwork = () => {
  return process.env.IS_LIVE_NETWORK === 'true'
}

32 33 34 35 36 37 38 39 40
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
  }
41

42 43 44 45 46 47 48
  return num()._parse(gasPrice).toString()
})

const procEnv = cleanEnv(process.env, {
  L1_GAS_PRICE: gasPriceValidator({
    default: '0',
  }),
49
  L1_URL: str({ default: 'http://localhost:9545' }),
50
  L1_POLLING_INTERVAL: num({ default: 10 }),
51 52 53 54 55 56

  L2_CHAINID: num({ default: 420 }),
  L2_GAS_PRICE: gasPriceValidator({
    default: 'onchain',
  }),
  L2_URL: str({ default: 'http://localhost:8545' }),
57
  L2_POLLING_INTERVAL: num({ default: 10 }),
58 59 60 61 62 63 64 65
  L2_WALLET_MIN_BALANCE_ETH: num({
    default: 2,
  }),
  L2_WALLET_TOP_UP_AMOUNT_ETH: num({
    default: 3,
  }),

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

68 69
  VERIFIER_URL: str({ default: 'http://localhost:8547' }),

70 71 72 73 74 75 76
  PRIVATE_KEY: str({
    default:
      '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
  }),
  ADDRESS_MANAGER: str({
    default: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
  }),
77 78 79 80
  GAS_PRICE_ORACLE_PRIVATE_KEY: str({
    default:
      '0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba',
  }),
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

  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,
  }),
  RUN_DEBUG_TRACE_TESTS: bool({
    default: true,
  }),
  RUN_STRESS_TESTS: bool({
    default: true,
  }),
101 102 103
  RUN_VERIFIER_TESTS: bool({
    default: true,
  }),
104 105 106 107 108 109 110

  MOCHA_TIMEOUT: num({
    default: 120_000,
  }),
  MOCHA_BAIL: bool({
    default: false,
  }),
111 112
})

113 114
export const envConfig = procEnv

115
// The hardhat instance
116 117
export const l1Provider = new providers.JsonRpcProvider(procEnv.L1_URL)
l1Provider.pollingInterval = procEnv.L1_POLLING_INTERVAL
118

119
export const l2Provider = asL2Provider(
120
  new providers.JsonRpcProvider(procEnv.L2_URL)
121
)
122
l2Provider.pollingInterval = procEnv.L2_POLLING_INTERVAL
123

124
export const replicaProvider = asL2Provider(
125
  new providers.JsonRpcProvider(procEnv.REPLICA_URL)
126
)
127
replicaProvider.pollingInterval = procEnv.REPLICA_POLLING_INTERVAL
128

129
export const verifierProvider = asL2Provider(
130 131 132 133
  new providers.JsonRpcProvider(procEnv.VERIFIER_URL)
)
verifierProvider.pollingInterval = procEnv.L2_POLLING_INTERVAL

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

// 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)

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

147
// Predeploys
148
export const OVM_ETH_ADDRESS = predeploys.OVM_ETH
149

150
export const L2_CHAINID = procEnv.L2_CHAINID
151

152 153 154
export const getAddressManager = (provider: any) => {
  return getContractFactory('Lib_AddressManager')
    .connect(provider)
155
    .attach(procEnv.ADDRESS_MANAGER)
156
}
157

158 159
// Gets the bridge contract
export const getL1Bridge = async (wallet: Wallet, AddressManager: Contract) => {
160
  const l1BridgeInterface = getContractInterface('L1StandardBridge')
161
  const ProxyBridgeAddress = await AddressManager.getAddress(
162
    'Proxy__OVM_L1StandardBridge'
163
  )
164 165 166 167 168

  if (
    !utils.isAddress(ProxyBridgeAddress) ||
    ProxyBridgeAddress === constants.AddressZero
  ) {
169
    throw new Error('Proxy__OVM_L1StandardBridge not found')
170 171
  }

172
  return new Contract(ProxyBridgeAddress, l1BridgeInterface, wallet)
173 174 175
}

export const getL2Bridge = async (wallet: Wallet) => {
176
  const L2BridgeInterface = getContractInterface('L2StandardBridge')
177

178
  return new Contract(predeploys.L2StandardBridge, L2BridgeInterface, wallet)
179 180 181
}

export const getOvmEth = (wallet: Wallet) => {
182
  return new Contract(OVM_ETH_ADDRESS, getContractInterface('OVM_ETH'), wallet)
183 184 185
}

export const fundUser = async (
186 187
  messenger: CrossChainMessenger,
  amount: NumberLike,
188 189
  recipient?: string
) => {
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  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()
  }
206 207
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
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()
224
  }).timeout(timeout || envConfig.MOCHA_TIMEOUT * 2)
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
}

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.'
  )

244 245 246 247
const abiCoder = new utils.AbiCoder()
export const encodeSolidityRevertMessage = (_reason: string): string => {
  return '0x08c379a0' + remove0x(abiCoder.encode(['string'], [_reason]))
}
248

249 250 251 252 253 254 255 256
export const defaultTransactionFactory = () => {
  return {
    to: '0x' + '1234'.repeat(10),
    gasLimit: 8_000_000,
    gasPrice: BigNumber.from(0),
    data: '0x',
    value: 0,
  }
257
}
258

259 260 261
export const gasPriceForL2 = async () => {
  if (procEnv.L2_GAS_PRICE === ON_CHAIN_GAS_PRICE) {
    return l2Wallet.getGasPrice()
Matthew Slipper's avatar
Matthew Slipper committed
262 263
  }

264
  return utils.parseUnits(procEnv.L2_GAS_PRICE, 'wei')
265 266
}

267 268 269
export const gasPriceForL1 = async () => {
  if (procEnv.L1_GAS_PRICE === ON_CHAIN_GAS_PRICE) {
    return l1Wallet.getGasPrice()
270
  }
271 272

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

275 276 277
export const isHardhat = async () => {
  const chainId = await l1Wallet.getChainId()
  return chainId === HARDHAT_CHAIN_ID
Matthew Slipper's avatar
Matthew Slipper committed
278
}
279 280 281 282 283 284 285 286 287

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

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