In this tutorial you'll learn some fundamental ideas needed started with Pact. You’ll review some of the built-in functions Pact provides, review the language syntax, and write a few functions for yourself.

Topics covered in this tutorial

Pact Language Reference

Pact Syntax

Basic Commands

Built in Functions

Create Functions

Key Takeaway

The Pact Language Reference includes an overview of the syntax, basic commands, and built-in functions you’ll use to create smart contracts. By getting familiar with these commands, you’ll be prepared to build simple and safe smart contracts with the Pact programming language.

Follow Along

Open the online editor at pact.kadena.io to follow along with this tutorial. You can run each of the commands described to get more familiar with the Pact programming language.

To get started with the Pact Language Basics, navigate to the Pact Smart Contract Language reference. The Pact Smart Contract Language reference describes the syntax and semantics of the Pact language.

If you haven’t already, take some time to review this documentation to better understand the Pact language.

Start by reviewing the language syntax. Pact syntax can be found using the navigation on the left or by scrolling down to “Syntax” on the home page.

Try running each of the commands on this page in the Pact Online Editor. Along with those presented in the documentation, this tutorial reviews some of the more common commands you’ll work with when developing smart contracts.

Running basic operations in Pact is simple. Try running each of the commands below in your editor to get more familiar with basic Pact commands.

Note

If you are comfortable with data types from other languages, feel free to skip to the built-in functions section. Pact follows conventional standards that are reviewed here to help those get started who may be new to programming.

Try to run the following string. Do you know what is causing the error?

pact > hello
:0:0: Cannot resolve hello

What went wrong?

The issue is that Pact does not know what hello means. To make it a string, remember to put quotes around it.

pact > "hello"
hello

There is also an alternative way to represent strings using Pact.

pact > `hello
hello

Preceding strings with a single ` is commonly used to mark unique items such as function or table names. It does not support whitespace or multi-line strings, but can be a helpful way to identify strings more succinctly.

Time has many different properties and is supported by Pact. Look through the following example along with the supporting summary of time formats to better understand one possibility of representing time with Pact.

After getting familiar with the Pact language syntax, you are ready to start working with some of Pacts built-in functions. To access the Pact built-in functions, navigate to Built-in Functions using the navigation on the left.

As you’ll see, there are many functions organized into a few key categories.

Pact Standard Library Built-in Function Categories

General

Database

Time

Operators

Keysets

Capabilities

REPL-only functions

While many of these are valuable to learn, this tutorial will focus on only a few of them to get you started. Many other functions will come up in later tutorials as they become relevant to the programs you create.

Another group of helpful operators is the comparison operators. These check whether values are equal, not equal, greater than, less than, and includes other common comparisons.

Here’s a table showing each of the comparison operators available in Pact.

Along with arithmetic operators and comparison operators, Pact supports boolean, exponential, rounding, and many other common operators. You can learn more about each of these in the operator documentation.

!=

True if X does not equal Y.

(!= "hello" "goodbye") = true

<

True if X < Y.

(< 1 3) = true

<=

True if X <= Y.

(<= 5.24 2.52) = false

=

True if X equals Y.

(= [1 2 3] [1 2 3]) = true

>

True if X > Y.

(> 1 3) = false

>=

True if X >= Y.

(>= 1 3) = false

Along with arithmetic operators and comparison operators, Pact supports boolean, exponential, rounding, and many other common operators. You can learn more about each of these in the operator documentation.

To create a binding, use the keyword bind followed by a source object. Follow this object with another object containing a specific value within the source using the := symbol.

pact > (bind { "a": 1, "b": 2 } { "a" := a-value } a-value)
1

Example

Bindings are valuable when you want to bind the values of a table to a variable. Here is a brief example showing how they can be used within a function.

(defunpay(fromto)(with-readpaymentsfrom{"balance":=from-bal}...code)

This example reads a table named payments that includes a user from that is sending a balance. A binding is used in this case to map the balance, which is a column in the table, to the value of from-bal that is provided by the user.

This allows you to call the balance of the user using the variable balance rather than the variable from-bal.

Map allows you to apply a specific operation to all elements within a list and return the results.

To create a mapping, use the keyword map followed by the operation and the list.

pact > (map (+ 1) [1 2 3])
[2 3 4]

After running this command, a new list is returned with the value 1 added to each element within the original list.

You can also use this operation to map other values, including strings. For example, if you have a list of names, you can map “Hello ” to each of them to returning a friendly message for each list item.

Format allows you to piece messages together using a mix of strings and variables. Formatting is great to use any time you need to send messages to your users.

To get started with the format function, paste the example from the documentation into your terminal. When you run this, you’ll see that what you get back is a string that inserts the variables provided in the postfix into the original string where the brackets are located.

(format"My {} has {}"["dog""fleas"])"My dog has fleas"

You can now place whatever values you’d like into this string.

The first set of curly brackets corresponds to the first value in the list, the 2nd corresponds to the 2nd, and so on for as many values as exist.

Throughout this tutorial, we introduced the basics of the Pact programming language. You went over the Pact Standard Library, basic syntax, commands, and built-in functions. You also wrote a few functions for yourself.

If you’re familiar with other programming languages, a lot of these ideas should be familiar. The goal here was to make sure you know where to find helpful resources and are prepared to start writing smart contracts for yourself.

If you’re new to programming, this tutorial hopefully helped you better understand some of the fundamental concepts of programming. You're now ready to try some new ideas for yourself.

Take some time to explore the documentation and try a few more of the Pact built-in functions. Smart contracts you create will depend on you being familiar with these basic ideas. You'll see plenty more examples of these concepts throughout the upcoming tutorials.