mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-05-18 02:29:29 +00:00
fix: only allow linea sepolia
This commit is contained in:
parent
1bd6a6d920
commit
a09444ab9f
@ -5,11 +5,11 @@ import {
|
|||||||
PublicClient,
|
PublicClient,
|
||||||
WalletClient
|
WalletClient
|
||||||
} from "viem";
|
} from "viem";
|
||||||
import { type Chain, lineaSepolia } from "viem/chains";
|
import { lineaSepolia } from "viem/chains";
|
||||||
|
|
||||||
export const createViemClientFromWindow = async (
|
export const createViemClientFromWindow = async (): Promise<
|
||||||
chain: Chain = lineaSepolia
|
WalletClient & PublicClient
|
||||||
): Promise<WalletClient & PublicClient> => {
|
> => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
const ethereum = (window as any).ethereum;
|
const ethereum = (window as any).ethereum;
|
||||||
|
|
||||||
@ -23,9 +23,26 @@ export const createViemClientFromWindow = async (
|
|||||||
|
|
||||||
const rpcClient = createWalletClient({
|
const rpcClient = createWalletClient({
|
||||||
account,
|
account,
|
||||||
chain,
|
chain: lineaSepolia,
|
||||||
transport: custom(ethereum)
|
transport: custom(ethereum)
|
||||||
}).extend(publicActions);
|
}).extend(publicActions);
|
||||||
|
|
||||||
|
// Ensure wallet is connected to Linea Sepolia
|
||||||
|
try {
|
||||||
|
await rpcClient.switchChain({ id: lineaSepolia.id });
|
||||||
|
} catch (error: unknown) {
|
||||||
|
// This error code indicates that the chain has not been added to the wallet
|
||||||
|
if (
|
||||||
|
typeof error === "object" &&
|
||||||
|
error !== null &&
|
||||||
|
"code" in error &&
|
||||||
|
error.code === 4902
|
||||||
|
) {
|
||||||
|
await rpcClient.addChain({ chain: lineaSepolia });
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return rpcClient;
|
return rpcClient;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user