[SOLVED] useWeb3Contract function not working, how to use ethers directly

Hey guys,

I’m trying to test my contract on the Mumbai Testnet, however the following function returns a MetaMask RPC error: block gas limit exceeded.

  const {
    runContractFunction: createClone,
    isFetching,
    isLoading,
  } = useWeb3Contract({
    abi: wfABI,
    contractAddress: "0xa56F180aF0A73d948b6B386616f549279F4d6e4d",
    functionName: "createClone",
    params: {},
    msgValue: ethers.utils.parseUnits("1000000000000000", "wei"),
  });

The same function works on the Mumbai testnet when I run it on the Hardhat CLI and it also works from the front end when I use Hardhat, however for some reason it’s not working on my moralis front end when I try it with Polygon Mumbai. I have double checked the addresses, abi, etc, and I still can’t figure out why this isn’t working. I’ve added gasLimit using ethers manually as well.

This is the exact error I get: MetaMask - RPC Error: [ethjs-query] while formatting outputs from RPC '{"value":{"code":-32603,"data":{"code":-32000,"message":"exceeds block gas limit"}}}' {code: -32603, message: [ethjs-query] while formatting outputs from RPC ‘{…e":-32000,“message”:“exceeds block gas limit”}}}’}

Thanks!

1 Like

what Is the gas cost associated with that transaction?

that error looks like too much gas was associated with that transaction

Nope, it’s not really a lot of gas. Also, it works when I use Hardhat CLI on the testnet it works. I changed the RPC Url and now I’m getting a different error:

inpage.js:1 MetaMask - RPC Error: JsonRpcEngine: Response has no error or result for request:
{
  "method": "eth_getBalance",
  "params": [
    "0xdd8c868ee486e2d0778b7c174917a15ce2a4530a",
    "0x1b70aac"
  ],
  "jsonrpc": "2.0",
  "id": 1087160063,
  "origin": "http://localhost:3000",
  "tabId": 96231278

Maybe is related to the RPC url. You are trying that with local hardhat RPC url added in MetaMask?

Nope, with the Polygon Mumbai RPC. It works on hardhat.

It works from hardhat CLI on polygon mumbai as well. You can check out this address on Polygon scan, I just did a transaction: 0xa56F180aF0A73d948b6B386616f549279F4d6e4d

It’s only not working from my front end for some reason.

this is a normal RPC request that tries to check the balance for an address, it does not try to deploy a contract

I’m not trying to deploy a contract, I’m trying to perform a transaction.

In my frontend, is there anywhere for me to specify chainID? (incase it’s something to do with that)

you select the network in metamask and the frontend should use the chain/network that is selected in metamask

I tried doing it on Goerli and I got an unhandled runtime error which usually means gas estimation failed. It definitely has something to do with gas.

When I run it on Hardhat, I specify gasLimit like this:

tx = await wf.connect(creator).createClone({
                value: ethers.utils.parseUnits("1000000000000000", "wei"),
                gasLimit: 6000000,
              })

As of now, I’m specifying gas limit like this in the front end:

                onClick={createInstance == true ? async function () {
                  await createClone({
                    onSuccess: handleSuccess,
                    onError: (error) => console.log(error),
                    gasLimit: 6000000,
                  });

Is this the correct with to specify gasLimit?

I am not sure about that syntax, when it works in hardhat, what is the total gas cost for that transaction?

gas cost is 0.09 Matic. I’m pretty sure the issue is I have to explicitly mention the gasLimit. I remember this failing on hardhat as well until I mentioned the gasLimit.

can you also try to specify the gas cost?

I’ve tried, it didn’t work. Is there a way to use ethers syntax?

you can use ethers syntax directly, you don’t have to use uweWeb3Contract

and how do I make that work with a button? Do I put that all inside a const?

I’m not expert in react, I guess that you add a callback function to a button and you call it

1 Like

One more question, what provider do I use if I’m using ConnectButton from web3uikit?

I don’t know this, there has to be a way to get access to ethers provider, like ethers = await Moralis.enableWeb3() was the syntax in vanilla js

Hey @cryptokid,

I turned on ‘manual gas limit’ in advanced settings of metamask and when I set it to 6000000 the function worked. However, now I’m wondering how to do this via Moralis. I know you said I could use ethers directly, and I looked at the web3provider section of the docs, however that gave me some errors. This was my code:

const create = async function () {

address = “<address>”

const ethers = Moralis.web3Library; 
const web3Provider = await Moralis.enableWeb3(); 
const gasPrice = await web3Provider.getGasPrice();
const signer = web3Provider.getSigner();
const contract = new ethers.Contract(address, whoopyAbi, signer);

const transaction = await contract.deposit({
	
value: Moralis.Units.ETH('1'),

gasLimit: 100000000,

gasPrice: gasPrice,

});

await transaction.wait();

}

When I use this, I get the invalid address or ENS name error.

Do you have any recommendations on how I can go about this?
Thanks!