简体   繁体   中英

How would I set a manual gas limit in web3? To get the smart contract to execute?

I'm having a problem executing my smart contract. I figure due to the error code is must be a gas-limit problem. Though I don't know where I would place the code in order for it to execute properly. Any suggestions?

Solidity Code Below: pragma solidity ^0.8.0;

import 'hardhat/console.sol';

contract WavePort { 

//TrackWaves
uint totalWaves; //State Variable, Permanetly Stored In Contract Storage

constructor() {
    console.log("Yo, I am I contract");
    }


//Function Increments Waves On The Blockchain/ Immutable Never Decreasing 
function wave() public { 
    totalWaves += 1;
    console.log("%s has waved!", msg.sender);
}

//Function Returns Us the The Total Waves To Be Viewed
function getTotalWaves()public view returns(uint256) { 
    console.log("We have %d total waves", totalWaves);
    return totalWaves;
}
                
}

Javascript Code Below:

const  App = () => {

  const [currentAccount, setCurrentAccount] = useState("")
  const contractAddress = "contractAddress"
  const contractABI = abi.abi

const checkIfWalletConnected = async () => { 
  let web3
  try { 
    const {ethereum} = window; 

    if(!ethereum) { 
      window.alert("Make sure you have metamask !");
      return;
    }else {
      web3 = new Web3(window.ethereum)
      console.log("We have the ethereum object", ethereum)}

    const accounts = await ethereum.request({method: 'eth_accounts'})
      if(accounts.length !== 0) { 
        const account = accounts[0]
      
        console.log("Found an Authorized account:", account, );
        setCurrentAccount(account)

      } else { 
        window.alert("NO authorized account found")
      }

      const EthBalance = await web3.eth.getBalance(accounts[0])
      console.log(EthBalance)
  }catch(err) {console.log(err)}
}
 const connectWallet = async () => { 
    try { 
      const {ethereum} = window; 
      if(!ethereum) {alert("Get Metamask extenstion")
    return 
  } else { 
  const accounts = await ethereum.request({method: 'eth_accounts'})
  console.log("Connected", accounts[0])
  window.alert("Wallet is Connected")
  setCurrentAccount(accounts[0])
  const ethBlance = await Web3.eth.getBalance(currentAccount)
  console.log(ethBlance)
  }
    }catch(err) {
      console.log(err)
    }
  }

  const wave = async () => { 
    try { 
    const {ethereum} = window;

    if(ethereum) { 
      const provider = new ethers.providers.Web3Provider(ethereum);
      const signer = provider.getSigner(); 
      const wavePortalContract = new ethers.Contract(contractAddress, contractABI, signer);

      let count = await wavePortalContract.getTotalWaves( ); 

      const waveTxn = await wavePortalContract.wave({gas:10000000 }, {gasPrice:80000000000});
      console.log("Mining....", waveTxn.hash)

      await waveTxn.wait( );
      console.log("Mined-- ", waveTxn.hash)

      count = await await wavePortalContract.getTotalWaves( );
      console.log("Retrieved total wave count.. ", count.toNumber())
      }else { 
      console.log("Eth Object doesn't exist!")
    }
    } catch(err) {console.log(`${err} hello world`) }
}

  useEffect(() => {
    checkIfWalletConnected();
  }, [])

The Error Code I receive:

Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"code":-32000,"message":"execution reverted"}, method="call", transaction={"from":"0xD49a9a33F180D1e35A30F0ae2Dbfe5716a740Ebc","to":"0x5FbDB2315678afecb367f032d93F642f64180aa3","data":"0x9a2cdc08","accessList":null}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.5.1)

While calling a contract Abi method, you can pass an object of additional gas-related params in the second argument (or first if you don't have any arguments in your method).

So calling of wave function should looks like following:

const waveTxn = await wavePortalContract.wave({gasLimit:30000});

Check all additional params in the below documentation.

https://docs.ethers.io/v5/api/contract/contract/#contract-functionsSend

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM