Daml 101: An Overview to the Daml Language [2022]
Nothing is loaded from YouTube until you press play, and then without tracking cookies.
About this video
📚Check out Daml Learning Center: bit.ly/LearnDaml 📖Join our forum and get started: bit.ly/tryDaml
This is a multi episode series intended to take you from the brief context behind blockchain and smart contract to the development of smart contracts using a next-gen smart contract language called Daml. The entire series can be found here: bit.ly/damlone
Daml is a development framework for creating systems of record that securely connect across business processes and data boundaries while retaining privacy, guaranteeing data integrity, and enforcing strict authorization. Allowing such systems to cross boundaries — legal, regulatory, organizational, or other — enables them to form seamless economic networks.
Transcript
2,641 wordsCaptions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.
- 0:00
hi this is steve shaw from digital asset in this video we're gonna dive right into demo think of me as a tour guide hi this is steve sale from digital asset in this video we're gonna dive right into demo think of me as a tour guide you're visiting an island that you've never been to before and instead of showing you town to town to town i'm going to put you on a chopper and we're going to fly around
- 0:31
the island just to check out the key features and once we have that mental map we'll use that to dive into specific topics in future videos if you're ready buckle in let's start the tour with the front end the front end is your choice you may be building a mobile app you may be building a web app or a native desktop app the key here is to get your front end communicating with the back end that
- 1:08
is managed by the demo framework and the way we're gonna do it is to have the front end talk to the json api exposed by the demo framework there is more than one way to communicate with the data in the demo framework but for the purposes and scope of this video will focus on using the json api the responsibility of the demo framework behind the scenes is to maintain a virtual shared ledger that your front-end will eventually query against or create contracts out of
- 1:42
the beauty of demo is that you don't have to worry about consensus or wiring up proof of stake or anything like that or setting up hardware because all that can be taken care of for you because you can deploy your demo app on a distributed blockchain like corda or vmware blockchain or hyperledger fabric just to name a few for the scope of this exercise we're going to use something called demo hub which is our cloud hosted platform or if you want to deploy it to a
- 2:08
centralized database you can easily deploy this to postgres or amazon qrdb now think about it this really frees you up to work on your platform your business logic your user experience in your architecture because all that in the background is handled for you by the framework now when was the last time we have to worry about how our database handle collisions or consistency or availability we don't because it happens in the background the code that you will write will reside
- 2:39
in something called a demo file now a demo file is then compiled into a dar file or a demo archive file now the dar shouldn't be confused with dart files which you find in in frameworks like flutter now the dot dar file is really what defines what we call a template in the framework and a template is what the framework will use to create contracts or a list of commits of contracts that is the part that is immutable a way to think about
- 3:11
templates and contracts in the framework is to think of the template like a cookie cutter so when you use the cookie cutter it produces a contract with some data in it and this is a unit of data that cannot be modified now suppose there is a reason to update information about this chain of data we would have to use the template again to create a new contract based on the old one now the latest one will be marked as active and any ones prior to that will
- 3:41
be archived remembered in blockchain we cannot mute it old data let's use a very simple example suppose we have a template and the template captures information on the ownership of a car we have the year of the car and we have the information about the car and the odometer reading the template itself will have no persistent information it will be used to create a contract reflecting a particular owner that took the car and bought the car when it had 2
- 4:11
000 miles on the odometer now this car is sold to a second owner and the second owner at this time will accept the car and by now it has 5 000 on the odometer reading note that other information about the car cannot be mutated it stays as a 2005 car now when it's passed on to the next owner and so forth the changes are reflected and a chain of these data is created so there's a transaction of not just the owners but information about the car that's meaningful it is entirely
- 4:45
possible and common for your demo project to have more than one template for example template a can create a contract and that contract produces and triggers the creation of another contract based off another template let's look at an example suppose you are selling your house and we are using template a to create a contract and the contract is really a listing stating the starting price of the house now we have a template b and a template b will
- 5:18
produce another type of contract that's really the bid amounts that someone wants to put in for example the first person puts in six hundred thousand dollars and that creates a contract of template b now a second person comes in and say well i want to pay 650 now that's a new bid we do not want to forget the old bid so we create a new chain and now there's a contract that says 650 is the latest highest bid for the house and the third
- 5:48
person can come in and put in 750k and now a new contract is created off template b to say that the latest amount highest amount is 750 and perhaps at this point this the seller can decide that it is sold let's take a closer look at a demo file using a very simple example that you will come across in many of the other tutorials now if the coding convention and syntax looks familiar it is because this is based off a version of haskell you do not need to know haskell or
- 6:24
functional programming in order to write demo code however if you know haskell or another functional programming language like scala it would definitely be useful let's take a look at the three core components of a demo file there's always a data model there's always permissions which controls who can do what and see what data and finally there will always be your business logic let's dive deeper into a demo file and
- 6:57
we'll start with the most important thing commenting in demo you comment a single line with two dashes and if you need to comment up a block you can use the curly braces like so if we move down the line we'll see the first keyword being used called module module is like in namespace and it is used to create the abstract type but we don't have to worry about that technically there can be more than one module in a file but for a demo project
- 7:27
keep it to one module in which everything is defined it is really what's inside of this module that is meaningful next the module you see the word user now this is the name of the module and you set this value it has to begin with an upper case now it doesn't have to be the name of the file itself but i recommend that you keep it consistent because it's very common in demo development to import a module into another module so this will help
- 7:59
tremendously when you have a lot of modules that you're dealing with in your project right after user you see the keyword where where defines your scope and your block so it groups your logic together now comes the fun part the template the template keyword defines the behavior the data model and the rights and obligations in other words it's like the recipe for a contract remember that contracts are instances of a template template takes in an argument which is
- 8:32
the name of the template in this case it so happened to be the same name as the module but it doesn't have to be it must stop with an upper case now this name is what the contracts that it produce will be a type of for example when we had a vehicle registration example the template could be named vehicle registration and whenever a contract is created out of that that contract has a type and is a type of vehicle registration
- 9:06
and it's totally possible to have multiple templates inside of a module for example if i have a module called orders it can have a template for drinks and it can have a template for appetizers next we see the keyword with with we'll start to define the required input arguments or parameters that the template needs in order to create a contract in the vehicle registration example we are going to need name of the car the year of the car and the current odometer reading once the required information is provided a contract can
- 9:41
be created right underneath with you will start to find the create arguments or parameters think of this as the section where you define the variables that are required to create a contract out of this particular template the names of the variables must start with a lower case now this is a great moment to call up an interesting structure of demo code demo is what we call white space sensitive just like fortran or python
- 10:12
notice that there's no use of semicolons to end a line of code or the use of curly braces to block up a chunk of related code that is because the language is going to look at the indentation or the white spaces to define meaningful blocks of code another requirement is that at minimal there must be a input argument that has the party type let's take a closer look this colon here means of type so this
- 10:44
line would read that username is of the type party note that this is using one colon not two as it would in pure haskell following the colon is always a data type and there are built-in types including a tuple which you can use to compose a mix of different data types moving down we see a second use of the keyword where the first time we saw it was at line four when the module was defined and the entire scope of the module was created
- 11:16
this where defines a template body so all of the arguments that were defined under width will be accessible inside of this scope underneath where is where you'll find one of the most important keywords signatory a signatory is a keyword that grants authorization roles to a defined party signatories must consent to the creation of a contract from a template so you got to have at least one signatory if you need to define more than one party you can always use an array inside
- 11:51
of the with block now there are other party types as well like observers and controller which we'll go into later on so the way you define who is the signatory for this particular template is to specify the the argument name that was used above in the width block in this case we have the username it has to be a party type so remember that there are two parts here first username has to be defined as a party and then in line 12 it has to be
- 12:23
made a signatory we're going to skip the concepts of key and maintainer for a later video and go right into the controller a controller is a party who can exercise an action on the contract and these actions are called choices another way to write this is to not start with the controller but list the choices first these are called flexible controllers right after the keyword controller is where you specify which party can be a controller in this
- 12:55
case it is the user name now this username remember has to be defined inside of that with block in line 8. now you will notice that in the example on the left there is no keyword can after username and that is because they are using the flexible controller style of specifying the choice for learning purposes i recommend starting with a alternate style where we start by specifying the controller and then the define party and then the word can
- 13:27
followed by the choices that that controller can do so in this case after we specify who is the controller we will start to construct the choice in this case follow is the name of a choice the name has to start with an uppercase and this will be exercised or invoked by the controller which can either be a signatory or an observer remember that the signatory is the person or the party that must authorize
- 14:00
the creation of a contract out of a template and the signatory can also exercise choices on a template in addition to the observer so a simple example would be the selling of the home example that we used earlier on so this the seller of the house will be the signatory who authorizes the creation of a contract from a template that's basically a listing and when the listing has gone on for a while there may be a choice in that template that allows her to go time is
- 14:40
up the bidding has finished and she can exercise a choice on her template that closes out the bidding process so she's both a signatory as well as a controller a default behavior that you should be aware of is that every time a choice is exercised it automatically archives that contract now there may be instances where you need the contract to remain active in that case you want to use the
- 15:12
keyword non-consuming to overwrite that behavior ah here we see the colon again and remember every time you see a colon is always followed by a type so this line tells me that exercising this particular choice is going to return something and that something is a contract and that contract is built from the user template now although it says contract id it doesn't just return the id it returns a
- 15:44
newly created contract from that template here we see another use of the keyword with and sure enough we see the next few lines defining the required input arguments or parameters now the difference is that now we can supply arguments or variables into the choice as we're trying to exercise it or we can get it from the variables that are within scope for example if there is a definition of the year of a car
- 16:18
2005 then we can get it from within that scope instead of asking it to be supplied by whoever is exercising it and finally we have the do keyword now this starts the choice body and this is where you start to code and define the actions that you have to take when the choice is exercised that was a fun ride wasn't it in the next few episodes we're gonna fire up visual studio and dive right in i'm gonna show you how to fire up what we call a sandbox so that you can get
- 16:56
your demo app up and running locally on your laptop see you next time