Skip to content
Discussions/App Development/Best way to get count of active contracts for a specific templateForum ↗

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

App Development9 posts481 views6 likesLast activity Feb 2025
JU
Judy_WuOP
Dec 2022

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!

CO
cocreature
Dec 2022

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.

JU
Judy_Wu
Dec 2022

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?

CO
cocreature
Dec 2022

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

GA
Gary_Verhaegen
Dec 2022

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).

BA
bartcant
Feb 2023

Is there an easy way to use postman to query grpc for the ledger api ?

Postman Blog – 13 Jan 22

Postman Now Supports gRPC | Postman Blog

We are excited to announce first-class support for gRPC in Postman. Learn how to get started today.

Est. reading time: 5 minutes

WA
WallaceKelly
Feb 2023

See Using Postman to Explore the Ledger API

ST
Stephen
Feb 2023

Please create separate topics for new questions.

WA
WallaceKelly
Feb 2025
Judy_Wu:

I wonder what is the best/easiest way to get the number of active contracts for a particular template.

The PQS and Daml Shell can give you this information.

(HT: @Drew_Harding)

← Back to Discussions