DAML: Getting started with building Templates
DAML seems to be a promising technology to achieve the excellence of Blockchain and it’s a programming language used to write distributed applications.
Join the DZone community and get the full member experience.
Join For FreeHumankind 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 an online platform that passes your order to your nearest bakery once an order is placed and a 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 decentralized 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 the 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?
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.
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
The template is a 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
The name of the Template is preceded by template
the 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 the form of the record type.
Signatory
where
signatory owner
Signatories are the parties that 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
A 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 Declaration
token_test_1 = scenario do
The scenario is a top-level variable and introduced using scenario do
that begins the block.
Party Initialization
alice <- getParty "Alice"
The function getParty
initializes the party with the 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
a keyword. The submit
keyword takes two arguments party
and Update
.
Update
is a recipe for the transaction. create Token with owner = alice
is an Update
, which translates to the transaction creating a Token
with the 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.
Published at DZone with permission of Krishna Singh. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Using OpenAI Embeddings Search With SingleStoreDB
-
Tactics and Strategies on Software Development: How To Reach Successful Software [Video]
-
Knowing and Valuing Apache Kafka’s ISR (In-Sync Replicas)
-
A Data-Driven Approach to Application Modernization
Comments