Dheeraj Kumar
Dheeraj Kumar

Reputation: 410

How to Emit events from the Smart Contract (Chaincode) using the Node fabric-contract-api?

I tried the commercial-paper Tutorial from the Fabric-1.4 docs. Everything works fine.

Now I want to emit events from the papercontract.js, let's say in the "Issue" Transaction.

Is there an emit event functionality in the fabric-contract-api, which I can use or do I have to use fabric-shim methods to emit events?

Upvotes: 0

Views: 1019

Answers (1)

R Thatcher
R Thatcher

Reputation: 5570

You would use the setEvent method from the chaincode stub: https://fabric-shim.github.io/release-1.4/fabric-shim.ChaincodeStub.html#setEvent__anchor

and used as follows:

        // Emit the tradeEvent - passing the whole Commodity Object as the Payload.
        ctx.stub.setEvent('tradeEvent', Buffer.from(JSON.stringify(commodity)));

From a client perspective consuming the events, there is a JIRA currently being worked on to simplify the Event Handling, in the same way the submitting transactions has been simplified in 1.4. https://jira.hyperledger.org/browse/FABN-1100

The Event Handling JIRA is expected to be delivered in Fabric 1.4.1 (and in Fabric 2.0).

Upvotes: 1

Related Questions