Skip to content

Plutus Pioneer Program - Part 2: How to “deploy” a Smart Contract in Cardano

Plutus Pioneer Program - Part 2: How to “deploy” a Smart Contract in Cardano

This blog post will demonstrate how to simulate the execution of a smart contract and how to **“**deploy“ it to the testnet through using various tools. Using the term “deploying” a smart contract is not accurate in the context of the Cardano Blockchain. We will explain the reason behind it later.

I will try to break down technical concepts using straightforward and simple language. My goal is to make these technical terms understandable for all audiences. Ultimately, this blog post is a worthwhile not only for DApps developers but also for non-technical Cardano community members. After all, the knowledge here will empower your faith in Cardano when you discover Plutus’ various powerful tools, helping every developer to build scalable and secure DApps.

In this blog post, we will present the concepts explained in the third lecture of the Plutus Pioneer Program, where we implemented a vesting smart contract. Here, a donor locks a specific fund in a script address. Then, only certain beneficiaries can obtain these funds once a deadline has been reached.

1- Emulator Trace Monad:

There are two ways to simulate a smart contract’s execution. The first method by the Plutus playground was outlined in the previous blog post.

However, the second method involves using the emulator trace monad. Contrary to the Plutus Playground, where you should write and configure the simulated scenario by hand, the emulator trace monad forces you to script the simulator tab.


Figure 1: Overview of final state of the wallets after the smart contract execution using the emulator trace monad

Maybe the notion of monads is a little bit strange for you; this is especially true if you are not familiar with functional programming. That’s why Lars has explained the notion of monads in lecture 4 through a separate long-form video (around one and a half hours).

Although monads may seem a little complicated, learning this concept is extremely useful when working with Plutus. This is because you have to understand the two types of monads in Plutus. These are the emulator trace monad, as described previously, and the contract monad, which allows you to write off chain code. Also, the contract monad is also called the wallet code. Finally, the same logic of the contract monad could be constructed using the Cardano cli.

2- Preparing for the contract deployment to the cardano testnet

Contrary to Ethereum, the Validator Script in Cardano doesn’t live in the Blockchain. Instead, the hash of the script is submitted within the transaction when interacting with a smart contract instance. Thus, the notion of “deployment” doesn’t exist in Cardano. However, for the sake of this example, we will still use the term contract “deployment” in the blog post.

As discussed at the blog’s beginning, we have written a vesting smart contract. After simulating contract execution by using the Plutus Playground or the Emulator trace monad, we can deploy it to the testent and execute it.

But, first, we need to complete some preparatory work.

2.1- Installing and Running a Cardano Node and Creating the addresses and the key pairs:

To interact with the Cardano testnet blockchain, we must submit transactions using the Cardano CLI. Hence, we have to install and run a node on the testnet.

There is vast documentation available to show how to complete that process perfectly.

You can find such help in this link.

Next, we should create two addresses and their corresponding keypairs. The first address should correspond to the donor, while the second address will correspond to the beneficiary.

Figure 2: A Shell script to run a Cardano Node locally on the Cardano testnet

Figure 3: Cardano Node running and synchronizing with the Cardano testnet

2.2- Requesting funds from the faucet

To submit transactions to the blockchain, we must first fund both addresses with some Test ADA in our address.

The request can be submitted through Cardano testnet faucet.

Figure 4: Cardano testnet faucet

2.3- Serialize and write to disc some Plutus Types to be able to deploy the validator script using Cardano CLI

To be able to deploy a Plutus script using the Cardano cli, we must do some preparation work. Also, we have to make changes to the script to serialize and write to disc various Plutus types. For simplicity reasons, I will not outline all steps. Instead, to learn more about that, you can check lecture 3 of the Plutus Pioneer Program.

3- Deploy the Smart Contract with the Cardano CLI:

After making the changes to the Plutus script, we can now build and generate the script file and the script address.

Figure 5: Building and generating the script file and address using the Cardano cli

Next, we can construct a “give” shell script, which consists of building the transaction and locking the funds at the script address.

After submitting these transactions, we can verify that the funds sent from our first address are sitting at the script address and locked, waiting for the deadline to pass and for the beneficiary address to grab them.

Figure 6: Overview of the “give.sh” shell script

We can now construct the “grab” shell script. This script allows the beneficiary address to grab the funds locked in our contract upon reaching the deadline.

After executing the “grab.sh” script, we can check that all funds have been transferred from the script address to the beneficiary address.

Now, we have successfully tested the full deployment and execution scenario on the testnet using the Cardano cli.

Figure 7: Overview of the “grab.sh” shell script

4- Deploy the Smart Contract with the Plutus Application Backend (PAB)

There is a second way to deploy a smart contract using the PAB. With this approach, you can avoid changing the types manually in the Plutus Script, as previous point 2.3 of this blog post describes. Using the power of the PAB, we can interact with an instance of our contract simply using some APIs.

Figure 8: SwaggerUI API Specification for Interacting with the Smart Contracts through the PAB using APIs

Still, first, we must set up and run various tools: the Plutus PAB, the Cardano Wallet, the Chain Index, and a Cardano Node.

Figure 9: The hosted deployment scenario for the PAB with the WBE (wallet backend)

Lars effectively explains this method in lecture 6 of the Plutus Pioneer Program.

To know more about the PAB, you can check this link.

You can find the original article published in PeakChain Medium Publication under this link.

Support also our PeakChain Automotive Solutions in Project Catalyst Fund 9!

1- PeakChain Car Wallet Device

2- PeakChain Uber on Cardano

3- PeakChain Fleet Management Platform

4- PeakChain Car-Sharing Platform

Author: Oussama Benmahmoud, CEO of PeakSoft GmbH and Founder of PeakChain