2

Q1. I find this concept to be very confusing, when should I be using watch, call or get to listen to an event? how are they different and when should they be used?

Also,

var event1 = newInst.getSupplier({'OwnAdd': someaddr},{fromBlock:'latest, toBlock:'pending'}, function(error, result) {
    console.log("listening bro1");
    if (!error)
        console.log(result.args);
});

It returns me the structure twice.

{OwnAdd: "0xfd4e38348590a41e91b5ee57b69b99e746783990", _NameoProd: "lime", _QuantityoProd: BigNumber, _MnfofProd: "Done", _toSrv: "Meh", …}OwnAdd: "0xfd4e38348590a41e91b5ee57b69b99e746783990"_MnfofProd: "Done"_NameoProd: "lime"_QuantityoProd: BigNumber {s: 1, e: 1, c: Array(1)}_comments: "Bajaj"_status: "Saul "_toSrv: "Meh"__proto__: Object
eventwatcher.html:327 listening bro1
eventwatcher.html:329 {OwnAdd: "0xfd4e38348590a41e91b5ee57b69b99e746783990", _NameoProd: "lime", _QuantityoProd: BigNumber, _MnfofProd: "Done", _toSrv: "Meh", …}OwnAdd: "0xfd4e38348590a41e91b5ee57b69b99e746783990"_MnfofProd: "Done"_NameoProd: "lime"_QuantityoProd: BigNumber {s: 1, e: 1, c: Array(1)}_comments: "Bajaj"_status: "Saul "_toSrv: "Meh"__proto__: Object

Q2. I can't figure out why it sends me twice, can anyone figure this out? Note: I am just trying to get the pending data and not all the records with the above event

Smart Contract:

pragma solidity ^0.4.20;

contract Supplier{
    struct supply{
        string NameoProd;
        uint QuantityoProd;
        string MnfofProd;
        string toSrv;
        string stat;
        string comments;
    }
    uint SaleID=100000;
    mapping (uint=>supply) fmr;
    event getSupplier( address indexed OwnAdd, string _NameoProd,
        uint _QuantityoProd,
        string _MnfofProd,
        string _toSrv,
        string _status,
        string _comments);

    function setSupply(string _NameoProd,
        uint _QuantityoProd,
        string _MnfofProd,
        string _toSrv,
        string _status,
        string _comments) public{
        address snd = msg.sender;
        fmr[SaleID]=supply(_NameoProd,_QuantityoProd,_MnfofProd,_toSrv,_status,_comments);
        emit getSupplier(snd, _NameoProd,_QuantityoProd,_MnfofProd,_toSrv,_status,_comments);
        SaleID++;
    }
  function getSomething(uint ID) public view returns (string , uint  , string , string , string , 
    string  ){
        supply a=fmr[ID];
       return (a.NameoProd, a.QuantityoProd, a.MnfofProd, a.toSrv, a.stat, a.comments);
    }
    function update(uint ID, string rpf) public {
        supply b =fmr[ID];
        b.stat=rpf;
        fmr[ID]=b;
        emit getSupplier(msg.sender, b.NameoProd,b.QuantityoProd,b.MnfofProd,b.toSrv,b.stat,b.comments);
    }

    }

Q3. To get values back to a UI is it more feasible to get the data ID by ID or is it better to use events (what I am trying to achieve now). If I use events then when I call update I will get all the records from the events on page reload. I will have the old records as well as new record for that SupplyID. I can write a filter on the Dapp but can something else be done?

1
  • This post contains multiple questions. It'd be ideal if you split them into individual posts so you can choose the correct answer for each question you have individually.
    – ReyHaynes
    Apr 27, 2018 at 12:54

1 Answer 1

2

For events you only have event.watch() and event.get() (event() is equivalent to event.watch()).

  • event.get() will return existing events between the two blocks passed as parameters. A dapp can use it to rebuild the current state of the contract.

  • event.watch() will start listening for future events. A dapp can use it to keep the state up to date.

For the duplicated events it might be that you have two separate listeners watching events, or your contract is generating the two events.

Sometimes setting an event watch filtering with the user address can work. But it depends on the specifics of your design.

6
  • if I state a fromBlock:'pending', I will always get a return as to something that is about to be mined right, but not currently mined? Also lets say I made changes on specific SupplyID, updated the status or something, how do I get the updated record cause fromBlock:0 toBlock:'latest' will give me the old well as the new, can this be done only through a logical filter in the Dapp or something can be done from the smart contract PoV as well?
    – kjroger94
    Apr 29, 2018 at 8:17
  • The issue with relying on pending transactions is that it is very likely your node will not have all of them, and transaction will be added or removed all the time. The event system is quite limited it doesn't provide advanced queries or the possibility of modifying an already set watch. It only has basic support for more common use cases.
    – Ismael
    Apr 29, 2018 at 17:31
  • So to get the data properly, should I just write a getSupply func that just returns me the data according to the supply ID? But then I will have continously call for all one by one. And I can't return all by a loop in the smart contract because it is gas intensive. What do you suggest be done once data is committed, which is the most feasible way to get it.
    – kjroger94
    Apr 30, 2018 at 4:46
  • It depends on the problem you want to solve. For example in one of my dapps on startup I retrieve all past events and save the state in a local database, from that moment it watch for new events to keep the state updated. At the next startup it will read the local database and request only the missing blocks then setup to listen for new events. Since you are in a blockchain you need to be aware of possible chain reorgs.
    – Ismael
    Apr 30, 2018 at 6:19
  • Thanks a lot for your explanation, any source that I can read about chain reorg ? and how does one know that blocks are missing? Do you store the block number as well?
    – kjroger94
    Apr 30, 2018 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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