|
1 | 1 | import type { Account } from "viem/accounts"; |
2 | 2 |
|
3 | 3 | import type { Chain } from "../../chains/types.js"; |
| 4 | +import { getCachedChain } from "../../chains/utils.js"; |
4 | 5 | import type { ThirdwebClient } from "../../client/client.js"; |
5 | 6 | import { getRpcClient } from "../../rpc/rpc.js"; |
6 | 7 | import { estimateGas } from "../../transaction/actions/estimate-gas.js"; |
7 | 8 | import { sendTransaction } from "../../transaction/actions/send-transaction.js"; |
8 | 9 | import { prepareTransaction } from "../../transaction/prepare-transaction.js"; |
| 10 | +import { hexToNumber, isHex } from "../../utils/encoding/hex.js"; |
9 | 11 | import type { Wallet } from "../../wallets/interfaces/wallet.js"; |
10 | 12 | import type { EIP1193Provider } from "./types.js"; |
11 | 13 |
|
@@ -128,6 +130,22 @@ export function toProvider(options: ToEip1193ProviderOptions): EIP1193Provider { |
128 | 130 | } |
129 | 131 | return [account.address]; |
130 | 132 | } |
| 133 | + if ( |
| 134 | + request.method === "wallet_switchEthereumChain" || |
| 135 | + request.method === "wallet_addEthereumChain" |
| 136 | + ) { |
| 137 | + const data = request.params[0]; |
| 138 | + const chainIdHex = data.chainId; |
| 139 | + if (!chainIdHex) { |
| 140 | + throw new Error("Chain ID is required"); |
| 141 | + } |
| 142 | + // chainId is hex most likely, convert to number |
| 143 | + const chainId = isHex(chainIdHex) |
| 144 | + ? hexToNumber(chainIdHex) |
| 145 | + : chainIdHex; |
| 146 | + const chain = getCachedChain(chainId); |
| 147 | + return wallet.switchChain(chain); |
| 148 | + } |
131 | 149 | return rpcClient(request); |
132 | 150 | }, |
133 | 151 | }; |
|
0 commit comments