Commit 977493bc authored by smartcontracts's avatar smartcontracts Committed by GitHub

feat(sdk): use L2 chain ID as source of truth (#2799)

Updates the SDK to use the L2 chain ID, instead of the L1 chain ID, as
the source of truth when loading chain-specific data. We're going to
need this now that we're planning to have multiple L2s attached to the
same L1 (goerli) during our Bedrock testing plan. Part of a series of
PRs meant to simplify the SDK and prepare it for Bedrock.
Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: default avatarMark Tyneway <mark.tyneway@gmail.com>
parent 45803ffc
---
'@eth-optimism/sdk': minor
---
Have SDK use L2 chain ID as the source of truth.
---
'@eth-optimism/integration-tests': patch
'@eth-optimism/contracts-governance': patch
'@eth-optimism/fault-detector': patch
'@eth-optimism/message-relayer': patch
---
Update SDK version and usage to account for new constructor
......@@ -4,7 +4,7 @@ import {
TransactionResponse,
TransactionReceipt,
} from '@ethersproject/providers'
import { sleep } from '@eth-optimism/core-utils'
import { getChainId, sleep } from '@eth-optimism/core-utils'
import {
CrossChainMessenger,
MessageStatus,
......@@ -58,8 +58,6 @@ export class OptimismEnv {
}
static async new(): Promise<OptimismEnv> {
const network = await l1Provider.getNetwork()
let bridgeOverrides: BridgeAdapterData
if (envConfig.L1_STANDARD_BRIDGE) {
bridgeOverrides = {
......@@ -79,7 +77,8 @@ export class OptimismEnv {
const messenger = new CrossChainMessenger({
l1SignerOrProvider: l1Wallet,
l2SignerOrProvider: l2Wallet,
l1ChainId: network.chainId,
l1ChainId: await getChainId(l1Provider),
l2ChainId: await getChainId(l2Provider),
contracts: {
l1: {
AddressManager: envConfig.ADDRESS_MANAGER,
......
......@@ -7,6 +7,7 @@ import 'solidity-coverage'
import { task, types } from 'hardhat/config'
import { providers, utils, Wallet } from 'ethers'
import { CrossChainMessenger } from '@eth-optimism/sdk'
import { getChainId } from '@eth-optimism/core-utils'
import './scripts/deploy-token'
import './scripts/multi-send'
......@@ -42,11 +43,13 @@ task('deposit', 'Deposits funds onto Optimism.')
}
const l1Provider = new providers.JsonRpcProvider(l1ProviderUrl)
const l2Provider = new providers.JsonRpcProvider(l2ProviderUrl)
const l1Wallet = new Wallet(privateKey, l1Provider)
const messenger = new CrossChainMessenger({
l1SignerOrProvider: l1Wallet,
l2SignerOrProvider: l2ProviderUrl,
l1ChainId: (await l1Provider.getNetwork()).chainId,
l2SignerOrProvider: l2Provider,
l1ChainId: await getChainId(l1Provider),
l2ChainId: await getChainId(l2Provider),
})
const amountWei = utils.parseEther(amountEth)
......@@ -57,8 +60,6 @@ task('deposit', 'Deposits funds onto Optimism.')
console.log(`Got TX hash ${tx.hash}. Waiting...`)
await tx.wait()
const l2Provider = new providers.JsonRpcProvider(l2ProviderUrl)
const l1WalletOnL2 = new Wallet(privateKey, l2Provider)
await l1WalletOnL2.sendTransaction({
to,
......
......@@ -28,6 +28,7 @@
"deploy:mainnet": "hardhat deploy-token --network 'optimism-mainnet'"
},
"dependencies": {
"@eth-optimism/core-utils": "^0.8.7",
"@eth-optimism/sdk": "^1.1.9",
"@ethersproject/hardware-wallets": "^5.6.1",
"@nomiclabs/hardhat-ethers": "^2.0.2",
......@@ -37,8 +38,8 @@
"@openzeppelin/contracts": "4.5.0",
"commander": "^9.3.0",
"csv-parse": "^5.0.4",
"ethereumjs-util": "^7.1.4",
"eth-sig-util": "^3.0.1",
"ethereumjs-util": "^7.1.4",
"ethers": "^5.6.8",
"hardhat": "^2.9.6"
},
......
......@@ -81,6 +81,7 @@ export class FaultDetector extends BaseServiceV2<Options, Metrics, State> {
l1SignerOrProvider: this.options.l1RpcProvider,
l2SignerOrProvider: this.options.l2RpcProvider,
l1ChainId: await getChainId(this.options.l1RpcProvider),
l2ChainId: await getChainId(this.options.l2RpcProvider),
})
// We use this a lot, a bit cleaner to pull out to the top level of the state object.
......
......@@ -89,6 +89,7 @@ export class MessageRelayerService extends BaseServiceV2<
l1SignerOrProvider: this.state.wallet,
l2SignerOrProvider: this.options.l2RpcProvider,
l1ChainId: await getChainId(this.state.wallet.provider),
l2ChainId: await getChainId(this.options.l2RpcProvider),
})
this.state.highestCheckedL2Tx = this.options.fromL2TransactionIndex || 1
......
......@@ -54,6 +54,7 @@ export class CrossChainMessenger implements ICrossChainMessenger {
public l1SignerOrProvider: Signer | Provider
public l2SignerOrProvider: Signer | Provider
public l1ChainId: number
public l2ChainId: number
public contracts: OEContracts
public bridges: BridgeAdapters
public depositConfirmationBlocks: number
......@@ -66,6 +67,7 @@ export class CrossChainMessenger implements ICrossChainMessenger {
* @param opts.l1SignerOrProvider Signer or Provider for the L1 chain, or a JSON-RPC url.
* @param opts.l2SignerOrProvider Signer or Provider for the L2 chain, or a JSON-RPC url.
* @param opts.l1ChainId Chain ID for the L1 chain.
* @param opts.l2ChainId Chain ID for the L2 chain.
* @param opts.depositConfirmationBlocks Optional number of blocks before a deposit is confirmed.
* @param opts.l1BlockTimeSeconds Optional estimated block time in seconds for the L1 chain.
* @param opts.contracts Optional contract address overrides.
......@@ -75,6 +77,7 @@ export class CrossChainMessenger implements ICrossChainMessenger {
l1SignerOrProvider: SignerOrProviderLike
l2SignerOrProvider: SignerOrProviderLike
l1ChainId: NumberLike
l2ChainId: NumberLike
depositConfirmationBlocks?: NumberLike
l1BlockTimeSeconds?: NumberLike
contracts?: DeepPartial<OEContractsLike>
......@@ -83,24 +86,25 @@ export class CrossChainMessenger implements ICrossChainMessenger {
this.l1SignerOrProvider = toSignerOrProvider(opts.l1SignerOrProvider)
this.l2SignerOrProvider = toSignerOrProvider(opts.l2SignerOrProvider)
this.l1ChainId = toNumber(opts.l1ChainId)
this.l2ChainId = toNumber(opts.l2ChainId)
this.depositConfirmationBlocks =
opts?.depositConfirmationBlocks !== undefined
? toNumber(opts.depositConfirmationBlocks)
: DEPOSIT_CONFIRMATION_BLOCKS[this.l1ChainId] || 0
: DEPOSIT_CONFIRMATION_BLOCKS[this.l2ChainId] || 0
this.l1BlockTimeSeconds =
opts?.l1BlockTimeSeconds !== undefined
? toNumber(opts.l1BlockTimeSeconds)
: CHAIN_BLOCK_TIMES[this.l1ChainId] || 1
this.contracts = getAllOEContracts(this.l1ChainId, {
this.contracts = getAllOEContracts(this.l2ChainId, {
l1SignerOrProvider: this.l1SignerOrProvider,
l2SignerOrProvider: this.l2SignerOrProvider,
overrides: opts.contracts,
})
this.bridges = getBridgeAdapters(this.l1ChainId, this, {
this.bridges = getBridgeAdapters(this.l2ChainId, this, {
overrides: opts.bridges,
})
}
......
......@@ -46,6 +46,11 @@ export interface ICrossChainMessenger {
*/
l1ChainId: number
/**
* Chain ID for the L2 network.
*/
l2ChainId: number
/**
* Contract objects attached to their respective providers and addresses.
*/
......
......@@ -10,15 +10,26 @@ import { ICrossChainMessenger } from './cross-chain-messenger'
import { IBridgeAdapter } from './bridge-adapter'
/**
* Commonly used Chain IDs
* L1 network chain IDs
*/
export enum Chain {
export enum L1ChainID {
MAINNET = 1,
GOERLI = 5,
KOVAN = 42,
HARDHAT_LOCAL = 31337,
}
/**
* L2 network chain IDs
*/
export enum L2ChainID {
OPTIMISM = 10,
OPTIMISM_GOERLI = 420,
OPTIMISM_KOVAN = 69,
OPTIMISM_HARDHAT_LOCAL = 31337,
OPTIMISM_HARDHAT_DEVNET = 17,
}
/**
* L1 contract references.
*/
......
import { Chain } from '../interfaces'
import { L1ChainID, L2ChainID } from '../interfaces'
export const DEPOSIT_CONFIRMATION_BLOCKS = {
[Chain.MAINNET]: 50 as const,
[Chain.GOERLI]: 12 as const,
[Chain.KOVAN]: 12 as const,
// 2 just for testing purposes
[Chain.HARDHAT_LOCAL]: 2 as const,
export const DEPOSIT_CONFIRMATION_BLOCKS: {
[ChainID in L2ChainID]: number
} = {
[L2ChainID.OPTIMISM]: 50 as const,
[L2ChainID.OPTIMISM_GOERLI]: 12 as const,
[L2ChainID.OPTIMISM_KOVAN]: 12 as const,
[L2ChainID.OPTIMISM_HARDHAT_LOCAL]: 2 as const,
[L2ChainID.OPTIMISM_HARDHAT_DEVNET]: 2 as const,
}
export const CHAIN_BLOCK_TIMES = {
[Chain.MAINNET]: 13 as const,
[Chain.GOERLI]: 15 as const,
[Chain.KOVAN]: 4 as const,
[Chain.HARDHAT_LOCAL]: 1 as const,
export const CHAIN_BLOCK_TIMES: {
[ChainID in L1ChainID]: number
} = {
[L1ChainID.MAINNET]: 13 as const,
[L1ChainID.GOERLI]: 15 as const,
[L1ChainID.KOVAN]: 4 as const,
[L1ChainID.HARDHAT_LOCAL]: 1 as const,
}
......@@ -13,7 +13,7 @@ import {
BridgeAdapters,
BridgeAdapterData,
ICrossChainMessenger,
Chain,
L2ChainID,
} from '../interfaces'
import {
StandardBridgeAdapter,
......@@ -52,9 +52,9 @@ const NAME_REMAPPING = {
* contract name.
*/
export const CONTRACT_ADDRESSES: {
[l1ChainId: number]: OEContractsLike
[ChainID in L2ChainID]: OEContractsLike
} = {
[Chain.MAINNET]: {
[L2ChainID.OPTIMISM]: {
l1: {
AddressManager: '0xdE1FCfB0851916CA5101820A69b13a4E276bd81F' as const,
L1CrossDomainMessenger:
......@@ -68,7 +68,7 @@ export const CONTRACT_ADDRESSES: {
},
l2: DEFAULT_L2_CONTRACT_ADDRESSES,
},
[Chain.KOVAN]: {
[L2ChainID.OPTIMISM_KOVAN]: {
l1: {
AddressManager: '0x100Dd3b414Df5BbA2B542864fF94aF8024aFdf3a' as const,
L1CrossDomainMessenger:
......@@ -82,7 +82,7 @@ export const CONTRACT_ADDRESSES: {
},
l2: DEFAULT_L2_CONTRACT_ADDRESSES,
},
[Chain.GOERLI]: {
[L2ChainID.OPTIMISM_GOERLI]: {
l1: {
AddressManager: '0x2F7E3cAC91b5148d336BbffB224B4dC79F09f01D' as const,
L1CrossDomainMessenger:
......@@ -96,7 +96,21 @@ export const CONTRACT_ADDRESSES: {
},
l2: DEFAULT_L2_CONTRACT_ADDRESSES,
},
[Chain.HARDHAT_LOCAL]: {
[L2ChainID.OPTIMISM_HARDHAT_LOCAL]: {
l1: {
AddressManager: '0x5FbDB2315678afecb367f032d93F642f64180aa3' as const,
L1CrossDomainMessenger:
'0x8A791620dd6260079BF849Dc5567aDC3F2FdC318' as const,
L1StandardBridge: '0x610178dA211FEF7D417bC0e6FeD39F05609AD788' as const,
StateCommitmentChain:
'0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9' as const,
CanonicalTransactionChain:
'0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9' as const,
BondManager: '0x5FC8d32690cc91D4c39d9d3abcBD16989F875707' as const,
},
l2: DEFAULT_L2_CONTRACT_ADDRESSES,
},
[L2ChainID.OPTIMISM_HARDHAT_DEVNET]: {
l1: {
AddressManager: '0x5FbDB2315678afecb367f032d93F642f64180aa3' as const,
L1CrossDomainMessenger:
......@@ -116,21 +130,9 @@ export const CONTRACT_ADDRESSES: {
* Mapping of L1 chain IDs to the list of custom bridge addresses for each chain.
*/
export const BRIDGE_ADAPTER_DATA: {
[l1ChainId: number]: BridgeAdapterData
[ChainID in L2ChainID]?: BridgeAdapterData
} = {
// TODO: Maybe we can pull these automatically from the token list?
// Alternatively, check against the token list in CI.
[Chain.MAINNET]: {
Standard: {
Adapter: StandardBridgeAdapter,
l1Bridge: CONTRACT_ADDRESSES[1].l1.L1StandardBridge,
l2Bridge: predeploys.L2StandardBridge,
},
ETH: {
Adapter: ETHBridgeAdapter,
l1Bridge: CONTRACT_ADDRESSES[1].l1.L1StandardBridge,
l2Bridge: predeploys.L2StandardBridge,
},
[L2ChainID.OPTIMISM]: {
BitBTC: {
Adapter: StandardBridgeAdapter,
l1Bridge: '0xaBA2c5F108F7E820C049D5Af70B16ac266c8f128' as const,
......@@ -142,17 +144,7 @@ export const BRIDGE_ADAPTER_DATA: {
l2Bridge: '0x467194771dAe2967Aef3ECbEDD3Bf9a310C76C65' as const,
},
},
[Chain.KOVAN]: {
Standard: {
Adapter: StandardBridgeAdapter,
l1Bridge: CONTRACT_ADDRESSES[42].l1.L1StandardBridge,
l2Bridge: predeploys.L2StandardBridge,
},
ETH: {
Adapter: ETHBridgeAdapter,
l1Bridge: CONTRACT_ADDRESSES[42].l1.L1StandardBridge,
l2Bridge: predeploys.L2StandardBridge,
},
[L2ChainID.OPTIMISM_KOVAN]: {
BitBTC: {
Adapter: StandardBridgeAdapter,
l1Bridge: '0x0b651A42F32069d62d5ECf4f2a7e5Bd3E9438746' as const,
......@@ -169,56 +161,16 @@ export const BRIDGE_ADAPTER_DATA: {
l2Bridge: '0x467194771dAe2967Aef3ECbEDD3Bf9a310C76C65' as const,
},
},
[Chain.GOERLI]: {
Standard: {
Adapter: StandardBridgeAdapter,
l1Bridge: CONTRACT_ADDRESSES[5].l1.L1StandardBridge,
l2Bridge: predeploys.L2StandardBridge,
},
ETH: {
Adapter: ETHBridgeAdapter,
l1Bridge: CONTRACT_ADDRESSES[5].l1.L1StandardBridge,
l2Bridge: predeploys.L2StandardBridge,
},
},
[Chain.HARDHAT_LOCAL]: {
Standard: {
Adapter: StandardBridgeAdapter,
l1Bridge: CONTRACT_ADDRESSES[31337].l1.L1StandardBridge,
l2Bridge: predeploys.L2StandardBridge,
},
ETH: {
Adapter: ETHBridgeAdapter,
l1Bridge: CONTRACT_ADDRESSES[31337].l1.L1StandardBridge,
l2Bridge: predeploys.L2StandardBridge,
},
},
}
// TODO: PR is big enough as-is, will add support for SNX in another PR
// MAINNET
// l1: {
// SNX: '0xCd9D4988C0AE61887B075bA77f08cbFAd2b65068',
// },
// l2: {
// SNX: '0x3f87Ff1de58128eF8FCb4c807eFD776E1aC72E51',
// },
// KOVAN
// l1: {
// SNX: '0xD134Db47DDF5A6feB245452af17cCAf92ee53D3c',
// },
// l2: {
// SNX: '0x5C3f51CEd0C2F6157e2be67c029264D6C44bfe42',
// },
/**
* Returns an ethers.Contract object for the given name, connected to the appropriate address for
* the given L1 chain ID. Users can also provide a custom address to connect the contract to
* the given L2 chain ID. Users can also provide a custom address to connect the contract to
* instead. If the chain ID is not known then the user MUST provide a custom address or this
* function will throw an error.
*
* @param contractName Name of the contract to connect to.
* @param l1ChainId Chain ID for the L1 network where the OE contracts are deployed.
* @param l2ChainId Chain ID for the L2 network.
* @param opts Additional options for connecting to the contract.
* @param opts.address Custom address to connect to the contract.
* @param opts.signerOrProvider Signer or provider to connect to the contract.
......@@ -226,16 +178,16 @@ export const BRIDGE_ADAPTER_DATA: {
*/
export const getOEContract = (
contractName: keyof OEL1Contracts | keyof OEL2Contracts,
l1ChainId: number,
l2ChainId: number,
opts: {
address?: AddressLike
signerOrProvider?: ethers.Signer | ethers.providers.Provider
} = {}
): Contract => {
const addresses = CONTRACT_ADDRESSES[l1ChainId]
const addresses = CONTRACT_ADDRESSES[l2ChainId]
if (addresses === undefined && opts.address === undefined) {
throw new Error(
`cannot get contract ${contractName} for unknown L1 chain ID ${l1ChainId}, you must provide an address`
`cannot get contract ${contractName} for unknown L2 chain ID ${l2ChainId}, you must provide an address`
)
}
......@@ -249,12 +201,12 @@ export const getOEContract = (
}
/**
* Automatically connects to all contract addresses, both L1 and L2, for the given L1 chain ID. The
* Automatically connects to all contract addresses, both L1 and L2, for the given L2 chain ID. The
* user can provide custom contract address overrides for L1 or L2 contracts. If the given chain ID
* is not known then the user MUST provide custom contract addresses for ALL L1 contracts or this
* function will throw an error.
*
* @param l1ChainId Chain ID for the L1 network where the OE contracts are deployed.
* @param l2ChainId Chain ID for the L2 network.
* @param opts Additional options for connecting to the contracts.
* @param opts.l1SignerOrProvider: Signer or provider to connect to the L1 contracts.
* @param opts.l2SignerOrProvider: Signer or provider to connect to the L2 contracts.
......@@ -263,14 +215,14 @@ export const getOEContract = (
* both L1 and L2.
*/
export const getAllOEContracts = (
l1ChainId: number,
l2ChainId: number,
opts: {
l1SignerOrProvider?: ethers.Signer | ethers.providers.Provider
l2SignerOrProvider?: ethers.Signer | ethers.providers.Provider
overrides?: DeepPartial<OEContractsLike>
} = {}
): OEContracts => {
const addresses = CONTRACT_ADDRESSES[l1ChainId] || {
const addresses = CONTRACT_ADDRESSES[l2ChainId] || {
l1: {
AddressManager: undefined,
L1CrossDomainMessenger: undefined,
......@@ -287,7 +239,7 @@ export const getAllOEContracts = (
for (const [contractName, contractAddress] of Object.entries(addresses.l1)) {
l1Contracts[contractName] = getOEContract(
contractName as keyof OEL1Contracts,
l1ChainId,
l2ChainId,
{
address: opts.overrides?.l1?.[contractName] || contractAddress,
signerOrProvider: opts.l1SignerOrProvider,
......@@ -300,7 +252,7 @@ export const getAllOEContracts = (
for (const [contractName, contractAddress] of Object.entries(addresses.l2)) {
l2Contracts[contractName] = getOEContract(
contractName as keyof OEL2Contracts,
l1ChainId,
l2ChainId,
{
address: opts.overrides?.l2?.[contractName] || contractAddress,
signerOrProvider: opts.l2SignerOrProvider,
......@@ -315,26 +267,42 @@ export const getAllOEContracts = (
}
/**
* Gets a series of bridge adapters for the given L1 chain ID.
* Gets a series of bridge adapters for the given L2 chain ID.
*
* @param l1ChainId L1 chain ID for the L1 network where the custom bridges are deployed.
* @param l2ChainId Chain ID for the L2 network.
* @param messenger Cross chain messenger to connect to the bridge adapters
* @param opts Additional options for connecting to the custom bridges.
* @param opts.overrides Custom bridge adapters.
* @returns An object containing all bridge adapters
*/
export const getBridgeAdapters = (
l1ChainId: number,
l2ChainId: number,
messenger: ICrossChainMessenger,
opts?: {
overrides?: BridgeAdapterData
}
): BridgeAdapters => {
const adapters: BridgeAdapters = {}
for (const [bridgeName, bridgeData] of Object.entries({
...(BRIDGE_ADAPTER_DATA[l1ChainId] || {}),
const adapterData: BridgeAdapterData = {
...(CONTRACT_ADDRESSES[l2ChainId]
? {
Standard: {
Adapter: StandardBridgeAdapter,
l1Bridge: CONTRACT_ADDRESSES[l2ChainId].l1.L1StandardBridge,
l2Bridge: predeploys.L2StandardBridge,
},
ETH: {
Adapter: ETHBridgeAdapter,
l1Bridge: CONTRACT_ADDRESSES[l2ChainId].l1.L1StandardBridge,
l2Bridge: predeploys.L2StandardBridge,
},
}
: {}),
...(BRIDGE_ADAPTER_DATA[l2ChainId] || {}),
...(opts?.overrides || {}),
})) {
}
const adapters: BridgeAdapters = {}
for (const [bridgeName, bridgeData] of Object.entries(adapterData)) {
adapters[bridgeName] = new bridgeData.Adapter({
messenger,
l1Bridge: bridgeData.l1Bridge,
......
......@@ -15,6 +15,8 @@ import {
CrossChainMessenger,
StandardBridgeAdapter,
ETHBridgeAdapter,
L1ChainID,
L2ChainID,
} from '../src'
import { DUMMY_MESSAGE } from './helpers'
......@@ -31,7 +33,8 @@ describe('CrossChainMessenger', () => {
const messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: ethers.provider,
l1ChainId: 1,
l1ChainId: L1ChainID.MAINNET,
l2ChainId: L2ChainID.OPTIMISM,
})
expect(messenger.l1Provider).to.equal(ethers.provider)
......@@ -43,7 +46,8 @@ describe('CrossChainMessenger', () => {
const messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: ethers.provider,
l1ChainId: 1,
l1ChainId: L1ChainID.MAINNET,
l2ChainId: L2ChainID.OPTIMISM,
})
expect(messenger.l2Provider).to.equal(ethers.provider)
......@@ -55,7 +59,8 @@ describe('CrossChainMessenger', () => {
const messenger = new CrossChainMessenger({
l1SignerOrProvider: 'https://localhost:8545',
l2SignerOrProvider: ethers.provider,
l1ChainId: 1,
l1ChainId: L1ChainID.MAINNET,
l2ChainId: L2ChainID.OPTIMISM,
})
expect(Provider.isProvider(messenger.l1Provider)).to.be.true
......@@ -67,7 +72,8 @@ describe('CrossChainMessenger', () => {
const messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: 'https://localhost:8545',
l1ChainId: 1,
l1ChainId: L1ChainID.MAINNET,
l2ChainId: L2ChainID.OPTIMISM,
})
expect(Provider.isProvider(messenger.l2Provider)).to.be.true
......@@ -80,10 +86,11 @@ describe('CrossChainMessenger', () => {
const messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: 'https://localhost:8545',
l1ChainId: 1,
l1ChainId: L1ChainID.MAINNET,
l2ChainId: L2ChainID.OPTIMISM,
})
const addresses = CONTRACT_ADDRESSES[1]
const addresses = CONTRACT_ADDRESSES[messenger.l2ChainId]
for (const [contractName, contractAddress] of Object.entries(
addresses.l1
)) {
......@@ -99,13 +106,14 @@ describe('CrossChainMessenger', () => {
})
})
describe('when given an unknown chain ID', () => {
describe('when given an unknown L2 chain ID', () => {
it('should throw an error', () => {
expect(() => {
new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: 'https://localhost:8545',
l1ChainId: 1234,
l1ChainId: L1ChainID.MAINNET,
l2ChainId: 1234,
})
}).to.throw()
})
......@@ -126,11 +134,12 @@ describe('CrossChainMessenger', () => {
const messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: 'https://localhost:8545',
l1ChainId: 1,
l1ChainId: L1ChainID.MAINNET,
l2ChainId: L2ChainID.OPTIMISM,
contracts: overrides,
})
const addresses = CONTRACT_ADDRESSES[1]
const addresses = CONTRACT_ADDRESSES[messenger.l2ChainId]
for (const [contractName, contractAddress] of Object.entries(
addresses.l1
)) {
......@@ -156,7 +165,7 @@ describe('CrossChainMessenger', () => {
})
})
describe('when given an unknown chain ID', () => {
describe('when given an unknown L2 chain ID', () => {
describe('when all L1 addresses are provided', () => {
it('should use custom addresses where provided', () => {
const overrides = {
......@@ -172,14 +181,16 @@ describe('CrossChainMessenger', () => {
L2CrossDomainMessenger: '0x' + '22'.repeat(20),
},
}
const messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: 'https://localhost:8545',
l1ChainId: 1234,
l1ChainId: L1ChainID.MAINNET,
l2ChainId: 1234,
contracts: overrides,
})
const addresses = CONTRACT_ADDRESSES[1]
const addresses = CONTRACT_ADDRESSES[L2ChainID.OPTIMISM]
for (const [contractName, contractAddress] of Object.entries(
addresses.l1
)) {
......@@ -211,7 +222,8 @@ describe('CrossChainMessenger', () => {
new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: 'https://localhost:8545',
l1ChainId: 1234,
l1ChainId: L1ChainID.MAINNET,
l2ChainId: 1234,
contracts: {
l1: {
// Missing some required L1 addresses
......@@ -246,7 +258,8 @@ describe('CrossChainMessenger', () => {
messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: ethers.provider,
l1ChainId: 31337,
l1ChainId: L1ChainID.HARDHAT_LOCAL,
l2ChainId: L2ChainID.OPTIMISM_HARDHAT_LOCAL,
contracts: {
l1: {
L1CrossDomainMessenger: l1Messenger.address,
......@@ -433,7 +446,8 @@ describe('CrossChainMessenger', () => {
messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: ethers.provider,
l1ChainId: 31337,
l1ChainId: L1ChainID.HARDHAT_LOCAL,
l2ChainId: L2ChainID.OPTIMISM_HARDHAT_LOCAL,
contracts: {
l1: {
L1CrossDomainMessenger: l1Messenger.address,
......@@ -557,7 +571,8 @@ describe('CrossChainMessenger', () => {
messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: ethers.provider,
l1ChainId: 31337,
l1ChainId: L1ChainID.HARDHAT_LOCAL,
l2ChainId: L2ChainID.OPTIMISM_HARDHAT_LOCAL,
contracts: {
l1: {
L1CrossDomainMessenger: l1Messenger.address,
......@@ -761,7 +776,8 @@ describe('CrossChainMessenger', () => {
messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: ethers.provider,
l1ChainId: 31337,
l1ChainId: L1ChainID.HARDHAT_LOCAL,
l2ChainId: L2ChainID.OPTIMISM_HARDHAT_LOCAL,
contracts: {
l1: {
L1CrossDomainMessenger: l1Messenger.address,
......@@ -910,7 +926,8 @@ describe('CrossChainMessenger', () => {
messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: ethers.provider,
l1ChainId: 31337,
l1ChainId: L1ChainID.HARDHAT_LOCAL,
l2ChainId: L2ChainID.OPTIMISM_HARDHAT_LOCAL,
contracts: {
l2: {
L2CrossDomainMessenger: l2Messenger.address,
......@@ -1014,7 +1031,8 @@ describe('CrossChainMessenger', () => {
messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: ethers.provider,
l1ChainId: 31337,
l1ChainId: L1ChainID.HARDHAT_LOCAL,
l2ChainId: L2ChainID.OPTIMISM_HARDHAT_LOCAL,
})
})
......@@ -1117,7 +1135,8 @@ describe('CrossChainMessenger', () => {
messenger = new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: ethers.provider,
l1ChainId: 31337,
l1ChainId: L1ChainID.HARDHAT_LOCAL,
l2ChainId: L2ChainID.OPTIMISM_HARDHAT_LOCAL,
contracts: {
l1: {
L1CrossDomainMessenger: l1Messenger.address,
......@@ -1275,7 +1294,8 @@ describe('CrossChainMessenger', () => {
messenger = new CrossChainMessenger({
l1SignerOrProvider: l1Signer,
l2SignerOrProvider: l2Signer,
l1ChainId: 31337,
l1ChainId: L1ChainID.HARDHAT_LOCAL,
l2ChainId: L2ChainID.OPTIMISM_HARDHAT_LOCAL,
contracts: {
l1: {
L1CrossDomainMessenger: l1Messenger.address,
......@@ -1370,7 +1390,8 @@ describe('CrossChainMessenger', () => {
messenger = new CrossChainMessenger({
l1SignerOrProvider: l1Signer,
l2SignerOrProvider: l2Signer,
l1ChainId: 31337,
l1ChainId: L1ChainID.HARDHAT_LOCAL,
l2ChainId: L2ChainID.OPTIMISM_HARDHAT_LOCAL,
contracts: {
l1: {
L1CrossDomainMessenger: l1Messenger.address,
......@@ -1466,7 +1487,8 @@ describe('CrossChainMessenger', () => {
messenger = new CrossChainMessenger({
l1SignerOrProvider: l1Signer,
l2SignerOrProvider: l2Signer,
l1ChainId: 31337,
l1ChainId: L1ChainID.HARDHAT_LOCAL,
l2ChainId: L2ChainID.OPTIMISM_HARDHAT_LOCAL,
contracts: {
l1: {
L1CrossDomainMessenger: l1Messenger.address,
......@@ -1522,7 +1544,8 @@ describe('CrossChainMessenger', () => {
messenger = new CrossChainMessenger({
l1SignerOrProvider: l1Signer,
l2SignerOrProvider: l2Signer,
l1ChainId: 31337,
l1ChainId: L1ChainID.HARDHAT_LOCAL,
l2ChainId: L2ChainID.OPTIMISM_HARDHAT_LOCAL,
contracts: {
l1: {
L1CrossDomainMessenger: l1Messenger.address,
......
......@@ -8,6 +8,7 @@ import {
getAllOEContracts,
CONTRACT_ADDRESSES,
DEFAULT_L2_CONTRACT_ADDRESSES,
L2ChainID,
} from '../../src'
describe('contract connection utils', () => {
......@@ -20,12 +21,15 @@ describe('contract connection utils', () => {
describe('when given a known chain ID', () => {
describe('when not given an address override', () => {
it('should use the address for the given contract name and chain ID', () => {
const addresses = CONTRACT_ADDRESSES[1]
const addresses = CONTRACT_ADDRESSES[L2ChainID.OPTIMISM]
for (const [contractName, contractAddress] of [
...Object.entries(addresses.l1),
...Object.entries(addresses.l2),
]) {
const contract = getOEContract(contractName as any, 1)
const contract = getOEContract(
contractName as any,
L2ChainID.OPTIMISM
)
expect(contract.address).to.equal(contractAddress)
}
})
......@@ -33,7 +37,7 @@ describe('contract connection utils', () => {
describe('when given an address override', () => {
it('should use the custom address', () => {
const addresses = CONTRACT_ADDRESSES[1]
const addresses = CONTRACT_ADDRESSES[L2ChainID.OPTIMISM]
for (const contractName of [
...Object.keys(addresses.l1),
...Object.keys(addresses.l2),
......@@ -68,13 +72,19 @@ describe('contract connection utils', () => {
describe('when connected to a valid address', () => {
it('should have the correct interface for the contract name', () => {
const contract = getOEContract('L1CrossDomainMessenger', 1)
const contract = getOEContract(
'L1CrossDomainMessenger',
L2ChainID.OPTIMISM
)
expect(contract.sendMessage).to.not.be.undefined
})
describe('when not given a signer or provider', () => {
it('should not have a signer or provider', () => {
const contract = getOEContract('L1CrossDomainMessenger', 1)
const contract = getOEContract(
'L1CrossDomainMessenger',
L2ChainID.OPTIMISM
)
expect(contract.signer).to.be.null
expect(contract.provider).to.be.null
})
......@@ -82,18 +92,26 @@ describe('contract connection utils', () => {
describe('when given a signer', () => {
it('should attach the given signer', () => {
const contract = getOEContract('L1CrossDomainMessenger', 1, {
signerOrProvider: signers[0],
})
const contract = getOEContract(
'L1CrossDomainMessenger',
L2ChainID.OPTIMISM,
{
signerOrProvider: signers[0],
}
)
expect(contract.signer).to.deep.equal(signers[0])
})
})
describe('when given a provider', () => {
it('should attach the given provider', () => {
const contract = getOEContract('L1CrossDomainMessenger', 1, {
signerOrProvider: ethers.provider as any,
})
const contract = getOEContract(
'L1CrossDomainMessenger',
L2ChainID.OPTIMISM,
{
signerOrProvider: ethers.provider as any,
}
)
expect(contract.signer).to.be.null
expect(contract.provider).to.deep.equal(ethers.provider)
})
......@@ -105,8 +123,8 @@ describe('contract connection utils', () => {
describe('when given a known chain ID', () => {
describe('when not given any address overrides', () => {
it('should return all contracts connected to the default addresses', () => {
const contracts = getAllOEContracts(1)
const addresses = CONTRACT_ADDRESSES[1]
const contracts = getAllOEContracts(L2ChainID.OPTIMISM)
const addresses = CONTRACT_ADDRESSES[L2ChainID.OPTIMISM]
for (const [contractName, contractAddress] of Object.entries(
addresses.l1
)) {
......@@ -132,8 +150,8 @@ describe('contract connection utils', () => {
L2CrossDomainMessenger: '0x' + '22'.repeat(20),
},
}
const contracts = getAllOEContracts(1, { overrides })
const addresses = CONTRACT_ADDRESSES[1]
const contracts = getAllOEContracts(L2ChainID.OPTIMISM, { overrides })
const addresses = CONTRACT_ADDRESSES[L2ChainID.OPTIMISM]
for (const [contractName, contractAddress] of Object.entries(
addresses.l1
)) {
......@@ -163,7 +181,9 @@ describe('contract connection utils', () => {
describe('when given address overrides for L2 contracts', () => {
it('should return contracts connected to the overridden addresses where given', () => {
const l1Overrides = {}
for (const contractName of Object.keys(CONTRACT_ADDRESSES[1].l1)) {
for (const contractName of Object.keys(
CONTRACT_ADDRESSES[L2ChainID.OPTIMISM].l1
)) {
l1Overrides[contractName] = '0x' + '11'.repeat(20)
}
......@@ -191,7 +211,9 @@ describe('contract connection utils', () => {
describe('when not given address overrides for L2 contracts', () => {
it('should return contracts connected to the default L2 addresses and custom L1 addresses', () => {
const l1Overrides = {}
for (const contractName of Object.keys(CONTRACT_ADDRESSES[1].l1)) {
for (const contractName of Object.keys(
CONTRACT_ADDRESSES[L2ChainID.OPTIMISM].l1
)) {
l1Overrides[contractName] = '0x' + '11'.repeat(20)
}
......@@ -241,7 +263,7 @@ describe('contract connection utils', () => {
describe('when not given a signer or provider', () => {
it('should not attach a signer or provider to any contracts', () => {
const contracts = getAllOEContracts(1)
const contracts = getAllOEContracts(L2ChainID.OPTIMISM)
for (const contract of Object.values(contracts.l1)) {
expect(contract.signer).to.be.null
expect(contract.provider).to.be.null
......@@ -255,7 +277,7 @@ describe('contract connection utils', () => {
describe('when given an L1 signer', () => {
it('should attach the signer to the L1 contracts only', () => {
const contracts = getAllOEContracts(1, {
const contracts = getAllOEContracts(L2ChainID.OPTIMISM, {
l1SignerOrProvider: signers[0],
})
for (const contract of Object.values(contracts.l1)) {
......@@ -270,7 +292,7 @@ describe('contract connection utils', () => {
describe('when given an L2 signer', () => {
it('should attach the signer to the L2 contracts only', () => {
const contracts = getAllOEContracts(1, {
const contracts = getAllOEContracts(L2ChainID.OPTIMISM, {
l2SignerOrProvider: signers[0],
})
for (const contract of Object.values(contracts.l1)) {
......@@ -285,7 +307,7 @@ describe('contract connection utils', () => {
describe('when given an L1 signer and an L2 signer', () => {
it('should attach the signer to both sets of contracts', () => {
const contracts = getAllOEContracts(1, {
const contracts = getAllOEContracts(L2ChainID.OPTIMISM, {
l1SignerOrProvider: signers[0],
l2SignerOrProvider: signers[1],
})
......@@ -300,7 +322,7 @@ describe('contract connection utils', () => {
describe('when given an L1 provider', () => {
it('should attach the provider to the L1 contracts only', () => {
const contracts = getAllOEContracts(1, {
const contracts = getAllOEContracts(L2ChainID.OPTIMISM, {
l1SignerOrProvider: ethers.provider as any,
})
for (const contract of Object.values(contracts.l1)) {
......@@ -316,7 +338,7 @@ describe('contract connection utils', () => {
describe('when given an L2 provider', () => {
it('should attach the provider to the L2 contracts only', () => {
const contracts = getAllOEContracts(1, {
const contracts = getAllOEContracts(L2ChainID.OPTIMISM, {
l2SignerOrProvider: ethers.provider as any,
})
for (const contract of Object.values(contracts.l1)) {
......@@ -332,7 +354,7 @@ describe('contract connection utils', () => {
describe('when given an L1 provider and an L2 provider', () => {
it('should attach the provider to both sets of contracts', () => {
const contracts = getAllOEContracts(1, {
const contracts = getAllOEContracts(L2ChainID.OPTIMISM, {
l1SignerOrProvider: ethers.provider as any,
l2SignerOrProvider: ethers.provider as any,
})
......
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