I'm new to Ethereum and encountered a problem when filtering an event and watching it in Web3.
My simple solidity contract is the following:
pragma solidity ^0.4.4;
contract Test
{
bool approval=false;
bytes32 UID;
event update(bytes32 indexed _UID);
event Notification (string notification);
function fireEvent(bytes32 _UID) public
{
UID = _UID;
update(UID);
}
function getStatus() constant returns(bool)
{
return approval;
}
function updateStatus(bool approval_UI) returns(bool)
{
if (approval_UI)
{
Notification("ACCEPTED");
approval=true;
return approval;
}
else
{
Notification("REJECTED");
approval=false;
return approval;
}
}
}
My web3.js code which filters the event with a specific value of the _UID, then watches for the event and calls a callback function to change the contract state variable 'approval' from false (default value) to true is the following:
var account = web3.eth.accounts[0];
web3.eth.defaultAccount=account;
if(web3.personal.unlockAccount(account,"1234",0))
{console.log(account);}
var abi=[contract-abi-here]
var address ="[contract-address-here]";
var contract = web3.eth.contract(abi).at(address);
var filter = contract.update({_UID: "123"});
filter.watch(function(error, result){
if (error) {
console.log(error);
return;
}
else
{
contract.updateStatus(true,{from: web3.eth.accounts[0], gas:200000});
console.log('contract STATE changed!');
}
});
However, when I connect the Solidity Remix with a Web3 provider (a private geth node) and fire the event as follows:
fireEvent("123");
The Web3 js code seems that never enters the else block to call the updateStatus contract function, i.e. this code snippet is NOT executed:
...
else
{
contract.updateStatus(true,{from: web3.eth.accounts[0], gas:200000});
console.log('contract STATE changed!');
}
and the function getStatus() returns always false (i.e. the default value)
Note though that if I directly call this function (without filtering and watching the Event)
contract.updateStatus(true,{from: web3.eth.accounts[0], gas:200000});
then the function getStatus() returns true, i.e. the line above is executed as per normal.
Note: after each transaction, I make sure that it is mined.
Any help would be appreciated!
Regards, Natassa