簡體   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