From fc64f77dfe4e9de687babf4febb40ace1f3c70ae Mon Sep 17 00:00:00 2001 From: Doug <4741454+douglance@users.noreply.github.com> Date: Wed, 27 Dec 2023 11:20:51 -0500 Subject: [PATCH] extracts shared validate chainId function --- src/orbitClient.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/orbitClient.ts b/src/orbitClient.ts index fb075a0f..d92562aa 100644 --- a/src/orbitClient.ts +++ b/src/orbitClient.ts @@ -1,4 +1,4 @@ -import { PublicClient, PublicClientConfig, createPublicClient } from 'viem'; +import { Client, PublicClient, PublicClientConfig, createPublicClient } from 'viem'; import { rollupCreator } from './contracts'; import { ParentChainId, validParentChainId } from './types/ParentChain'; @@ -7,24 +7,23 @@ export interface OrbitClient extends PublicClient { getValidChainId(): ParentChainId; } +const validateClientChainId = (client: Client) => { + const chainId = client.chain?.id; + if (!validParentChainId(chainId)) { + throw new Error('chainId is undefined'); + } + return chainId; +}; + export function createOrbitClient({ chain, transport }: PublicClientConfig): OrbitClient { return createPublicClient({ chain, transport, - }).extend((client) => ({ + }).extend((client: Client) => ({ getRollupCreatorAddress: () => { - const chainId = client.chain?.id; - if (!validParentChainId(chainId)) { - throw new Error('chainId is undefined'); - } + const chainId = validateClientChainId(client); return rollupCreator.address[chainId]; }, - getValidChainId: () => { - const chainId = client.chain?.id; - if (!validParentChainId(chainId)) { - throw new Error('chainId is undefined'); - } - return chainId; - }, + getValidChainId: () => validateClientChainId(client), })); }