繁体   English   中英

如何在 web3 中设置手动气体限制? 让智能合约执行?

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

我在执行我的智能合约时遇到问题。 我认为由于错误代码必须是气体限制问题。 虽然我不知道我会将代码放在哪里以便它正确执行。 有什么建议么?

下面的 Solidity 代码: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 代码如下:

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();
  }, [])

我收到的错误代码:

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)

在调用合约 Abi 方法时,您可以在第二个参数中传递额外的气体相关参数的 object(或者如果您的方法中没有任何 arguments,则首先传递)。

因此wave function 的调用应如下所示:

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

检查以下文档中的所有其他参数。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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