contracts.ts 9.58 KB
Newer Older
1 2
import { getContractInterface, predeploys } from '@eth-optimism/contracts'
import { ethers, Contract } from 'ethers'
3 4 5 6 7 8 9 10 11 12 13 14 15 16
import l1StandardBridge from '@eth-optimism/contracts-bedrock/forge-artifacts/L1StandardBridge.sol/L1StandardBridge.json'
import l2StandardBridge from '@eth-optimism/contracts-bedrock/forge-artifacts/L2StandardBridge.sol/L2StandardBridge.json'
import optimismMintableERC20 from '@eth-optimism/contracts-bedrock/forge-artifacts/OptimismMintableERC20.sol/OptimismMintableERC20.json'
import optimismPortal from '@eth-optimism/contracts-bedrock/forge-artifacts/OptimismPortal.sol/OptimismPortal.json'
import l1CrossDomainMessenger from '@eth-optimism/contracts-bedrock/forge-artifacts/L1CrossDomainMessenger.sol/L1CrossDomainMessenger.json'
import l2CrossDomainMessenger from '@eth-optimism/contracts-bedrock/forge-artifacts/L2CrossDomainMessenger.sol/L2CrossDomainMessenger.json'
import optimismMintableERC20Factory from '@eth-optimism/contracts-bedrock/forge-artifacts/OptimismMintableERC20Factory.sol/OptimismMintableERC20Factory.json'
import proxyAdmin from '@eth-optimism/contracts-bedrock/forge-artifacts/ProxyAdmin.sol/ProxyAdmin.json'
import l2OutputOracle from '@eth-optimism/contracts-bedrock/forge-artifacts/L2OutputOracle.sol/L2OutputOracle.json'
import l1ERC721Bridge from '@eth-optimism/contracts-bedrock/forge-artifacts/L1ERC721Bridge.sol/L1ERC721Bridge.json'
import l2ERC721Bridge from '@eth-optimism/contracts-bedrock/forge-artifacts/L2ERC721Bridge.sol/L2ERC721Bridge.json'
import l1Block from '@eth-optimism/contracts-bedrock/forge-artifacts/L1Block.sol/L1Block.json'
import l2ToL1MessagePasser from '@eth-optimism/contracts-bedrock/forge-artifacts/L2ToL1MessagePasser.sol/L2ToL1MessagePasser.json'
import gasPriceOracle from '@eth-optimism/contracts-bedrock/forge-artifacts/GasPriceOracle.sol/GasPriceOracle.json'
17 18 19

import { toAddress } from './coercion'
import { DeepPartial } from './type-utils'
20
import { CrossChainMessenger } from '../cross-chain-messenger'
21 22 23 24 25 26
import { StandardBridgeAdapter, ETHBridgeAdapter } from '../adapters'
import {
  CONTRACT_ADDRESSES,
  DEFAULT_L2_CONTRACT_ADDRESSES,
  BRIDGE_ADAPTER_DATA,
} from './chain-constants'
27 28 29 30 31 32
import {
  OEContracts,
  OEL1Contracts,
  OEL2Contracts,
  OEContractsLike,
  AddressLike,
33 34
  BridgeAdapters,
  BridgeAdapterData,
35 36 37 38 39 40 41
} from '../interfaces'

/**
 * We've changed some contract names in this SDK to be a bit nicer. Here we remap these nicer names
 * back to the original contract names so we can look them up.
 */
const NAME_REMAPPING = {
42 43 44
  AddressManager: 'Lib_AddressManager' as const,
  OVM_L1BlockNumber: 'iOVM_L1BlockNumber' as const,
  WETH: 'WETH9' as const,
45
  BedrockMessagePasser: 'L2ToL1MessagePasser' as const,
46 47
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
const getContractInterfaceBedrock = (name: string): ethers.utils.Interface => {
  let artifact: any = ''
  switch (name) {
    case 'Lib_AddressManager':
    case 'AddressManager':
      artifact = ''
      break
    case 'L1CrossDomainMessenger':
      artifact = l1CrossDomainMessenger
      break
    case 'L1ERC721Bridge':
      artifact = l1ERC721Bridge
      break
    case 'L2OutputOracle':
      artifact = l2OutputOracle
      break
    case 'OptimismMintableERC20Factory':
      artifact = optimismMintableERC20Factory
      break
    case 'ProxyAdmin':
      artifact = proxyAdmin
      break
    case 'L1StandardBridge':
      artifact = l1StandardBridge
      break
    case 'L2StandardBridge':
      artifact = l2StandardBridge
      break
    case 'OptimismPortal':
      artifact = optimismPortal
      break
    case 'L2CrossDomainMessenger':
      artifact = l2CrossDomainMessenger
      break
    case 'OptimismMintableERC20':
      artifact = optimismMintableERC20
      break
    case 'L2ERC721Bridge':
      artifact = l2ERC721Bridge
      break
    case 'L1Block':
      artifact = l1Block
      break
    case 'L2ToL1MessagePasser':
      artifact = l2ToL1MessagePasser
      break
    case 'GasPriceOracle':
      artifact = gasPriceOracle
      break
  }
  return new ethers.utils.Interface(artifact.abi)
}

101 102
/**
 * Returns an ethers.Contract object for the given name, connected to the appropriate address for
103
 * the given L2 chain ID. Users can also provide a custom address to connect the contract to
104 105 106 107
 * instead. If the chain ID is not known then the user MUST provide a custom address or this
 * function will throw an error.
 *
 * @param contractName Name of the contract to connect to.
108
 * @param l2ChainId Chain ID for the L2 network.
109 110 111 112 113 114 115
 * @param opts Additional options for connecting to the contract.
 * @param opts.address Custom address to connect to the contract.
 * @param opts.signerOrProvider Signer or provider to connect to the contract.
 * @returns An ethers.Contract object connected to the appropriate address and interface.
 */
export const getOEContract = (
  contractName: keyof OEL1Contracts | keyof OEL2Contracts,
116
  l2ChainId: number,
117 118 119 120 121
  opts: {
    address?: AddressLike
    signerOrProvider?: ethers.Signer | ethers.providers.Provider
  } = {}
): Contract => {
122
  const addresses = CONTRACT_ADDRESSES[l2ChainId]
123 124
  if (addresses === undefined && opts.address === undefined) {
    throw new Error(
125
      `cannot get contract ${contractName} for unknown L2 chain ID ${l2ChainId}, you must provide an address`
126 127 128
    )
  }

129 130 131 132 133 134 135 136 137 138
  // Bedrock interfaces are backwards compatible. We can prefer Bedrock interfaces over legacy
  // interfaces if they exist.
  const name = NAME_REMAPPING[contractName] || contractName
  let iface: ethers.utils.Interface
  try {
    iface = getContractInterfaceBedrock(name)
  } catch (err) {
    iface = getContractInterface(name)
  }

139 140 141 142
  return new Contract(
    toAddress(
      opts.address || addresses.l1[contractName] || addresses.l2[contractName]
    ),
143
    iface,
144 145 146 147 148
    opts.signerOrProvider
  )
}

/**
149
 * Automatically connects to all contract addresses, both L1 and L2, for the given L2 chain ID. The
150 151 152 153
 * user can provide custom contract address overrides for L1 or L2 contracts. If the given chain ID
 * is not known then the user MUST provide custom contract addresses for ALL L1 contracts or this
 * function will throw an error.
 *
154
 * @param l2ChainId Chain ID for the L2 network.
155 156 157 158 159 160 161 162
 * @param opts Additional options for connecting to the contracts.
 * @param opts.l1SignerOrProvider: Signer or provider to connect to the L1 contracts.
 * @param opts.l2SignerOrProvider: Signer or provider to connect to the L2 contracts.
 * @param opts.overrides Custom contract address overrides for L1 or L2 contracts.
 * @returns An object containing ethers.Contract objects connected to the appropriate addresses on
 * both L1 and L2.
 */
export const getAllOEContracts = (
163
  l2ChainId: number,
164 165 166 167 168 169
  opts: {
    l1SignerOrProvider?: ethers.Signer | ethers.providers.Provider
    l2SignerOrProvider?: ethers.Signer | ethers.providers.Provider
    overrides?: DeepPartial<OEContractsLike>
  } = {}
): OEContracts => {
170
  const addresses = CONTRACT_ADDRESSES[l2ChainId] || {
171 172 173 174 175 176 177
    l1: {
      AddressManager: undefined,
      L1CrossDomainMessenger: undefined,
      L1StandardBridge: undefined,
      StateCommitmentChain: undefined,
      CanonicalTransactionChain: undefined,
      BondManager: undefined,
178 179
      OptimismPortal: undefined,
      L2OutputOracle: undefined,
180 181 182 183 184
    },
    l2: DEFAULT_L2_CONTRACT_ADDRESSES,
  }

  // Attach all L1 contracts.
185
  const l1Contracts = {} as OEL1Contracts
186
  for (const [contractName, contractAddress] of Object.entries(addresses.l1)) {
187 188
    l1Contracts[contractName] = getOEContract(
      contractName as keyof OEL1Contracts,
189
      l2ChainId,
190 191 192 193 194
      {
        address: opts.overrides?.l1?.[contractName] || contractAddress,
        signerOrProvider: opts.l1SignerOrProvider,
      }
    )
195 196 197
  }

  // Attach all L2 contracts.
198
  const l2Contracts = {} as OEL2Contracts
199
  for (const [contractName, contractAddress] of Object.entries(addresses.l2)) {
200 201
    l2Contracts[contractName] = getOEContract(
      contractName as keyof OEL2Contracts,
202
      l2ChainId,
203 204 205 206 207
      {
        address: opts.overrides?.l2?.[contractName] || contractAddress,
        signerOrProvider: opts.l2SignerOrProvider,
      }
    )
208 209 210 211 212 213 214
  }

  return {
    l1: l1Contracts,
    l2: l2Contracts,
  }
}
215 216

/**
217
 * Gets a series of bridge adapters for the given L2 chain ID.
218
 *
219
 * @param l2ChainId Chain ID for the L2 network.
220
 * @param messenger Cross chain messenger to connect to the bridge adapters
221
 * @param opts Additional options for connecting to the custom bridges.
222 223
 * @param opts.overrides Custom bridge adapters.
 * @returns An object containing all bridge adapters
224
 */
225
export const getBridgeAdapters = (
226
  l2ChainId: number,
227
  messenger: CrossChainMessenger,
228 229
  opts?: {
    overrides?: BridgeAdapterData
230
    contracts?: DeepPartial<OEContractsLike>
231
  }
232
): BridgeAdapters => {
233
  const adapterData: BridgeAdapterData = {
234
    ...(CONTRACT_ADDRESSES[l2ChainId] || opts?.contracts?.l1?.L1StandardBridge
235 236 237
      ? {
          Standard: {
            Adapter: StandardBridgeAdapter,
238
            l1Bridge:
239
              opts?.contracts?.l1?.L1StandardBridge ||
240
              CONTRACT_ADDRESSES[l2ChainId].l1.L1StandardBridge,
241 242 243 244
            l2Bridge: predeploys.L2StandardBridge,
          },
          ETH: {
            Adapter: ETHBridgeAdapter,
245
            l1Bridge:
246
              opts?.contracts?.l1?.L1StandardBridge ||
247
              CONTRACT_ADDRESSES[l2ChainId].l1.L1StandardBridge,
248 249 250 251 252
            l2Bridge: predeploys.L2StandardBridge,
          },
        }
      : {}),
    ...(BRIDGE_ADAPTER_DATA[l2ChainId] || {}),
253
    ...(opts?.overrides || {}),
254 255 256 257
  }

  const adapters: BridgeAdapters = {}
  for (const [bridgeName, bridgeData] of Object.entries(adapterData)) {
258
    adapters[bridgeName] = new bridgeData.Adapter({
259
      messenger,
260 261 262
      l1Bridge: bridgeData.l1Bridge,
      l2Bridge: bridgeData.l2Bridge,
    })
263 264
  }

265
  return adapters
266
}