Home > Blockchain >  daml - how to fetch contract by key using Java bindings?
daml - how to fetch contract by key using Java bindings?

Time:09-08

I have Daml template with key defined. I'm building a Java service that tries to access Daml ledger using bindings-rxjava. The access pattern is a simple CRUD, and I'm stuck on R(ead) part.

I'm not able to find how to issue fetchByKey request using Java. I've looked through examples and see how it's done in @daml/react example here. It uses JSON-API, and it's /v1/fetch endpoint. But I don't see similar requests in quickstart-java and ex-java-bindings repos. Also, both examples don't have a template with key defined.

The only working solution I can think of (based on the examples above) is to subscribe to ActiveContractsClient.getActiveContracts for my template, then store received contracts in DB and fetch by key from DB. But that sounds overly complicated comparing to JS examples.

Please, point me towards a working example for fetching by key using Java bindings. I'm okay with any JVM language (Java, Kotlin, Scala, Clojure, Groovy).

I'm also okay with calling JSON-API node from Java, but don't see any bindings for this. Am I stuck with manually constructing the JSON request in this case?

CodePudding user response:

"store received contracts in DB and fetch by key from DB" is how the JSON API provides the fetch-by-key functionality, when you have a DB configured. When you don't have a DB configured, it scans all of getActiveContracts until it finds a match. It does not have a quick-and-easy way to fetch by key itself, which is also why no such API exists in the Java bindings.

I recommend following the approach you've hit upon, with the qualifier that you should store ledger offset as well, and use the offset with the transaction stream to update the DB. (This is also how JSON API updates its database, rather than starting with getActiveContracts from scratch.)

  • Related