Skip to content

Commit

Permalink
Fix "Cannot convert undefined or null to object"
Browse files Browse the repository at this point in the history
Sometime there is an error "Cannot convert undefined or null to object"
when trying to connect to walletconnect or ledger live. To reproduce it
(before the fix in this commit of course) you should:
1. Connect through Ledger Live
2. Disconnect
3. Connect through Ledger Live again (with the same account)
4. Disconnect
5. Connect through WalletConnect (it should automatically connect with
the same account that you used while connecting to Ledger Live)
6. Disconnect
7. Connect again through Ledger Live
8. The error should appear

The similar issue was descibed here: WalletConnect/walletconnect-monorepo#3165

This commit fixes that issue by clearing out the local storage from the
walleconnect realted data.
  • Loading branch information
michalsmiarowski committed Aug 14, 2023
1 parent 1f996cd commit 6cfe182
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/web3/connectors/ledgerLive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ export class LedgerLiveConnector extends AbstractConnector {
if (!this.supportedChainIds) {
throw new Error("Supported chain ids are not defined.")
}
// Removes all local storage entries that starts with "wc@2"
// This is a workaround for "Cannot convert undefined or null to object"
// error that sometime occur with WalletConnect
Object.keys(localStorage)
.filter((x) => x.startsWith("wc@2"))
.forEach((x) => localStorage.removeItem(x))
let account = ""
const connectKit = await this.connectKitPromise
const chainId = this.supportedChainIds[0]
Expand Down
6 changes: 6 additions & 0 deletions src/web3/connectors/walletConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export class WalletConnectConnector extends AbstractConnector {
}

public async activate(): Promise<ConnectorUpdate> {
// Removes all local storage entries that starts with "wc@2"
// This is a workaround for "Cannot convert undefined or null to object"
// error that sometime occur with WalletConnect
Object.keys(localStorage)
.filter((x) => x.startsWith("wc@2"))
.forEach((x) => localStorage.removeItem(x))
if (!this.provider) {
const chains = getSupportedChains(this.config)
if (chains.length === 0) throw new Error("Chains not specified!")
Expand Down

0 comments on commit 6cfe182

Please sign in to comment.