Ok, it seems like this issue is only present in chrome browser I am working with
Version 64.0.3282.140 (Official Build) Built on Ubuntu , running on Ubuntu 16.04 (64-bit)
Actually I had some good running code, but since yesterday if I want to check the contract event log, I simply get no callback. I'm not sure if I missed an update or did something stupid... I tested everything on mainnet and rinkby
Now, I replicated everything with minimal running code. You can also check out this git repo.
To run everything you need MetaMask isntalled:
- Deploy to your fav Ethereum net (Mainnet, Testnet, private RPC,...)
- Enter the contract address in the app.js file (on Rinkby the contract address is
0xd5c138d1c289a417fba456b4f8db0e88573e556b
- if no troll destroyed the contract) - Simply serve the files with your favorite node server (http-server, express, ...)
I am not able to check the event log of the contract any more. Maybe the MetaMask provider has some issues or maybe I'm just too stupid :/
Solidity contract:
pragma solidity ^0.4.18;
contract test {
uint256 public counter = 0;
event testEvent(address indexed _sender, uint256 _value, uint256 _counter);
function test() public{
counter += 1;
}
function() public payable {
raiseCounter();
}
function raiseCounter() public payable{
testEvent(msg.sender, msg.value, counter);
counter += 1;
}
function destroy() public{
selfdestruct(msg.sender);
}
}
Frontend js:
window.addEventListener('load', function() {
if (typeof web3 !== 'undefined') {
window.web3 = new Web3(web3.currentProvider);
startApp();
} else {
console.log('No web3? You should consider trying MetaMask!');
}
});
function startApp(){
console.log("start App");
var contractABI = web3.eth.contract(
[
{
"constant": true,
"inputs": [],
"name": "counter",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "destroy",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "raiseCounter",
"outputs": [],
"payable": true,
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"payable": true,
"stateMutability": "payable",
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "_sender",
"type": "address"
},
{
"indexed": false,
"name": "_value",
"type": "uint256"
},
{
"indexed": false,
"name": "_counter",
"type": "uint256"
}
],
"name": "testEvent",
"type": "event"
}
]
);
var contractAddress = 'ENTER DEPLOYED ADDRESS HERE';
var Contract = contractABI.at(contractAddress);
var userAcc = returnAccount();
if(userAcc != null) checkBlockNo(Contract);
getAllValues(Contract);
addListeners(Contract);
}
function returnAccount(err, accounts){
if (err != null) {
console.error("An error occurred: "+err);
return null;
} else if (web3.eth.accounts[0] == undefined) {
return null;
} else {
console.log("Account: " + web3.eth.accounts[0]);
return web3.eth.accounts[0];
}
}
function checkBlockNo(_Contract){
var filter = web3.eth.filter('latest');
filter.watch(function(_err, _resp){
if(_err != null){
}else{
var block = web3.eth.getBlockNumber(returnBlockNumber);
console.log(block);
}
});
}
function getAllValues(_Contract){
document.getElementById('getAllValues_btn').addEventListener("click", function() {
console.log("========= CONTRACT INFO ==========");
web3.eth.getBalance(_Contract.address,(_err, _resp) => {
if(_err!=null){
console.log(_err);
}else{
console.log("Balance: "+ web3.fromWei(_resp,'ether').toString(10));
}
});
_Contract.counter((_err, _resp) => {
if(_err!=null){
console.log(_err);
}else{
console.log("Counter: "+_resp);
}
});
});
}
function addListeners(_Contract){
console.log("Adding FunctionListeners");
document.getElementById('FunctionBtn_RaiseCounter').addEventListener("click", function() {
_Contract.raiseCounter.sendTransaction({from: web3.eth.accounts[0], value: 0}, (_err, _resp) => {
if(_err!=null){
console.log(_err);
}else{
console.log(_resp);
}
});
});
console.log("Adding EventListeners");
document.getElementById('EventBtn_TestEvents').addEventListener("click", function() {
console.log("Start query...");
_Contract.testEvent({},{fromBlock: 0, toBlock: 'latest'}).get((error,events) =>{
if(error!=null){
console.log(error);
}else{
console.log(events);
if(events[0] != undefined){
events.forEach(function(_item, _index){
console.log("======= TestEvent =======");
console.log("Sender: " + _item.args._sender.toString());
console.log("Amount: " + web3.fromWei(_item.args._amount,'ether').toString(10));
console.log("Counter: " + _item.args._counter.toString());
});
}
}
});
});
}
Frontend HTML:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<p><button id="getAllValues_btn" class="btns">Get all values</button></p>
<p><button id="FunctionBtn_RaiseCounter" class="btns">Raise Counter</button></p>
<p><button id="EventBtn_TestEvents" class="btns">Check Event</button></p>
<p><button id="Destroy_btn" class="btns">Destroy!</button></p>
</body>
<script type="text/javascript" src="app.js"></script>
</html>