Mapping through Parties
- ChenavOP 2
Hello DAML people,
I apologize for the straightforward question, yet, I’m not very good at Haskell nor Mapping functions.
Assume that I have the Template below;
template Agreement with signatories: [Party] observers : [Party] where observer observers signatory signatories ensure unique signatoriesI want to have only chosen parties as signatories or observers. Therefore I need some kind of mapping through them. It might be something similar to this:
ensure signatories `elem` mapOptional partyFromText ["Jean", "Pierre", "Paul"]In order to iterate through each single party in “signatories” or “observers” lists in a way that these parties are assured to be one of the named parties who are specified explicitly there.
In the end, No signatory or observer that is not specified in the given list can be added to signatories or observers. Thank you.
- Stephen 2
By default you have an
allfunction, which can be used like this:ensure all (\p -> ...) signatories -- fill in for ...The problem you have with
elemis that the left-hand argument in your example must be aParty, but your expression passes a[Party], right? Well, usingallabove,phas typeParty, and...is aBoolexpression. Not coincidentally,elemreturns aBoolitself, and if you check the docs forall, it precisely matches up with your “no [element] that is [not matching the given predicate function] can [occur in the list]” requirement.Assuming you want to check both signatories and observers against the same list, you can append them together like
(signatories <> observers)to check both lists at once. But maybe you mean to check against separate approved-party lists. - Chenav
Thank you very much @Stephen. It works as its expected.
- Gary_Verhaegen 2
I feel compelled to note that, while Stephen’s answer does explain how to make this work, this is actually not something you should do in most Daml systems.
Hard-coding party identifiers is a really bad idea. If you want to restrict what parties can do, it’s much better to do that through contracts on the ledger.
- Chenav
Thank you for the alert @Gary. Yes, you’re definitely right. I already changed it.