Getting all active contracts w/o filtering by template id with a Java integration

Is it possible to get all active contracts with a Java integration, without explicitly listing all the template ids contained in the Daml model?

If I understand correctly in the Active Contracts Service this is not possible.

If you specify an empty set of template ids in your filter you will get back all contracts the party is a stakeholder on regardless of the template.

1 Like

How can I use InclusiveFilter instead of ContractFilter?

I’m trying to make work the Daml IOU Quickstart Tutorial example.

I have created emptyFilter as an InclusiveFilter instance, but I cannot use it as an argument to getActiveContracts because of the type mismatch.

Type casting doesn’t seem to work because ContractFilter is parametric in the contract type.

    Set<Identifier> emptyIdentifierSet = emptySet();
    InclusiveFilter emptyFilter = ofTemplateIds(emptyIdentifierSet);

    client
        .getActiveContractSetClient()
        .getActiveContracts(emptyFilter, Collections.singleton(party), true) // THIS DOESN'T WORK
//        .getActiveContracts(Iou.contractFilter(), Collections.singleton(party), true)

You need to use the more generic version of getActiveContracts here that accepts a TransactionFilter.

That TransactionFilter can then be a [FiltersByParty] and you specify the inclusive filter as the Filter for your party there.

1 Like

I see how it can work with an empty set of template ids, but I see that the ‘Identifier’ Object has three @NonNull String values: packageId, moduleName and entityName. Is it possible if I want to specify only the packageId, and leave the moduleName and entityName blank? I see that these should all be @NonNull value, so if possible, would I need to put an empty String here? The intention here would be to get all active contracts based on packageId.

That’s not possible at the moment, if you specify a template id on the grpc api you need to specify all 3 and empty strings are not allowed.

As part of the improvements to Daml upgrading, this will be relaxed and you will be able to omit the package id but there is no timeline for that yet.

2 Likes

Thanks, it seems to work.

Could you also help with how I can get from a Flowable<GetActiveContractsResponse> value a Flowable<ActiveContracts<Contract>> value which is required by the rest of the example code?

The return type of the original getActiveContracts(Iou.contractFilter(), Collections.singleton(party), true) is Flowable<ActiveContracts<Contract>>, but the return type of getActiveContracts(emptyTransactionFilter, true) is Flowable<GetActiveContractsResponse>.

So after just changing the getActiveContracts input type, I get type mismatch later:

Set<Identifier> emptyIdentifierSet = emptySet();
InclusiveFilter emptyFilter = ofTemplateIds(emptyIdentifierSet);
TransactionFilter emptyTransactionFilter = new FiltersByParty(Map.of(party,emptyFilter));

client
    .getActiveContractSetClient()
    .getActiveContracts(emptyTransactionFilter, true)
//        .getActiveContracts(Iou.contractFilter(), Collections.singleton(party), true)
    .blockingForEach(
        response -> { // Errors in the closure
          response.offset.ifPresent(offset -> acsOffset.set(new LedgerOffset.Absolute(offset)));
          response.activeContracts.forEach(
              contract -> {
                long id = idCounter.getAndIncrement();
                contracts.put(id, contract.data);
                idMap.put(id, contract.id);
              });
        });

You can take a look how the conversion is implemented here https://github.com/digital-asset/daml/blob/6a9168e1cf6c14dfd4f290c01ebc5aaf1fe98b1a/language-support/java/bindings-rxjava/src/main/java/com/daml/ledger/rxjava/grpc/ActiveContractClientImpl.java#L72