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.
Key Takeaway
Accounts and transfers are a key feature of many smart contracts. Using Pact, you can define tables that track account keysets and values, allowing you to set and update these values as needed.
Subscribe to our YouTube channel to access the latest Pact tutorials.
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.
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.
Info
If you’re unfamiliar with modules and keyset, our Pact Modules Tutorial is a great place to get started.
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
Info
Schema definitions are introduced in the Pact Schemas and Tables Tutorial.
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.
Info
You can review each of the function types in the Schemas and Tables Tutorial as well as the Pact Language Basics Tutorial.
First, add a function that allows the administrator to create accounts. This will allow you to add as many accounts as you’d like.
Info
Try using enforce to regulate who has access to create an account.
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.
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.
You have now completed the module. Outside of the module you can create the table that you defined earlier.
Info
At this point you have completed the module. You will notice the previous challenge containing a final parenthesis to close out the module. The remaining steps are meant to help you call functions from within the module you created to put your smart contract to use.
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.
The final step is to make a payment from one account to another. You can do this using the pay function created earlier.
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.
fieldname | fieldtype |
---|---|
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.
balance
decimal
keyset
keyset