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.
web3.eth.subscribe(type [, options] [, callback]);
as defined here