Search code examples
daml

How to mention keys of a Map as observers in DAML?


I want to have a Map of (Party, CustomObj) as an attribute/argument in my Contract Template. Also I want to specify the list of keys as observers in the Contract Template. How can I achieve this?

Thanks.


Solution

  • observer accepts arbitrary expressions with the template arguments in scope. So you can write an expression that extracts the keys from your map. To do so, first convert to a list of (key, value) pairs and then use map fst to throw away the values. Here’s a full example:

    module Main where
    
    import DA.Next.Map (Map)
    import qualified DA.Next.Map as Map
    
    data CustomObj = CustomObj
      deriving (Eq, Show)
    
    template T
      with
        sig : Party
        m : Map Party CustomObj
      where
        signatory sig
        observer map fst (Map.toList m)