Great blogpost @Nishchal_Vashisht! I took the liberty to write up a DAML Trigger version of your example to see how it compares. Very interest to hear your thoughts! Note that it does behave slightly differently (intentionally). In particular, it only archives proposals between the same parties and for the same model and it also makes sure to not try to archive proposals where queryParty
and company
are reversed which would fail.
module Trigger where
import DA.Action
import DA.Foldable
import DA.Next.Map (Map)
import Daml.Trigger
import Main
archiveTrigger : Trigger () = Trigger with
initialize = \_ -> ()
updateState = \_ _ () -> ()
rule = archiveRule
registeredTemplates = AllInDar
heartbeat = None
archiveRule : Party -> ACS -> Time -> Map CommandId [Command] -> () -> TriggerA ()
archiveRule p acs _ _ _ = do
let proposals = getContracts @CarProposal acs
let finalAgreements = getContracts @FinalAgreement acs
forA_ finalAgreements $ \(_cid, c) -> do
-- Only match on agreements where we are the user
when (c.user == p) $ do
-- Filter out proposals for the same model between the same parties
let redundantProposals =
filter
(\(_, proposal) ->
proposal.companyName == c.company &&
proposal.queryParty == c.user &&
proposal.carModel == c.model)
proposals
forA_ redundantProposals $ \(cid, _) ->
emitCommands [exerciseCmd cid RejectOffer] [toAnyContractId cid]
If you get a chance to try out triggers, let us know how it goes!