wait-for-final-batch.ts 2.48 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 41 42 43 44 45 46 47 48 49 50
import { task, types } from 'hardhat/config'
import '@nomiclabs/hardhat-ethers'
import 'hardhat-deploy'
import { HardhatRuntimeEnvironment } from 'hardhat/types'
import { Contract } from 'ethers'
import { sleep } from '@eth-optimism/core-utils'

task('wait-for-final-batch', 'Waits for the final batch to be submitted')
  .addParam(
    'l1RpcUrl',
    'L1 RPC URL remote node',
    'http://127.0.0.1:8545',
    types.string
  )
  .addParam(
    'l2RpcUrl',
    'L2 RPC URL remote node',
    'http://127.0.0.1:9545',
    types.string
  )
  .setAction(async (args, hre: HardhatRuntimeEnvironment) => {
    const l1Provider = new hre.ethers.providers.StaticJsonRpcProvider(
      args.l1RpcUrl
    )
    const l2Provider = new hre.ethers.providers.StaticJsonRpcProvider(
      args.l2RpcUrl
    )

    const Deployment__CanonicalTransactionChain = await hre.deployments.get(
      'CanonicalTransactionChain'
    )
    const CanonicalTransactionChain = new hre.ethers.Contract(
      Deployment__CanonicalTransactionChain.address,
      Deployment__CanonicalTransactionChain.abi,
      l1Provider
    )

    const Deployment__StateCommitmentChain = await hre.deployments.get(
      'StateCommitmentChain'
    )

    const StateCommitmentChain = new hre.ethers.Contract(
      Deployment__StateCommitmentChain.address,
      Deployment__StateCommitmentChain.abi,
      l1Provider
    )

    const wait = async (contract: Contract) => {
      let height = await l2Provider.getBlockNumber()
      let totalElements = await contract.getTotalElements()
51 52 53 54
      console.log(`  - height: ${height}`)
      console.log(`  - totalElements: ${totalElements}`)

      while (totalElements.toNumber() !== height) {
55
        console.log('Total elements does not match')
56
        console.log(`  - height: ${height}`)
57
        console.log(`  - totalElements: ${totalElements}`)
58 59 60
        console.log(
          `Waiting for ${height - totalElements} elements to be submitted`
        )
61 62
        totalElements = await contract.getTotalElements()
        height = await l2Provider.getBlockNumber()
63
        await sleep(5 * 1000)
64 65 66 67 68 69
      }
    }

    console.log('Waiting for the CanonicalTransactionChain...')
    await wait(CanonicalTransactionChain)
    console.log('All transaction batches have been submitted')
70
    console.log()
71 72 73 74

    console.log('Waiting for the StateCommitmentChain...')
    await wait(StateCommitmentChain)
    console.log('All state root batches have been submitted')
75
    console.log()
76 77 78

    console.log('All batches have been submitted')
  })