Are you searching for a DAML tutorial? If you do, then you have come to the right place. In this article, we will be doing a proper DAML blockchain tutorial.

The tutorial will go through a basic understanding of DAML, why it is needed, and code examples to make more sense out of it. If you are a beginner, you will find the tutorial to get started with DAML. In short, this is a perfect DAML tutorial for beginners. However, it is not a complete DAML development tutorial as we are not going to build any complete application in the tutorial as it is beyond the scope of the article.

DAML Tutorial: Getting Started with DAML

DAML Tutorial

So, what is DAML? Let’s explore.

Need a blockchain refresher? Check out the Blockchain for Beginners guide to get started! Also, check out our Ultimate Guide to DAML.

What is DAML?

DAML is an open-source programming language for developing distributed applications. It lets developers create those distributed applications concisely, quickly, and correctly.

Hyperledger maintains DAML and has made it part of its ecosystem. This also makes DAML one of the programming languages that run on top of the leading blockchain platforms out there. Yes, it is accepted by multiple platforms. It also makes it easy for developers to quickly develop their application and then decide where they want to deploy it.

What makes DAML so special?

DAML is a programming language to build distributed languages. With more and more companies understanding the importance of distributed applications, DAML provides a tool for those companies and their developers to achieve their goals. It is, in fact, one of those amazing tools that let you take your distributed application in your control.

DAML tackles the hard design problems head-on as it provides a solution to complex problems such as distributed state synchronization and cryptography. To achieve the goal, it is redesigned.

The underlying design or implementation is abstracted when a developer writes his blockchain application. It lets the developer focus on the abstract requirement and let DAML handle all the hard work. By knowing that DAML is special, you can better appreciate learning DAML using our DAML tutorial.

DAML features

In this section of our DAML tutorial, we will learn about DAML key features.

Data Model: DAML lets developers create complex data schemes for their application with ease. This makes it easy to design and implement complex requirements or business processes.

Fine-grained permissions: The DAML contracts are fine-tuned to set permissions. This means that developers can set who could sign the contract, who can see it, and at what conditions.

Business Logic: Business logic can also be integrated with ease. A developer can write actions that are required on the contract, its assertions, parameters and more!

Scenario-based Testing: Scenario-based Testing is also possible with DAML. It lets the developer test the business logic and other aspects of your decentralized apps, such as workflows.

Runtime Features

Apart from the above four core features, we also have the DAML Runtime. Runtime refers to the environment created by the application during its execution. In this state that has key features that make the program run efficiently and effectively. By learning about them in the DAML tutorial, you will be able to solidify your DAML learning.

Let’s talk about them below.

Storage Abstraction → Storage abstraction offers a persistence layer that ensures that all the DAML programs are storage-agnostic. In simple terms, the DAML programs don’t depend on the chosen storage as it can convert the said data into a digestible format.

storage-abstraction

Authorization Checks → The authorization checks all the actions within a contract. If the action doesn’t pass proper authorization, then it will not be allowed to execute.

daml-tutorial-authorization-checks

The contract will then throw a runtime error and inform the buyer or the developer.

daml-tutorial-error.

Accountability Tracking → The runtime is also responsible for accountability tracking. It makes sure that the parties voluntarily enter the agreement. To make it happen, signatories signatures were required. This is done to protect the buyer from any forceful behavior from the organizer or seller.

Atomic Composability → DAML supports atomical design. This means that all actions are performed atomically and hence can either be committed or not at all. There is no middle ground when it comes to executing contracts. This is done to ensure the safety of the workflow and keep exploiters away.

No Double Spends → With DAML runtime, the contracts are designed to make sure that no double spending is possible. This means that the same contract can not be executed twice.

Need-to-know Privacy → DAML offers a sub-transaction level, which makes the information available when approved.

Deterministic Execution → Lastly, the runtime supports deterministic execution. This means that any action’s effect depends on the ledger’s current state.

Getting Started with DAML

In this section of our DAML tutorial, we will look into the getting started guide for the DAML blockchain tutorial. Before you can use DAML, you need to install it.

To use DAML, you need to follow a two-step process.

1) Installing Dependencies

To use DAML, you first need to install the dependencies. To do so, you need to download the SDK and install it on your operating system.

Other than that, it would be best if you also had the following

  • Visual Studio Code or any compatible integrated development environment
  • JDK 8 or greater.

2) SDK installation

If you are using Windows, you can go to the link, and download the executable file.

For Linux or MAC, you need to run the following command using curl.

curl –sSL https://get.daml.com/ | sh

It will ask you to add ~/.daml/bin to your PATH variable. Once done, you are ready to use DAML.

DAML Tutorial Beginner’s Guide

Have you never worked with DAML? Then, don’t worry, as in this section, we are going to go through the basics of DAML, its data types, templates, functions, expressions and so on!

But, before we do, we need to understand the (Digital Asset)DA Ledger Model. It is an important part of our DAML tutorial.

DA Ledger Model

DA Ledger Model is at the core of DAML. It offers multi-party workflows with the help of a virtual shared ledger. To get a better idea, you can check the screenshot below.

DAML-Tutorial-Ledger-Model

Source: DAML documentation

The model is used to define, DA ledgers structure(what), integrity model(who can request), and privacy model(who can see).

To get a more in-depth understanding of the DA Ledger Model, check out the official documentation here.

Basic Contracts

At the core of DAML, you will find a DAML ledger. We are going to go through a small template as it will help us understand essential concepts, including transactions, templates contracts, signatories, DAML modules, and files.

Commits

DAML Ledger contains a “commits” list. In simple terms, a commit is a submitted transaction to the ledger. As of contract, you can think of it as an active transaction that needs to be executed, and its execution depends on the contract attributes and conditions.

DAML modules and files

Before you start, you need to mention the DAML version. It can be done at the top of the DAML file. The version will notify the compiler which version of the language is being used.

daml1.2

To import the module, you need to use the “module” keyword.

moduleTokenwhere

Comments

If you want to add comments, you can do it using the “–” keyword.

— This is a comment

Templates

A template is used to define the contract type. This defines the entities that have access to execute the contract. You can think of contracts as template instances.

template Token

with

owner:Party

where

signatory owner

As you can see, we defined a Token template using the “template” keyword. It also takes an argument. One more thing that you can take notice of is that it is whitespace-oriented. If you have used Python before, you will understand how the code is structured and intended.

Lastly, you can see the signatory keyword, which signifies the contract instances signatories. These party’s authority is required to take actions on the contract, including archiving and creating it.

Understanding the scenarios using templates

Now that we have understood the basic structure of a template, we are now going to understand the scenarios using two other templates in our DAML tutorial. But, before we do, let’s understand what exactly is a “scenario.

Scenario

A scenario can be best described as a test recipe that can be used to check if the templates are behaving as they should. It can be used to perform transaction tests. Let’s take a look at the example below.

token_test_one = scenario do

sam <– getParty “sam”

submit sam do

create Token with owner = sam

The above is the basic scenario which deals with the Token for a party known as “sam.”

To run the scenario, you need to use the DAML studio.

To learn more about the scenario, we suggest you check out the scenario documentation page here.

Data Type

Just like any programming language, DAML also supports data types. The data types let you define a variable and store data there.

To make it simpler for you, let’s think templates as database tables. Now, you can save data in the template and help yourself manage them easily through template design.

There are many native data types that DAML comes in. It includes the following.

  • Party → To store entity identity. Parties can submit transactions and sign contracts.
  • Text → Stores Unicode character
  • Int → stores 64-bit integers
  • Decime → stores fixed-point numbers
  • Date → stores a date
  • Time → stores time in UTC
  • RelTime → Stores time difference
  • ContractId → reference to contract type

Below is the code to understand some of the native types.

native_test = scenario do

Sam <- getParty “Sam”

Mysterio <- getParty “Mysterio”

let

my_int = –657

my_dec = 0.001:Decimal

my_text = “Sam”

my_bool = False

 

assert (Sam /= Mysterio)

assert (-my_int == 123)

assert (1000.0 * my_dec == 1.0)

assert (my_text == “Sam”)

DAML also supports other data types including Tuples, Lists, and Records.

To learn about the data types in DAML, check out their documentation page here.

Transforming data using choices

DAML supports immutability. That means whenever there is a need to update data; a new contract needs to be created with the new data. However, that’s not always the case as there is always something very small to change and modify. For instance, a company might want to change its telephone number to do it using choices.

Let’s take a look at an example taken in the DAML documentation.

template Contact

with

owner :Party

party :Party

address :Text

telephone :Text

where

signatory owner

 

controller owner can

UpdateTelephone

:ContractIdContact

with

newTelephone :Text

do

create this with

telephone = newTelephone

Most of the code is self-explanatory above. The only thing here you need to know is that we defined a choice known as “UpdateTelephone.”

Choices can also be used as a delegation. Lastly, they can also be integrated into the Ledger Model.

To learn more about the DAML choices, you can check out the official documentation page here.

Adding constraints to a contract

You can also add constraints to the DAML contract using the “ensure” keyword. You can also use the other mechanism which utilizes assert, abort and error keyword to add constraints to the contract.

But, before you do so, you need to have proper template preconditions set in your contract.

To learn more about the constraints and restrictions, you can check the documentation here.

Parties and Authority

DAML has a proper way to handle parties and authorize them to access a contract. You can use them to not only pass authority but also write advanced choices that can change how the ledger works. This flexibility is important as it gives parties enough tools to change things and also keep the core idea of the contract intact.

For instance, you can prevent IOU revocation or better do one-off authorization using propose-accept workflows. There are plenty of options that are made within the DAML to provide a solution for almost every scenario out there.

Read more about parties and authorities here.

Conclusion

This leads us to the end of our DAML tutorial. In this tutorial, we managed to learn about DAML and what makes it special. We also learned about the DAML’s internal workings and other key aspects of the programming language, such as data types, scenarios, and so on. These key concepts will help you to engage yourself with the DAML projects that you are going to take in the future.

So, what do you think of DAML? Do you think that it has the potential to become a ubiquitous programming language to create decentralized applications? Comment below and let us know.