1

I am having a trouble invoking contract via web 3 js in browser. I am using truffle develop and have migrated and run tests successfully.

I am also getting the info when I console.log the contract.at();

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

when i run the above in console of 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

I get this

my contract 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;


    }
}

my script running in browser

 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);
0

“Out of gas” is an error you’ll generally get when there is a problem with method execution, typically when there is an error thrown.

You need to pass additional parameters to specify the account that is calling the method, and you need to make sure that account is unlocked and has Ether to cover the gas:

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

Also you should specify a gas limit.

0

Check if you have ETH in the account (address) from where you're calling the contract function for gas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.