Pact Accounts and Transfers
Last updated
Last updated
This tutorial covers the following topics
The goal of this tutorial is to help you build an application that transfers value between two accounts. To do this, you’ll build a smart contract that implements this functionality named Simple Payments. This is an important function of smart contracts and will set you up to create more complex applications using accounts and transfers.
To get 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 simple payment smart contract.
As you can see, you will create a payments module including 3 functions; create-account, get-balance, and pay. These functions will store data on a payments-table which manages payments between 2 accounts Sarah and James.
Now that you have a basic understanding of the requirements, you can start building the project for yourself!
To get started, choose a project directory and clone the project resources into your local environment.
Change into the loans directory to begin working on this project.
Open this directory in atom to see each of the files provided.
As you’ll see, there are a few separate folders available to you.
start
Provides a starting point with all comments for every challenge.
challenges
Challenges in the demo are broken out into separate files, allowing you to build your application over time while having the flexibility to experiment with your own ideas.
finish
Includes all comments and code for the final application.
loans
Includes final application without the challenge comments.
Each of these options are meant to help support you as you work through these challenges. Feel free to use them however you’d like to.
The first step is to set up the module and keysets for the smart contract.
Code Challenge
Define and read the admin-keyset, create the payments module, and give the admin-keyset access to the module.
The next step is to define the schema and table for the smart contract.
The payments-table, will keep track of the balance of the accounts and associate that to the account’s keyset.
Payments Table
balance
decimal
keyset
keyset
Code Challenge
Define a schema and table with columns balance and keyset.
This smart contract will contain 3 functions create-account, get-balance, and pay. Each of these are essential functions to allow users to manage their accounts.
First, add a function that allows the administrator to create accounts. This will allow you to add as many accounts as you’d like.
Code Challenge
Create a function create-account that allows administrator to create accounts.
Now that you can create accounts, it is helpful to be able to view the balance of these accounts. In this case, we’ll allow both users and administrators to view the balance.
Code Challenge
Create a function get-balance that allows administrators and users to view the balance of their account.
Next, you’ll create the function that allows one account to pay another account. This allows accounts to transfer value from their account to another to begin making payments and managing their finances.
Code Challenge
Create a function pay that allows an account to pay another account.
You have now completed the module. Outside of the module you can create the table that you defined earlier.
Code Challenge
Create the payments-table.
The next step is to create the accounts that will transfer value.
For this tutorial, create 2 accounts.
Sarah
James
To do this, you use the create-account function built earlier. This function takes 3 arguments; id, initial-balance, and keyset.
Code Challenge
Call the create-account function to create accounts for Sarah and James.
The final step is to make a payment from one account to another. You can do this using the pay function created earlier.
Code Challenge
Use the pay function to transfer 25.0 to James from Sarah’s account. After making the payment, read the balance of both Sarah and James.
Congratulations, at this point you have completed the Simple Payment smart contract!
If you’d like, you can try deploying this smart contract. You can deploy this contract using the Pact Online Editor or from the Pact Atom SDK. If you choose to deploy this locally, you’ll need the REPL file which you can find inside of the repository you cloned.
For help getting started and deploying in each of these environments, try the following tutorials.
Congratulations on completing the Accounts and Transfers Tutorial!
In this tutorial, you built a Simple Payment application that creates accounts, views account balances, and makes payments between accounts. This is an important function of smart contracts and will set you up to create more complex applications using accounts and transfers.
This is a key feature of many smart contracts and can be extended into all types of use cases. Take some time now to experiment with these features to try them out in creative new ways.
Subscribe to our to access the latest Pact tutorials.
If you’re unfamiliar with modules and keyset, our is a great place to get started.
Schema definitions are introduced in the .
You can review each of the function types in the as well as the .
Try using to regulate who has access to create an account.