Commit 412688d5 authored by Richard Guan's avatar Richard Guan Committed by GitHub

Replace getNetwork() calls with getChainId() utility (#2535)

* refactor: replace calls to getNetwork() with getChainId util

* requested changes
parent f414d95f
---
'@eth-optimism/integration-tests': patch
'@eth-optimism/contracts': patch
'@eth-optimism/data-transport-layer': patch
'@eth-optimism/message-relayer': patch
---
Replace calls to getNetwork() with getChainId util
import { task } from 'hardhat/config' import { task } from 'hardhat/config'
import { providers } from 'ethers' import { providers } from 'ethers'
import { getChainId } from '@eth-optimism/core-utils'
import { die, logStderr } from '../test/shared/utils' import { die, logStderr } from '../test/shared/utils'
...@@ -13,22 +14,22 @@ task( ...@@ -13,22 +14,22 @@ task(
const providerA = new providers.JsonRpcProvider(replicaA) const providerA = new providers.JsonRpcProvider(replicaA)
const providerB = new providers.JsonRpcProvider(replicaB) const providerB = new providers.JsonRpcProvider(replicaB)
let netA let chainIdA
let netB let chainIdB
try { try {
netA = await providerA.getNetwork() chainIdA = await getChainId(providerA)
} catch (e) { } catch (e) {
console.error(`Error getting network from ${replicaA}:`) console.error(`Error getting network chainId from ${replicaA}:`)
die(e) die(e)
} }
try { try {
netB = await providerA.getNetwork() chainIdB = await getChainId(providerB)
} catch (e) { } catch (e) {
console.error(`Error getting network from ${replicaB}:`) console.error(`Error getting network chainId from ${replicaB}:`)
die(e) die(e)
} }
if (netA.chainId !== netB.chainId) { if (chainIdA !== chainIdB) {
die('Chain IDs do not match') die('Chain IDs do not match')
return return
} }
......
/* Imports: External */ /* Imports: External */
import { expectApprox, sleep } from '@eth-optimism/core-utils' import { expectApprox, getChainId, sleep } from '@eth-optimism/core-utils'
import { Wallet, BigNumber, Contract, ContractFactory, constants } from 'ethers' import { Wallet, BigNumber, Contract, ContractFactory, constants } from 'ethers'
import { serialize } from '@ethersproject/transactions' import { serialize } from '@ethersproject/transactions'
import { ethers } from 'hardhat' import { ethers } from 'hardhat'
...@@ -471,8 +471,7 @@ describe('Basic RPC tests', () => { ...@@ -471,8 +471,7 @@ describe('Basic RPC tests', () => {
describe('eth_chainId', () => { describe('eth_chainId', () => {
it('should get the correct chainid', async () => { it('should get the correct chainid', async () => {
const { chainId } = await env.l2Provider.getNetwork() expect(await getChainId(env.l2Provider)).to.be.eq(L2_CHAINID)
expect(chainId).to.be.eq(L2_CHAINID)
}) })
}) })
......
import { ethers, Contract } from 'ethers' import { ethers, Contract } from 'ethers'
import { Provider } from '@ethersproject/abstract-provider' import { Provider } from '@ethersproject/abstract-provider'
import { Signer } from '@ethersproject/abstract-signer' import { Signer } from '@ethersproject/abstract-signer'
import { sleep, awaitCondition } from '@eth-optimism/core-utils' import { sleep, awaitCondition, getChainId } from '@eth-optimism/core-utils'
import { HttpNetworkConfig } from 'hardhat/types' import { HttpNetworkConfig } from 'hardhat/types'
import { getDeployConfig } from './deploy-config' import { getDeployConfig } from './deploy-config'
...@@ -270,8 +270,7 @@ export const getContractFromArtifact = async ( ...@@ -270,8 +270,7 @@ export const getContractFromArtifact = async (
} }
export const isHardhatNode = async (hre) => { export const isHardhatNode = async (hre) => {
const { chainId } = await hre.ethers.provider.getNetwork() return (await getChainId(hre.ethers.provider)) === 31337
return chainId === 31337
} }
// Large balance to fund accounts with. // Large balance to fund accounts with.
......
/* Imports: External */ /* Imports: External */
import { BaseService, Metrics } from '@eth-optimism/common-ts' import { BaseService, Metrics } from '@eth-optimism/common-ts'
import { StaticJsonRpcProvider } from '@ethersproject/providers' import { StaticJsonRpcProvider } from '@ethersproject/providers'
import { sleep, toRpcHexString } from '@eth-optimism/core-utils' import { getChainId, sleep, toRpcHexString } from '@eth-optimism/core-utils'
import { BigNumber } from 'ethers' import { BigNumber } from 'ethers'
import { LevelUp } from 'levelup' import { LevelUp } from 'levelup'
import axios from 'axios' import axios from 'axios'
...@@ -127,9 +127,9 @@ export class L2IngestionService extends BaseService<L2IngestionServiceOptions> { ...@@ -127,9 +127,9 @@ export class L2IngestionService extends BaseService<L2IngestionServiceOptions> {
} }
protected async checkConsistency(): Promise<void> { protected async checkConsistency(): Promise<void> {
const network = await this.state.l2RpcProvider.getNetwork() const chainId = await getChainId(this.state.l2RpcProvider)
const shouldDoCheck = !(await this.state.db.getConsistencyCheckFlag()) const shouldDoCheck = !(await this.state.db.getConsistencyCheckFlag())
if (shouldDoCheck && network.chainId === 69) { if (shouldDoCheck && chainId === 69) {
this.logger.info('performing consistency check') this.logger.info('performing consistency check')
const highestBlock = const highestBlock =
await this.state.db.getHighestSyncedUnconfirmedBlock() await this.state.db.getHighestSyncedUnconfirmedBlock()
......
/* Imports: External */ /* Imports: External */
import { Signer } from 'ethers' import { Signer } from 'ethers'
import { sleep } from '@eth-optimism/core-utils' import { getChainId, sleep } from '@eth-optimism/core-utils'
import { import {
BaseServiceV2, BaseServiceV2,
validators, validators,
...@@ -80,12 +80,10 @@ export class MessageRelayerService extends BaseServiceV2< ...@@ -80,12 +80,10 @@ export class MessageRelayerService extends BaseServiceV2<
this.options.l1RpcProvider this.options.l1RpcProvider
) )
const l1Network = await this.state.wallet.provider.getNetwork()
const l1ChainId = l1Network.chainId
this.state.messenger = new CrossChainMessenger({ this.state.messenger = new CrossChainMessenger({
l1SignerOrProvider: this.state.wallet, l1SignerOrProvider: this.state.wallet,
l2SignerOrProvider: this.options.l2RpcProvider, l2SignerOrProvider: this.options.l2RpcProvider,
l1ChainId, l1ChainId: await getChainId(this.state.wallet.provider),
}) })
this.state.highestCheckedL2Tx = this.options.fromL2TransactionIndex || 1 this.state.highestCheckedL2Tx = this.options.fromL2TransactionIndex || 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment