Fetch by multiple contract keys
- alex_mOP 2
Let’s say you have a contract template
template Thing with description: Text smallThingKeys: [SmallThingKey] ...where
SmallThingKeyis a contract key to aSmallThing.ThingandSmallThingcontain data that is to be shown in the user interface. When fetching theSmallThings, is it possible to submit a single query (containing all thesmallThingKeys) and receive theSmallThings in one result? I know this can be done by making multiple separate queries, but this would not be ideal given that I am trying to render a table of manyThings.Thanks again for the help.
Alex
(Ideal solution would use the
@daml/ledgerJS library. That being said, I am considering writing some Java middleware.) - Leonid_Shlyapnikov
Hi @alex_m !
Multiple contract keys per request are not supported in the
fetchByKeyfunction.@daml/ledgerJS library uses JSON API.JSON API does not support multiple keys in the
Fetch Contract by Keyrequest (/v1/fetch):
https://docs.daml.com/json-api/index.html#fetch-contract-by-keyPlease submit a JSON API feature request in here: HTTP JSON API Maintenance Milestone · GitHub
- Leonid_Shlyapnikov 1
Have you considered using Streaming API?
See the docs:
Streaming API (
/v1/stream/fetch) lets you specify multiple keys per request:[ {"templateId": "Account:Account", "key": {"_1": "Alice", "_2": "abc123"}}, {"templateId": "Account:Account", "key": {"_1": "Alice", "_2": "def345"}} ] - alex_m
Cheers @Leonid_Shlyapnikov, I’ll give this a try tomorrow!
- Luciano
Here’s an idea! Try this: write a choice like so
controller c can LookupMany : Update [ (ContractId SmallThing, SmallThing) ] with smallThingKeys = do mapA fetchByKey smallThingKeys(n.b. untested)
This uses
mapAfromTraversable. It allows you to sequence a[ Update ](i.e. your key lookups) into anUpdate []. This means, that the transaction will fail in whole if one of the lookups fail; otherwise it will return all the results in a list.I hope I’ve understood your question correctly.
Let me know if this helps!
- bernhard 1
Something like that would work, but requires a ledger round-trip. A fetch from the JSON API is much faster, probably an order of magnitude.
- alex_m 2
Leonid_Shlyapnikov:Please submit a JSON API feature request in here: HTTP JSON API Maintenance Milestone · GitHub
@Leonid_Shlyapnikov I have submitted a feature request: Fetch API - Fetch by multiple contract keys · Issue #7047 · digital-asset/daml · GitHub
Thanks for the help everyone, I take it there is no way to do this by standard fetch ATM.
@Luciano so execute a choice when wanting to view all the
SmallThings? Looks interesting.TBH ideal solution would be a fetch by multiple contract keys; not stream because I don’t think it’d be good to open many web sockets for a table of many
Things.