How to run a trigger only by using the hearbeat?

G-d willing

How can I make sure that my trigger will be executed ONLY using the heartbeat?
If I set the registeredTemplatesto [] it acts like AllInDar.

Thanks

Hi @cohen.avraham,

You cannot control execution of your trigger rule to only happen upon receiving a Heartbeat message, you can however implement your rule to only do your intended operation when a heartbeat message was received using user defined trigger state. I came up with this naive example to demonstrate:

template HeartbeatResult
  with
    p : Party
    ts : Time
  where
    signatory p

data HeartbeatState = HeartbeatState {
    heartbeatsReceived : Int
}

heartbeatTrigger: Trigger HeartbeatState 
heartbeatTrigger = Trigger with
  initialize = pure $ HeartbeatState 0
  updateState = heartbeatUpdateState
  rule = heartbeatRule
  registeredTemplates = AllInDar
  heartbeat = Some (seconds 5)

registerHeartbeat : HeartbeatState -> HeartbeatState
registerHeartbeat hs = do
  hs with heartbeatsReceived = hs.heartbeatsReceived + 1
  

heartbeatUpdateState : Message -> TriggerUpdateA HeartbeatState ()
heartbeatUpdateState m = case m of 
  (MHeartbeat) -> modify $ registerHeartbeat
  _            -> pure()

heartbeatRule : Party -> TriggerA HeartbeatState ()
heartbeatRule p = do
  hs <- get
  ts <- getTime
  heartbeats <- query @HeartbeatResult
  debug $ "HeartbeatTrigger: " <> show ts <> " Current number of HeartbeatResults: " <> show (length heartbeats) 
  if hs.heartbeatsReceived > 0 
  then do
    debug $ "HeartbeatTrigger: " <> show ts <> " Triggered by heartbeat message, creating HeartbeatResult."
    dedupCreate $ HeartbeatResult with ..
    put $ HeartbeatState (hs.heartbeatsReceived - 1)
  else do
    debug $ "HeartbeatTrigger: " <> show ts <> " Triggered by something else, not creating HeartbeatResult."
    put $ HeartbeatState 0

Additionally, please note that the registeredTemplates also limits which templates from the ACS your trigger can query, so having an empty set would make it impossible to fulfill the main aim of triggers, to react to ledger events.