1

In my solidity contract, I just have one event which is:

transferOwnerShip (uint  id, address indexed newowner);

and I want to filter the events log in order to retrieve only the events that belong to specific **newowner address**

I used this JavaScript code

 var newlog;
         var events = DeviceManagerContractInstance.allEvents(
           {fromBlock: 0, toBlock: 'latest'} , 
           {newowner:"0xe6b56e8f11f2dd687d63c75cda3f261ae1124054"});

         events.get(function(error, log)  {

          if(!error)

           {           

             console.log(log);

        for( var property in log) {
        newlog += "Event " + property + ': '+'\n' + JSON.stringify(log[property],null,4)+'\r\n';

            document.getElementById("eventID").innerHTML = newlog;

        }

            }

   });

where DeviceManagerContractInstance is my contract instance

but I received all the events for all addresses. So, What is the best way to retrieve event based on specific data args?

Besides, Is there any way to print specific data from the event instead of the whole object.

  • Truffle v5.0.27 (core: 5.0.27)
  • Solidity v0.5.0 (solc-js)
  • Node v10.15.3
  • Web3.js v1.0.0-beta.37
  • geth Version: 1.9.6-stable
0

You can try this, but first double check if your contract instance is defined properly:

DeviceManagerContractInstance.getPastEvents('transferOwnerShip', {
    filter: {newowner: "0xe6b56e8f11f2dd687d63c75cda3f261ae1124054"},
    fromBlock: 0,
    toBlock: 'latest'
}, function(error, result) {
    if(!error) {
        console.log(result, 'result');
    } else {
        console.log(error, 'error')
    }
});
|improve this answer|||||
  • Thanks, @Miroslav for ur answer, I tried but my Dapp threw this error (DeviceManagerContractInstance.getPastEvents is not a function) , besides, this is how I defined my contract instance App.contracts.DeviceManager.deployed().then(function(instance) { DeviceManagerContractInstance = instance; – Byo0ona Dec 20 '19 at 12:46
  • Ok, this happens, because most likely your web3 provider is overwriting the web3 version of your library and you end up using web3 version 0.2.x. web3js.readthedocs.io/en/v1.2.4/… Look here the method I suggested is for web3 1.x.x and github.com/ethereum/wiki/wiki/JavaScript-API#contract-allevents .allEvents() is part of the web3 0.2.x documentation. What you have to do is this: – Miroslav Nedelchev Dec 20 '19 at 14:12
  • DeviceManagerContractInstance.transferOwnerShip({ newowner: "0xe6b56e8f11f2dd687d63c75cda3f261ae1124054" }, { fromBlock: 0, toBlock: 'latest' }, function(error, result) { if (!error) { console.log(result); } }); – Miroslav Nedelchev Dec 20 '19 at 14:18
  • Thanks a lot for your quick response. I tried your suggestion. Unfortunately, it didn't work, and my output showed an empty array object. As well, I noticed that I have to use allEvents to have an output, otherwise, it throws an error or an empty array object. length: 0 ​ <prototype>: Array [] Any new suggestions will be greatly appreciated. – Byo0ona Dec 20 '19 at 21:37
  • Try this please: DeviceManagerContractInstance.transferOwnerShip({ newowner: '0xe6b56e8f11f2dd687d63c75cda3f261ae1124054' }, { fromBlock: 0, toBlock: 'latest' }).get(function (error, eventResult) { console.log(eventResult, 'eventResult'); }); – Miroslav Nedelchev Dec 22 '19 at 19:02

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.