2

I am new to DAML. I am experimenting DAML for lifecycle management for financial instruments use case. One question comes to my mind is how do we extend the default Party interface? Currently I see that a Party can only have a name and id.

A lender/borrower can have its own properties like legal entity id, legal account number and corp id etc... How can we associate those details to a Party in this case lender or borrower?

Eric Zhu
  • 23
  • 3

1 Answers1

0

The way to model workflows in Daml is through templates for smart contracts. In your case you want to create role template for borrower, lender etc. In addition to storing the details you mentioned, a role contract allows to implement rights and obligations that the role should have. E.g. a borrower has the right to apply for a loan, which can be implemented using a nonconsuming choice on a Borrower Daml template.
A role contract can be associated with a Daml Party by way of a contract key. Contract key ensures uniqueness of the contract for the key (in the example below for the combination of system operator and borrower parties) and allows to fetch the contract from the ledger by key. Here's a quick example

template Borrower
  with
    sysOperator : Party
    borrower : Party
    fullName : Text
    lei : Text
    accNum : Int
  where
    signatory sysOperator, borrower
    key (sysOperator, borrower) : (Party, Party)
    maintainer key._1

    nonconsuming choice ApplyForLoan : ContractId LoanApplication
      with
        lender : Party
        amount : Decimal
        expiryDate : Date
      controller borrower 
      do
        create LoanApplication with ..
  • Thanks for the example Alex, the example really helps me understanding template modeling. I am still trying to get used to the maintainer key part. Let's say I have a contract which lender and borrower both are the signatories. How do I reference the borrower in this case? (borrower: Borrower?) – Eric Zhu Jul 26 '22 at 00:38
  • Also if I access useParty api on the ui side, will I be able to get back the details from the borrower? – Eric Zhu Jul 26 '22 at 00:52
  • @EricZhu, including a key in the contract is not mandatory. You only need to include the key if you want to be able to retrieve the contract by key or if you want to ensure the uniqueness of the contract for a given key. In my example the reason for the key I included in the Borrower template is to be able to retrieve the Borrower contract given the borrower party (and the system operator party). In other words with this key, if you have the borrower party, you can retrieve the details from the Borrower contract (like LEI etc.), assuming you have visibility of the contract. – Alex Putkov Jul 26 '22 at 09:25
  • In other words, you want to add a key to the contract when you need a stable reference to the contract and when your model implies that there can ever be only one active contract of a given template for the given key value. In my example the model assumes that any party can only have one associated Borrower contract in the marketplace. E.g. a party cannot have two active Borrower contracts in the same marketplace with say two different LEIs. In your example a contract that has both the lender and the borrower as signatories likely represents some kind of relationship between them. – Alex Putkov Jul 26 '22 at 09:42
  • And it's likely that there can be more than one such relationship. E.g. if the contract represents a loan, a borrower may have several loans from the same lender. So, adding a key to this contract may not be appropriate. This said, if you need to add a key to such contract, you can do it. A key could include any data type supported by Daml (Int, Decimal, Text etc.) although we generally recommend against using heavy data structures as keys for performance reasons. The maintainer however must be one or several parties. – Alex Putkov Jul 26 '22 at 09:50
  • Coming back to your question, I'm not sure I understand it. If borrower is a signatory (and signatory must be a Party), then using borrower party as a maintainer of the key is straightforward. You cause the same reference you use in the signatory clause. – Alex Putkov Jul 26 '22 at 09:59
  • Whether you want the borrower to be a maintainer of the key is a whole different question, which requires a detailed explanation for the role of the maintainer. I suggest you refer to the [documentation](https://docs.daml.com/2.0.0/daml/reference/contract-keys.html?_ga=2.80362004.209375909.1658733464-2106970549.1651592805&_gl=1*lqkxsv*_ga*MjEwNjk3MDU0OS4xNjUxNTkyODA1*_ga_GVK9ZHZSMR*MTY1ODgyNzEyNS4yOS4xLjE2NTg4MjkzMjcuMA..#specifying-maintainers) in the first instance. – Alex Putkov Jul 26 '22 at 10:00
  • Finally, I'm also not sure what you mean by "useParty api on the ui side". If you use Daml HTTP JSON API in the UI, you can certainly fetch the Borrower contract from the ledger and view the contract's payload provided the user that submits the request has visibility of the contract. E.g. if you use the [integration with the React framework](https://docs.daml.com/app-dev/bindings-ts/index.html) that Digital Asset provides, you can call [useFetchByKey](https://docs.daml.com/app-dev/bindings-ts/daml-react/modules.html#useFetchByKey) function to fetch a contract from the ledger by key. – Alex Putkov Jul 26 '22 at 10:12