Retrieving contract - issues

Hi, @claudia_giannoni! It appears you are making great progress!

Let’s address this first:

I also do not understand from the documentation why I need and Helper template to call ‘fetchByKey’

I’m assuming you read this thread in which cocreature explained that fetchByKey cannot be called from a Daml Script.

In summary, fetchByKey can only be used in a choice. It cannot be used in a script. Consequently, if you want to write a script that retrieves a contract based on a key, you will need to first write a choice that calls fetchByKey. That choice needs to be on a template. You might as well name that template with “helper” in the name to clarify that its purpose is to help with some other template.

I also do not understand from the documentation … how to use it.

The general idea of fetching a contract by key is that the key does not change… even as a contract is updated. When a contract is updated, the contract id changes. If you have defined a key on a contract that stays the same even as the contract is changed, then you can always retrieve the “current version” of the asset.

However, notice that in your code, the UpdateAsset choice allows the idCode to be changed. You probably don’t want to do that. The idea with keys is that they don’t change. You probably want to remove the new_idCode parameter from your UpdateAsset choice, so that the idCode can never be changed after the contract is initially created.

Additionally, I see in your code that you have a choice named FetchAssetByKey on the Asset template. That’s a good start, but does not actually make sense. The reason it does not make sense is that to exercise that FetchAssetByKey choice, you would need to already have the contract id of the asset. If you already have the contract id of the asset, you do not need to fetch it by key.

So imagine you don’t know the contract id of an asset (because the asset has been updated a couple times since it was created and gets a new contract id on every update). But, you do know the key (the idCode in your example).

That is where the concept of a “helper” comes into play. You need a whole new AssetHelper template, with a FetchByKey choice. Here is a sample implementation:

template AssetHelper
  with
    caller : Party
  where
    signatory caller
    choice GetAssetByIdCode : (AssetId, Asset)
      with idCode : Text
      controller caller
      do
        fetchByKey @Asset (caller, idCode)

With the above defined, you can create new instances of the AssetHelper and exercise the GetAssetByIdCode choice whenever you want. Like this, for example:

(currentAssetId, currentAsset) <- submit cms do
  createAndExerciseCmd (AssetHelper cms) GetAssetByIdCode with
    idCode = "XX2325XXX"

Notice the above expressions returns the current asset that has the key XX2325XXX. You can place the above createAndExerciseCmd in your script after the asset is initially created and after the contract has been updated. In both cases, it will give you the “current” version of contract XX2325XXX.

Does that get you moving forward again?