DAML: Getting started with building Templates

Reading Time: 4 minutes

Humankind is standing on the brink of another industrial revolution. Few technologies which are going to play a vital role in this are IoT, Artificial Intelligence, Blockchain and some more. This blog contemplates more on Blockchain based language i.e. DAML. So let’s get started.

Why Blockchain ?

WEB 2.0 gave usage of information and blockchain grants digital ownership. So let’s take a basic example to understand more about Blockchain.

Suppose there is a cake delivering online platform thatpasses your order to your nearest bakery once an order is placed amd successful payment is done. Here, the user is dependent on the intermediary platform and its services for the order and processing. And in the cases of wrong/missed items delivery, the user has to go through a complex procedure to get a refund.

The blockchain network has no central authority, it is the very definition of a decentralised system. Since it is a shared and immutable ledger, the information in it is open for anyone and everyone to see. Thus, anything built on the blockchain is transparent and everyone involved is accountable for their actions.

A smart contract in blockchain is a self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of code. They are immutable, which makes them very secure.

Using blockchain in our above example, we can design the smart contract to not credit the payment for cake, until the bakery delivers. And if they don’t, the amount gets credited back to the user’s account without any complex procedure.

How to achieve it with DAML?

DAML seems to be a promising technology to achieve the excellence of Blockchain and it’s an open-source programming language used to write distributed applications quickly, concisely and correctly. Notably, the design of the system is in a way that machines and humans can understand the information included in the contract.

What makes DAML better from other languages namely Solidity, AML, BOScoin and many more that the contract is of private type because shortcoming of public type is that every node on the platform can view data that is present in smart contracts which prevents the complete adoption of enterprise blockchain.

Now, what to do?

Let’s start by setting up the environment:

Install Dependencies

  • You can refer here to download dependencies and to install SDK.

Setup Visual Studio

  • Create new DAML project
    daml new PROJECTNAME
  • To open the project in Visual Studio. Navigate to Project folder and run:-
    daml studio
  • Make sure DAML studio extension is installed:-
    • Click on the Extensions icon at the bottom of the VS Code sidebar.
    • Click on the DAML Studio extension that should be listed on the pane.
../_images/daml_studio_extension_view.png

DAML Module

  • Create a new file in Visual Studio and save it by name as Test.daml
  • At the start of a new file write “module ANY-NAME where” because every .daml file is treated as a module.

Templates

Template is well-defined and straightforward structure, which contains both the data model and parameters of the Contract. It includes setting such as signatories (who authorize actions on Contract).

module Token where 
template Token 
with   
owner : Party 

amount : Int
where    
signatory owner

Template Name

template Token

Name of Template is preceded by template keyword. Here, we are trying to build a Token template where the user can set an amount for transactions.

Template Parameters

with   
owner : Party 
amount : Int

Under with keyword the parameters are in form of record type.

Signatory

where    
signatory owner

Signatories are the parties what must consent to the creation of an instance of this contract. They are the parties who would be put into an obligable position when this contract is created.

Scenarios

Scenarios is like a recipe for a test, where you can script different parties submitting a series of transactions, to check that your templates behave as you’d expect.

  • Now to verify everything is working fine, create a scenario to test the template.
token_test_1 = scenario do
alice <- getParty "Alice"
submit alice do
create Token with owner = alice

Scenario Declartion

token_test_1 = scenario do

Scenario is top level variable and introduced using scenario do that begins the block.

Party Initialization

alice <- getParty "Alice"

The function getParty initializes the party with name “Alice” who is the owner of the token.

Scenario Defined

submit alice do
create Token with owner = alice

You can submit your first transaction to Ledger by using submit keyword. The submit keyword takes two argument party and Update.

Update is recipe for transaction. create Token with owner = alice is an Update, which translates to the transaction creating a Token with owner Alice.

Running Scenarios in DAML studio

  • Click on  Scenario results that appear at the top corner.
  • The scenario results will look like below,

And that’s how you set up your first DAML based template. You can explore more about DAML from here.

References

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading