Skip to content

Commit

Permalink
extracts shared validate chainId function
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Dec 27, 2023
1 parent db96d7d commit fc64f77
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/orbitClient.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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),
}));
}

0 comments on commit fc64f77

Please sign in to comment.