Rift Simplified
Getting Started
Install Rift
To start, ensure you have Python 3.10 or higher installed. Then, install Rift with the following command:
Initialize the Project
Open your terminal and run the command below to set up a new project:
The –base option determines the template used for the initial setup. You should see a confirmation message like this:
Project Structure
After setting up your project, open the directory in your preferred editor or IDE. If you don’t have one, we recommend using VSCode. The project directory structure will look like this:
Project Setup
The project setup details are found in the project.toml file. This file must be updated to define new targets, include test and deployment scripts, or adjust Rift’s configuration. Below are the contents of this file:
Contracts Directory
All of your contract files will be located in the contracts/ directory. These contracts are implemented as classes that extend the base Contract class. The code within these contracts is executed, and the execution flow is tracked and translated into FunC (and eventually TVM). Any operations that cannot be traced will still be recorded in the execution results. For more in-depth information, refer to the documentation here.
Tests
Test scripts should be placed in the tests/ directory. Make sure to list the test script in your target configuration file. These scripts are straightforward Python files; you can import contracts from the contracts package and utilize standard Python code for testing purposes.
Deployers
Deployment scripts should be located in the deployers/ directory. The script specified in your target configuration will be executed whenever you run the command rift deploy [TARGET].
Storage Contract
Specifications
In this section, we will create a storage contract designed to hold an unsigned 64-bit integer value. The contract will have the following features:
- The admin of the contract will be able to update this value by sending an internal message.
- A get-method will be provided to access the stored data.
- Configure the Project
We’ll start by restructuring the existing files. Rename contracts/bare_template.py to contracts/storage.py and update its contents as follows:
The next step is to make the necessary adjustments to the project configuration in project.toml in order to reflect the aforementioned changes.
- Define the Contract Data
Add the Data class to the Storage class to define the structure of the. This class should extend Model:
- Define the contract message structure
Specify the structure of the contract message body by adding a Payload class:
- Implement the change Function
In the internal_receive function, check if the sender is the admin and update the data with the new value. You can access the full message via self.message and the message body with self.body:
- Specify the get-method
Define a get-method to expose the value. There are two approaches:
- Automatic Handling: Add a configuration docstring to the class for automatic handling during compile time:
This method is suitable for directly exposing a field without additional processing.
- Manual Method Definition: Define a new method in the contract with the @method_id() annotation:
- Finalise
Your contract is now complete. Compile it using the following command:
The compiled files will be located in the build/ directory.
Testing
A crucial component of developing contracts is verifying their accuracy and security. Even small errors can lead to substantial financial repercussions for those involved. Thus, thorough testing with well-designed test cases is a vital part of the development process. Fortunately, Rift offers an integrated testing framework that simplifies the process of testing contracts.
To start, rename the tests/test_example.py file to tests/test_storage.py and update it with the following code:
In the testing setup, we can effortlessly import the target contract and create an instance using any cell as its initial data. Afterward, we can invoke its methods and evaluate the outcomes.
To start, we’ll test the get_value method to verify that it accurately retrieves data from our cell:
To test the get_value method, we first create a Storage instance with an initial data cell containing the admin address and a value of 1. We then call the get_value method and verify that it correctly returns the value:
Next, we will test the internal_receive method to ensure it correctly handles the change_value call. We will create a message to update the value and check if the new value is reflected:
We should also verify that the contract does not allow unauthorized changes by testing with a different admin address:
To execute the tests, use the following command in the terminal:
The output should reflect the test results accordingly.
Deploying the Contract
In this revised deployment function, we first obtain our wallet and its address. We then prepare the initial contract data, which includes the admin address and sets the initial value to 0. Next, we create an initial message for the contract, setting the new value to 1. Finally, we submit the deployment request with 0.1 TON allocated for fees.
To deploy the contract on the testnet, use the following command:
On your first attempt, you will need to set up a wallet with Rift and ensure it is funded. Once you execute the command, you should receive a confirmation message indicating that the contract is being deployed to a specific address.
Conclusion
This tutorial has guided you through the process of developing and deploying a basic storage contract on the testnet using Rift. This serves as a foundation for future development tutorials with Rift. You can access the full project implementation here. For updates and to join our community, check out our channel. We focus on providing clear, step-by-step guides and continuously update our documentation. Stay tuned for more!