What are Choices in Daml?
Nothing is loaded from YouTube until you press play, and then without tracking cookies.
About this video
๐ Get started with Daml: bit.ly/tryDaml ๐ Learn more about Daml Smart Contracts: bit.ly/learn_daml ๐ฎCheck out our blogs: bit.ly/daml-blog-engineering ๐ Become a Daml certified expert: bit.ly/daml-certification
The #choices of a contract template specify the rules on how and by whom contract data can be changed.
DAML is an open-source smart contract language designed to build composable applications on an abstract ledger model.
DAML is a high level language that focuses on data privacy and authorization of distributed applications. These concepts are represented first class in the language.
By abstracting data privacy and authorization, DAML takes the burden off the programmer to think about concrete cryptographic primitives and lets her focus on workflow logic.
DAML is a statically typed functional language.
Applications specified in DAML can be deployed on a growing number of platforms including Amazon Aurora , VMWare Concord , R3 Corda , Hyperledger Fabric , Hyperledger Sawtooth , Project DABL and PostgreSQL .
We will show how to: choice consuming nonconsuming
#distributedapplications #smartcontracts
Transcript
1,240 wordsCaptions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.
- 0:00
hey everybody uh welcome to now my fourth video on the demo cheat sheet today i'm going to be showing you choices uh and how they're exercised on a contract choices are actually a fairly important and common construct within demo programming so what we mean by a choice essentially is that we have a contract and you probably saw this in my last video and then you have options that you can exercise on that contract so choices come in one of two flavors they
- 0:32
come in consuming choices which is generally by default and non-consuming choices which you have to add an additional command in order to do so for consuming choices what a consuming choice means is that it essentially updates the existing contract it archives the existing contract and replaces it with a new one whereas a non-consuming choice um will do something but not archive the current contract the reason you generally want consuming choices for most of your workflow
- 1:03
is because it makes sure that when you do your updates uh they don't go stale and you don't end up with conflicting data in conflicting states uh and in most cases you can kind of think of a consuming choice kind of like an asset update to say your sql database where essentially you had some old data and then you went and replaced it with new data and so in this case you're not only
- 1:27
replacing data but you're actually doing you know full-on executions that replace the data and that's why we generally keep choices that's consuming but there's also the option of having non-consuming choices for example if you had one contract that was then used to later make other types of contracts you would use a non-consuming choice but i'll just briefly go over the structure and then i'll show you a real example from one of our programs
- 1:53
so this is a choice uh it's specified by using choice and optionally you can specify non-consuming before it if you want it to be non-consuming if you want it to be consuming you just leave this out then you have the name and you have its return type in this case the return type is just uh pure nothing and much like with templates your choices can specify certain arguments that they need to be called with so for example this one has a couple parties and an integer
- 2:25
uh then the choice basically specifies who can control it so in this case uh both of these parties party one and party to our controllers to this choice and so either one of these parties are capable of exercising this choice and then what they can do is they can essentially assert that certain things are constant so this is just obviously an example but in this case the choice is only going to do anything
- 2:56
if i is equal to 42. uh then the choice can also create other contracts um this would be a case where you know if you were had a consuming choice and you were replacing your existing contract this is would be where you would actually return your your final your final uh contract that that replaces the the previous one and then you also have the ability to exercise other contracts and to return uh whatever you're returning in general you
- 3:26
actually wouldn't normally have return at the end of most of your choices you'd probably have a create or a cr normally a create command at the end of your choice because these are two functions so they work much the same way they would in something like haskell where once whatever's at the end of your do function is basically what the do function's going to return uh in this case we're just returning
- 3:51
nothing as an example so that's why we have to specifically do that there but for most cases you wouldn't and i'm actually going to now change over to an example uh so this here is from i believe the the kanban boards or dan bin as we're calling it uh which is essentially a a a clone of a kanban board i think it was adapted from another project but built to use on top of a demo back end and this is basically demo enforcing all the
- 4:25
rules of that board so in this case we're going to look at the add user choice uh here that's the name of our choice add user and what we're going to actually be returning is some board data it doesn't matter what that is but generally your choices are going to be returning a contract id of some type and in this case it's support data then it accepts a couple parties and they are the ones that can exercise this
- 4:51
choice uh and then what it can do is it can go and call this check access function which is going to it's not shown here but it'll check to make sure that the admin specified is actually able to do what they should be able to do which is add a user and if they're not it's going to not allow this choice to continue any further then it's going to go we're going to go ahead and assert something and make sure that our user is not already existing and if they are we
- 5:23
obviously don't want to create two of the same user so that'll that'll stop the execution as well uh and then finally if we've checked past those checks you know the admin is okay to make this update and the user doesn't already exist we could go ahead and create the new user and then as our final uh our final execution in this in this uh choice is we're going to go ahead and exercise some other contract and provide it with
- 5:54
and provide it with an update and this other contract's called update obs and that'll do something else that's part of the the workflow but this also shows uh that essentially when you can you can essentially execute one choice and then it will either you know just do one thing or you can have it do many many things and you can make sure that these things happen in the order that
- 6:17
they're supposed to happen in and that you know you're not ending up in a in a case or having to specially code for a case where you're doing an update uh before another part is changed so this is all atomic so if for example the update obs contract that's being exercised right now failed in some way then this choice also wouldn't execute and yeah so that's basically a brief look at choices obviously there's a lot to unpack here but they are ultimately at their base very very
- 6:49
simple things that uh should be fairly easy to grasp at least at the lower case and then you have to you know start thinking about the implications of it but yeah it's a pretty typical construct in demo it's also having you know side effects in do loop in uh two statements is very common in other functional programming languages like haskell and yeah so thank you very much for watching this and i look forward to giving you the next update