8

It appears my events are not being fired in my node application when I transact the contract in the ethereum wallet gui.

  1. I deployed a custom contract via ethereum-wallet
  2. I have fired up geth attach
  3. in geth I have started the rpc admin.startRPC("127.0.0.1", 8545, "*", "web3,db,net,eth")
  4. within the node repl shell I have done the following:

    > var Web3 = require('web3')
    > web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    > var contractAddress = "0x807bF45B0245d8FA96F68E319116E18a15b07A10"
    > var contractCode = "<paste in the code above for the contract>"
    > var compiledContract = web3.eth.compile.solidity(contractCode)
    > var contractDefinition = compiledContract.ShareClass.info.abiDefinition
    > var reference = web3.eth.contract(contractDefinition).at(contractAddress)
    > reference.Transfer().watch(
         function(error, result){
             console.log(result.args.from + " => "+result.args.to + " ["+result.args.value+"]");
         });
    

When I then call the Transfer method on my contract in ethereum wallet, the contract balance changes, but the console logging does not take place in my node app.

  • a bit hard to debug if we can't see the code that emits the event. Please include at least the snippet of code emitting the event. You could try allEvents and see if it's a filtering issue. – Paul S Mar 18 '16 at 20:38
  • one possible reason is when web3.eth.defaultAccountis not set or has no Ether. Then it can still call readonly functions, but won't emit events. – didi_X8 Nov 19 '16 at 17:08
4

I think you're supposed to add watch to the event object, not the function.

Solidity:

contract Test {
    Event MyLog(string);

    function MyFunction() {
        MyLog("Hello World!");
    }
}

Javascript:

// NOT reference.MyFunction().watch
reference.MyLog().watch(function(error, result) {
    if (!err) {
        console.log(result);
    } else {
        console.log("Error! + " error);
    } 
});
  • MyFunction is calling the event with the wrong name. – Ismael May 31 '16 at 18:09
0

Event listening is only available for WebSocket or IPC provider

https://github.com/ethereum/web3.js/issues/989#issuecomment-369232471

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.