How to build DApps on the Cardano blockchain - LogRocket Blog (2024)

The Cardano blockchain is one of the fastest-rising and most widely used blockchains in 2022. The Cardano blockchain is a third-generation, proof-of-stake blockchain platform and home to the ADA cryptocurrency.

How to build DApps on the Cardano blockchain - LogRocket Blog (1)

In this tutorial, we’ll show you how to build a DApp on the Cardano blockchain. We’ll cover the following:

  • What is Cardano?
  • What is a blockchain?
  • What are DApps?
  • What is ADA?
  • Cardano and native tokens
  • What is Plutus?
  • Writing smart contracts using Plutus
  • Example: Writing a “Hello, World!” app in Plutus
  • Example: sayInput

Let’s begin 🎉

What is Cardano?

The Cardano blockchain is a new blockchain based on the Shelley consensus algorithm. It’s designed to be a secure, scalable, and easy-to-use platform for building and running decentralized applications.

Shelley is a software stack that provides a layer of abstraction over the underlying blockchains.

What is a blockchain?

A blockchain is a distributed database of records linked together by a series of cryptographic proofs. The records are called blocks; the blockchain is a collection of blocks.

Ethereum is a blockchain that is based on the Ethereum Virtual Machine. To make transactions on the Ethereum blockchain, we’ll spend some Ethers. This is the cryptocurrency that is used to pay for the gas in the Ethereum blockchain.

Likewise, in the Cardano blockchain, we’ll spend ADA to pay the gas fees.

What are DApps?

A decentralized, distributed app is an application that is not controlled by a single entity. Otherwise, a DApp is just like any conventional app. A DApp can be a web app, command-line app, desktop app, or mobile.

The difference between a DApp and a conventional app is that the conventional app is controlled by a single entity. A DApp has a blockchain as its backend.

For example, a React to-do app might have its data (the to-dos) stored in an Ethereum blockchain. Or, you could build a voting system in Angular and have its data stored in the Cardano blockchain.

What is ADA?

As mentioned above, ADA is the native cryptocurrency of the Cardano blockchain. You can use and transfer ADA on the Cardano blockchain just as you would ETH on the Ethereum blockchain.

ADA can be used as both a cryptocurrency and a token. ADA being used as Cardano’s native currency means that it is:

  • Accepted as fee-payment
  • Accepted to make deposits
  • The only currency in which rewards are distributed

Just like ETH is divided into denominations (e.g., Wei), ADA has denominations as well. Lovelace is the smallest unit of ADA; 1 ADA is equal to 10^(-18) Lovelace, which can be divided into a total of 1,000,000 Lovelaces.

Cardano and native tokens

The Cardano blockchain makes it possible to build native tokens. These tokens can be used to represent a value and to trade on the Cardano blockchain.

Tokens can be fungible or non-fungible. Fungible tokens represent a value, whereas non-fungible tokens represent real world items (e.g., a car or a piece of artwork).

In the following sections, we’ll walk through how to write and create smart contracts on the Cardano blockchain using the Plutus programming language.

What is Plutus?

Plutus is a programming language used for writing smart contracts on the Cardano blockchain. Other programming languages you can use for the Cardano blockchain include Glow lang, Marlowe, etc., but Plutus is the fastest among them.

Plutus is based on Haskell, so using it requires prior knowledge of Haskell programming.

Writing smart contracts using Plutus

Let’s walk through how to write smart contracts using the Plutus programming language. These apps can run off-chain and manage active contract instances.

According to Cardano Testnets, Plutus contracts consist of parts that run on the blockchain (on-chain code) and parts that run on a user’s machine (off-chain or client code). We’ll write our smart contracts in a Plutus online editor and simulator.

A Plutus smart contract is a Haskell code that runs in a user’s wallet and sends code to the blockchain to be run by the nodes in the blockchain. The smart contracts are run on the blockchain, not on the user’s machine.

Plutus smart contracts have definitions that are used to define the smart contract and its state. Inside these definitions are endpoints, which define the smart contract’s behavior. These endpoints are functions that are executed by the wallet. They are known as off-chain functions and are used to build transactions and send them to the blockchain.

Example: Writing a “Hello, World!” app in Plutus

Let’s write a simple DApp in the Cardano blockchain using the Plutus programming language.

More great articles from LogRocket:

  • Don't miss a moment with The Replay, a curated newsletter from LogRocket
  • Learn how LogRocket's Galileo cuts through the noise to proactively resolve issues in your app
  • Use React's useEffect to optimize your application's performance
  • Switch between multiple versions of Node
  • Discover how to use the React children prop with TypeScript
  • Explore creating a custom mouse cursor with CSS
  • Advisory boards aren’t just for executives. Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.

We will write a smart contract that will print “Hello, World!” to the console.

Open the Plutus online editor and simulator.

import Data.Text qualified as Timport Playground.Contractimport Plutus.Contractimport PlutusTx.Preludeimport Prelude qualified as Haskell-- | A 'Contract' that logs a message.hello :: Contract () EmptySchema T.Text ()hello = logInfo @Haskell.String "Hello, world"endpoints :: Contract () EmptySchema T.Text ()endpoints = hellotype DummySchema = Endpoint "dummy" ()mkSchemaDefinitions ''DummySchema$(mkKnownCurrencies [])

The above will already be populated for us. Let’s go through the code:

import Data.Text qualified as Timport Playground.Contractimport Plutus.Contractimport PlutusTx.Preludeimport Prelude qualified as Haskell

Here, we imported the libraries and built-in functions we will need:

  • The Data.Text library is used to convert strings to text
  • The Playground.Contract imports the smart contract interfaces defined in the playground
  • The Plutus.Contract library is used to define the smart contract from the Plutus core library
  • The PlutusTx.Prelude library replaces the normal Haskell Prelude library and includes functions that are refined and easier for the PlutusTx compiler to compile
hello :: Contract () EmptySchema T.Text ()hello = logInfo @Haskell.String "Hello, world"

The above defines a hello function. The Contract () EmptySchema T.Text () tells the compiler the function will return nothing. The logInfo function is a built-in function that logs a message to the console. So, in summary. the hello function will log Hello, world to the console.

endpoints :: Contract () EmptySchema T.Text ()endpoints = hello

This creates an endpoints function, which will be used to run the hello function. This exposes the hello function to the blockchain.

type DummySchema = Endpoint "dummy" ()mkSchemaDefinitions ''DummySchema$(mkKnownCurrencies [])

The above creates the DummySchema type, which is used to define the smart contract’s state. Finally, it exposes the endpoints to the blockchain.

In our Plutus playground, we can click on the green Compile button to compile our contracts. The blue Simulate button will open up a page where we can run the smart contracts on the blockchain.

How to build DApps on the Cardano blockchain - LogRocket Blog (4)

Click on the green Compile button. After a successful compilation, the blue Simulate button will open a page where we can run the smart contracts on the blockchain:

How to build DApps on the Cardano blockchain - LogRocket Blog (5)

How to build DApps on the Cardano blockchain - LogRocket Blog (6)

You’ll notice that we have two wallets, each with an open balance of 100 Lovelaces. The Available functions in each wallet will show the functions that are available to be used.

In our case, we see our hello function and another Pay to Wallet function. The Pay to Wallet function is used to send funds to another wallet, and it’s defined by the Plutus contract. We don’t get to see the code because it’s added there when we import the below modules:

import Playground.Contractimport Plutus.Contract

If we look in the below part of the page, we’ll see an Actionssection. This section is where the functions we want to run are added when the wallet addresses card above is clicked on.

We have a card in the Actions already, the card simulates delay on the blockchain. We can remove this card by clicking on the “X” icon.

Now, click on the dummy button and you’ll see a card appear on the Actions section:

How to build DApps on the Cardano blockchain - LogRocket Blog (7)

Click on the green Evaluate button and you will see the hello function run on the blockchain. After some time, a Transactions page will appear:

How to build DApps on the Cardano blockchain - LogRocket Blog (8)

Scroll down to the Logs sectionand you will see the hello function log Hello, world to the console:

How to build DApps on the Cardano blockchain - LogRocket Blog (9)

Example: Using the sayInput function

Let’s write another program that will take a string and print it to the console.

Add the below code to your Plutus online editor:

import Data.Text qualified as Timport Playground.Contractimport Plutus.Contractimport PlutusTx.Preludeimport Prelude qualified as Haskelltype Schema = Endpoint "sayInput" Haskell.Stringcontract :: AsContractError e => Contract () Schema e ()contract = selectList [sayInput]-- | The "sayInput" contract endpoint.sayInput :: AsContractError e => Promise () Schema e ()sayInput = endpoint @"sayInput" $ \inputValue -> do logInfo @Haskell.String $ inputValueendpoints :: AsContractError e => Contract () Schema e ()endpoints = contractmkSchemaDefinitions ''Schema$(mkKnownCurrencies [])

We’ve already seen the first part of the code; here, we imported the libraries we need to run the app.

Next, we defined a Schema type. This type is used to define the smart contract’s state. After that, we set up a contract endpoint list and added the sayInput endpoint.

We also defined the sayInput endpoint, which takes a string as input and logs it to the console.

Finally, we made the endpoint available to the blockchain.

Click on the green Compile button. After a successful compilation, you should see this:

How to build DApps on the Cardano blockchain - LogRocket Blog (10)

Click on the blue Simulate button. This will open up a page where we can run the smart contracts on the blockchain.

How to build DApps on the Cardano blockchain - LogRocket Blog (11)

Notice that we have the sayInput function on the wallet addresses. Click on it.

The sayInput function will appear on the Actions section:

How to build DApps on the Cardano blockchain - LogRocket Blog (12)

Now, type any string into the input field and click on the Evaluate button:

How to build DApps on the Cardano blockchain - LogRocket Blog (13)

You’ll see the string you typed in the Transactions page.

Conclusion

In this tutorial, we demonstrated how to write a DApp on the Cardano blockchain using the Plutus programming language.

We started by introducing the Cardano blockchain and the Plutus programming language. Next, we explained Cardano by comparing it to the Ethereum blockchain. We then introduced the Plutus programming language and described how we can use it to compile and run smart contracts on the Cardano blockchain.

Finally, we walked through some examples of smart contracts written in the Plutus programming language and run on a simulated Cardano blockchain.

Join organizations like Bitso and Coinsquare who use LogRocket to proactively monitor their Web3 apps

Client-side issues that impact users’ ability to activate and transact in your apps can drastically affect your bottom line. If you’re interested in monitoring UX issues, automatically surfacing JavaScript errors, and tracking slow network requests and component load time, try LogRocket.https://logrocket.com/signup/

LogRocket is like a DVR for web and mobile apps, recording everything that happens in your web app or site. Instead of guessing why problems happen, you can aggregate and report on key frontend performance metrics, replay user sessions along with application state, log network requests, and automatically surface all errors.

Modernize how you debug web and mobile apps — Start monitoring for free.

How to build DApps on the Cardano blockchain - LogRocket Blog (2024)
Top Articles
Disadvantages Of Blockchain Technology
What is data sharing? Everything you need to know
Snohomish County Craigslist General For Sale
Retire Early Wsbtv.com Free Book
Geheimtipp Anna Maria Island- die schönsten Strände Floridas | Reiseblog und Fotografieblog aus Österreich
R&B Music Near Me Tonight
Clafi Arab
Aldi Nearme
Hypno Deviantart
Pa Speedtest Rcn Merlin
Seat Number Usana Seating Chart With Rows
Pogoda długoterminowa Warszawa na 16, 25 i 45 dni – Długoterminowa prognoza pogody w INTERIA.PL
Armslist Kansas City Missouri
공지/공고 - 10 Things That Your Family Teach You About Triple Bunk Bed With Mattresses
Cavalli Residential Flat Arabian Peninsula
Dupage County Fcrc
Varsity Competition Results 2022
Shadow Under The Mountain Skyrim
Pollen Count Los Altos
German American Bank Owenton Ky
Petco Clinic Hours
The Boogeyman Showtimes Near Century 20 Daly City And Xd
Craigslist Class A Drivers Texas
Jessica Oldwyn Carroll Update
Griffin Exterminating New Bern Nc
Sams La Habra Gas Price
Uncensored Wisconsin Volleyball Photos
Peach Cat Gifs
15+ Best Things to Do in St George Utah » Local Adventurer
Rural King Annual Flowers
Methodist Laborworkx
Savannah Riddle Marshall Tx
Gina Wilson All Things Algebra Unit 3 Homework 2
Ew41.Ultipro.com
Mikahhlynn Instagram
Bbwchan Blueberry
Craigslist Pets Huntsville Alabama
How to Sell Cars on Craigslist: A Guide for Car Dealers | ACV Auctions
Antiterrorism Level 1 Pretest Answers
Schüleruniversität | TU Bergakademie Freiberg
23 Thrift Stores In TEXAS (Quirky, Vintage, & Distinctive)
Deleon Malik Taylor-Griffin
Mychart.exact Sciences Labs Com
Why rivalry match between Pitt and Penn State volleyball is bigger than the Xs and Os
Craigslist Louisiana Cars And Trucks - By Owner
Ap Spanish Score Calculator
E21 Ultipro Com
Kuronime List
Tiraj Florida 1H30
The meaning and origin of the name Tetiana
Compass | History, Uses & Types
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6120

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.