Associated records on a ledger

I am new to DAML, and smart contracts in general. I am working on an application and am unsure about how to implement something which is probably quite simple…

In a product processing chain, a given product may go through various processing steps. Each of these steps involves the creation of a ProcessingRecord. It is required that these records are stored on the ledger. It is also required that, given a product, all processing records are associated.

I thought to do something like this:

template ProcessingRecord
  with
    processor : Party
    label : Text
    quantity : Decimal
    ...
  where
    signatory processor

template Product
  with
    owner : Party
    processingSteps : [ContractId ProcessingRecord]
    ...
  where
    signatory owner

Any help would be great. I am happy to provide clarification on the scenario.

1 Like

Hi @alex_m, welcome to the DAML community!

I think you are on the right track with this. To get the details right, you need to think about what the exact workflow is you want to enable, and what safeguards you want the smart contracts to have.

E.g.

  • Who creates a Product? Are the ProcessingRecord contracts created at the same time?
  • Does the list of Records change after initialization?
  • Who initiates and authorizes the addition of a new ProcessingRecord?
  • Do Records change after initialization?
  • Who is allowed to see which contracts? Do all processors see all Records? Do processors see the Product? Does the ownser see the Records?

Thank you for your reply. Here are the answers to your questions:

  • A Product entry is first created by a ‘producer’ who is assigned as the owner. The signatory is the owner so subsequent changes to the product are signed by the owner. Ownership can be transferred, but I know how to do that!
  • ProcessingRecord contracts are not created at the same time. They are created when the relevant processing step has been completed.
  • The list of records does change after initialisation because new processing steps may be added.
  • “Who initiates and authorizes the addition of a new ProcessingRecord?” : Honestly I am not sure of that yet but for the time being let’s say that it is the product owner that initiates and authorises. EDIT: The processing record is created/signed by the processor itself.
  • Records do not change after initialisation. I.e. a ProcessingRecord itself will not change after creation.
  • The owner can see all processing records and contracts. Each processor can only see their own processing steps.

Thank you!

@bernhard I am not expecting a full solution, but I would like to know: is the design pattern of having a list of ContractId is correct?

Given that they don’t change that’s OK. I’d probably go with contract keys just in case you start adding functionality that does change the Records though.

Okay. In my above implementation, would the Product owner be able to view the ProcessingRecord? I have just realised that the only stakeholder on that contract is the processor. How would I handle the case where I want the owner (which could vary) to always be able to view every record? @bernhard thank you

The easiest way would be to add the owner as an observer to the Record.

Thank you @bernhard, but what if the owner changes? Would that mean every record would have to be updated each time ownership is transferred?

If the new owner isn’t already an observer, I think so.

1 Like