3

I have created a simple contract with one event. I use web3 via MetaMask to watch for these events, however I can see the transactions on Etherscan but the Javascript callback is not being fired for the new event.

Events that happened before page load I can get.

The contract is on Ropsten

pragma solidity ^0.4.18;

contract MyContract {
    address creator;
    event HasAccepted(address sender, bool isCreator);

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

    function accept () 
    public
    {
        bool isCreator = msg.sender == creator;
        HasAccepted(msg.sender, isCreator);
    }
}

and associated web3 code below:

<html>
  <script>
    var contractAddr = "0x33743a345c0e20f556bf15d19ca12968c47ae4d8";
    var contractAbi = [{"constant":false,"inputs":[],"name":"accept","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"isCreator","type":"bool"}],"name":"HasAccepted","type":"event"}];

    function startApp() {
        var MyContract = web3.eth.contract(contractAbi);
        var contractInstance = MyContract.at(contractAddr);

        document.getElementById('acceptBtn').addEventListener('click', function(){
            contractInstance.accept({value: 0, gas: 210000}, console.log);
        });

        var event = contractInstance.HasAccepted(function(error, result){
            // Expect to log when click 'Run accept' button
            console.log("HasAccepted", error, result);
        });
    }

    window.addEventListener('load', function() {
      if (typeof web3 !== 'undefined') {
        window.web3 = new Web3(web3.currentProvider);
      }
      startApp();
    })
  </script>
  <body>
    <button type="button" id="acceptBtn">Run accept</button>
  </body>
</html>

I would expect that I would see output from the callback given to the 'HasAccepted' event.

3

1 Answer 1

1

There were two main issues with your code.
- The method.event() function needs at least one variable: indexed filter or {} if unneeded
- It seeems metamask requires fromBlock for event pooling or watching (Sometimes, Metamask syncing to the network can be delayed, not often though)

var fromBlock = 1908935;
var contractAddr = "0x33743a345c0e20f556bf15d19ca12968c47ae4d8";
...

    var event = contractInstance.HasAccepted({},{fromBlock:fromBlock},function(error, result){
        // Expect to log when click 'Run accept' button
        console.log("HasAccepted", error, result);
    });
}

I hope this helps.

1
  • Thanks this works! However, strangely the code I posted above also works for me too. I think there could have been an issue in Metamask/web3 that got fixed in the last few days. v3.11.2
    – snclucas
    Oct 23, 2017 at 14:29

Your Answer

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

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