最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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

Solidity code:

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:

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 get:

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)

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

Solidity code:

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:

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 get:

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)

Share Improve this question edited Sep 16, 2022 at 19:06 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Dec 7, 2021 at 13:36 slim.sydslim.syd 552 silver badges7 bronze badges 1
  • try putting gas and gas price in the same object – jhonny Commented Dec 7, 2021 at 14:36
Add a ment  | 

1 Answer 1

Reset to default 4

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

发布评论

评论列表(0)

  1. 暂无评论