connect-contracts.ts 2.89 KB
Newer Older
1
import { Signer, Contract } from 'ethers'
2
import { Provider } from '@ethersproject/abstract-provider'
3 4 5
import { getContractArtifact } from './contract-artifacts'
import { getDeployedContractArtifact } from './contract-deployed-artifacts'
import { predeploys } from './predeploys'
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

export type Network = 'goerli' | 'kovan' | 'mainnet'
interface L1Contracts {
  addressManager: Contract
  canonicalTransactionChain: Contract
  stateCommitmentChain: Contract
  xDomainMessengerProxy: Contract
  bondManager: Contract
}

interface L2Contracts {
  eth: Contract
  xDomainMessenger: Contract
  messagePasser: Contract
  deployerWhiteList: Contract
}

/**
 * Validates user provided a singer or provider & throws error if not
 *
 * @param signerOrProvider
 */
const checkSignerType = (signerOrProvider: Signer | Provider) => {
  if (!signerOrProvider) {
    throw Error('signerOrProvider argument is undefined')
  }
  if (
    !Provider.isProvider(signerOrProvider) &&
    !Signer.isSigner(signerOrProvider)
  ) {
    throw Error('signerOrProvider argument is the wrong type')
  }
}

/**
 * Connects a signer/provider to layer 1 contracts on a given network
 *
 * @param signerOrProvider ethers signer or provider
 * @param network string denoting network
 * @returns l1 contracts connected to signer/provider
 */
export const connectL1Contracts = async (
  signerOrProvider: Signer | Provider,
  network: Network
): Promise<L1Contracts> => {
  checkSignerType(signerOrProvider)

  if (!['mainnet', 'kovan', 'goerli'].includes(network)) {
    throw Error('Must specify network: mainnet, kovan, or goerli.')
  }

57 58 59 60
  const getEthersContract = (name: string) => {
    const artifact = getDeployedContractArtifact(name, network)
    return new Contract(artifact.address, artifact.abi, signerOrProvider)
  }
61 62

  return {
63
    addressManager: getEthersContract('Lib_AddressManager'),
Kelvin Fichter's avatar
Kelvin Fichter committed
64
    canonicalTransactionChain: getEthersContract('CanonicalTransactionChain'),
65
    stateCommitmentChain: getEthersContract('StateCommitmentChain'),
Kelvin Fichter's avatar
Kelvin Fichter committed
66
    xDomainMessengerProxy: getEthersContract('Proxy__L1CrossDomainMessenger'),
67
    bondManager: getEthersContract('mockBondManager'),
68 69 70 71 72 73 74 75 76 77
  }
}

/**
 * Connects a signer/provider to layer 2 contracts (network agnostic)
 *
 * @param signerOrProvider ethers signer or provider
 * @returns l2 contracts connected to signer/provider
 */
export const connectL2Contracts = async (
78
  signerOrProvider: any
79 80 81
): Promise<L2Contracts> => {
  checkSignerType(signerOrProvider)

82 83 84 85 86
  const getEthersContract = (name: string, iface?: string) => {
    const artifact = getContractArtifact(iface || name)
    const address = predeploys[name]
    return new Contract(address, artifact.abi, signerOrProvider)
  }
87 88

  return {
89
    eth: getEthersContract('OVM_ETH'),
90
    xDomainMessenger: getEthersContract('L2CrossDomainMessenger'),
91 92
    messagePasser: getEthersContract('OVM_L2ToL1MessagePasser'),
    deployerWhiteList: getEthersContract('OVM_DeployerWhitelist'),
93 94
  }
}