deploy.ts 1.91 KB
Newer Older
1 2 3 4 5 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
/* External Imports */
import { Signer, Contract, ContractFactory } from 'ethers'

/* Internal Imports */
import { RollupDeployConfig, makeContractDeployConfig } from './config'
import { getContractFactory } from '../contract-defs'

export interface DeployResult {
  AddressManager: Contract
  failedDeployments: string[]
  contracts: {
    [name: string]: Contract
  }
}

export const deploy = async (
  config: RollupDeployConfig
): Promise<DeployResult> => {
  const Factory__SimpleProxy: ContractFactory = getContractFactory(
    'Helper_SimpleProxy',
    config.deploymentSigner
  )
  const AddressManager: Contract = await getContractFactory(
    'Lib_AddressManager',
    config.deploymentSigner
  ).deploy()

  const contractDeployConfig = await makeContractDeployConfig(
    config,
    AddressManager
  )

  const failedDeployments: string[] = []
  const contracts: {
    [name: string]: Contract
  } = {}

  for (const [name, contractDeployParameters] of Object.entries(
    contractDeployConfig
  )) {
41 42 43 44
    if (config.dependencies && !config.dependencies.includes(name)) {
      continue
    }

45 46 47
    const SimpleProxy = await Factory__SimpleProxy.deploy()
    await AddressManager.setAddress(name, SimpleProxy.address)

48 49
    contracts[`Proxy__${name}`] = SimpleProxy

50 51 52
    try {
      contracts[name] = await contractDeployParameters.factory
        .connect(config.deploymentSigner)
53
        .deploy(...(contractDeployParameters.params || []))
54 55 56 57 58 59
      await SimpleProxy.setTarget(contracts[name].address)
    } catch (err) {
      failedDeployments.push(name)
    }
  }

60 61 62
  for (const [name, contractDeployParameters] of Object.entries(
    contractDeployConfig
  )) {
63 64 65 66
    if (config.dependencies && !config.dependencies.includes(name)) {
      continue
    }

67 68 69 70 71 72 73 74 75 76 77
    if (contractDeployParameters.afterDeploy) {
      await contractDeployParameters.afterDeploy(contracts)
    }
  }

  return {
    AddressManager,
    failedDeployments,
    contracts,
  }
}