0

I'm using the fabcar project: https://github.com/IBM/blockchain-application-using-fabric-java-sdk

I would like to know if there is a way to get the transaction history with a certain key (Not just querying a simple car o listing all of them.

Thank you.

2 Answers2

2

Yes, you have an API called GetHistoryForKey()

You can read more here : How to fetch asset modification history in hyperledger fabric

1

Here is the process which I have tried and got the result. I have implemented below code in my chaincode.

private Response getQueryHistory(ChaincodeStub chaincodeStub, List<String> args) {
        // method for getting the history for key
        QueryResultsIterator<KeyModification> queryResults = chaincodeStub.getHistoryForKey(args.get(0));
        return newSuccessResponse(prepareJsonFromQueryResult(queryResults));
    }


 private String prepareJsonFromQueryResult(QueryResultsIterator<KeyModification> queryResults) {

        // here build your json object from query result

        return "your json object";
    }

There are several already implemented method of ChaincodeStub For more information of method, have look https://hyperledger.github.io/fabric-chaincode-java/release-1.4/api/org/hyperledger/fabric/shim/ChaincodeStub.html

R.A.Munna
  • 1,699
  • 1
  • 15
  • 29