Let’s create a Hello World application in Plutus! We hope this will be the beginning of your epic Cardano programming.
This tutorial covers programming on Cardano platform and will give you a good initial understanding of how Cardano programming works. If you want to learn more we recommend joining Ivan on Tech Academy.
Cardano Plutus Programming 101
In order to write smart contracts on the Cardano platform, you need to understand and be able to use the Plutus programming language. Plutus is a functional programming language with many similarities to Haskell.
The Plutus Playground is composed of three parts – the Editor, the Simulator and the Transactions log.
When you first enter the playground you will see the Editor. The Editor is used for writing Plutus code and will contain some boilerplate code when you enter the Playground for the first time.
Plutus Playground is an educational tool for developers. To make it easier for the developers to understand how different types of applications are built and how Plutus is used to accomplish different tasks for different use-cases IOHK has provided a few demo applications.
You can view these demo applications by clicking on either Crowdfunding, Game, Messages, Starter or Vesting.
Compiling a Cardano Smart Contract
Try to compile a demo application by selecting one and pressing the Compile button at the bottom of the page, you will then be moved to the Simulator tool where you can use a simple interface to call the exposed methods from the smart contract.
If this sounds complicated – don’t worry!
Let’s backtrack a bit.
We will now start from scratch create a very simple Hello World Plutus smart contract that outputs a simple message to the log. This simple application will give you an initial understanding of how a Plutus program works.
Programming Cardano Hello World!
This simple Plutus program shows some of the features of the Plutus playground.
Copy and Paste this to the Editor:
import Playground.Contract (mkFunctions, printSchemas) import Language.PlutusTx.Prelude import Playground.Contract import Wallet(MonadWallet, logMsg) helloWorld :: MonadWallet m => m () helloWorld = logMsg "Hello World" $(mkFunctions ['helloWorld])
In order to compile our program – press the Compile button below the Editor.
After compiling the Simulation tab loads. Select the function helloWorld for a wallet and press Evaluate.
After the functions are evaluated the last tab Transactions are shown with the resulting log message function.
With the import statement, we import functions and definitions from outside modules. Because Plutus is based on Haskell, you can read the Haskell documentation about the import statement to learn more.
The Prelude module contains the framework for Plutus. The library of standard data types, classes, and functions. If you are interested you can check out the source code for this module: Language.PlutusTx.Prelude Source.
If we scroll down in the source code for the module we can see that it imports a subset of the full Haskell Prelude module. There is also a set of Language.PlutusTx.* included to extend the Haskell Prelude module for the smart contract language.
This module contains functions that are needed when creating a Contract. In our example, it’s needed to be able to publish the helloWorld function with mkFuntions.
The Wallet is implementing wallet functions.
In our case, we use the MonadWallet data type and the logMsg function to simply log the message “Hello World”.
The main part
Here we declare helloWorld as a type class MonadWallet and return the logMsg function with the string “Hello World”. Finally, the function is added to mkFunctions to make it available in the Simulation user interface.
helloWorld :: MonadWallet m => m () helloWorld = logMsg "Hello World" $(mkFunctions ['helloWorld])
This was our first Plutus tutorial.
Please leave your questions and comments in the comment section below.