Skip to content

Commit 0574eac

Browse files
[SDK] feat: Enable chain switching for EIP1193 provider (#6269)
1 parent 52cbcd2 commit 0574eac

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

.changeset/dirty-scissors-shake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Enable chain switching for toEIP1194 provider

apps/portal/src/app/react/v5/adapters/page.mdx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Stack, InstallTabs, Callout, Steps, Step, GithubTemplateCard } from "@doc";
2+
13
# Adapters
24

35
The thirdweb SDK can work side by side with:
@@ -16,7 +18,17 @@ Adapters allow you to use contracts, providers and wallets from these libraries
1618

1719
### Using thirdweb in-app wallets with wagmi
1820

19-
You can use in-app and ecosystem wallets in your wagmi application by using the `@thirdweb-dev/wagmi-adapter` package.
21+
View the demo repo for using thirdweb in-app / smart wallets with wagmi:
22+
23+
<Stack>
24+
<GithubTemplateCard
25+
title="wagmi + thirdweb demo repo"
26+
description="A demo repo for using thirdweb in-app / smart wallets with wagmi"
27+
href="https://github.com/thirdweb-example/wagmi-inapp-smart-wallets"
28+
/>
29+
</Stack>
30+
31+
You can use thirdweb's in-app, ecosystem and smart wallets in your wagmi application by using the `@thirdweb-dev/wagmi-adapter` package.
2032

2133
```shell
2234
npm install thirdweb @thirdweb-dev/wagmi-adapter

packages/thirdweb/src/adapters/eip1193/to-eip1193.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { Account } from "viem/accounts";
22

33
import type { Chain } from "../../chains/types.js";
4+
import { getCachedChain } from "../../chains/utils.js";
45
import type { ThirdwebClient } from "../../client/client.js";
56
import { getRpcClient } from "../../rpc/rpc.js";
67
import { estimateGas } from "../../transaction/actions/estimate-gas.js";
78
import { sendTransaction } from "../../transaction/actions/send-transaction.js";
89
import { prepareTransaction } from "../../transaction/prepare-transaction.js";
10+
import { hexToNumber, isHex } from "../../utils/encoding/hex.js";
911
import type { Wallet } from "../../wallets/interfaces/wallet.js";
1012
import type { EIP1193Provider } from "./types.js";
1113

@@ -128,6 +130,22 @@ export function toProvider(options: ToEip1193ProviderOptions): EIP1193Provider {
128130
}
129131
return [account.address];
130132
}
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+
}
131149
return rpcClient(request);
132150
},
133151
};

0 commit comments

Comments
 (0)