[SOLVED] Moralis.enableWeb3() already has been called, but is not finished yet

Hello. I try to make a read only contract call to check its balance but get this error:

Moralis Moralis.enableWeb3() already has been called, but is not finished yet

Here is my code:

const { Moralis } = useMoralis();

  await Moralis.enableWeb3();

  const options = {

    contractAddress: "0x9652dB185714460cC6918117FD1E35593cD40450",

    functionName: "getBalance",

    abi: getBalanceAbi,

  };

  const balance = await Moralis.executeFunction(options);

And here is my function ABI:

export const getBalanceAbi = [

  {

    inputs: [],

    name: "getBalance",

    outputs: [

      {

        internalType: "uint256",

        name: "",

        type: "uint256",

      },

    ],

    stateMutability: "view",

    type: "function",

  },

];

What am I doing wrong and how can I call this function and save its result?

The chain is matic testnet. I’m using Moralis 1.5.9 SDK and react-moralis 1.3.5.

Thank you.

You don’t need to call this everytime, you can call it once the app is initialized and that is enough

It was giving the “you need to enable web3 error”, that’s why I added it. I’ve solved my problem with the code below. Not sure why it wasn’t working initially when I tried it, but now it works. Thank you.

const { native } = useMoralisWeb3Api();

  const options = {

    chain: "mumbai",

    address: "0x9652dB185714460cC6918117FD1E35593cD40450",

    function_name: "getBalance",

    abi: getBalanceAbi,

  };

  const { fetch, data, error, isLoading } = useMoralisWeb3ApiCall(

    native.runContractFunction,

    { ...options }

  );

You can call enableWeb3 in the appjs when the component is mounted. Having such

const { isWeb3Enabled, isAuthenticated } = useMoralis();
useEffect(() => {
  if (!isWeb3Enabled && isAuthenticated) {
    enableWeb3();
  }
}, [isWeb3Enabled, isAuthenticated]);
1 Like

Okay, will keep in mind. Thank you.