0

I was going through events in solidity and how to catch them in my web3J (web3 java), my requirement was to get all the latest transaction that is being added in the blocks, for that from the documentation I came across transaction observable. The code is as under

Observable<BigInteger> transactionValue = web3j.transactionObservable()
            .take(COUNT)
            .map(Transaction::getValue)
            .reduce(BigInteger.ZERO, BigInteger::add);

Understanding so far: transactionValue is observable for getting notified for all the new transaction that is being added in blocks throughout the network, take operator only take first COUNT transaction, map operator map the result to only transaction value , but I don't have any idea what reduce is doing here?

Can any one explain that?

  • In case someone else is also confused with above code then I got the answer, reduce operator of Rx observable is used to reduce the result into a single element for example above code is taking down COUNT transactions value and summing them all up using reduce operator. For example run this code Integer soruce[]={1,2,3,4,5}; Observable<Integer> observable=rx.Observable.from(soruce) .reduce(0,(total,a)-> total+a); observable.subscribe(System.out::print); – Thinker Apr 8 '18 at 15:38

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.