Moralis speedy nodes issue wallet connect

Hello, I’m stuck on an error, trying to search on forum, documentation, I still didn’t find something useful to solve it.

I’m using a custom connector for wallet connect, taken from here:

this is my code where I’m using it

export const LoginComponent = ({ closeModal }) => {
  const { authenticate, isAuthenticated } = useMoralis();
  const [authError, setAuthError] = useState(null);
  const [isAuthenticating, setIsAuthenticating] = useState(false);

  async function walletConnectLogin() {
    try {
      setAuthError(null);
      setIsAuthenticating(true);
      await Moralis.enableWeb3({});
      await Moralis.start({
        apiKey: process.env.NEXT_PUBLIC_MORALIS_API_KEY,
        serverUrl: process.env.NEXT_PUBLIC_MORALIS_SERVER_URL,
        appId: process.env.NEXT_PUBLIC_MORALIS_APP_ID
      });

      const { account, chainId } = Moralis;

      if (!account) {
        throw new Error(
          "Connecting to chain failed, as no connected account was found"
        );
      }
      if (!chainId) {
        throw new Error(
          "Connecting to chain failed, as no connected chain was found"
        );
      }

      const { message } = await Moralis.Cloud.run("requestMessage", {
        address: account,
        chain: parseInt(chainId, 16),
        network: "evm",
      });
      await authenticate({ signingMessage: message, connector: WalletConnectWeb3Connector });
    } catch (error) {
      setAuthError(error);
    } finally {
      setIsAuthenticating(false);
    }
  }

  useEffect(() => {
    if (isAuthenticated) {
      closeModal()
    }
  }, [closeModal, isAuthenticated])

  return (
    <div className='w-full text-center py-12 grid px-8'>
      
      <button onClick={() => walletConnectLogin()}>
        Walletconnect</button>
      <div className='pt-6 text-center text-xs animate-pulse font-bold'>{isAuthenticating ? "Authenticating..." : ""}</div>

    </div>
  )
}

I’m using as RPCs in the specific file the following:

137: `https://matic.getblock.io/<API ID>/mainnet/`,
80001: `https://matic.getblock.io/<API ID>/testnet/`,

with my api ID

Now the behavior is the following:

this is working perfectly on a browser (e.g. Chrome) where I have the MetaMask extension enabled. Is considering the right RPCs, when I use wallet connect a popup on my MetaMask app on the phone ask me to sign, if I sign from the popup, I’m correctly logged in from my desktop Chrome browser.

As soon as I use it on mobile, or on a browser where I don’t have MetaMask, e.g. Safari, immediately I receive these three errors in loop:

[Error] XMLHttpRequest cannot load https://speedy-nodes-nyc.moralis.io/WalletConnect/polygon/mumbai due to access control checks.

Failed to load resource: Preflight response is not successful. Status code: 429
index.js:51

Error: PollingBlockTracker - encountered an error while attempting to update latest block:
undefined

My question is, why in the first case is going on the right RPCs, and in the second case is trying to reach https://speedy-nodes-nyc.moralis.io/WalletConnect/polygon/mumbai ?

Any suggestion?

Thanks

Hi @gerar

Could you try adapting to the latest v2 sdk for using wallet connect authentication?

Since wallet connect is going to deprecate its v1 sdk soon, the authentication function in Moralis v1 sdk will not work with wallet connect.

So migrating to Moralis v2 sdk will be a better option.

Hi, would be a major update for the app, together with the migration that we are now forced to do (since a huge change in the paid plan with 99$ more / month for each instance if we want to keep the current servers) would be again more time on adapting to Moralis changes than working on our project.

What would be the impacts on other elements different by authentication?

For example, if we want to connect to blockchain functions and contracts, will we be able to do so again with the Moralis V2?

I’m referring to code elements like the following:

const { Moralis, web3, enableWeb3, isWeb3Enabled, isAuthenticated, account, user, isWeb3EnableLoading } = useMoralis();

const contract = new ethers.Contract(CONTRACT_ADDRESS, contractAbi, web3);

useEffect(() => {
    async function getWeb3() {
      if (!isWeb3Enabled && !isWeb3EnableLoading) {
        if (!window.ethereum) {
          await enableWeb3({
            provider: "walletconnect",
            chainId: process.env.NEXT_PUBLIC_CHAIN_ID_NORMAL
          });
        }
        else { await enableWeb3(); }
      }
      else { return null }

    };

    getWeb3();
  }, []);
 isListed: async () => {
      try {
        if (isAuthenticated && isWeb3Enabled) {

          let isListed = false;

          isListed = await contract.isPriorityListed(account);

          return isPrioritylisted;
        }
      } catch (error) {
        console.error(error);
      }

    },
  const playersOptions = {
    contractAddress: CONTRACT_ADDRESS,
    abi: contractAbi,
  };
 let transaction = await Moralis.executeFunction({
            functionName: "mintBatch",
            msgValue: Moralis.Units.ETH(mintingPrice),
            ...playersOptions,
          });

Thanks

We have a few tutorials on how you can call smart contract functions when using v2 sdk. So it is possible to adapt your smart contract write functions when using v2 sdk.
Please have a look at this

But once the walletconnect depreciates its v1 SDK, Moralis v1 SDK might not work with walletconnect.
If you still have Moralis server you will still be able to use other features from v1 sdk but not wallet connect authentication.

Ok this is clear thanks.

But now that is not deprecated yet, any idea why I’m getting those errors trying to call the Moralis speedy nodes instead of the RPC I’ve passed with the custom connector in the code above?

Since it is happening only with wallet connect, i think it internally uses speedy node to keep the walletconnect connection active between client and wallet.

Do you see a way I can reproduce this error?

I think it’s possible to reproduce it using the custom connector for walletconnect that I used here

then calling the function as the code posted above, and trying to connect via a browser where you don’t have any wallet extension

Thank you

Can you once try the solutions from here. :raised_hands:

I will try thanks.

Is there a way in a transitory timeframe to use both V1 and V2 together for different purposes, or they are mutually exclusive?

In the backend both v1 and v2 sdk use the same api endpoints. sdk’s are just wrappers. So if you have any trouble with sdk you can fix it temporarily by API calls.