Daml 101: Let's write a Daml template from scratch [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,804 wordsCaptions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.
- 0:00
hi this is steve style from digital asset in this video we're going to use a scenario to identify a problem that we can solve with demo we will run this scenario through our checklist and then take a look at the architecture and make sure that we have the business logic the data model all figured out and then we'll open up visual studio code and write the code line by line i'm
- 0:22
excited stay tuned so here's a problem that i've not been able to solve over the years friends borrowed things from me and failed to return them or worse no one remembers who borrowed what not even me so maybe we can solve this using smart contracts here's a solution why don't we create a smart contract app that can record what i borrowed so there'll be no more misunderstandings and both parties will be kept honest that sounds feasible but let's run it
- 1:06
through the checklist that we have seen in previous videos so we're going to build an app that tracks borrowed items between friends and here are five things we need to check for are we tracking something of value is something valuable that's important yes how about data is data being changed yes it is in a way that custody the chain of custody is moving from one person to another and then back to me the history is important because if more than one
- 1:33
person borrows it i want to know at what point the camera broke should the data be immutable absolutely the data should be immutable because i don't want someone to go back and say i did not borrow your camera fantastic and what about actions are the actions that we can take yes we can return the item we can release the item to somebody in a previous video we looked at our project in four components the asset which is the camera the people and the party which are friends and the data in this case we're tracking
- 2:05
the camera and the information and the use of the camera and probably most important of all what happens when you break my camera how long can you borrow it for so the logic in this case or the business logic will be the rules and the duration and things like that let's take a closer look at our architecture we have the owner and a friend and let's keep it simple and say that the owner can release an item and a friend can return the item and instead of writing our own front end
- 2:36
we're going to make use of the navigator to help us test our templates and contracts and we'll fire everything up in a sandbox so there'll be a ledger that we can write to let's jump into the code fantastic let's uh fire up a terminal here and start a new demo project we're going to use the demo new command let's take a look at the templates we have we really want to use the empty skeleton template so let's try that we're going to do a my borrow app let's
- 3:08
call it that using that particular template let's cd into the directory and fire up demo studio demo studio is up and if you open up the demo folder you realize that there are no demo files because we're gonna write one from scratch so let's do a new file and uh let's go ahead and save it and remember that you do have to save the file according to the name of the module so i should say it should be identical to the name of the module in this case let's call it main.demo
- 3:47
so let's do main demo and that'll be the name of our module and let's save that okay let's start coding the first line is always the module so let's make sure that we have this first line remember that where always defines your scope so next line in three we're gonna do import demo script because we're gonna use demo script to test our code on the fly more on that in a moment then let's start writing the template we're just going to use one template we're going to call it item
- 4:18
custody because the item is being moved from one person to another and we are really tracking the chain of custody here so let's call it item custody the next line will be with and now we start to define the input variables that's required to create this template or to create a contract so we've got owner the custodian who's going to take my camera and the item name let's keep it simple for now and again we're gonna define another scope and this is where
- 4:48
we start to add the signatory who is allowed to create this contract remember this is different than the controller who are the people who can perform actions on it or exercise choices so in this case we're gonna say that the owner can release an item and what we need to release is just the name of a the friend who is going to receive the item and what happens when a release item 2 is exercised well we're going to create
- 5:28
a contract in the chain out of this template and we do that by setting a new custodian so we're going to copy this and duplicate another choice here and this choice allows the custodian to exercise the choice of returning the um the borrowed item so this two will add to the chain but this time we're gonna set the owner back to the rightful owner of the item so now we have two choices now we really want to use script to test our code so there's one thing we need to do and that is to open up the demo.yaml
- 6:10
file and write the following lines to initiate the script and i'm going to change this to jerry and elaine because i want a navigator to have those two identities built in so that i can test my code all right let's go back to the code and we can start writing our script to test our template here so the first thing we want to do is to specify a return type and this will be a contract id minted from the item custody template
- 6:44
the first thing i will do is to create two parties to play with in this script one is called jerry and the next one will be elaine now this is different in setting them in the demo.yaml this is the test script the other one is for the navigator awesome i got jerry i got elaine now notice this uh squiggly here that tells me something's wrong you can fix it by encapsulating this in a pair of parentheses but notice that we use it
- 7:15
quite a bit in our code so what you can do is to create a type and let's call it item custody id and define it as what we see at the bottom and instead of the two we'll replace that with item contract item custody id done awesome now the first thing we're going to do is we're going to have jerry buy the camera and give himself custody of the camera so he's going to create this new uh collect a new property that he owns and give himself custody and he can
- 7:52
start to do things to the item like borrowing it to friends so that's why the custodian is still jerry in the first round this is a an expensive camera okay that's good let's call it a really expensive camera now that he bought the camera and has custody of the camera we can have jerry give or borrow the camera to elaine and that is the choice that we're gonna use called release item two
- 8:31
so let's do a exercise command and we're gonna exercise uh the particular camera and the choice and we need to specify a friend in this case it's elaine and that should be good now we're going to copy the line starting from submit and have elaine return the camera back to jerry and she has to act on an exercise the contract that was hers in line 44. so she is going to return item to the rightful owner the
- 9:11
input parameter name and the value is jerry so let's take a look at a script and we see that it's correct we have a archived instance and you see that elaine doesn't get to see what jerry bought in the first place that is correct a correct behavior until she's involved she doesn't need to see it so we've got these four components and let's take a closer look at the data what if we want to track
- 9:37
more information about the camera most cameras like dslr or mirrorless has a shutter count which means how many times you snap the picture so let's use that since we can count it and it tells us how much use was uh inflicted on the camera so let's call it meter counting let's call it let's set it as an integer so now we have to modify our create command here i'll create lines here and set the meter count so remember that
- 10:07
this is the shutter clicks that we can measure as it moves from person to person in a chain here at the bottom as well well we should really change this to um something that we can input so jerry can input and say well this was x number of uh shutter clicks when i created it when i load it out so let's go back to let's go down to where we exercise the borrow release to command and set the curry um set the current meter count to let's say 347.
- 10:49
you know what let's change that to 360 assuming that he got it at 347 and he clicked it a couple of times and it became 360. at the top we want to be able to return the item and likewise we're going to have a meter count reported when it's returned and let's say elaine goes crazy and cl adds 10 000 clicks to it and you see that it's being tracked from 340 to 360 to 1004 clicks jerry is not going to be happy anyway you know how we all have that nosy neighbor who
- 11:23
wants to know everything let's add that nosy neighbor in asm observer can do anything but just want to be able to know what's happening with everything we're going to add a new party and at the bottom we're going to create a cramer as a party and this cramer will be the nosey neighbor now how this nosy neighbor will work into the contract is that he will be an observer an observer gets to see the transactions and you see that kramer is correctly reflected in the output in
- 11:57
that he can see the contracts okay we are ready for the navigator so let's uh comment out the top line that imports this the demo.script and comment out this entire block here so we disabled the script from running and go to download.yaml and add the uh where you see this line just comment it up with a with a hashtag and then let's add kramer so that the navigator gives us kramer as a choice awesome let's go to our terminal and do
- 12:30
a demo start give it a few seconds and you should see the same box and the navigator fired up and when the navigator is done you automatically uh see a browser let's open it up and we see we have a choice of the three names we'll log in as jerry and see that uh there's a contract there's a template that he can uh start working with let's do that let's create uh the very first item by specifying the owner the custodian
- 13:03
fantastic and this will be the nosy neighbor kramer the item we're talking about is the camera and let's say the meter count we i think we say it started at 3 47 and now we have our very first contract created out of that template and it states that he has custody of it and now that he has custody of it he can do things with it but just to be sure let's take a look at kramer and see if he can see all that yep he can see that very same contract
- 13:42
that jerry created because he's an observer now if we were to log in as elaine what would she see absolutely nothing because she's not involved in this contract quite yet but jerry can get involved by letting her borrow the camera so we're going to go ahead and do that this particular item is going to be transferred to her custody and now we have a new block added to the chain
- 14:14
if we were to take a look at the contracts here we would see that there is an archived which is the very first instance and the second one and the second one would state that same item same owner same same observer but now the cameras is in the custody of elaine and who can see this contract well kramer the noisy neighbor and elaine because she's involved in this contract let's make sure elaine can see the contract good and let's log in as kramer kramer can
- 14:45
see the contract fantastic so both of them made observers of this particular chain here now let's log as login as elaine and return the item back to jerry so we're gonna go ahead and say that we're gonna return this to jerry and we added over 10 000 clicks so that thing goes back a new block is added to the chain and you see that it is returned to jerry now back in jairus account can he see the latest custody
- 15:26
oops we got a problem the custodian is still listed as elaine it should be jerry now that he has the camera back no sweat we'll fix it let's head back to vs code and take a quick look uh the first thing i'm going to do is to re-enable the script so that i can see if we have fixed it correctly so let's remove the comment here and then at the bottom let's remove the block of comments and don't forget
- 15:57
the demo.yaml file take that off right back to the code and uh everything looks good here let me check something real quick to make sure that the demo script output is correct for the current value here let me change that to yup that's working fantastic let me see what happens in a do block now i have assigned the owner here so it should really be custodian the owner doesn't change throughout the lifetime of the contract so the custodian should be
- 16:28
the rightful owner here so let's try that save it and go back to our navigator but before i do that let's just rerun the sandbox and navigator and once it is up and running let's do exactly what we did and see if the if jerry's made the rightful owner all right here's the navigator let's log in as jerry and jerry will start by uh creating a a camera item setting himself as the custodian to start
- 17:07
adding kramer as an observer and 347 and first entry is done now we are going to check to make sure that yup so jerry has custody and he's going to let elaine borrow it at 360 clicks now let's login as elaine mc to make sure that elaine has custody perfect now let's uh return the let elaine return the camera back to jerry at 10 000 clicks she returns it now let's go back to um elaine up yup she can no longer see the contract because she has archived it
- 17:50
from her end but we can go back to jared because the baton has been passed to him he sees a negative contract and ta-da the camera is back in his possession wonderful so i hope you found that useful granted this was a very simple scenario and in a real world context you're going to have more templates parties choices in future videos we'll use more complex scenarios but i hope this video gave you a good idea of how the demo framework
- 18:22
works join me in future videos see ya you