Fetching all the contracts of a specific template

I’m wondering whether there is a way to fetch all the contracts of a specific template in Daml (that are visible to the submitting party)?



1 Like

That depends a little bit of what you mean exactly by “in Daml”. Daml code can run in different contexts, and different actions are available depending on the context.

For example, in the context of a Daml Script, you can query for contracts of a given template using the Daml.Script.query* family of functions. In the context of a Daml Trigger, you can use similar Daml.Trigger.query* functions.

However, in the context of a Daml choice, you do not have any querying action available: you can only attempt to fetch contracts based on known contract IDs or contract keys.


Thanks for the quick reply!
I was considering the context of a choice, unfortunately.

1 Like

There are good reasons for preventing queries from choices. I’m not the best person to explain them, though.

1 Like

Doing any kind of global check transactionally in a distributed system is quite tricky. At best, you’re just creating a giant lock and ruin performance, at worst you’re just out of luck and can’t do that in certain setups.

Negative are one example of global assertions where you assert that no contract exists and that already fails in multi-domain Canton. Queries are similarly global properties (they’re not quite comparable since you have no concepts of maintainers outside of keys) and similarly challenging.