1

My function is:

function watchEvents(){
                myEvent = contract_instance.NumberIsIncreased({},{fromBlock: 'latest', toBlock: 'latest'});
                console.log(myEvent);
                myEvent.watch(function(error, result){
                    if(error){
                        console.log(error);
                    } else{
                        console.log(result);
                        document.getElementById("events").innerHTML = document.getElementById("events").innerHTML + "<br />" + JSON.stringify(result);
                    }
                });

            }

This is the contract:

event NumberIsIncreased(address indexed whoIncreased, uint256 indexed oldNumber, uint256 indexed newNumber);


function setMyNumber(uint256 myNewNumber) public {
        NumberIsIncreased(msg.sender, myNumber, myNewNumber);
        myNumber = myNewNumber;
    }

Although I can access other functions on the smart contract, when I'm calling the event, it's showing the error Uncaught TypeError: contract_instance.NumberIsIncreased is not a function.

I can track events in the Mist browser too.

I'm using geth version : 1.7.2-stable.

Edit 1:

I'm using web3.js version : 0.14.0

Other functions that are working:

function getCounter(){
                contract_instance.getMyNumber(function(e,r){
                    document.getElementById("myCounter").innerText = r.toNumber();
                });
            }

function increaseCounter(){
                contract_instance.getMyNumber(function(e,r){
                    currentNumber = r;
                    currentNumber++;
                    web3.eth.getAccounts(function(e,accounts){
                        web3.personal.unlockAccount(accounts,"password", function(e,r){});
                        contract_instance.setMyNumber(currentNumber, {from:accounts[0]  , gas:200000}, function(error, result){
                            if (error){
                                console.error(error);
                            } else {
                                var txHash = result;
                                console.log(txHash);
                                callWhenMined(txHash, getCounter);
                            }
                        });
                    });
                });
            }

My contract file :

pragma solidity ^0.4.0;

contract MyContract {
    address creator;
    uint256 myNumber;

    event NumberIsIncreased(address indexed whoIncreased, uint256 indexed oldNumber, uint256 indexed newNumber);

    function MyContract() public {
        creator = msg.sender;
        myNumber = 3;
    }

    function getCreator() public constant returns (address) {
        return creator;
    }

    function getMyNumber() public constant returns (uint256) {
        return myNumber;
    }

    function setMyNumber(uint256 myNewNumber) public {
        NumberIsIncreased(msg.sender, myNumber, myNewNumber);
        myNumber = myNewNumber;
    }


    function kill() public {
        if (msg.sender == creator) {
            selfdestruct(creator);
        }
    }

}
  • which version of web3js are you using? It might be necessary to supply some more code such as the other functions of the smart contract that you can access. – SteveJaxon Nov 14 '17 at 22:42
  • @SteveJaxon, I have added the other functions and my solidity file now. – sacros Nov 15 '17 at 7:59
1

Events are not functions you can call externally from outside of the contract (contract_instance.NumberIsIncreased() is invalid); they are only called within a function in the smart contract itself. When you call setMyNumber(), that function calls the event and the event will be logged in the transaction object that is returned by calling setMyNumber.

Events are typically called at the end of the function and after state changes, only once the computations in the main body of the function are completed and successful, to allow for logging and confirmation that the function succeeded.

Logging events also allows for monitoring and watching of functions being called on a contract.

On a side note, would suggest a naming convention of prefixing events with “Log” (i.e. “LogNumberIsIncreased”), to allow for easier code readability, making it more clear when you’re calling events. This is particularly helpful once your contracts get more complex and you’re logging more and more events.

Another suggested best practice, calling the event should come last, i.e. after you set the number myNumber = myNewNumber. You may need a temp variable myOldNumber. In this example, it might be OK, because if setting the number fails, then the whole function would revert and it wouldn’t actually log the event (even though the event call comes before the number is set). But as general best practice of “fail fast”, which again gets more important once you write more complex contracts, steps that could fail (here changing the number / changing the state) should come first. If for whatever reason that operation fails, by calling the event after the operation, you wouldn’t need to waste any computation or do unnecessary steps (log event), because the function would fail before reaching that now unnecessary computation.

  • Following solidity.readthedocs.io/en/develop/contracts.html#events, I want to add a watch for my event. The method I followed, is similar to what is present there. Please have a look at the solidity code, and use in JavaScript API given in the documentation. Also, thanks for the suggestions, I'm a newbie, learning from everywhere I can. – sacros Nov 15 '17 at 11:30
0

Since someone also got this error, I'm answering my own question.

So the problem here was solved when I rebuild my contracts.

truffle migrate --reset --all

And in metamask, you can do reset account.

After that I was getting the event logs. You should be able to see your events in your contract instance in the truffle console too.

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.