Skip to content

Commit

Permalink
fix: multiple account syncs on wagmi (reown-com#2575)
Browse files Browse the repository at this point in the history
Co-authored-by: enesozturk <enesozturk.d@gmail.com>
  • Loading branch information
zoruka and enesozturk authored Jul 23, 2024
1 parent fea5064 commit 13f051f
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 51 deletions.
2 changes: 1 addition & 1 deletion apps/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"sonner": "1.4.3",
"tailwind-merge": "2.2.1",
"vaul": "0.9.0",
"viem": "2.16.2",
"viem": "2.17.8",
"wagmi": "2.10.9",
"zustand": "4.5.2"
},
Expand Down
2 changes: 1 addition & 1 deletion apps/laboratory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"react-icons": "4.12.0",
"rpc-websockets": "7.11.0",
"valtio": "1.11.2",
"viem": "2.16.2",
"viem": "2.17.8",
"wagmi": "2.10.9"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/next-wagmi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"next": "14.2.3",
"react": "18.2.0",
"react-dom": "18.2.0",
"viem": "2.16.2",
"viem": "2.17.8",
"wagmi": "2.10.9"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/react-wagmi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"react": "18.2.0",
"react-dom": "18.2.0",
"vite": "5.2.11",
"viem": "2.16.2",
"viem": "2.17.8",
"wagmi": "2.10.9"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cdn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@wagmi/core": "2.11.6",
"@web3modal/wagmi": "workspace:*",
"@web3modal/ethers": "workspace:*",
"viem": "2.16.2",
"viem": "2.17.8",
"vite": "5.2.11"
}
}
2 changes: 1 addition & 1 deletion packages/wagmi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"@wagmi/core": "2.11.6",
"react": "18.2.0",
"react-dom": "18.2.0",
"viem": "2.16.2",
"viem": "2.17.8",
"vue": "3.4.3",
"wagmi": "2.10.9"
},
Expand Down
16 changes: 11 additions & 5 deletions packages/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,22 +447,25 @@ export class Web3Modal extends Web3ModalScaffold {
private async syncAccount({
address,
isConnected,
isDisconnected,
chainId,
connector,
addresses
}: Partial<
Pick<GetAccountReturnType, 'address' | 'isConnected' | 'chainId' | 'connector' | 'addresses'>
Pick<
GetAccountReturnType,
'address' | 'isConnected' | 'isDisconnected' | 'chainId' | 'connector' | 'addresses'
>
>) {
const caipAddress: CaipAddress = `${ConstantsUtil.EIP155}:${chainId}:${address}`

if (this.getCaipAddress() === caipAddress) {
return
}

this.resetAccount()
this.syncNetwork(address, chainId, isConnected)

if (isConnected && address && chainId) {
this.resetAccount()
this.syncNetwork(address, chainId, isConnected)
this.setIsConnected(isConnected)
this.setCaipAddress(caipAddress)
await Promise.all([
Expand All @@ -481,10 +484,13 @@ export class Web3Modal extends Web3ModalScaffold {
}

this.hasSyncedConnectedAccount = true
} else if (!isConnected && this.hasSyncedConnectedAccount) {
} else if (isDisconnected && this.hasSyncedConnectedAccount) {
this.resetAccount()
this.resetWcConnection()
this.resetNetwork()
this.setAllAccounts([])

this.hasSyncedConnectedAccount = false
}
}

Expand Down
12 changes: 11 additions & 1 deletion packages/wagmi/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ export function getTransport({ chain, projectId }: { chain: Chain; projectId: st
}

return fallback([
http(`${RPC_URL}/v1/?chainId=${ConstantsUtil.EIP155}:${chain.id}&projectId=${projectId}`),
http(`${RPC_URL}/v1/?chainId=${ConstantsUtil.EIP155}:${chain.id}&projectId=${projectId}`, {
/*
* The Blockchain API uses "Content-Type: text/plain" to avoid OPTIONS preflight requests
* It will only work for viem >= 2.17.7
*/
fetchOptions: {
headers: {
'Content-Type': 'text/plain'
}
}
}),
http(chainDefaultUrl)
])
}
126 changes: 87 additions & 39 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

0 comments on commit 13f051f

Please sign in to comment.