utils.ts 4.61 KB
Newer Older
1
/* Imports: External */
2 3 4 5 6 7 8
import {
  Contract,
  Wallet,
  constants,
  providers,
  BigNumberish,
  BigNumber,
9
  utils,
10
} from 'ethers'
11 12 13 14 15 16 17 18 19 20 21
import {
  getContractFactory,
  getContractInterface,
  predeploys,
} from '@eth-optimism/contracts'
import { injectL2Context, remove0x, Watcher } from '@eth-optimism/core-utils'
import { cleanEnv, str, num, bool } from 'envalid'
import dotenv from 'dotenv'

/* Imports: Internal */
import { Direction, waitForXDomainTransaction } from './watcher-utils'
22

23 24
export const GWEI = BigNumber.from(1e9)

25 26 27 28
if (process.env.IS_LIVE_NETWORK === 'true') {
  dotenv.config()
}

29
const env = cleanEnv(process.env, {
30 31
  L1_URL: str({ default: 'http://localhost:9545' }),
  L2_URL: str({ default: 'http://localhost:8545' }),
32
  VERIFIER_URL: str({ default: 'http://localhost:8547' }),
33
  REPLICA_URL: str({ default: 'http://localhost:8549' }),
34 35
  L1_POLLING_INTERVAL: num({ default: 10 }),
  L2_POLLING_INTERVAL: num({ default: 10 }),
36
  VERIFIER_POLLING_INTERVAL: num({ default: 10 }),
37
  REPLICA_POLLING_INTERVAL: num({ default: 10 }),
38 39 40 41 42 43 44
  PRIVATE_KEY: str({
    default:
      '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
  }),
  ADDRESS_MANAGER: str({
    default: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
  }),
45 46
  L2_CHAINID: num({ default: 420 }),
  IS_LIVE_NETWORK: bool({ default: false }),
47 48
})

49
// The hardhat instance
50 51 52 53 54
export const l1Provider = new providers.JsonRpcProvider(env.L1_URL)
l1Provider.pollingInterval = env.L1_POLLING_INTERVAL

export const l2Provider = new providers.JsonRpcProvider(env.L2_URL)
l2Provider.pollingInterval = env.L2_POLLING_INTERVAL
55

56 57 58
export const verifierProvider = new providers.JsonRpcProvider(env.VERIFIER_URL)
verifierProvider.pollingInterval = env.VERIFIER_POLLING_INTERVAL

59 60 61
export const replicaProvider = new providers.JsonRpcProvider(env.REPLICA_URL)
replicaProvider.pollingInterval = env.REPLICA_POLLING_INTERVAL

62
// The sequencer private key which is funded on L1
63
export const l1Wallet = new Wallet(env.PRIVATE_KEY, l1Provider)
64 65 66 67 68 69 70 71

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

// Predeploys
export const PROXY_SEQUENCER_ENTRYPOINT_ADDRESS =
  '0x4200000000000000000000000000000000000004'
72
export const OVM_ETH_ADDRESS = predeploys.OVM_ETH
73

74 75 76
export const L2_CHAINID = env.L2_CHAINID
export const IS_LIVE_NETWORK = env.IS_LIVE_NETWORK

77 78 79
export const getAddressManager = (provider: any) => {
  return getContractFactory('Lib_AddressManager')
    .connect(provider)
80
    .attach(env.ADDRESS_MANAGER)
81
}
82

83 84
// Gets the bridge contract
export const getL1Bridge = async (wallet: Wallet, AddressManager: Contract) => {
85
  const l1BridgeInterface = getContractInterface('L1StandardBridge')
86
  const ProxyBridgeAddress = await AddressManager.getAddress(
87
    'Proxy__OVM_L1StandardBridge'
88
  )
89 90 91 92 93

  if (
    !utils.isAddress(ProxyBridgeAddress) ||
    ProxyBridgeAddress === constants.AddressZero
  ) {
94
    throw new Error('Proxy__OVM_L1StandardBridge not found')
95 96
  }

97
  const L1StandardBridge = new Contract(
98 99
    ProxyBridgeAddress,
    l1BridgeInterface,
100 101
    wallet
  )
102
  return L1StandardBridge
103 104 105
}

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

108 109
  const L2StandardBridge = new Contract(
    predeploys.L2StandardBridge,
110 111 112
    L2BridgeInterface,
    wallet
  )
113
  return L2StandardBridge
114 115 116 117 118 119 120 121 122 123 124 125 126 127
}

export const getOvmEth = (wallet: Wallet) => {
  const OVM_ETH = new Contract(
    OVM_ETH_ADDRESS,
    getContractInterface('OVM_ETH'),
    wallet
  )

  return OVM_ETH
}

export const fundUser = async (
  watcher: Watcher,
128
  bridge: Contract,
129 130 131 132 133
  amount: BigNumberish,
  recipient?: string
) => {
  const value = BigNumber.from(amount)
  const tx = recipient
134 135
    ? bridge.depositETHTo(recipient, 1_300_000, '0x', { value })
    : bridge.depositETH(1_300_000, '0x', { value })
136

137 138 139 140
  await waitForXDomainTransaction(watcher, tx, Direction.L1ToL2)
}

export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
141 142 143 144 145

const abiCoder = new utils.AbiCoder()
export const encodeSolidityRevertMessage = (_reason: string): string => {
  return '0x08c379a0' + remove0x(abiCoder.encode(['string'], [_reason]))
}
146 147 148

export const DEFAULT_TRANSACTION = {
  to: '0x' + '1234'.repeat(10),
149
  gasLimit: 8_000_000,
150 151 152 153
  gasPrice: 0,
  data: '0x',
  value: 0,
}
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168
export const waitForL2Geth = async (
  provider: providers.JsonRpcProvider
): Promise<providers.JsonRpcProvider> => {
  let ready: boolean = false
  while (!ready) {
    try {
      await provider.getNetwork()
      ready = true
    } catch (error) {
      await sleep(1000)
    }
  }
  return injectL2Context(provider)
}