In DAML Script, do we have equivalent command for lookupbykey, fetchbykey?

Hi @lour.mae.g.balbin, welcome to the forum!
Great question! DAML Script communicates via the gRPC Ledger API so it is limited to the 4 commands provided by that:

  1. createCmd
  2. exerciseCmd
  3. exerciseByKeyCmd
  4. createAndExerciseCmd.

However, that doesn’t stop you from doing other things. You can call arbitrary Update actions via createAndExerciseCmd. Let’s look at a simple example. Here’s a template with a key and a scenario that fetches that key:

template TKey
  with
    p : Party
  where
    signatory p
    key p : Party
    maintainer key

fetchByKeyScenario = scenario do
  p <- getParty "p"
  cid <- submit p (create (TKey p))
  r <- submit p (fetchByKey @TKey p)
  debug r
  assert (r._1 == cid)

To do the same in DAML Script, we first create a helper template with a choice that will fetch the contract with the given key:

template TKeyHelper
  with
    p : Party
  where
    signatory p
    choice FetchByKey : (ContractId TKey, TKey)
      with
        keyP : Party
      controller p
      do fetchByKey @TKey keyP

You can then call this FetchByKey choice using createAndExerciseCmd:

fetchByKeyScript = do
  p <- allocateParty "p"
  cid <- submit p (createCmd (TKey p))
  r <- submit p (createAndExerciseCmd (TKeyHelper p) (FetchByKey p))
  debug r
  assert (r._1 == cid)

Depending on your needs, another option is to create only one instance of this helper template and make FetchByKey non-consuming. You can then call the choice via a regular exerciseCmd or if you add a key, exerciseByKeyCmd.

3 Likes