In this tutorial, you'll learn how to create, deploy, and run functions on a Hello World smart contract with Pact.

Topics covered in this tutorial

Hello World Overview

Write the Smart Contract

Deploy to the Testnet

Call the Deployed Contract

The goal of this tutorial is to help you get familiar with the essential Pact concepts needed to write, deploy, and run a smart contract. Learning these concepts will help you to create more complex smart contracts later in the series.

Key Takeaway

It’s simple to create and deploy a Hello World smart contract with Pact. You can deploy smart contracts from the online editor, call its functions, and view the output from the REPL.

To get started, navigate to the Pact online editor at pact.kadena.io/. Once on the site, you can open the “Hello World” module. To do this, navigate to the module explorer on the tool panel and open the “Hello World” example.

Note

If you’re not familiar with the module explorer, you can learn more here.

You can also copy the code below and paste it into your editor.

;;;; "Hello, world!" smart contract/module;;;;---------------------------------;;;; Create an 'admin-keyset' and add some key, for loading this contract!;;;; Make sure the message is signed with this added key as well.;;;;---------------------------------;; Keysets cannot be created in code, thus we read them in;; from the load message data.(define-keyset'admin-keyset(read-keyset"admin-keyset"));; Define the module.(modulehelloWorld'admin-keyset"A smart contract to greet the world."(defunhello(name)"Do the hello-world dance"(format"Hello {}!"[name])));; and say hello!(hello"world")

As you can see, the “Hello World” smart contract starts by defining and reading a keyset. Keysets are a way to specify credentials for a user of the smart contract. The code you write within the smart contract can restrict access to users that own the keysets you define. You will see this done in the Hello World module.

Line 16 (shown below) defines and reads a keyset named admin-keyset.

(define-keyset'admin-keyset(read-keyset"admin-keyset"))

Error

If you look on line 16 line you will notice the following error. No such key in message: “admin-keyset”

This error exists because you are reading a keyset that does not exist. To get rid of this error you need to create a keyset named admin-keyset using the panel on the right.

The next step is to create the contract functions. Functions are defined within Pact modules using the keyword defun. For this smart contract, you’ll define a function named hello that takes a parameter called name.

Within this function, you’ll write a formatted line to output into the REPL. You can do this using the keyword format. Format allows you to manipulate strings and will help you specify the output of the function.

(format"Hello {}!"[name])

This line outputs a string to the REPL that says Hello followed by the string input to the function parameter name.

The module for this smart contract is already complete! While this module is pretty simple, it gives you a great start to begin building more complex smart contracts.

Loading into the REPL helps you test that your contract is working correctly, but it doesn't deploy it to a blockchain. When you're ready, you'll need to complete a few more steps to deploy your Hello World smart contract.

Here’s a quick summary of the steps needed to deploy to the blockchain.

First, you need to update the module name. This name needs to be unique across all module names that exist on the blockchain.

Choose any unique name you’d like. For example, my module name is helloWorld-tutorial (choose a unique name that is different than mine).

(modulehelloWorld-tutorial'admin-keyset;; code goes here)

Note

This unique module name is a requirement caused by a feature in Pact that allows you to upgrade your smart contracts. You will explore this idea in further detail later when you learn about upgrading contracts.

Error

You may come across the following error message when you attempt to deploy your contract.
terminal
ERROR: Command execution failed: (read-keyset "admin-keyset-hel...: Failure: No such key in message: "admin-keyset-helloworld"
This error means that an existing module on the blockchain already has the name you chose. Update the module name and redeploy the contract to resolve this error.

Along with a unique module name, you’ll also need a unique keyset name. This unique keyset ensures that only users with the keyset you define can gain access to the module. Delete the admin-keyset and create a new keyset with any unique name you like.

At the top of the screen, select Deploy. This will open a box that allows you to set your deployment settings.

Once in the deployment settings, select either test-chain-01 or test-chain-02 to deploy your contract. Next, select the checkbox to sign this deployment with the key you created. Finally, select "deploy contract".

If everything worked correctly, you should now see the string “Hello World!” appear on the right panel as a message.

At this point, you have deployed your Hello World smart contract with Pact. Congratulations!

To do this, you’ll need to first find the contract from the module explorer.

Start by selecting the module explorer from the tool panel. Once there, navigate to the Deployed Contracts section and type the name of your contract. The name of your contract will be the same name as the module you created. After it appears, select view to see functions that are available for your smart contract.

Throughout this tutorial you built, deployed, and ran functions on your Hello World smart contract with Pact. You’re now prepared to play around and try anything you’d like. You can create and define new keysets, change the parameters, or add anything that comes to mind.

If you get lost, you can always go back to the module explorer to reload the original code. You can also visit the documentation using the link on the top right of the screen. Here you'll find more information about terminology, syntax, and other important ideas. If you’re up for it, you can also load in different modules using the explorer to see how a different program looks.