useQuery in the @daml/react library, forces me to pass in a template

I need to retrieve all contracts visible to a party on the ledger using the recommended path (which means getting @daml/react from npm), but the only suitable function available there is useQuery, which forces me to pass in a contract template thereby restricting the result to contracts of that type.

Previously this could be accomplished as follows, where the ledger context was part of the source code:

import { useLedgerState, getContracts } from '../../context/LedgerContext';

function Default() {

  const ledger = useLedgerState();
  const allContracts = getContracts(ledger);

How is this achieved now?


I do not know how to achieve this with @daml/react (cc @Martin_Huschenbett, @shaynefletcher).

However it seems like it is by design: useQuery takes a Template as an argument, allowing to query only contracts of the specified template. The QueryResult is strongly typed, it needs to know the exact Template type to be able to deserialize the contracts returned by the query.

In any case there is the JSON API endpoint that might be useful:

URL: /v1/query
Method: GET
List all currently active contracts for all known templates.

1 Like

Hi @chris.norris,

@Leonid_Shlyapnikov is absolutely right. The @daml/react library is designed with strong static-typying in mind and for use cases where the structure of the templates on the ledger is known during development of the UI. Any forms of meta-programming are beyond the scope of this library.

As @Leonid_Shlyapnikov also pointed out, using the JSON API directly is currently your only way to get hold of all active contracts.

Iā€™m not sure what library your code snippet is referring to. This is definitely not an old version of @daml/react.


Thanks for the guidance! The snippet is taken from before the use of daml/react, where the context file was provided as part of the scaffolding.