FetchByKey in Java SDK?


Apologies for the trivial question, but I can’t seem to find a java equivalent to fetchByKey of a template neither in the code generated nor in the SDK itself. Is there way to do this?

Best regards

1 Like

The Java bindings just reflect the gRPC Ledger API here so they abide by the same limitations as e.g. DAML Script. For command submissions you have 4 different commands:

  1. create
  2. exercise
  3. createAndExercise
  4. exerciseByKey

fetchByKey is not one of them so you cannot call it directly. What you can do is to define a helper template and call that via createAndExercise:

template T
    p : Party
    v : Int
    signatory p
    key (p, v): TKeyType
    maintainer key._1

type TKeyType = (Party, Int)

template Helper
    p : Party
    signatory p
    choice FetchByKey : T
        key : TKeyType
      controller p
      do fetchByKey @T key

Alternatively you can query the ACS endpoint and filter by key on the client side.


Thanks a lot @cocreature!

this should however be fixed no? Should it really be the app developer to go and write helper functions for such a common task?


Writing a template for that is indeed not a practical solution.

While it solves the issue, it is, as @Jean_Safar mentioned, a waste of developer time, it adds to project complexity and maintenance cost, it’s a waste of ledger storage and it takes a percentage out of the actual transaction throughput as we have to issue two transactions in order to perform what is essentially a read on the ledger.

Not to talk about the fact that conceptually, a read on any system should have no side effects on the state of that system, and with this solution, a contract is created and archived so side effect there is.

+1 to add the capability in the gRPC API.


+1 but @entzik, it is a work around that is wasteful not a solution ! :wink:

Merry Christmas and Happy New Year to all !!!