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 diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index e50daeee28..558f5f6e79 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -836,16 +836,37 @@ 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: AdapterBlueprint.ConnectResult | undefined = undefined + 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] + }) + /** + * 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') + } + await adapter.reconnect({ + id, + info, + type, + provider, + chainId: this.getCaipNetwork()?.id + }) + } if (res) { this.syncProvider({