From 93200da8dcd66b05a20360f06475305e8a14b0f3 Mon Sep 17 00:00:00 2001 From: Sven Date: Wed, 11 Dec 2024 17:28:33 +0100 Subject: [PATCH 1/4] fix reconnect issue --- apps/laboratory/src/pages/library/wagmi.tsx | 6 +--- packages/appkit/src/client.ts | 34 +++++++++++++++------ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/apps/laboratory/src/pages/library/wagmi.tsx b/apps/laboratory/src/pages/library/wagmi.tsx index 035dfd6df2..00c9f9a931 100644 --- a/apps/laboratory/src/pages/library/wagmi.tsx +++ b/apps/laboratory/src/pages/library/wagmi.tsx @@ -7,17 +7,13 @@ import { WagmiAdapter } from '@reown/appkit-adapter-wagmi' import { createAppKit } from '@reown/appkit/react' import { ConstantsUtil } from '../../utils/ConstantsUtil' import { ThemeStore } from '../../utils/StoreUtil' -import { mainnet } from 'viem/chains' const queryClient = new QueryClient() const wagmiAdapter = new WagmiAdapter({ ssr: true, networks: ConstantsUtil.EvmNetworks, - projectId: ConstantsUtil.ProjectId, - transports: { - [mainnet.id]: http('https://foo-bar-baz.quiknode.pro') - } + projectId: ConstantsUtil.ProjectId }) const modal = createAppKit({ diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index e50daeee28..86585012ae 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -836,16 +836,30 @@ export class AppKit { throw new Error('Adapter not found') } - const res = await adapter.connect({ - id, - info, - type, - provider, - chainId: caipNetwork?.id || this.getCaipNetwork()?.id, - rpcUrl: - caipNetwork?.rpcUrls?.default?.http?.[0] || - this.getCaipNetwork()?.rpcUrls?.default?.http?.[0] - }) + let res + try { + res = await adapter.connect({ + id, + info, + type, + provider, + chainId: caipNetwork?.id || this.getCaipNetwork()?.id, + rpcUrl: + caipNetwork?.rpcUrls?.default?.http?.[0] || + this.getCaipNetwork()?.rpcUrls?.default?.http?.[0] + }) + } catch (error) { + if (!adapter?.reconnect) { + throw new Error('Adapter is not able to connect') + } + res = await adapter.reconnect({ + id, + info, + type, + provider, + chainId: this.getCaipNetwork()?.id + }) + } if (res) { this.syncProvider({ From ced597d46c53c9e113e74913356c07e2ffff29f2 Mon Sep 17 00:00:00 2001 From: Sven Date: Wed, 11 Dec 2024 17:29:11 +0100 Subject: [PATCH 2/4] fix wagmi config --- apps/laboratory/src/pages/library/wagmi.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/laboratory/src/pages/library/wagmi.tsx b/apps/laboratory/src/pages/library/wagmi.tsx index 00c9f9a931..1972e7f61d 100644 --- a/apps/laboratory/src/pages/library/wagmi.tsx +++ b/apps/laboratory/src/pages/library/wagmi.tsx @@ -1,5 +1,5 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query' -import { fallback, http, WagmiProvider } from 'wagmi' +import { WagmiProvider } from 'wagmi' import { AppKitButtons } from '../../components/AppKitButtons' import { WagmiTests } from '../../components/Wagmi/WagmiTests' import { WagmiModalInfo } from '../../components/Wagmi/WagmiModalInfo' From 1f5d46cbb8034576ae9bc3f70dd8b81e8842b1e8 Mon Sep 17 00:00:00 2001 From: Sven Date: Wed, 11 Dec 2024 17:32:33 +0100 Subject: [PATCH 3/4] add comment --- packages/appkit/src/client.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index 86585012ae..558f5f6e79 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -836,7 +836,7 @@ export class AppKit { throw new Error('Adapter not found') } - let res + let res: AdapterBlueprint.ConnectResult | undefined = undefined try { res = await adapter.connect({ id, @@ -848,11 +848,18 @@ export class AppKit { caipNetwork?.rpcUrls?.default?.http?.[0] || this.getCaipNetwork()?.rpcUrls?.default?.http?.[0] }) + /** + * In some cases with wagmi connectors, the connector is already connected + * which throws an `Is already connected`error. In such cases, we need to reconnect + * to restore the session. + * We check if the reconnect method exists (which it does for wagmi connectors) and if so + * we attempt to reconnect and restore the session state. + */ } catch (error) { if (!adapter?.reconnect) { throw new Error('Adapter is not able to connect') } - res = await adapter.reconnect({ + await adapter.reconnect({ id, info, type, From 97c644fd6f35fcf532e39d4881ec30e3695d407b Mon Sep 17 00:00:00 2001 From: Sven Date: Wed, 11 Dec 2024 17:41:23 +0100 Subject: [PATCH 4/4] add changeset --- .changeset/swift-ladybugs-smile.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .changeset/swift-ladybugs-smile.md diff --git a/.changeset/swift-ladybugs-smile.md b/.changeset/swift-ladybugs-smile.md new file mode 100644 index 0000000000..82c9331c61 --- /dev/null +++ b/.changeset/swift-ladybugs-smile.md @@ -0,0 +1,23 @@ +--- +'@reown/appkit': patch +'@apps/builder': patch +'@reown/appkit-adapter-ethers': patch +'@reown/appkit-adapter-ethers5': patch +'@reown/appkit-adapter-solana': patch +'@reown/appkit-adapter-wagmi': patch +'@reown/appkit-utils': patch +'@reown/appkit-cdn': patch +'@reown/appkit-cli': patch +'@reown/appkit-common': patch +'@reown/appkit-core': patch +'@reown/appkit-experimental': patch +'@reown/appkit-polyfills': patch +'@reown/appkit-scaffold-ui': patch +'@reown/appkit-siwe': patch +'@reown/appkit-siwx': patch +'@reown/appkit-ui': patch +'@reown/appkit-wallet': patch +'@reown/appkit-wallet-button': patch +--- + +Fix an issue where wagmi connectors are unable to restore a session