Solidity

通過 web3 js 執行合約時沒油了

  • September 1, 2021

我在瀏覽器中通過 web 3 js 呼叫契約時遇到問題。我正在使用 truffle develop 並已成功遷移和執行測試。

當我 console.log the contract.at(); 時,我也會得到資訊。

eventHost.createEvent("testevent1",5,30);

當我在 chrome 的控制台中執行上述內容時

 web3.min.js:2 Uncaught Error: VM Exception while processing transaction: out of gas
       at Object.InvalidResponse (web3.min.js:1)
       at s.send (web3.min.js:2)
       at r.e [as sendTransaction] (web3.min.js:2)
       at c.sendTransaction (web3.min.js:2)
       at c.execute (web3.min.js:2)
       at <anonymous>:1:11

我明白了

我的契約 Event.sol

contract Event {

   uint32 currentMaxNumOfEvents=0;
   struct eventDetail{
   uint32  eventId;
   address  eventHost;
   string  eventName;
   uint32  noOfDaysToRegsiter;
   uint32  maxMembers;
   address[]  memberAddress;
   }
   mapping(uint32 => eventDetail) public events;
   function joinEvent(uint32 selectedEventId)public returns (uint32){
        for (uint i = 0; i<events[selectedEventId].memberAddress.length; i++) {
            assert(events[selectedEventId].memberAddress[i]==msg.sender);
       }
       require(selectedEventId>=0&&selectedEventId<=currentMaxNumOfEvents);
       require(events[selectedEventId].memberAddress.length < events[selectedEventId].maxMembers);
       events[selectedEventId].memberAddress.push(msg.sender);
       return selectedEventId;
   }
   function createEvent(string eventName,uint32 noOfdays,uint32 maxMembers) public returns (uint32){
       events[currentMaxNumOfEvents].eventId= currentMaxNumOfEvents;
       events[currentMaxNumOfEvents].eventHost =msg.sender;
       events[currentMaxNumOfEvents].eventName = eventName;
       events[currentMaxNumOfEvents].noOfDaysToRegsiter = noOfdays;
       events[currentMaxNumOfEvents].maxMembers = maxMembers;
       currentMaxNumOfEvents += 1;
       return events[currentMaxNumOfEvents].eventId;


   }
}

我的腳本在瀏覽器中執行

if (typeof web3 !== 'undefined') {
           web3 = new Web3(web3.currentProvider);
       } else {
           // set the provider you want from Web3.providers
           web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:9545"));
       }
       web3.eth.defaultAccount = web3.eth.accounts[0];
       var contract = web3.eth.contract([{"constant":true,"inputs":[{"name":"","type":"uint32"}],"name":"events","outputs":[{"name":"eventId","type":"uint32"},{"name":"eventHost","type":"address"},{"name":"eventName","type":"string"},{"name":"noOfDaysToRegsiter","type":"uint32"},{"name":"maxMembers","type":"uint32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"selectedEventId","type":"uint32"}],"name":"joinEvent","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"eventName","type":"string"},{"name":"noOfdays","type":"uint32"},{"name":"maxMembers","type":"uint32"}],"name":"createEvent","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]);
       var eventHost = contract.at('0x345ca3e014aaf5dca488057592ee47305d9b3e10');
       console.log(eventHost);

“Out of gas”是當方法執行出現問題時通常會出現的錯誤,通常是在拋出錯誤時。

您需要傳遞其他參數來指定呼叫該方法的帳戶,並且您需要確保該帳戶已解鎖並且有 Ether 來支付 gas:

eventHost.createEvent("testevent1",5,30, { from: _account, gas: _gasLimit });

你也應該指定一個氣體限制。

檢查你呼叫gas合約函式的賬戶(地址)中是否有ETH

引用自:https://ethereum.stackexchange.com/questions/29878