Commit 680714c1 authored by smartcontracts's avatar smartcontracts Committed by GitHub

fix(sdk): throw a better error for bad chain ID (#3065)

Updates the SDK to throw a better error when the user provides a bad
chain ID. Would've helped an end-user more easily debug an issue where
the chain ID parameter was missing.
Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent 90630336
---
'@eth-optimism/sdk': patch
---
Updates the CCM to throw a better error for missing or invalid chain IDs
...@@ -85,8 +85,18 @@ export class CrossChainMessenger implements ICrossChainMessenger { ...@@ -85,8 +85,18 @@ export class CrossChainMessenger implements ICrossChainMessenger {
}) { }) {
this.l1SignerOrProvider = toSignerOrProvider(opts.l1SignerOrProvider) this.l1SignerOrProvider = toSignerOrProvider(opts.l1SignerOrProvider)
this.l2SignerOrProvider = toSignerOrProvider(opts.l2SignerOrProvider) this.l2SignerOrProvider = toSignerOrProvider(opts.l2SignerOrProvider)
this.l1ChainId = toNumber(opts.l1ChainId)
this.l2ChainId = toNumber(opts.l2ChainId) try {
this.l1ChainId = toNumber(opts.l1ChainId)
} catch (err) {
throw new Error(`L1 chain ID is missing or invalid: ${opts.l1ChainId}`)
}
try {
this.l2ChainId = toNumber(opts.l2ChainId)
} catch (err) {
throw new Error(`L2 chain ID is missing or invalid: ${opts.l2ChainId}`)
}
this.depositConfirmationBlocks = this.depositConfirmationBlocks =
opts?.depositConfirmationBlocks !== undefined opts?.depositConfirmationBlocks !== undefined
......
...@@ -80,6 +80,32 @@ describe('CrossChainMessenger', () => { ...@@ -80,6 +80,32 @@ describe('CrossChainMessenger', () => {
}) })
}) })
describe('when given a bad L1 chain ID', () => {
it('should throw an error', () => {
expect(() => {
new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: ethers.provider,
l1ChainId: undefined as any,
l2ChainId: L2ChainID.OPTIMISM,
})
}).to.throw('L1 chain ID is missing or invalid')
})
})
describe('when given a bad L2 chain ID', () => {
it('should throw an error', () => {
expect(() => {
new CrossChainMessenger({
l1SignerOrProvider: ethers.provider,
l2SignerOrProvider: ethers.provider,
l1ChainId: L1ChainID.MAINNET,
l2ChainId: undefined as any,
})
}).to.throw('L2 chain ID is missing or invalid')
})
})
describe('when no custom contract addresses are provided', () => { describe('when no custom contract addresses are provided', () => {
describe('when given a known chain ID', () => { describe('when given a known chain ID', () => {
it('should use the contract addresses for the known chain ID', () => { it('should use the contract addresses for the known chain ID', () => {
......
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