Call authenticate using walletconnect, and also have an enabled bridge connection [Solved]

Hi!

I need to call window.Moralis.Web3.authenticate({ provider: "walletconnect" }) for my users to sign in. I also need to call window.web3 = await window.Moralis.Web3.enable({ provider: "walletconnect" }) to allow subsequent contract interactions. The authenticate call opens a connection to the wallet but isnā€™t an instance that can be used for contract interactions. If I place the ā€˜enableā€™ after the authenticate it has no effect, and if I place it before the authenticate call the bridge session created by the earlier enable call is closed by the authenticate call.

Is there a way to open a walletconnect connection that allows both a Moralis authenticate and subsequent contract interactions in the same session?

Thank you!

Aha! I can see why the authenticate call closes an earlier enable call, we have this in the authenticate method in moralis.js:

              case 6:
                _context4.next = 8;
                return MoralisWeb3.cleanup();

So that closes the earlier connection, and a bit further down we call this:

              case 12:
                _context4.next = 14;
                return MoralisWeb3.enable(options);

Which gives us a brand new connection. So I wonder if I can manually call everything that comes next after case 12 after my own enable. . .

1 Like

Cracked it! The authenticate method calls enable, and you can get that original object back with the following:

const user = await window.Moralis.Web3.authenticate({ provider: "walletconnect" });
window.web3 = await window.Moralis.Web3.activeWeb3Provider.activate();

This means you make the connection to the wallet in authenticate and keep that connection for all subsequent contract interactions. :smiley:

2 Likes

Keep up the great work @keyoke! Love the enthusiasm and zeal! :star_struck:

I have a few issues with Walletconnect at the moment (using Metamask mobile), wonder how you solved them or if you have the same issues?

  1. connection is lost every time mobile locks itself. Canā€™t find any documentation on how to get the connection back or how to even register the event. Events like Moralis.Web3.on(ā€œdisconnectā€) dont fire. When I logged out of Metamask mobile and logged in again suddenly there was a queue of transactions to be signed.

  2. there seems to be no callback on contract methods. when I do this thereā€™s no ā€˜receiptā€™:

      await tokenContract.value.methods.approve(marketContractHash, token_id).send({from: sender})
      .on('receipt', () => {
        console.log(receipt); /// does not work
      })

Ah, sorry, missed this! Yeah, I find the same thing, havenā€™t been able to solve it yet.