Writing Your First Plutus Smart Contract

img

Writing Your First Haskell Code

Writing Plutus smart contracts requires writing both Haskell code, and Plutus code. This may seem challenging to use two languages at once, but in truth Plutus is simply a subset of Haskell (known as an eDSL) which simplifies a lot of the development process between off-chain and on-chain code. You can read a more in-depth explanation on this topic in my recent post, How Do Plutus Smart Contracts Work?

Carrying on, we will start writing our first Haskell code by naming the file/module which we are working on. While this isn’t strictly necessary, it is good practice.

1
module MyFirstPlutusSmartContract where

This goes at the top of the file, and the rest of you code proceeds after the where. Now that we have named the module, we can import the first two libraries that we require to write our smart contract.

1
2
import qualified Language.PlutusTx            as PlutusTx
import           Ledger

In short, importing libraries starts with an import and then the name of the library (ex. Ledger). This imports all of the exported code from library to be used. However it is also possible to add a qualified to enforce that the user must use the full name when calling a function. Using as, which allows the renaming of the imported library, along with qualified creates a much easier to use interface for the libraries without having the possibility of names conflicting.

So far this is the code we have written:

1
2
3
4
module MyFirstPlutusSmartContract where

import qualified Language.PlutusTx            as PlutusTx
import           Ledger

Feel free to write this code into the Plutus Playground editor and press Compile to make sure that there are no errors. In the next section we will be writing our very first Plutus code.

Get Updates

To keep up to date with the latest lessons, sign up and get them straight in your mailbox.
You can also follow us on Twitter or join our Telegram channel