I’ve checked some topics here in daml discuss but was not able to see the one i’m looking for so I’m creating this one here.
I’ve checked on some sample DAML Scripts I can find here in daml discuss, as well as the daml docs’ daml script library page (daml docs > daml script library) but was not able to see equivalent command we can use for lookupbykey and fetchbykey.
I can only see “query” which results in all the active contracts available for a queried template and party.
Do we have equivalent command for lookupbykey, fetchbykey in daml script?
If not, do we have any reason why?
Hoping for your response. Thank you.
Just wondering this as we can use the said commands via DAML Scenario. And that we are trying to convert a created DAML Scenario into a DAML Script. One scenario we have uses those commands. So we are trying to figure out if there is a direct way to do the said commands via DAML Script too.
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:
createCmd
exerciseCmd
exerciseByKeyCmd
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.
Hey @cocreature is this a design decision, or is the ultimate goal for daml-script to have feature-parity with scenarios, so as to replace them completely?
@Luciano DAML Script is limited by what the Ledger API provides. At this point it only provides those 4 commands. We do plan to extend DAML Script so that it can be used in the IDE. However, while it would be possible to add more commands there in principle, I don’t think this is a good idea. The fact that you can do more in a scenario than you can do via the API is pretty confusing and we have seen several examples where it has lead to models that were usable in a scenario but were not usable via the ledger API.