deploy.ts 2.43 KB
Newer Older
1
/* External Imports */
2
import { Contract } from 'ethers'
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

/* 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> => {
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  let AddressManager: Contract

  if (config.addressManager) {
    // console.log(`Connecting to existing address manager.`) //console.logs currently break our deployer
    AddressManager = getContractFactory(
      'Lib_AddressManager',
      config.deploymentSigner
    ).attach(config.addressManager)
  } else {
    // console.log(
    //   `Address manager wasn't provided, so we're deploying a new one.`
    // ) //console.logs currently break our deployer
    AddressManager = await getContractFactory(
      'Lib_AddressManager',
      config.deploymentSigner
    ).deploy()
35 36 37
    if (config.waitForReceipts) {
      await AddressManager.deployTransaction.wait()
    }
38
  }
39 40 41 42 43 44 45 46 47 48 49 50 51 52

  const contractDeployConfig = await makeContractDeployConfig(
    config,
    AddressManager
  )

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

  for (const [name, contractDeployParameters] of Object.entries(
    contractDeployConfig
  )) {
53 54 55 56
    if (config.dependencies && !config.dependencies.includes(name)) {
      continue
    }

57 58 59
    try {
      contracts[name] = await contractDeployParameters.factory
        .connect(config.deploymentSigner)
60 61
        .deploy(
          ...(contractDeployParameters.params || []),
62
          config.deployOverrides
63
        )
64 65 66 67 68 69 70
      if (config.waitForReceipts) {
        await contracts[name].deployTransaction.wait()
      }
      const res = await AddressManager.setAddress(name, contracts[name].address)
      if (config.waitForReceipts) {
        await res.wait()
      }
71
    } catch (err) {
72
      console.error(`Error deploying ${name}: ${err}`)
73 74 75 76
      failedDeployments.push(name)
    }
  }

77 78 79
  for (const [name, contractDeployParameters] of Object.entries(
    contractDeployConfig
  )) {
80 81 82 83
    if (config.dependencies && !config.dependencies.includes(name)) {
      continue
    }

84 85 86 87 88 89 90 91 92 93 94
    if (contractDeployParameters.afterDeploy) {
      await contractDeployParameters.afterDeploy(contracts)
    }
  }

  return {
    AddressManager,
    failedDeployments,
    contracts,
  }
}