Skip to content

Commit

Permalink
fix: wagmi siwe reconnect on refresh (#3289)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoruka authored Nov 20, 2024
1 parent c4d4d2c commit 8236837
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
24 changes: 24 additions & 0 deletions .changeset/rude-wasps-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@apps/demo': patch
'@apps/gallery': patch
'@apps/laboratory': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-polkadot': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': 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
---

Fix wagmi not reconnecting with siwe on page refresh
10 changes: 8 additions & 2 deletions apps/laboratory/tests/siwe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,19 @@ siweWalletTest('it should disconnect when cancel siwe from AppKit', async () =>
await walletValidator.expectSessionCard({ visible: false })
})

// WARNING: refreshing on wagmi-siwe doesn't auto connect
siweWalletTest.skip('it should be authenticated when refresh page', async () => {
siweWalletTest('it should be authenticated when refresh page', async () => {
await modalPage.qrCodeFlow(modalPage, walletPage)
await modalValidator.expectConnected()
await modalPage.page.reload()
await modalValidator.expectConnected()
await modalValidator.expectAuthenticated()
await modalPage.sign()
await walletPage.handleRequest({ accept: true })
await modalValidator.expectAcceptedSign()
await modalPage.disconnect()
await modalValidator.expectDisconnected()
await modalValidator.expectUnauthenticated()
await walletValidator.expectSessionCard({ visible: false })
})

siweWalletTest('it should be unauthenticated when disconnect', async () => {
Expand Down
23 changes: 2 additions & 21 deletions packages/adapters/wagmi/src/connectors/UniversalConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export function walletConnect(
}

let provider_: Provider | undefined
let providerPromise: Promise<typeof provider_>

let accountsChanged: UniversalConnector['onAccountsChanged'] | undefined
let chainChanged: UniversalConnector['onChainChanged'] | undefined
Expand Down Expand Up @@ -209,27 +208,8 @@ export function walletConnect(
return accounts as `0x${string}`[]
},
async getProvider({ chainId } = {}) {
async function initProvider() {
const optionalChains = caipNetworks.map(x => Number(x.id))

if (!optionalChains.length) {
return undefined
}

const provider = await appKit.getUniversalProvider()

if (!provider) {
throw new Error('Provider not found')
}

return provider
}

if (!provider_) {
if (!providerPromise) {
providerPromise = initProvider()
}
provider_ = await providerPromise
provider_ = await appKit.getUniversalProvider()
provider_?.events.setMaxListeners(Number.POSITIVE_INFINITY)
}

Expand Down Expand Up @@ -368,6 +348,7 @@ export function walletConnect(
async onConnect(connectInfo) {
const chainId = Number(connectInfo.chainId)
const accounts = await this.getAccounts()
this.setRequestedChainsIds(caipNetworks.map(x => Number(x.id)))
config.emitter.emit('connect', { accounts, chainId })
},
async onDisconnect(_error) {
Expand Down

0 comments on commit 8236837

Please sign in to comment.