The goal of this tutorial is to learn the basics of building interactions between modules in Pact. You will go over some of the fundamentals, then build a smart contract that allows users to both make and authorize payments using multiple Pact modules.

Key Takeaway

Modules can make use of functions within other modules. This is done first with use, and then by calling the functions from within the module you are using. Each of these files are then loaded into the REPL where you can specify function calls and test interactions between these modules.

Contract interaction allows you to set up more complex smart contracts by working with modules across multiple files. Having access to separate files allows you to break up your smart contracts into more manageable sizes. It also allows you to use helpful files that others have created.

In the image below, you can see that there are 2 files, module-1 and module-2. In this case, module-2 is using module-1 to call functions that exist within the module. The REPL file then loads and uses both of these modules to call functions and facilitate the interaction.

The three most helpful Pact functions to keep in mind for Contract Interaction are load, use, and function calls.

Load

Load and evaluate a file.

(load "file.repl")

Use

Import an existing module into a namespace.

(use MODULE)

Once the interaction is set up between files, all function calls can be done the same as if the function existed within that file.

The idea of working with multiple modules and files is easiest to demonstrate with an example. For that reason, the rest of this tutorial is focused on getting you up and running with your own project.

Before getting started with this application, take a look at the visual overview. This provides a summary of each of the features you will be creating for the Pact Contract Interaction project.

The goal of this project is to write a function in auth.pact to use within payments.pact. Payments.pact will then use auth.pact to call specific functions. You will then load modules from both of these files into the payments.repl file to run the interaction between these contracts.

Project Files

Each number in the image corresponds to one of the files you will work with.

auth.pact

Responsible for authorizing users.

payments.pact

Responsible for handling payments between users.

Payments.repl

Loads and runs modules from both auth.pact and payments.pact.

Note

Both auth.pact and payments.pact are smart contracts that you have worked with in previous tutorials. If you would like to learn more about these smart contracts, feel free to view each of their tutorials.

There’s not much code to write for this project. Most of it has been written previously in the tutorials linked above. For this tutorial, focus on understanding the interactions between the smart contracts, and to take some time to study how the functions are interacting with each other.

Making the conceptual leap toward understanding the interactions between modules is going to be extremely valuable as you make more complicated smart contracts.

The function you will write will enforce user authentication for payments made with accounts. This will be a valuable function for restricting access to either admins or account users, ensuring each user is the only person that has control over their account.

Code Challenge

Define a function enforce-user-auth that you can use to verify user authorization.

Congratulations on completing this introduction to Pact Contract Interaction!

In this tutorial, you learned the basics of contract interaction and built a smart contract that allowed users to both make and authorize payments. This allowed you to extend the functionality of your smart contract to include multiple files, and you can now use this to build more complex smart contracts in any project.

Take a look back at some of the modules you have written or examples that you’ve come across. Are there any interesting new ways that you think these contracts could work together? Give it a try and see if you can come up with an entirely new smart contract that combines these interesting features in new ways!