Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
brtkx committed Aug 21, 2023
1 parent 7de3b79 commit af42cc1
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions src/lib/dataEntities/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,10 @@ export const getL2Network = (
return getNetwork(signerOrProviderOrChainID, 2) as Promise<L2Network>
}

/**
* Returns a chain that is associated with at least one child chain
* @param signerOrProviderOrChainID
* @returns Chain that is associated with at least one child chain
*/
export const getParentChain = async (
signerOrProviderOrChainID: SignerOrProvider | number
): Promise<ParentChain> => {
const getParentChainOrChain = async (
signerOrProviderOrChainID: SignerOrProvider | number,
type: 'ParentChain' | 'Chain'
) => {
const chainID = await (async () => {
if (typeof signerOrProviderOrChainID === 'number') {
return signerOrProviderOrChainID
Expand All @@ -386,13 +382,28 @@ export const getParentChain = async (
return chainId
})()

const chain = parentChains[chainID]
const _chains = type === 'ParentChain' ? parentChains : chains
const chain = _chains[chainID]

if (chain) {
return chain
}

throw new ArbSdkError(`Unrecognized parent chain ${chainID}`)
throw new ArbSdkError(`Unrecognized ${type} ${chainID}.`)
}

/**
* Returns a chain that is associated with at least one child chain
* @param signerOrProviderOrChainID
* @returns Chain that is associated with at least one child chain
*/
export const getParentChain = async (
signerOrProviderOrChainID: SignerOrProvider | number
): Promise<ParentChain> => {
return getParentChainOrChain(
signerOrProviderOrChainID,
'ParentChain'
) as Promise<ParentChain>
}

/**
Expand All @@ -403,25 +414,10 @@ export const getParentChain = async (
export const getChain = async (
signerOrProviderOrChainID: SignerOrProvider | number
): Promise<Chain> => {
const chainID = await (async () => {
if (typeof signerOrProviderOrChainID === 'number') {
return signerOrProviderOrChainID
}
const provider = SignerProviderUtils.getProviderOrThrow(
signerOrProviderOrChainID
)

const { chainId } = await provider.getNetwork()
return chainId
})()

const chain = chains[chainID]

if (chain) {
return chain
}

throw new ArbSdkError(`Unrecognized chain ${chainID}`)
return getParentChainOrChain(
signerOrProviderOrChainID,
'Chain'
) as Promise<Chain>
}

/**
Expand Down

0 comments on commit af42cc1

Please sign in to comment.