1

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

1

After going through your smart contract I think there is a problem with the contract. You are not updating the value of bool approval, it was set to False when initiated and never changed. To solve that problem I think what you can do is use, a mapping between UID and Approval, with this your getStatus problem might be solved, reason being if there is no UID in the mapping and you initiate it then it will return False or another thing you can use is structs.

0

If you already add indexed to event, but it was working by filtering event on web3. Please make sure events declaration is in top of contract inheritance, not the base contract.

You can check if indexed is working of ABI json file. For example

{
  "anonymous": false,
  "inputs": [
    {
      "indexed": true,
      "name": "from",
      "type": "address"
    },
    {
      "indexed": true,
      "name": "to",
      "type": "address"
    },
    {
      "indexed": false,
      "name": "tokenId",
      "type": "uint256"
    }
  ],
  "name": "Transfer",
  "type": "event"
},

"indexed" should be true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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