deploy.ts 4.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import { Wallet } from 'ethers'
import path from 'path'
import dirtree from 'directory-tree'
import fs from 'fs'

// Ensures that all relevant environment vars are properly set. These lines *must* come before the
// hardhat import because importing will load the config (which relies on these vars). Necessary
// because CI currently uses different var names than the ones we've chosen here.
// TODO: Update CI so that we don't have to do this anymore.
process.env.HARDHAT_NETWORK = 'custom' // "custom" here is an arbitrary name. only used for CI.
process.env.CONTRACTS_TARGET_NETWORK = 'custom'
process.env.CONTRACTS_DEPLOYER_KEY = process.env.DEPLOYER_PRIVATE_KEY
process.env.CONTRACTS_RPC_URL =
  process.env.L1_NODE_WEB3_URL || 'http://127.0.0.1:8545'

import hre from 'hardhat'

18 19 20
const sequencer = new Wallet(process.env.SEQUENCER_PRIVATE_KEY)
const deployer = new Wallet(process.env.DEPLOYER_PRIVATE_KEY)

21 22 23 24 25 26 27 28 29 30 31 32 33
const parseEnv = () => {
  const ensure = (env, type) => {
    if (typeof process.env[env] === 'undefined') {
      return undefined
    }
    if (type === 'number') {
      return parseInt(process.env[env], 10)
    }
    return process.env[env]
  }

  return {
    l1BlockTimeSeconds: ensure('BLOCK_TIME_SECONDS', 'number'),
34 35 36 37
    ctcForceInclusionPeriodSeconds: ensure(
      'FORCE_INCLUSION_PERIOD_SECONDS',
      'number'
    ),
38 39 40 41 42 43 44
    ctcMaxTransactionGasLimit: ensure('MAX_TRANSACTION_GAS_LIMIT', 'number'),
    emMinTransactionGasLimit: ensure('MIN_TRANSACTION_GAS_LIMIT', 'number'),
    emMaxtransactionGasLimit: ensure('MAX_TRANSACTION_GAS_LIMIT', 'number'),
    emMaxGasPerQueuePerEpoch: ensure('MAX_GAS_PER_QUEUE_PER_EPOCH', 'number'),
    emSecondsPerEpoch: ensure('ECONDS_PER_EPOCH', 'number'),
    emOvmChainId: ensure('CHAIN_ID', 'number'),
    sccFraudProofWindow: ensure('FRAUD_PROOF_WINDOW_SECONDS', 'number'),
45 46 47 48
    sccSequencerPublishWindow: ensure(
      'SEQUENCER_PUBLISH_WINDOW_SECONDS',
      'number'
    ),
49 50
  }
}
51

52
const main = async () => {
53 54
  const config = parseEnv()

55
  await hre.run('deploy', {
56 57 58 59 60 61 62 63 64 65
    l1BlockTimeSeconds: config.l1BlockTimeSeconds,
    ctcForceInclusionPeriodSeconds: config.ctcForceInclusionPeriodSeconds,
    ctcMaxTransactionGasLimit: config.ctcMaxTransactionGasLimit,
    emMinTransactionGasLimit: config.emMinTransactionGasLimit,
    emMaxtransactionGasLimit: config.emMaxtransactionGasLimit,
    emMaxGasPerQueuePerEpoch: config.emMaxGasPerQueuePerEpoch,
    emSecondsPerEpoch: config.emSecondsPerEpoch,
    emOvmChainId: config.emOvmChainId,
    sccFraudProofWindow: config.sccFraudProofWindow,
    sccSequencerPublishWindow: config.sccFraudProofWindow,
66 67 68 69
    ovmSequencerAddress: sequencer.address,
    ovmProposerAddress: sequencer.address,
    ovmRelayerAddress: sequencer.address,
    ovmAddressManagerOwner: deployer.address,
70
    noCompile: process.env.NO_COMPILE ? true : false,
71 72 73 74 75 76
  })

  // Stuff below this line is currently required for CI to work properly. We probably want to
  // update our CI so this is no longer necessary. But I'm adding it for backwards compat so we can
  // get the hardhat-deploy stuff merged. Woot.
  const nicknames = {
77 78
    Lib_AddressManager: 'AddressManager',
    mockOVM_BondManager: 'OVM_BondManager',
79 80
  }

81
  const contracts: any = dirtree(
82
    path.resolve(__dirname, `../deployments/custom`)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  )
    .children.filter((child) => {
      return child.extension === '.json'
    })
    .reduce((contractsAccumulator, child) => {
      const contractName = child.name.replace('.json', '')
      // eslint-disable-next-line @typescript-eslint/no-var-requires
      const artifact = require(path.resolve(
        __dirname,
        `../deployments/custom/${child.name}`
      ))
      contractsAccumulator[nicknames[contractName] || contractName] =
        artifact.address
      return contractsAccumulator
    }, {})
98

99 100 101
  contracts.OVM_Sequencer = await sequencer.getAddress()
  contracts.Deployer = await deployer.getAddress()

102
  const addresses = JSON.stringify(contracts, null, 2)
103
  const dumpsPath = path.resolve(__dirname, '../dist/dumps')
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  if (!fs.existsSync(dumpsPath)) {
    fs.mkdirSync(dumpsPath)
  }
  const addrsPath = path.resolve(dumpsPath, 'addresses.json')
  fs.writeFileSync(addrsPath, addresses)
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.log(
      JSON.stringify({ error: error.message, stack: error.stack }, null, 2)
    )
    process.exit(1)
  })