Fabric-sdk-jAVA #fabric-sdk-java


In the chaincode,an event is emitted.
In the blockdata from the blockevent listener,the event data is visible.
Is it possible to extract just the event data from the blockdata?

Mark Lewis

You can add a chaincode / contract listener instead of a block listener and receive only the chaincode events. Use addContractListener() method on your Contract object:


If using Fabric v2.4 (or later) the Fabric Gateway client API provides an equivalent getChaincodeEvents() method on the Network object:



data appears like this:


this is the event:CreatePrivateAssetEvent
I want to extract this


is it possible to extract this from the blockevent itself?

Mark Lewis

The ContractEvent (fabric-gateway-java) and ChaincodeEvent (Fabric Gateway client API) that get delivered to you by contract/chaincode event listening both have getPayload() methods. Similar to the values returned by transaction invocation, these are binary (byte[]) payloads containing whatever data your smart contract put there. If it's string data you might want to turn it back into a string to view it. If it's JSON data, you might want to use a JSON parser.

If you really want to, you can extract the chaincode events from block events yourself. I would suggest looking at the fabric-gateway-java source to see how this is done:


The chaincode event listening provided by the Fabric Gateway client API actually uses a new service to directly retrieve only the chaincode events from the peer, precisely to avoid the overhead of retrieving the large amount of additional data contained in the block event, and the work of parsing that extremely complex data structure to extract the chaincode event at the client end.