Is it possible to add some events to a template? For example, whenever a contract of a specific template is created, a choice within this template gets exercised (similar to constructors in Object Oriented) or even links it to an existing function (the same way it is being done in Triggers).
I have already been in several situations when this type of feature was needed. For now, I am solving it using triggers, but it is not always a doable solution.
There is nothing built into that. Daml contracts are fundamentally passive: Until commands get submitted through the ledger API nothing changes. I don’t expect us to lift that restriction. Can you explain why triggers (or other types of automation) are not always a workable solution? Maybe the problem is that automation is harder to write or orchestrate rather than the need for automation itself?
It’s hard to say without more details, but if you want something to happen on creation, you can sometimes use CreateAndExercise on a related template rather than creating the target contract directly. (Or just plain Exercise on a long-lived “creator” contract.)
Note that there is no way to completely enforce that template A can only be created through a CreateAndExercise on template B.
Yes, I am aware of the
CreateAndExercise, but there are 2 main issues with it:
- I cannot ensure it will be called every time a new contract of the specific template will be created. I cannot ensure it with the developers or with the users (e.g. Navigator).
- The choice can be called more than once, something I don’t want to have. Even though that can be resolved by using another boolean data field that I initialize with False, and once the choice is executed I run it only if it’s False, and at the end of the command I set it to True. Still, this is not 100% sure (as this boolean can be changed to True in some other choice…)…
While triggers can ensure this for me, I cannot execute this kind of trigger on behalf of every user on the system. Let’s say that I have 100,000 users, and a specific contract can be created for every party on the system… this is not a realistic solution.
What I am suggesting is adding a reserved-name choice, something like the same name of the template (the same way as there is with the
Archive), which will be executed upon the creation of the contract. And if there is no implementation of this choice, nothing will happen.
And the create command can perform like the
submit command (which behind the scenes calls the
submitMulti), this will call something like
createEx that does the two things.
There is a limit to what you can achieve with Daml code directly, and things beyond that limit need to be achieved through consensus and external agreements.
Yes, it is possible for someone to bypass your CreateAndExercise, and thus create an “invalid” contract. As the developer, there is nothing you can do to prevent that.
If your Daml system has a UI, you can make sure the UI creates valid contracts.
As a user of the system, you can make sure you don’t yourself sign invalid contracts, and then you won’t be affected by other people creating invalid contracts.
Note that I am not part of the teams that would be involved in implementing your suggestion. I am merely describing the current state, not trying to make a call about whether or not what you are asking for is desirable or feasible.
I completely understand this and till now I am following these guidelines. But I think having this, will make Daml more robust, and more effective, not to mention that it will provide a wider range of solutions that can be done using Daml.
Anyway, this is a feature I am suggesting, that in my experience can be very helpful.
Thanks @Gary_Verhaegen & @cocreature for your replies on this matter.