Best way to get count of active contracts for a specific template

Hello I wonder what is the best/easiest way to get the number of active contracts for a particular template.
Do we need to
1.write a grpc client (e.g. grpcurl) and get active contract set and increment the count for each contract?
2.use canton console participant.ledger_api.acs (the acs seems to be a sequence of size 1000 tho, so directly getting length, does not seem to work, if we have more than 1000 contracts?)
3. directly count from database (probably not recommended? as we are not supposed to query from database directly)

Or is there any better and faster way?

Thanks a lot!

3 is out for the reason you mentioned: The database should not be queried directly.

2 just calls the underlying gRPC internally so it’s just a variant of 1.

So in the end, querying it via gRPC is your only (and therefore also the best :)) option. The ACS endpoint gives you a stream so your client doesn’t need to keep the entire ACS in memory to do the counting. I would probably implement that using the Java bindings but any gRPC client will do.

1 Like

Hi @cocreature thanks so much for the note!
Just want to clarify about 2.
Understood that it’s using GRPC under the hood, yet when I do

participant.ledger_api.acs.of_party(myPartyId).length

I get 1000, even if in the DB I have more than 10k active contracts. (guessing the sequence was initialised with capacity of 1k)
Also when I try to filter by templateId, from the DB I can see that I have active contracts of template A, B, C, yet if I do get distinct templateIds on the acs as above in Canton Console I can only see A and B (which are the templates the first 1000 contracts on the ledger)
Is this an expected behaviour?

Yes that is expected. of_party takes a limit parameter which defaults to 1000. You can increase that if you want to get more.

Side note, as I don’t see it mentioned in this discussion: you can only get a count for the contracts of a particular template visible to a given Party (or possibly set of parties if you have a token with multiple parties).

It’s probably obvious from context but I thought it was worth mentioning explicitly: the Daml privacy model implies that it is impossible for a single party to know how many contracts there are in total (unless that Party can otherwise rely on being the only Party in the system, I suppose).

3 Likes