8

I want all js clients to react on new event being stored to blockchain:

contract MainContract{
    event Evt(address indexed _sender,string jsn);
    function deposit(string jsn) returns (int256) {
        Evt(msg.sender, jsn);
    }
}

Here I expect watch callback to be executed.

    var contract = web3.eth.contract(abi).at("0xe45866ac5d51067ce292bc656c790e94ddcf0766");
    var myEvent = contract.Evt({},{fromBlock: 0, toBlock: 'latest'});
    myEvent.watch(function(error, result){
        console.log("on watch"); 
        console.log(arguments);
    });
    // this call saves event data successfully!
    contract.deposit('hello there',function (res) {
        console.log(arguments)
    });

But I don't see 'on watch' in my console. Is this possible? How can I do this? Any example?

UPDATE: my best guess right now that this is metamask.io problem (https://github.com/MetaMask/metamask-plugin/issues/792)

UPDATE2: Looks like a bug in ethereum kernel, some people report same experience: https://github.com/ethereum/go-ethereum/issues/14670

4
  • For your comments it is working, but you can't see the "on watch" message no?
    – Gawey
    Jul 12, 2017 at 13:43
  • 'On watch' call back is not executed. Jul 12, 2017 at 13:51
  • So if 'myEvent' is not undefined, and you can call '.watch', yes maybe is a problem with event.watch. With filters.watch have errors very similars, but try to ' put if(!error) console.log(result); else console.error(error); }) '
    – Gawey
    Jul 12, 2017 at 14:00
  • 1
    myEvent is not underfined, i have no js errors. error is not null, callback is not called at all. Jul 12, 2017 at 14:31

3 Answers 3

2

I've solved my problem: I used wrong contract address. Note, that API wouldn't let you know that contract address is incorrect!

2

If you are using truffle, the deployed instance should have the event so you can listen to changes:

let Contract = artifacts.require('./Contract.sol');

contract('Contract', accounts => {

    let MyEvent;

    Contract.deployed().then(instance => {
        MyEvent = instance.SolidityEventMethod({});

        MyEvent.watch((error, result) => {
            if (error) {
                console.log(error);
            }

            console.log(result);
        });
    });

    it('should do something and trigger event', () => {
        return Contract.deployed().then(instance => {
            return instance.myEventTriggeringFunction();
        }).then(res => {
            // at this point the MyEvent.watch method should have logged the result
        }).catch(error => {
            console.log(error);
        });
    });
});
-1

I think the "problem" it is due to asynchronous functions. A workaround might be this:

 var contract;
 window.addEventListener('load', function() {
    if (typeof web3 !== 'undefined') {
        window.web3 = new Web3(web3.currentProvider);

        contract = web3.eth.contract(abi).at(contractAddress);
        var myEvent = contract.Evt({},{fromBlock: 0, toBlock: 'latest'});
        myEvent.watch(function(error, result){
            console.log("on watch");
            console.log(result);
            console.log(result.args);
        });

    } else {
        console.log('No web3? You should consider trying MetaMask!')
    }
})


function callDeposit(){
    contract.deposit('hello there',function (error, result) {
            console.log(result);
    });
}

Also in your code the variable arguments is undefined. Hope this help.

2
  • I am already using skeleton like this. And you can see from my code that I register watch callback before successfull call to .deposit(). Jul 12, 2017 at 9:56
  • variable arguments can't be undefined, because its js standard variable Jul 12, 2017 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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