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

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!

Is your wallet on the right chain for that contract? And doublecheck all your parameters - address, ABI, function name (deposit).

It’s working now! I switched some stuff around but am using the exact same syntax as above. I have a tiny issue though. How can I call the onSuccess and onError params as they aren’t working with my const function?

I got it, I just added the handleSuccess function at the end of the create function. Thanks guys! @alex @cryptokid

Hello Tom,

I ran into the same issue ā€œexceeds block gas limitā€ for useWeb3Contract only on mumbai but calling the same contract functions works perfectly fine on front end in localhost and in hardhat. I also noticed that only functions that change state are giving issues as view functions work fine. What was the solution that you used to resolve this?

Thanks,

Hey,

Yes, it’s only functions that change state which give issue. Even mine worked fine on hardhat but didn’t work on Mumbai.

The solution is to use ethers directly instead of useWeb3Contract and to specify a gasLimit of 6000000. The syntax is this:

  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, ABI, signer);
    
    const transaction = await contract.createClone({
    value: ethers.utils.parseUnits("1000000000000000", "wei"),
    gasLimit: 6000000,
    gasPrice: gasPrice,
    });
    
    await transaction.wait(3);
    await handleSuccess(transaction)
    
    }

You can also check this out: https://v1docs.moralis.io/moralis-dapp/web3/web3

2 Likes

hey facing similar issue, can u share how u fixed it?

Hey @tomwhiteman
I just wanted to let you know that I’ve been working on the same issue for the past two days, and your code helped me solve the problem. I’m forever grateful!

1 Like