Introduction to DAML

Shivaganesh
3 min readMay 31, 2022

DAML (Digital Asset Modelling Language) is an open-source smart contract language that allows the implementation of business logic without worrying about which DLT platform or database suits the use case. Official documentation can be found here https://docs.daml.com.

Install DAML SDK by referring steps in https://docs.daml.com/getting-started/installation.html

DAML Features

  1. Privacy and Fine-grained Permission: While writing the smart contract, permissions like who can sign the contract creation, view, update and act on it can be specified. Daml transactions are composed of sub transactions and every sub transaction has its own set of witnesses that are entitled to see that part of the overall execution thus allowing sub transaction privacy.
  2. Scenario-Based Testing: Daml Plugin for Visual Code IDE allows to write test case scenarios which allow visualizing the consequences of executing smart contract functionalities. Testcases also allow the creation of multiple test parties thereby allowing to test the smart contract as if it is deployed in a real Daml network.
  3. Easy Development: The Daml plugin evaluates the smart contract in real-time, thereby allowing enabling developers to follow fast test-driven development practices. Daml Assistant is a command-line tool helps in installing new daml version, creating daml project using templates, build the project, launch SDK tools namely Sandbox, Navigator, HTTP JSON API service, codegen (to generate Java, Scala, and JavaScript/TypeScript classes representing daml contract templates) and many more.

DAML Smart Contract

Now, let us learn how to write a simple smart contract and run it on DAML Sandbox. Use VSCode for better experience. DAML Plugin is available for VSCode which allows for syntax highlighting, code autocomplete, testing, data visualization etc.

After successful installation of DAML SDK, use the command

daml new <app_name> --template <template_name>
Eg:
daml new app1 --template empty-skeleton

This creates a DAML project with daml.yaml and daml folder. daml.yaml is the configuration file which specifies sdk-version, app name, version, dependencies and many more. In daml folder, keep all the daml smart contract files.

Here, Asset.daml file is created and added to daml folder.

Asset : This defines Asset properties and actions which can be taken on Asset

module Asset whereimport Daml.Script-- class like structure defining the fields & choices of a contract
template Asset
with
assetName : Text
assetType : Text
owner : Party
issuer : Party
where
-- parties who must sign for a valid asset
signatory owner, issuer
-- actions available on a contract
choice ProposeTransfer : ContractId AssetTransferProposal with
newOwner : Party
controller owner
do
create AssetTransferProposal with asset = this, newOwner

Notes:

  1. template is the keyword to define a state similar to class in Java.
  2. Specify the fields in the template which should necessarily contain a party who will be the signatory
  3. Signatory refers to parties who must sign inorder to create or archive the contract
  4. template can have choices which act on the contract to do certain actions. controller will perform this choice.

AssetTransferProposal : To transfer the asset to different party, we need the signature form the new owner which can get via initiate-accept pattern. Here observer party who will be the new owner to whom this asset should be transferred.

template AssetTransferProposal
with
asset : Asset
newOwner : Party
where
signatory asset.owner
-- parties who would be observing the contract and its evolution
observer newOwner
choice Accept : ContractId Asset with
controller newOwner
do
create asset with owner = newOwner
-- Reject transfer
choice Reject : ContractId Asset with
controller newOwner
do
create asset
-- Withdraw transfer
choice Withdraw : ContractId Asset with
controller asset.owner
do
create asset

Testing

Daml allows us to write testcases to verify the smart contract functionalities and visualize the flow in VSCode itself. Here we can execute all the functionalities which we have defined for a template. We need to add daml-script dependency in daml.yaml and import Daml.Script in the module.

testAsset = script do  -- test parties
issuer <- allocateParty "Samsung"
party1 <- allocateParty "Raam"
-- create an asset
assetTV <- submit issuer do
createCmd Asset with
assetName = "TV"
assetType = "Electronics"
owner = issuer
issuer
-- proposal for transferring the asset
proposal <- submit issuer do
exerciseCmd assetTV ProposeTransfer with
newOwner = party1
-- accept the transfer
transferredAssetTV <- submit party1 do
exerciseCmd proposal Accept

return ()

To learn more about DAML Smart contract development, go through the official documentation https://docs.daml.com/daml/intro/0_Intro.html. Refer the cheetsheet for any quck reference https://docs.daml.com/cheat-sheet/.

--

--

Shivaganesh

Blockchain Developer experienced in developing Supply chain, Pharma, Telecom domain solutions using Hyperledger Fabric, R3 Corda, Symbiont Assembly and DAML