Is it possible to connect my wallet without having to sign the transaction?

Hi guys, when I want to connect metamask to my website using moralis, I need to firm a tx to connect it. I saw on other website like Pancakeswap, that it automatically connects your wallet when you click on the connect button. Is there any settings on moralis to avoid this behaviour?

You sign that transaction in order to login to your Moralis server. If you don’t want to use that then you can use enableWeb3 and not authenticate

Thank you! But if I need to connect my wallet in order to execute a tx on a smart contract, do I need to initialize moralis? Also, can I retrieve the user wallet address without using the Moralis.Authenticate() method and use the current wallet on metamask to sign the tx?

yes, you can do that without authentication, you can do it with Moralis.enableWeb3() for example, or directly with web3 if you want

1 Like

I tried this:

await Moralis.enableWeb3();
var user = await Moralis.User.current();
await ethAddress = await user.get("ethAddress");

But it doesn’t retrieve the connected wallet if I don’t call the Authenticate() method. (the wallet is connected to the page with metamask).
I get:
TypeError: Cannot read properties of null (reading 'get')
Did I miss something?

I think I solved my problem with metamask doing this:

const web3Instance = await Moralis.enableWeb3();
const userAddress = web3Instance.givenProvider.selectedAddress;

But If I try with wallet connect doing:

const web3Instance = await Moralis.enableWeb3({ provider: 'walletconnect' });
const userAddress = web3Instance.givenProvider.selectedAddress;

The wallet is not connect after I confirm to connect it on Trust Wallet. Do I have to use authenticate if I want to connect with WalletConnect?

yeah because that address, is gotten from the DB, but if you’re using enableWeb3 instead of authenticate. First, those addresses will not be stored on DB, second your user will have no access to DB, so you need to detect the address manually with etherjs (version >= 1.0.0) or web3js (for all versions)

hmm yeah, but even with web3js if I do Web3.Eth.givenProvider, the selectedAddress is null after I sign the tx on trust wallet.

It recognize the chain Id and the network version but not my address (only with wallet connect). Also I don’t know why it says “isMetamask: true”

hmmm I don’t think you would use Web3 with capital W there, and to get address use (await web3.eth.getAccounts())[0] instead

Thank you, I solved the WalletConnect issue like this:

const web3Instance = await Moralis.enableWeb3({ provider: 'walletconnect', chainId: 56 });
const userAddress = web3Instance._provider.accounts[0];
localStorage.userAddress = userAddress;

Now my only problem is that if I refresh the page (when I’m connected throgh walletConnect with the method above), I lose the connection. How do I keep the wallet connected (trust wallet) without using the Autenticate() method?

Edit: I use Moralis.ExecuteFunction() and this shows metamask instead of popping up the tx on Trust Wallet If I reload the page

Solved, I should have called await Moralis.enableWeb3({ provider: 'walletconnect', chainId: 56 }); rather than await Moralis.enableWeb3();

1 Like