7

Dealing with the code below:

contract myContract {

    struct myStruct{
        bytes32 name;
        bool active;
        uint[] changes;
    }

    myStruct[2] myStr;
    uint[] steps;

    Event activatedEvent(uint time);

    function myContract{
        myStr[0] = packStruct('foo', true, steps);
        myStr[1] = packStruct('bar', false, steps);
    }

    function activate(){
        myStruct ms = myStr[0]; 
        ms.changes.push(now);
        activatedEvent(now);
    }
}

If I call the activate() function, the activatedEvent does not get triggered. Anybody knows where I'm wrong? Any help appreciated.

  • 1
    1. Have you tried increasing the gas? 2. Can you include the output of getTransactionReceipt ? – eth Jul 14 '16 at 17:15
  • Also make sure you are watching for the event correctly, you may want to paste your web3 code here as well – Tjaden Hess Jul 14 '16 at 18:51
  • Thank you so much for the answer, I increased the gas and now everything works great. – AM122 Jul 14 '16 at 21:25
  • 1
    Please add an answer detailing what you did, and accept it :) – Richard Horrocks Feb 21 '17 at 14:50
1

A common reason that a transaction won't run as expected is that it runs out of gas. You can find this out from web3.eth.getTransactionReceipt(transactionHash).

If you did run out of gas, then you can send more. For example, if you are using web3, you can set the amount of gas to 1 million with web3.eth.sendTransaction({ ... , gas: 1000000}).

Please note~ I take no credit for this answer: the real answer came from user:eth in a comment. I would just like to see this question marked as answered.

1

There are some issues with the contract. Firstly the event should have small 'e' and not a capital 'e' next, function myContract should be defined as myContract() and lastly packStruct was not defined

Below is the updated code.

pragma solidity ^0.4.4;

contract myContract {

    struct myStruct{
        bytes32 name;
        bool active;
        uint[] changes;
    }

    myStruct[2] myStr;
    uint[] steps;

    event activatedEvent(uint time);

    function myContract() {
        myStr[0] = myStruct('foo', true, steps);
        myStr[1] = myStruct('bar', false, steps);
    }

    function activate(){
        myStruct ms = myStr[0]; 
        ms.changes.push(now);
        activatedEvent(now);
    }
}

A screenshot from remix compiler

You can also use online compiler remix to compile your code.

Hope it helps!

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.