0xtrails
Version:
SDK for Trails
62 lines (55 loc) • 1.73 kB
text/typescript
import type { WalletClient } from "viem"
import { getChainInfo } from "./chains.js"
import { logger } from "./logger.js"
export type AttemptSwitchChainParams = {
walletClient: WalletClient
desiredChainId: number
}
export async function attemptSwitchChain({
walletClient,
desiredChainId,
}: AttemptSwitchChainParams): Promise<void> {
try {
// Check if the chain was switched successfully
let currentChainId = await walletClient.getChainId()
if (currentChainId === desiredChainId) {
logger.console.log(
"[trails-sdk] Chain already switched to:",
desiredChainId,
)
return
}
logger.console.log(
"[trails-sdk] Switching to chain:",
desiredChainId,
"currentChainId",
currentChainId,
)
await walletClient.switchChain({ id: desiredChainId })
// Check if the chain was switched successfully
currentChainId = await walletClient.getChainId()
if (currentChainId !== desiredChainId) {
throw new Error("Failed to switch chain")
}
logger.console.log("[trails-sdk] Chain switched to:", desiredChainId)
} catch (error: unknown) {
if (
error instanceof Error &&
error.message.includes("wallet_addEthereumChain")
) {
const chainInfo = getChainInfo(desiredChainId, {
usePublicRpc: true,
})
if (chainInfo) {
await walletClient.addChain({
chain: chainInfo,
})
return attemptSwitchChain({ walletClient, desiredChainId })
}
}
logger.console.error("[trails-sdk] Chain switch failed:", error)
throw new Error(
`[trails-sdk] Failed to switch chain: ${error instanceof Error ? error.message : "Unknown error"}`,
)
}
}