Testing fetchByKey

First, let’s address what it would take to get your code to compile. Secondly, we can address the overall design.


Your code does not compile as-is. The problem is in the test. Specifically the following does not work:

    submit cms do 
        createCmd SellAsset with
            
            asset = asset.Asset
            newOwnerSolicitor = mishconDeReya
            newOwner = silvia
            notes = "Questa villa e' stata venduta"

The above does not work because asset is not defined in this script and so asset.Asset fails. To get the test to work with the current template design, you could create an Asset contract first before trying to create the SellAsset.

Example implementation
module Main_I where
import Asset 
import SellAsset

import Daml.Script

sell_asset_test : Script SellAssetId
sell_asset_test = script do

    cms <- allocatePartyWithHint "CMS" (PartyIdHint "CMS")   
    claudia <- allocatePartyWithHint "Claudia" (PartyIdHint "Claudia")  
    christie<- allocatePartyWithHint "Christie" (PartyIdHint "Christie") 
    claudia_Budha <- allocatePartyWithHint "Claudia_Budha" (PartyIdHint "Claudia_Budha") 
    mishconDeReya <- allocatePartyWithHint "MishconDeReya" (PartyIdHint "MishconDeReya")   
    silvia <- allocatePartyWithHint "Silvia" (PartyIdHint "Silvia")  
    time <- getTime

    assetId <- submit claudia do
        createCmd Asset with
          ownerSolicitor = claudia
          owner = claudia
          auctionHouse = christie
          address = "1234 Address"
          idCode = "Asset 2345" 
          a_description = "Asset 2345 description"
          a_documents = []
          a_docLink = ""
          v_description = ""
          v_documents = []
          v_docLink = ""
          date = time
    
    Some asset <- queryContractId claudia assetId

    sellAsset <- submit claudia do 
        createCmd SellAsset with
            asset = asset
            newOwnerSolicitor = mishconDeReya
            newOwner = silvia
            notes = "Questa villa e' stata venduta"
    
    submit claudia do
      exerciseCmd sellAsset AssetDocOffer_GetAsset
    
    return sellAsset

However, you might want to take another look at the overall design. Your SellAsset template includes a member of type Asset. That means, the data for the Asset is stored directly into your SellAsset template. Typically, you would have an Asset contract on the ledger. And then when you create the SellAsset, you would not include the entire Asset instance within the SellAsset. Instead you would reference the asset with either:

  • a ContractId Asset, in which case you could retrieve the Asset with fetch.
  • the values used by the Asset’s key (for example, idCode), in which case you could retrieve the Asset with fetchByKey.

Here is a start at what SellAsset would look like, referencing the Asset by contract id:

template SellAsset
    with
        -- asset : Asset
        assetId : ContractId Asset
        ownerSolicitor : Party
        owner : Party
        auctionHouse : Party

        newOwnerSolicitor:Party
        newOwner:Party
        notes: Text

With the above, you would get the Asset using fetch.

Here is a start at what SellAsset would look like, referencing the Asset by key:

template SellAsset
    with
        -- asset : Asset
        idCode : Text
        ownerSolicitor : Party
        owner : Party
        auctionHouse : Party

        newOwnerSolicitor:Party
        newOwner:Party
        notes: Text

With the above, you would get the Asset using fetchByKey.

I hope that is helpful.