File tree Expand file tree Collapse file tree 1 file changed +22
-5
lines changed Expand file tree Collapse file tree 1 file changed +22
-5
lines changed Original file line number Diff line number Diff line change @@ -5,11 +5,11 @@ import {
55 PublicClient ,
66 WalletClient
77} from "viem" ;
8- import { type Chain , lineaSepolia } from "viem/chains" ;
8+ import { lineaSepolia } from "viem/chains" ;
99
10- export const createViemClientFromWindow = async (
11- chain : Chain = lineaSepolia
12- ) : Promise < WalletClient & PublicClient > => {
10+ export const createViemClientFromWindow = async ( ) : Promise <
11+ WalletClient & PublicClient
12+ > => {
1313 // eslint-disable-next-line @typescript-eslint/no-explicit-any
1414 const ethereum = ( window as any ) . ethereum ;
1515
@@ -23,9 +23,26 @@ export const createViemClientFromWindow = async (
2323
2424 const rpcClient = createWalletClient ( {
2525 account,
26- chain,
26+ chain : lineaSepolia ,
2727 transport : custom ( ethereum )
2828 } ) . extend ( publicActions ) ;
2929
30+ // Ensure wallet is connected to Linea Sepolia
31+ try {
32+ await rpcClient . switchChain ( { id : lineaSepolia . id } ) ;
33+ } catch ( error : unknown ) {
34+ // This error code indicates that the chain has not been added to the wallet
35+ if (
36+ typeof error === "object" &&
37+ error !== null &&
38+ "code" in error &&
39+ error . code === 4902
40+ ) {
41+ await rpcClient . addChain ( { chain : lineaSepolia } ) ;
42+ } else {
43+ throw error ;
44+ }
45+ }
46+
3047 return rpcClient ;
3148} ;
You can’t perform that action at this time.
0 commit comments