Environment
Creating a application environment is the first step for developing and testing your applications(dApps) on Cartesi Machine using this HLF. This guide will demonstrate how to set up an environment using CrabRolls, along with example code to get you started.
Setting Up an Application Environment
-
Import Required Dependencies: Start by importing the necessary dependencies from CrabRolls and other standard libraries to implement your application.
-
Define the Application Structure: Create a structure to represent your application. For this example, we’ll use a simple
MyApp
structure. -
Implement the Application Constructor: Implement a constructor for your application. This constructor will be used to create instances of your application and you can use it to initialize any required state or configurations.
-
Implement the Application Trait: The core of your dApp is defined by implementing the
Application
trait. This trait requires you to define methods likeadvance
andinspect
, which dictate how your application will process and respond to inputs.Both the
advance
andinspect
methods are asynchronous and return aResult
with aFinishStatus
or an error response,FinishStatus
can beAccept
orReject
. This allows you to handle the application’s state changes and responses effectively and return the appropriate status based on the processing outcome. If any error occurs during processing and not handled, the input will be rejected. Rejecting an input will make the Cartesi Machine ignore any Notice and Voucher generated by the application for that input, Report will still be generated because they are used for debugging and monitoring.In the
advance
method, we handle the payload received by the application, and can send notices, reports, and vouchers based on the processing outcome.The
inspect
method allows you to review the state of your application. This can be useful for debugging and monitoring the application’s behavior, only reports can be generated in this method.
Sending Notices, Reports, and Vouchers
To send notices, reports, and vouchers, you can use the Environment
trait provided by CrabRolls. This trait allows you to interact with the application and send the necessary data to the Cartesi Machine.
-
Send a Notice:
What is a Notice? A notice is a verifiable data declaration that attests to off-chain events or conditions and is accompanied by proof. - Cartesi Docs.
Use the
send_notice
method to send a notice to the Cartesi Machine. This method requires thepayload
to be sent, this payload can be any data that the application needs to process and be validated by a proof. -
Send a Report:
What is a Report? Reports are stateless logs, offering a means to record read-only information without changing the state. Primarily used for logging and diagnostic purposes, reports provide valuable insights into the operation and performance of a dApp. - Cartesi Docs.
Use the
send_report
method to send a report to the Cartesi Machine. This method requires thepayload
to be sent, this payload can be any data that the application needs to debug or monitor the application’s behavior, performance, state, etc. -
Send a Voucher:
What is a Voucher? Vouchers serve as a mechanism for facilitating on-chain actions initiated in the execution layer. - Cartesi Docs.
Use the
send_voucher
method to send a voucher to the Cartesi Machine. This method requires theaddress
andpayload
to be sent, this payload is the data ABI encoded that will be executed on-chain when the voucher is redeemed (can be manual executed after epoch length is reached), for example, a smart contract function call.You can see more about the Utils and Macros provided on Crabrolls to help you with the development of your dApp and the interactions.
Retrieving Metadata
To retrieve metadata from the application, you can use the Metadata
struct provided by CrabRolls. This struct contains information like the input_index
, sender
, block_number
, and timestamp
of the application. Only on the advance
method you can receive the Metadata
struct.
Running the Application
To run the application, you can use the Supervisor
struct provided by CrabRolls. This struct allows you to run your application and specify the options for the execution.
The RunOptions
struct allows you to specify the options for the execution of your application. You can set the rollup_url
, address_book
, and portal_config
to customize the execution environment. The Supervisor
struct will run your application using the specified options. For example:
To use the Cartesi CLI to run the application, you can follow the steps below:
-
Build the project:
-
Run the project:
The terminal will show the output of the application like:
-
You can use your application!
Now you have a basic understanding of how to set up an environment for developing and running your dApp using CrabRolls 🎉!