React runContractFunction error codes

Hi Guys,

I’m running this:
Of course all dependencies and hooks are properly imported in my component.
The ABI is in array form.

 const getCallPrice = async() => {
        const options = {
            chain:chainId,
            abi:coordinatorAbi,
            address:coordinatorAddress,
            functionName: "getCallPrice",
            }
        const result = await Web3Api.native.runContractFunction(options);
        return(result)
    } 

I always get the following in the console.

image

Can you help me to identify what code 141 entails.

Best Regards

Daniel

Hi,
I would need all the parameters that you used to figure it out,
you can also test on https://admin.moralis.io/web3Api in ​/{address}​/function endpoint where you should have better error messages.

In front end it may be easier to use Moralis.executeFunction

at a first look it looks like this code didn’t use function_name

Hi Thanks,

I changed to use the execute function hook, I’m sending you all the parameters as data.

With this hook, I always get null as the returned data value. Will appreciate your help,

const { data, error, isFetching, isLoading} = useWeb3ExecuteFunction({
        chain:chainId,
        abi:[
            {
                "anonymous": false,
                "inputs": [
                    {
                        "indexed": true,
                        "internalType": "bytes32",
                        "name": "categoryId",
                        "type": "bytes32"
                    },
                    {
                        "indexed": false,
                        "internalType": "string",
                        "name": "category",
                        "type": "string"
                    }
                ],
                "name": "CategoryCreated",
                "type": "event"
            },
            {
                "anonymous": false,
                "inputs": [
                    {
                        "indexed": true,
                        "internalType": "bytes32",
                        "name": "contentId",
                        "type": "bytes32"
                    },
                    {
                        "indexed": false,
                        "internalType": "string",
                        "name": "contentUri",
                        "type": "string"
                    }
                ],
                "name": "ContentAdded",
                "type": "event"
            },
            {
                "anonymous": false,
                "inputs": [
                    {
                        "indexed": true,
                        "internalType": "bytes32",
                        "name": "postId",
                        "type": "bytes32"
                    },
                    {
                        "indexed": true,
                        "internalType": "address",
                        "name": "postOwner",
                        "type": "address"
                    },
                    {
                        "indexed": true,
                        "internalType": "bytes32",
                        "name": "parentId",
                        "type": "bytes32"
                    },
                    {
                        "indexed": false,
                        "internalType": "bytes32",
                        "name": "contentId",
                        "type": "bytes32"
                    },
                    {
                        "indexed": false,
                        "internalType": "bytes32",
                        "name": "categoryId",
                        "type": "bytes32"
                    }
                ],
                "name": "PostCreated",
                "type": "event"
            },
            {
                "anonymous": false,
                "inputs": [
                    {
                        "indexed": true,
                        "internalType": "bytes32",
                        "name": "postId",
                        "type": "bytes32"
                    },
                    {
                        "indexed": true,
                        "internalType": "address",
                        "name": "postOwner",
                        "type": "address"
                    },
                    {
                        "indexed": true,
                        "internalType": "address",
                        "name": "voter",
                        "type": "address"
                    },
                    {
                        "indexed": false,
                        "internalType": "uint80",
                        "name": "reputationPostOwner",
                        "type": "uint80"
                    },
                    {
                        "indexed": false,
                        "internalType": "uint80",
                        "name": "reputationVoter",
                        "type": "uint80"
                    },
                    {
                        "indexed": false,
                        "internalType": "int40",
                        "name": "postVotes",
                        "type": "int40"
                    },
                    {
                        "indexed": false,
                        "internalType": "bool",
                        "name": "up",
                        "type": "bool"
                    },
                    {
                        "indexed": false,
                        "internalType": "uint8",
                        "name": "reputationAmount",
                        "type": "uint8"
                    }
                ],
                "name": "Voted",
                "type": "event"
            },
            {
                "inputs": [
                    {
                        "internalType": "string",
                        "name": "_category",
                        "type": "string"
                    }
                ],
                "name": "addCategory",
                "outputs": [],
                "stateMutability": "nonpayable",
                "type": "function"
            },
            {
                "inputs": [
                    {
                        "internalType": "bytes32",
                        "name": "_parentId",
                        "type": "bytes32"
                    },
                    {
                        "internalType": "string",
                        "name": "_contentUri",
                        "type": "string"
                    },
                    {
                        "internalType": "bytes32",
                        "name": "_categoryId",
                        "type": "bytes32"
                    }
                ],
                "name": "createPost",
                "outputs": [],
                "stateMutability": "nonpayable",
                "type": "function"
            },
            {
                "inputs": [
                    {
                        "internalType": "bytes32",
                        "name": "_categoryId",
                        "type": "bytes32"
                    }
                ],
                "name": "getCategory",
                "outputs": [
                    {
                        "internalType": "string",
                        "name": "",
                        "type": "string"
                    }
                ],
                "stateMutability": "view",
                "type": "function"
            },
            {
                "inputs": [
                    {
                        "internalType": "bytes32",
                        "name": "_contentId",
                        "type": "bytes32"
                    }
                ],
                "name": "getContent",
                "outputs": [
                    {
                        "internalType": "string",
                        "name": "",
                        "type": "string"
                    }
                ],
                "stateMutability": "view",
                "type": "function"
            },
            {
                "inputs": [
                    {
                        "internalType": "bytes32",
                        "name": "_postId",
                        "type": "bytes32"
                    }
                ],
                "name": "getPost",
                "outputs": [
                    {
                        "internalType": "address",
                        "name": "",
                        "type": "address"
                    },
                    {
                        "internalType": "bytes32",
                        "name": "",
                        "type": "bytes32"
                    },
                    {
                        "internalType": "bytes32",
                        "name": "",
                        "type": "bytes32"
                    },
                    {
                        "internalType": "int72",
                        "name": "",
                        "type": "int72"
                    },
                    {
                        "internalType": "bytes32",
                        "name": "",
                        "type": "bytes32"
                    }
                ],
                "stateMutability": "view",
                "type": "function"
            },
            {
                "inputs": [
                    {
                        "internalType": "address",
                        "name": "_address",
                        "type": "address"
                    },
                    {
                        "internalType": "bytes32",
                        "name": "_categoryID",
                        "type": "bytes32"
                    }
                ],
                "name": "getReputation",
                "outputs": [
                    {
                        "internalType": "uint80",
                        "name": "",
                        "type": "uint80"
                    }
                ],
                "stateMutability": "view",
                "type": "function"
            },
            {
                "inputs": [
                    {
                        "internalType": "bytes32",
                        "name": "_postId",
                        "type": "bytes32"
                    },
                    {
                        "internalType": "uint8",
                        "name": "_reputationTaken",
                        "type": "uint8"
                    }
                ],
                "name": "voteDown",
                "outputs": [],
                "stateMutability": "nonpayable",
                "type": "function"
            },
            {
                "inputs": [
                    {
                        "internalType": "bytes32",
                        "name": "_postId",
                        "type": "bytes32"
                    },
                    {
                        "internalType": "uint8",
                        "name": "_reputationAdded",
                        "type": "uint8"
                    }
                ],
                "name": "voteUp",
                "outputs": [],
                "stateMutability": "nonpayable",
                "type": "function"
            }
        ],
        address:'0x6c6Ae1d2eADa359c8fC8308707DeBa552f35F5C2',
        functionName: "getReputation",
        params: {
        _address:"0xbA6AADA3F43521296f92cdD6730108E13b927c1c",
        _categoryID: "0x91f9b2a50ec6433a1cb3014d0ac426a62822c084a87e5aade80c7a9f5ce3f4e5"
        }
    });

Expected value as seen on this screen should be “0”, not null.

Best Regards

I tested this in vanilla js:

x = await Moralis.executeFunction({
contractAddress:'0x6c6Ae1d2eADa359c8fC8308707DeBa552f35F5C2',
functionName: "getReputation",
abi: abi3,
params: {
        _address:"0xbA6AADA3F43521296f92cdD6730108E13b927c1c",
        _categoryID: "0x91f9b2a50ec6433a1cb3014d0ac426a62822c084a87e5aade80c7a9f5ce3f4e5"
        }
})

and it returns 0, but I had to add bsc testnet to may metamask and have that network selected. This request will go to metamask.

Now I think that it will be easier if we also add parameters to runContractFunction.

Yep in plain vanilla JS it worked for me as well.
Can you give an example on how to run this in react with the parameters. runContract function doesn’t mention parameters in the documentation. Thanks.

does this help you with react: https://github.com/ethereum-boilerplate/ethereum-boilerplate/blob/f9381a503c0af5df6d457180082c07c0040c601a/src/components/Contract/Contract.jsx#L97 ?

this also works for me in react:

const { Moralis } = useMoralis();

  const options = {
    contractAddress: "0x6c6Ae1d2eADa359c8fC8308707DeBa552f35F5C2",
    functionName: "getReputation",
    abi: [
      {
        inputs: [
          {
            internalType: "address",
            name: "_address",
            type: "address"
          },
          {
            internalType: "bytes32",
            name: "_categoryID",
            type: "bytes32"
          }
        ],
        name: "getReputation",
        outputs: [
          {
            internalType: "uint80",
            name: "",
            type: "uint80"
          }
        ],
        stateMutability: "view",
        type: "function"
      }
    ],
    params: {
      _address: "0xbA6AADA3F43521296f92cdD6730108E13b927c1c",
      _categoryID:
        "0x91f9b2a50ec6433a1cb3014d0ac426a62822c084a87e5aade80c7a9f5ce3f4e5"
    }
  };

  const x = async () => {
    await Moralis.enableWeb3();
    const b = await Moralis.executeFunction(options);
    console.log("response:", b);
  };

That one did work. Have you tried with the native useWeb3Execute function hook.

That was the one that sent me through this rabbit hole. I think it is good to report to the team that that specific hook was sending me the errors mentioned above. And as you can see, all the parameters were sent exactly in the same way. I think there is something wrong with that hook.

it looks like this syntax works fine for me too:

  const {
    data: data1,
    error: eror1,
    fetch: fetch1,
    isFetching: isFetching1,
    isLoading: isLoading1
  } = useWeb3ExecuteFunction(options);


      <button onClick={() => fetch1()} disabled={isFetching1}>
        Fetch data
      </button>
      {data1 && <pre>{data1}</pre>}

but I had to also use await Moralis.enableWeb3();

Where did you put the await Moralis.enableWeb3();

in particular I had two buttons when I tested, one button that was executing await Moralis.enableWeb3(); and another button for useWeb3ExecuteFunction

I am having the same problem with useWeb3ExecuteFunction(). The hook doesn’t seem to take chain as a parameter. How does it know which chain to execute the function on? Do I have to call enableWeb3() before using this hook? Is there any working code that successfully runs useWeb3ExecuteFunction() on non-ethereum chain? thanks

my sample code as follows:

const ERC20_ABI = [
    {
      constant: true,
      inputs: [],
      name: 'totalSupply',
      outputs: [{ name: '', type: 'uint256' }],
      payable: false,
      stateMutability: 'view',
      type: 'function'
    }
  ]

  const { data, error, fetch, isFetching, isLoading } = useWeb3ExecuteFunction({
    contractAddress: '0xe28984e1EE8D431346D32BeC9Ec800Efb643eef4'.toLowerCase(),
    abi: ERC20_ABI,
    functionName: 'totalSupply',
    params: {}
  })

  console.log(data)

If you want to run only this totalSupply function (or any other read only function), then you can use runContractFunction that doesn’t depend on local web3 instance: https://docs.moralis.io/moralis-server/web3-sdk/native#runcontractfunction

in particular useWeb3ExecuteFunction will use current web3 instance and it depends on what chain is selected there in metamask or in what wallet are you using

Hi,

I also looked at your code in this repository:

I ran the website locally. After authenticate, I can executeFunction no problem.

However, if I were to refresh the page, even though I’m still logged in, executeFunction no longer works. To make it work again, I’d have to logout then log back in.

This is the reason why I couldn’t get useWeb3ExecuteFunction to work previously. Can you please kindly take a look. It feels like a bug to me.

Thanks,

I think that you have to use something like Moralis.enableWeb3() before using executeFunction
The reason is that executeFunction uses a web3 connection to MetaMask.

But if you only want to use read only functions, you can use runContractFunction where you don’t need a web3 connection: https://docs.moralis.io/moralis-server/web3-sdk/native#runcontractfunction

1 Like

Thank you for the advice. Now I am running into another error with runContractFunction. The code is as follows.

import { useMoralisWeb3Api, useMoralisWeb3ApiCall } from "react-moralis";

const { native: { runContractFunction }, } = useMoralisWeb3Api();

const ERC20_ABI = [
    {
      constant: true,
      inputs: [],
      name: "totalSupply",
      outputs: [{ name: "", type: "uint256" }],
      payable: false,
      stateMutability: "view",
      type: "function",
    },
  ];

  const getTotalSupply = useMoralisWeb3ApiCall(runContractFunction, {
    chain: "avalanche",
    address: "0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7",
    function_name: "totalSupply",
    abi: ERC20_ABI,
  });

Error message is as follows:

Argument of type ‘{ chain: “avalanche”; address: string; function_name: string; abi: { constant: boolean; inputs: never[]; name: string; outputs: { name: string; type: string; }[]; payable: boolean; stateMutability: string; type: string; }[]; }’ is not assignable to parameter of type ‘{ chain?: “avalanche” | “eth” | “0x1” | “ropsten” | “0x3” | “rinkeby” | “0x4” | “goerli” | “0x5” | “kovan” | “0x2a” | “polygon” | “0x89” | “mumbai” | “0x13881” | “bsc” | “0x38” | “bsc testnet” | “0x61” | “0xa86a” | undefined; subdomain?: string | undefined; providerUrl?: string | undefined; function_name: string; } …’. Object literal may only specify known properties, and ‘abi’ does not exist in type ‘{ chain?: “avalanche” | “eth” | “0x1” | “ropsten” | “0x3” | “rinkeby” | “0x4” | “goerli” | “0x5” | “kovan” | “0x2a” | “polygon” | “0x89” | “mumbai” | “0x13881” | “bsc” | “0x38” | “bsc testnet” | “0x61” | “0xa86a” | undefined; subdomain?: string | undefined; providerUrl?: string | undefined; function_name: string; } …’. TS2345

I am not sure why it’s complaining about the abi parameter. appreciate your help.

it looks like this works fine in vanilla JS:

ERC20_ABI = [
    {
      constant: true,
      inputs: [],
      name: "totalSupply",
      outputs: [{ name: "", type: "uint256" }],
      payable: false,
      stateMutability: "view",
      type: "function",
    },
  ];

x = await Moralis.Web3API.native.runContractFunction({
    chain: "avalanche",
    address: "0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7",
    function_name: "totalSupply",
    abi: ERC20_ABI,
  })

=>
'19967027525055234432398593'

Thank you. Do you mind also showing me how to do this properly in the react-moralis framework? I find your code in this repository (https://github.com/MoralisWeb3/demo-apps/tree/main/react-moralis-examples) extremely helpful. It’ll be nice if you could add code there for using web3 api to call a read only function.

you can also use Moralis.Web3API.native directly in react.