Implementing scheduled and recurring actions in Daml 3.5
- nigam_archanaOP
Hi Team,
I am using Daml 3.5 on Canton Network.
I have a use case where certain actions need to be executed automatically when a specified time is reached.
Examples:
Close all open trades at End-Of-Day (EOD).
Execute periodic interest payments every month.
Trigger a settlement process when a contract’s maturity date is reached.I understand that choices are normally exercised by a party/application. However, I am looking for a mechanism that can initiate these actions automatically based on time.
Questions:
Is there a built-in scheduler available in Daml 3.5?
Are Daml Triggers still supported/recommended?
What is the recommended pattern for implementing one-time and recurring scheduled actions?
Should this logic be implemented using an external scheduler (e.g., Kubernetes CronJob, Quartz Scheduler, Temporal, Airflow) that invokes Ledger API commands?Any guidance or sample implementations would be appreciated.
- pmjpawelec
Hi Nigam,
There is indeed no built-in scheduler in Daml 3.5. All recurring actions need an external automation that calls the Ledger API. The documentation has a note on automation that might interest you: link. Daml Triggers were deprecated in Daml 2.x and are not part of Daml 3.
All technologies you listed can be used to call Ledger API. The important part is smart contract design to ensure no double runs happen, no out of schedule executions, or other desired properties. Daml has a time API that can aid with on-ledger validation, see additional notes here.
Here is an example template:
template RecurringAction with operator : Party scheduleId : Text occurrence : Int dueAt : Time nextDueAt : Optional Time where signatory operator choice Run : () controller operator do assertDeadlineExceeded "Action is not due yet" dueAt -- Action code goes here forA_ nextDueAt $ \next -> create this with occurrence = occurrence + 1 dueAt = next nextDueAt = determineNextDueAt nextThe important bits:
Runis a consuming choice, so each occurrence can succeed only once. It does not prevent duplicate attempts, so keep off-ledger side effects idempotent.assertDeadlineExceededis a function from Daml time API that blocks early runs.assertWithinDeadlinecan handle late runs the same way.determineNextDueAtneeds to be implemented according to your setup and requirements. It might require some subtlety so that your scheduling software will remain in-sync with this contract. For example, do not hardcode adding 24 hours for daily tasks, calculate the right time for the next execution instead. For terminating or one-time actions set thenextDueAttoNone.Hope this helps. Happy to expand on this if something is still unclear.
- orumedominic
Two things I’d add from shipping this.
A ceiling as well as a floor. Blocking an early run is easy. A run a week
Late because the scheduler was down is the one that hurts.And a count that runs out, so the schedule ends itself.
Archana—when it fires at the wrong time, the transaction is discarded, so
Nothing on the ledger says it was tried.Templates and tests:
github.comcantor8_kya_rails/step-0-refusal at main · dominicrume/cantor8_kya_rails
Records what an AI agent was REFUSED, not only what it did. Spending rules enforced in a Daml choice body; every attempt, blocked ones included, becomes a hash-chained receipt an auditor can verify...