Java gRPC - Get Contract By ContractId

Hi All,

I’m trying to fetch contract by Id using Java gRPC. Can anyone help me understand what is the difference between TransactionFilter & InclusiveFilter


TransactionServiceStub transactionService = TransactionServiceGrpc.newStub(channel);

		GetTransactionsRequest transactionsRequest = GetTransactionsRequest.newBuilder()
				.setLedgerId(grpcCommonObj.ledgerId)
				.setBegin(LedgerOffset.newBuilder().setBoundary(LedgerOffset.LedgerBoundary.LEDGER_BEGIN))
				.setFilter(TransactionFilter.newBuilder().putFiltersByParty(user, Filters.getDefaultInstance()))
				.build();

The TransactionFilter is an object that holds a generic predicate that you can use to filter flat transactions (i.e. contract state changes). Within it, you can have one of several possible predicates. In practice, right now this boils down to either NoFilter or an instance of InclusiveFilter, which specifies a positive predicate, in particular with regards to a set of template IDs. Note that you can filter by template ID only when querying for flat transactions (as you are doing in your snippet) but not for transaction trees (where the Ledger API querying capabilities are limited to specifying parties on behalf of which you’re reading).

1 Like

Thanks for the explanation @stefanobaghino-da.

But I’m still unable to achieve the filtering of contracts by Id. Any help with the code is appreciated.

Thank you!!

There is no server side filtering by contract id via the gRPC API. You have to fetch all contracts of a given template and filter on the client side. If you do that frequently, you can keep a custom ods and index by contract id to make that more efficient.

1 Like

Thank you @cocreature