Skip to content

Commit

Permalink
[SDK] feat: Enable chain switching for EIP1193 provider (#6269)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquim-verges authored Feb 16, 2025
1 parent 52cbcd2 commit 0574eac
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/dirty-scissors-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Enable chain switching for toEIP1194 provider
14 changes: 13 additions & 1 deletion apps/portal/src/app/react/v5/adapters/page.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Stack, InstallTabs, Callout, Steps, Step, GithubTemplateCard } from "@doc";

# Adapters

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

### Using thirdweb in-app wallets with wagmi

You can use in-app and ecosystem wallets in your wagmi application by using the `@thirdweb-dev/wagmi-adapter` package.
View the demo repo for using thirdweb in-app / smart wallets with wagmi:

<Stack>
<GithubTemplateCard
title="wagmi + thirdweb demo repo"
description="A demo repo for using thirdweb in-app / smart wallets with wagmi"
href="https://github.com/thirdweb-example/wagmi-inapp-smart-wallets"
/>
</Stack>

You can use thirdweb's in-app, ecosystem and smart wallets in your wagmi application by using the `@thirdweb-dev/wagmi-adapter` package.

```shell
npm install thirdweb @thirdweb-dev/wagmi-adapter
Expand Down
18 changes: 18 additions & 0 deletions packages/thirdweb/src/adapters/eip1193/to-eip1193.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { Account } from "viem/accounts";

import type { Chain } from "../../chains/types.js";
import { getCachedChain } from "../../chains/utils.js";
import type { ThirdwebClient } from "../../client/client.js";
import { getRpcClient } from "../../rpc/rpc.js";
import { estimateGas } from "../../transaction/actions/estimate-gas.js";
import { sendTransaction } from "../../transaction/actions/send-transaction.js";
import { prepareTransaction } from "../../transaction/prepare-transaction.js";
import { hexToNumber, isHex } from "../../utils/encoding/hex.js";
import type { Wallet } from "../../wallets/interfaces/wallet.js";
import type { EIP1193Provider } from "./types.js";

Expand Down Expand Up @@ -128,6 +130,22 @@ export function toProvider(options: ToEip1193ProviderOptions): EIP1193Provider {
}
return [account.address];
}
if (
request.method === "wallet_switchEthereumChain" ||
request.method === "wallet_addEthereumChain"
) {
const data = request.params[0];
const chainIdHex = data.chainId;
if (!chainIdHex) {
throw new Error("Chain ID is required");
}
// chainId is hex most likely, convert to number
const chainId = isHex(chainIdHex)
? hexToNumber(chainIdHex)
: chainIdHex;
const chain = getCachedChain(chainId);
return wallet.switchChain(chain);
}
return rpcClient(request);
},
};
Expand Down

0 comments on commit 0574eac

Please sign in to comment.