walletconnect-monorepo icon indicating copy to clipboard operation
walletconnect-monorepo copied to clipboard

QR modal doesn't open after being closed

Open MrPeanutz opened this issue 4 years ago • 7 comments
trafficstars

How to reproduce:

Lib: @walletconnect/web3-provider Version: 1.4.1

  1. call await wcProvider.enable()
  2. modal opens
  3. click the x button on top right corner
  4. modal closes
  5. call await wcProvider.enable() again
  6. nothing happens

MrPeanutz avatar Jun 30 '21 16:06 MrPeanutz

I have the same problem...

JOU-amjs avatar Jul 14 '21 06:07 JOU-amjs

+1

lanchana avatar Aug 03 '21 20:08 lanchana

+1

JohnathanWhite avatar Aug 03 '21 21:08 JohnathanWhite

Same as #243

Noisyfox avatar Oct 12 '21 02:10 Noisyfox

+1

pauloreis7 avatar Feb 24 '22 19:02 pauloreis7

I'm working around this in the @walletconnect/client and @walletconnect/qrcode-modal libraries. This is a pretty horrible hack, but works for now.

It promisifies the connect call, and after the first call it invokes the open method on the underlying modal forcing it to be opened every time. To track closing it uses a MutationObserver and resolves with the users address.

import WalletConnect from '@walletconnect/client'
import QRCodeModal from '@walletconnect/qrcode-modal'

export default class WalletConnectProvider {
  constructor() {
    this.name = 'WalletConnect'
    this.provider = new WalletConnect({
      bridge: 'https://bridge.walletconnect.org',
      qrcodeModal: QRCodeModal,
    })

    this._forceOpen = false
  }

  getAddress() {
    const [account] = this.provider.accounts
    return account
  }

  async connect() {
    return new Promise(async (r, j) => {
      let setForceOpen = false
      if (!this.provider.connected) {
        await this.provider.createSession()
        setForceOpen = true
      }

      // https://github.com/NoahZinsmeister/web3-react/issues/376
      if (this._forceOpen) {
        // Modal has already been opened once, force it to open
        this.provider._qrcodeModal.open(this.provider.uri, this.provider._qrcodeModalOptions)
      } else if (setForceOpen) {
        // Modal opened for the first time, force on future attempts
        this._forceOpen = true
      }

      const ob = new MutationObserver(([event]) => {
        // Check if the wallet connect wrapper was removed
        const removed = [...event.removedNodes].find(el => el.id === 'walletconnect-wrapper')
        if (!removed) return
        ob.disconnect() // kill observer
        return this.provider.connected ? r(this.getAddress()) : j()
      })
      ob.observe(document.querySelector('body'), { childList: true })
    })
  }

  async sign(message) {
    const encodedMessage = new TextEncoder().encode(message)
    return this.provider.signPersonalMessage([encodedMessage, this.getAddress()])
  }
}

0x62 avatar Mar 16 '22 01:03 0x62

I came up with a workaround using @walletconnect/web3-provider, hope it helps for now https://github.com/WalletConnect/walletconnect-monorepo/issues/747#issuecomment-1137952645

avivash avatar May 25 '22 23:05 avivash

@MrPeanutz @JOU-amjs @lanchana @JohnathanWhite @Noisyfox is this still an issue?

finessevanes avatar Dec 13 '22 23:12 finessevanes

const isRequestActive: Ref<UnwrapRef> = ref(false); if (isRequestActive.value) { console.warn("NEW A request is already active. Please wait."); return; }

console.log('if AFTER', );

try {
    // Example: Requesting account access
    const accounts = await (window.ethereum as any)?.request({ method: 'eth_requestAccounts' }).then((account) => console.log('account', account)).catch((error: unknown) => console.error('error', error)).finally(() => console.log('finally', ));
    // Proceed with additional actions like showing balance, switching chains, etc.
    console.log('NEW accounts', accounts);
    isRequestActive.value = true;
} catch (error) {
    console.error("NEW Error interacting with MetaMask:", error);
    // Handle error appropriately
    isRequestActive.value = false;
} finally {
    console.log('NEW finally isRequestActive.value', isRequestActive.value);
    isRequestActive.value = false;
}
console.log('metaMaskCall end', );

For catching error here the magic is done in this line of code above: 

const accounts = await (window.ethereum as any)?.request({ method: 'eth_requestAccounts' }).then((account) => console.log('account', account)).catch((error: unknown) => console.error('error', error)).finally(() => console.log('finally', ));

We caught this error in .catch method of request.

this code is for Vue 3 with typescript

TetianaMolchanova avatar Feb 28 '24 18:02 TetianaMolchanova