Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/few-hats-slide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Better handling of erc5792 getCapabilities
5 changes: 5 additions & 0 deletions .changeset/sixty-views-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/wagmi-adapter": patch
---

Better handling of autoconnect to last connected chain
4 changes: 2 additions & 2 deletions apps/wagmi-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"react": "19.1.0",
"react-dom": "19.1.0",
"thirdweb": "workspace:*",
"viem": "2.37.9",
"wagmi": "2.17.5"
"viem": "2.39.0",
"wagmi": "2.19.4"
},
"devDependencies": {
"@biomejs/biome": "2.0.6",
Expand Down
8 changes: 7 additions & 1 deletion apps/wagmi-demo/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ConnectionOptions } from "@thirdweb-dev/wagmi-adapter";
import { ConnectButton } from "thirdweb/react";
import { createWallet } from "thirdweb/wallets";
import {
useAccount,
useCallsStatus,
Expand Down Expand Up @@ -58,7 +59,12 @@ function App() {
<ConnectButton
client={client}
chain={thirdwebChainForWallet}
wallets={[wallet]}
wallets={[
wallet,
createWallet("io.metamask"),
createWallet("com.coinbase.wallet"),
createWallet("me.rainbow"),
]}
onConnect={(wallet) => {
// auto connect to wagmi on tw connect
const twConnector = connectors.find(
Expand Down
2 changes: 1 addition & 1 deletion packages/thirdweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"qrcode": "1.5.3",
"toml": "3.0.0",
"uqr": "0.1.2",
"viem": "2.37.9",
"viem": "2.39.0",
"x402": "0.7.0",
"zod": "3.25.75"
},
Expand Down
12 changes: 11 additions & 1 deletion packages/thirdweb/src/adapters/eip1193/to-eip1193.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,17 @@
if (!account.getCapabilities) {
throw new Error("Wallet does not support EIP-5792");
}
return account.getCapabilities({ chainId: chain.id });
const chains = request.params[1];
if (chains && Array.isArray(chains)) {
const firstChainStr = chains[0];
const firstChainId = isHex(firstChainStr)
? hexToNumber(firstChainStr)
: Number(firstChainStr);
return account.getCapabilities(
firstChainId ? { chainId: firstChainId } : {},
);
}
return account.getCapabilities({});

Check warning on line 177 in packages/thirdweb/src/adapters/eip1193/to-eip1193.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/adapters/eip1193/to-eip1193.ts#L167-L177

Added lines #L167 - L177 were not covered by tests
}
case "wallet_sendCalls": {
const account = wallet.getAccount();
Expand Down
6 changes: 4 additions & 2 deletions packages/thirdweb/src/wallets/eip5792/get-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

export function toGetCapabilitiesResult(
result: Record<string, WalletCapabilities>,
chainId?: number,
chainIdFilter?: number,

Check warning on line 57 in packages/thirdweb/src/wallets/eip5792/get-capabilities.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/eip5792/get-capabilities.ts#L57

Added line #L57 was not covered by tests
): GetCapabilitiesResult {
const capabilities = {} as WalletCapabilitiesRecord<
WalletCapabilities,
Expand All @@ -69,6 +69,8 @@
capabilities[Number(chainId)] = capabilitiesCopy;
}
return (
typeof chainId === "number" ? capabilities[chainId] : capabilities
typeof chainIdFilter === "number"
? { [chainIdFilter]: capabilities[chainIdFilter] }
: capabilities

Check warning on line 74 in packages/thirdweb/src/wallets/eip5792/get-capabilities.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/eip5792/get-capabilities.ts#L72-L74

Added lines #L72 - L74 were not covered by tests
) as never;
}
4 changes: 2 additions & 2 deletions packages/thirdweb/src/wallets/injected/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,13 @@
}
},
async getCapabilities(options) {
const chainId = options.chainId;
const chainIdFilter = options.chainId;

Check warning on line 377 in packages/thirdweb/src/wallets/injected/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/injected/index.ts#L377

Added line #L377 was not covered by tests
try {
const result = await provider.request({
method: "wallet_getCapabilities",
params: [getAddress(account.address)],
});
return toGetCapabilitiesResult(result, chainId);
return toGetCapabilitiesResult(result, chainIdFilter);

Check warning on line 383 in packages/thirdweb/src/wallets/injected/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/injected/index.ts#L383

Added line #L383 was not covered by tests
} catch (error: unknown) {
if (
/unsupport|not support|not available/i.test((error as Error).message)
Expand Down
37 changes: 23 additions & 14 deletions packages/wagmi-adapter/src/connector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { type CreateConnectorFn, createConnector } from "@wagmi/core";
import type { Prettify } from "@wagmi/core/chains";
import { defineChain, getAddress, type ThirdwebClient } from "thirdweb";
import {
type Chain,
defineChain,
getAddress,
type ThirdwebClient,
} from "thirdweb";
import {
EIP1193,
ecosystemWallet,
Expand Down Expand Up @@ -53,6 +58,7 @@ type StorageItem = {

const activeWalletIdKey = "thirdweb:active-wallet-id";
const connectedWalletIdsKey = "thirdweb:connected-wallet-ids";
const activeChainIdKey = "thirdweb:active-chain";

/**
* Connect to an in-app wallet using the auth strategy of your choice.
Expand Down Expand Up @@ -112,13 +118,14 @@ export function inAppWalletConnector(
? window.localStorage
: undefined;
return createConnector((config) => ({
connect: async (params?: ConnectionOptions<false>) => {
connect: async (params) => {
wallet =
params && "wallet" in params ? (params.wallet as Wallet) : wallet;
const lastChainIdStr = await config.storage?.getItem(
"thirdweb:lastChainId",
);
const lastChainId = lastChainIdStr ? Number(lastChainIdStr) : undefined;
const lastChainIdStr = rawStorage?.getItem(activeChainIdKey);
const lastChain = lastChainIdStr
? (JSON.parse(lastChainIdStr) as Chain)
: undefined;
const lastChainId = lastChain ? lastChain.id : undefined;
if (params?.isReconnecting) {
const { autoConnect } = await import("thirdweb/wallets");
const chainId = lastChainId || args.smartAccount?.chain?.id || 1;
Expand All @@ -133,7 +140,6 @@ export function inAppWalletConnector(
if (!account) {
throw new Error("Wallet failed to reconnect");
}

return {
accounts: [getAddress(account.address)] as any,
chainId: chainId,
Expand All @@ -144,7 +150,7 @@ export function inAppWalletConnector(
const alreadyConnectedAccount = wallet.getAccount();
if (alreadyConnectedAccount) {
return {
accounts: [getAddress(alreadyConnectedAccount.address)],
accounts: [getAddress(alreadyConnectedAccount.address)] as any,
chainId: wallet.getChain()?.id || 1,
};
}
Expand Down Expand Up @@ -191,9 +197,11 @@ export function inAppWalletConnector(
}
rawStorage.setItem(activeWalletIdKey, wallet.id);
}
await config.storage?.setItem("thirdweb:lastChainId", chain.id);
args.onConnect?.(wallet);
return { accounts: [getAddress(account.address)], chainId: chain.id };
return {
accounts: [getAddress(account.address)] as any,
chainId: chain.id,
};
},
disconnect: async () => {
await wallet.disconnect();
Expand All @@ -209,9 +217,7 @@ export function inAppWalletConnector(
return wallet.getChain()?.id || 1;
},
getProvider: async (params) => {
const lastChainIdStr = await config.storage?.getItem(
"thirdweb:lastChainId",
);
const lastChainIdStr = await rawStorage?.getItem(activeChainIdKey);
const lastChainId = lastChainIdStr ? Number(lastChainIdStr) : undefined;
const chain = defineChain(
params?.chainId || args.smartAccount?.chain?.id || lastChainId || 1,
Expand Down Expand Up @@ -262,7 +268,10 @@ export function inAppWalletConnector(
config.emitter.emit("change", {
chainId: chain.id,
});
await config.storage?.setItem("thirdweb:lastChainId", chain.id);
rawStorage?.setItem(
activeChainIdKey,
JSON.stringify(defineChain(chain.id)),
);
return chain;
},
type: "in-app",
Expand Down
Loading
Loading