Skip to content
Discussions/App Development/FetchByKey in Java SDK?Forum ↗

FetchByKey in Java SDK?

App Development5 posts499 views12 likesLast activity Dec 2020
JE
Jean_SafarOP
Dec 2020

Hi,

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

CO
cocreature
Dec 2020

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
  with
    p : Party
    v : Int
  where
    signatory p
    key (p, v): TKeyType
    maintainer key._1

type TKeyType = (Party, Int)

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

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

JE
Jean_Safar
Dec 2020

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?

EN
entzik
Dec 2020

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.

JE
Jean_Safar
Dec 2020

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

Merry Christmas and Happy New Year to all !!!

← Back to Discussions