Launch trigger

I have this trigger that acts when there is an event of the Distributions contract, the first thing I do is retrieve all of them and then filter the one with the triggered=false.

My question is, within subscribeRule there is some way to have the information of the contract that has caused the trigger to be launched, the contractId or the payload or something that can identify the contract that causes the trigger ?

subscriber : Trigger ()

subscriber = Trigger

  { initialize = pure ()

  , updateState = \_ -> pure ()

  , rule = subscriberRule

  , registeredTemplates = RegisteredTemplates [ registeredTemplate @Distributions]

  , heartbeat = None

  }

 

 

subscriberRule : Party -> TriggerA () ()

subscriberRule party = do

 

  distributions <- query @Distributions

  let newsDistSub = filter (\(_, s) -> (s.triggered == false)) distributions

  forA_ newsDistSub $ \(cid, distribution) -> do

    dedupExercise cid MarkTriggered

    pure()

  pure()

the line
distributions <- query @Distributions should return a tuple cid (contractID), contract
within the forA_ loop, you can see (cid,distribution), the distribution is the contract, with the data, so you can access the fields there, for example distribution.issuer, if issuer was a field there.

Hi @DavidBelinchon,

This is not the right way to think about triggers. Despite the name, they are not triggered by a specific contract event and designing them that way will lead to issues.

You should instead think of them as rules that enforce invariants on the ledger. What triggers the rule is incidental; what matters is that after the rule has run, the invariants have been enforced and hold for all data on the ledger.

If you want a specific choice to trigger a specific consequence, that consequence should simply be part of the choice’s body.

1 Like

I think this is a nice example of thinking in a declarative, rather than procedural way.