Launch trigger
- DavidBelinchonOP
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() - rikotacards
the line
distributions <- query @Distributionsshould return a tuplecid (contractID), contract
within theforA_loop, you can see(cid,distribution), thedistributionis the contract, with the data, so you can access the fields there, for exampledistribution.issuer, if issuer was a field there. - Gary_Verhaegen 1
DavidBelinchon: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 ?
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.
- gyorgybalazsi
Gary_Verhaegen:You should instead think of them as rules that enforce invariants on the ledger.
I think this is a nice example of thinking in a declarative, rather than procedural way.